class Digraph:
    def __init__(self):
        # source_id is always 0
        self.sink_id = 0
        self.nodes = 1
        self.edges = []
        self.odd = True # is there an odd number of vertices on source->sink paths?

    def singleton(self,label):
        self.sink_id = 1
        self.nodes = 2
        self.edges = [ (0,1,label) ]
        self.odd = False

    def __repr__(self): return str(self.nodes)+' '+str(self.sink_id)+' '+str(self.odd)+' '+repr(self.edges)

def multiply(G1,G2):
    G = Digraph()
    G.nodes = G1.nodes + G2.nodes - 1
    G.edges = [ e for e in G1.edges ]
    for x,y,label in G2.edges:
        newx = x + G1.nodes - 1
        if x==0: newx = G1.sink_id
        newy = y + G1.nodes - 1
        G.edges.append( (newx,newy,label) )
    G.odd = (G1.odd == G2.odd)
    G.sink_id = G2.sink_id + G1.nodes - 1
    if G2.sink_id == 0: G.sink_id = G1.sink_id
    return G

def add(G1,G2):
    G = Digraph()
    G.nodes = G1.nodes + G2.nodes - 1
    G.edges = [ e for e in G1.edges ]
    for x,y,label in G2.edges:
        newx = x + G1.nodes - 1
        if x==0: newx = 0
        newy = y + G1.nodes - 1
        G.edges.append( (newx,newy,label) )
    if G1.odd == G2.odd:
        G.sink_id = G.nodes
        G.nodes += 1
        G.edges.append( (G1.sink_id, G.sink_id, "1") )
        G.edges.append( (G2.sink_id+G1.nodes-1, G.sink_id, "1") )
        G.odd = not G2.odd
    else:
        G.edges.append( (G1.sink_id, G2.sink_id+G1.nodes-1, "1") )
        G.odd = G2.odd
        G.sink_id = G2.sink_id+G1.nodes-1
    return G

def finalize(G1):
    G = Digraph()
    if G1.odd:
        G.nodes = G1.nodes
        G.edges = [ e for e in G1.edges ]
        for n in range(1,G.nodes):
            if n != G1.sink_id:
                G.edges.append( (n,n,"1") )
        G.edges.append( (G1.sink_id,0,"1") )
    else:
        G.nodes = G1.nodes - 1
        G.edges = []
        for x,y,label in G1.edges:
            # rename sink to 0, decrease all ids >sink
            if x > G1.sink_id: x -= 1
            if y == G1.sink_id: y = 0
            elif y > G1.sink_id: y -= 1
            G.edges.append( (x,y,label) )
        for n in range(1,G.nodes):
            G.edges.append( (n,n,"1") )
    return G

def to_matrix(G):
    result = [ [ "0" for c in range(G.nodes) ] for r in range(G.nodes) ]
    for x,y,label in G.edges: result[x][y] = label
    # pad it to a multiple of 4
    while len(result)%4 != 0:
        for i in range(len(result)): result[i].append("0")
        result.append( ["0"]*(len(result)+1) )
        result[-1][-1] = "1"
    return result

def poly_to_graph(p):
    poly = ["+"] + p.split()

    # break the polynomial into a sum of terms
    # if a term is originally being subtracted, merge the minus sign into the term
    terms = []
    for i in range(len(poly)):
        if i%2==1:
            term = []
            j = 0
            while j<len(poly[i]) and poly[i][j].isdigit(): j += 1
            if j>0:
                term.append(poly[i][:j])
                poly[i] = poly[i][j:]
            for x in poly[i]: term.append(x)
            if poly[i-1]=='-':
                if term[0][0].isdigit(): term[0]='-'+term[0]
                else: term = ['-1']+term
            terms.append(term)

    # create the graph
    G = None
    for term in terms:
        H = Digraph()
        H.singleton(term[0])
        for factor in term[1:]:
            K = Digraph()
            K.singleton(factor)
            H = multiply(H,K)
        if G is None: G = H
        else: G = add(G,H)

    return G

########################################################################################

from sys import stdin
T = int(stdin.readline())
for t in range(T):
    row = stdin.readline()
    row = stdin.readline()
    if t==T-1:
        # special handling for the largest test case in m2.in
        G1, G2, G3 = poly_to_graph(row[2:51]), poly_to_graph(row[58:96]), poly_to_graph(row[101:])
        result = to_matrix(finalize( add(G3,multiply(G1,G2)) ))
    else:
        result = to_matrix(finalize(poly_to_graph(row)))
    print len(result)
    for row in result: print ' '.join(row)

