import os
from sys import argv, stdin, exit
from random import randint

assert len(argv)==3, "correct usage: %s input contestants_output" % argv[0]

inputdata = open(argv[1],'r').readlines()

def output_string_reader():
    for x in open(argv[2],'r').read().split(): yield x
next_output_string = output_string_reader().next

def substitute_cell(cell,values):
    try: return values[ ord(cell)-ord('a') ]
    except: return cell

def substitute(matrix,values):
    return [ [ substitute_cell(cell,values) for cell in row ] for row in matrix ]

def modexp(n,k,p): # n^k mod p
    if k==0: return 1%p
    if k==1: return n%p
    tmp = modexp(n,k//2,p)
    tmp *= tmp
    if k%2: tmp *= n
    return tmp%p

def inverse(n,p): return modexp(n,p-2,p)

def modular_determinant(matrix,p):
    # creates a copy of the matrix
    # runs gaussian elimination to obtain the determinant modulo p
    C = [ [ x%p for x in row ] for row in matrix ]
    N = len(matrix)
    result = 1
    for n in range(N):
        r2 = n
        while r2<N and C[r2][n]==0: r2+=1
        if r2==N: return 0 # matrix is not regular
        if r2>n:
            result *= -1
            C[n], C[r2] = C[r2], C[n]
        inv = inverse(C[n][n],p)
        result *= C[n][n]
        result %= p
        for i in range(n,N): C[n][i] = (C[n][i] * inv)%p
        for r in range(n+1,N):
            add = p - C[r][n]
            for i in range(n,N): C[r][i] = (C[r][i] + C[n][i]*add)%p
    return result%p

def magic(matrix,values,modulo):
    n = len(matrix)
    pn = n*(n-1)//2
    return ( (-1)**pn * modular_determinant(substitute(matrix,values),modulo) )%modulo

def evaluate_poly(poly,values,modulo):
    # assumes the form "3xxy - 47xz + 12"
    newpoly=" "
    for c in poly:
        if c.isalpha():
            if newpoly[-1]!=" ": newpoly += '*'
            newpoly += "(" + str(substitute_cell(c,values)) + ")"
        else: newpoly+=c
    return eval(newpoly)%modulo

primes = [213415541,913415543,1113415517,1013415553]

try:
    T = int(inputdata[0])
    for t in range(T):
        if verbose: print "judging test case %d" % (t+1)
        poly = inputdata[2*t+2]
        try: N = int(next_output_string())
        except: assert False, "Wrong answer: failed to read matrix size in test case %d." % (t+1)
        assert N > 0, "Wrong answer: failed to find a matrix that exists."
        assert N <= 70, "Wrong answer: matrix too large."
        try: matrix = [ [ next_output_string() for c in range(N) ] for r in range(N) ]
        except: assert False, "Wrong answer: failed to read the matrix in test case %d." % (t+1)

        for r in range(N):
            for c in range(N):
                try:
                    x = int(matrix[r][c])
                    assert x>=-(10**9) and x<=10**9, "Wrong answer: integer out of valid range."
                    matrix[r][c] = x
                except:
                    assert len(matrix[r][c])==1, "Wrong answer: an element is neither an integer, nor a variable."
                    assert matrix[r][c].islower(), "Wrong answer: an element is neither an integer, nor a variable."

        ok = True
        for P in primes:
            for kolo in range(20):
                values = [ randint(0,P-1) for i in range(26) ]
                v_polynom = evaluate_poly(poly,values,P)
                v_matica = magic(matrix,values,P)
                ok = ok and (v_polynom == v_matica)
                if not ok: break
            if not ok: break

        assert ok, "Wrong answer: incorrect matrix."
except Exception as error_message:
    print error_message
    exit(1)

print "OK"
