#!/usr/bin/python

from sys import argv, stdin, exit
from Queue import Queue
import re
RB = re.compile('^[RB]*$')

assert len(argv)==4, "correct usage: %s {easy,hard} input contestants_output" % argv[0]

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

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

def get_coordinates(array,letter):
    answer = []
    for i in range(len(array)):
        for j in range(len(array[i])):
            if array[i][j]==letter:
                answer.append((i,j))
    return answer

def normalize(coords):
    minx, miny = min( [ x for (x,y) in coords ] ), min( [ y for (x,y) in coords ] )
    return sorted( [ (x-minx,y-miny) for (x,y) in coords ] )

def rotate(coords): return normalize( [ (y,1000-x) for (x,y) in coords ] )
def flip(coords): return normalize( [ (x,1000-y) for (x,y) in coords ] )

def equal_lists(a,b):
    for x,y in zip(a,b):
        if x!=y: return False
    return True

def equal_set(reds,blues):
    reds, blues = normalize(reds), normalize(blues)
    for j in range(2):
        for i in range(4):
            blues = rotate(blues)
            if reds==blues: return True
        blues = flip(blues)
    return False

def connected(answer):
    R, C = len(answer), len(answer[0])
    dr, dc = [-1,1,0,0], [0,0,-1,1]
    visited = []
    for r in range(R): visited.append( [ False ] * C )
    char = answer[0][0]
    Q = Queue()
    Q.put((0,0))
    visited[0][0] = True
    while not Q.empty():
        r,c = Q.get()
        for d in range(4):
            nr,nc = r+dr[d],c+dc[d]
            if nr<0 or nr>=R or nc<0 or nc>=C: continue
            if visited[nr][nc]: continue
            if answer[nr][nc]!=char: continue
            visited[nr][nc] = True
            Q.put((nr,nc))
    for r in range(R):
        for c in range(C):
            if (not visited[r][c]) and (answer[r][c]==char): return False
    return True

try:
    T = next_input_int()
    for t in range(T):
        R, C, rR, cR, rB, cB = [ next_input_int() for i in range(6) ]
        rR, cR, rB, cB = rR-1, cR-1, rB-1, cB-1

        impossible = False
        if (R%2 == 1) and (C%2 == 1): impossible = True
        if argv[1]=="hard":
            if (R==1) and ((cR<C/2) == (cB<C/2)): impossible = True
            if (C==1) and ((rR<R/2) == (rB<R/2)): impossible = True

        if impossible:
            try: row = next_output_string()
            except: assert False, "Too few tokens in output."
            assert row == "IMPOSSIBLE", "Expected IMPOSSIBLE, got something else."
        else:
            answer = []
            try:
                answer.append(next_output_string())
            except:
                assert False, "Too few tokens in output."
            assert answer[0] != "IMPOSSIBLE", "Expected a solution, got IMPOSSIBLE."
            try:
                for r in range(R-1): answer.append(next_output_string())
            except:
                assert False, "Too few tokens in output."
            for row in answer:
                assert RB.match(row), "Solution row contains letters other than R and B."
                assert len(row)==C, "Solution row length does not match number of columns."
            assert answer[rR][cR]=='R', "Cell that should have been red is not red."
            assert answer[rB][cB]=='B', "Cell that should have been blue is not blue."
            reds, blues = get_coordinates(answer,'R'), get_coordinates(answer,'B')
            assert equal_set(reds,blues), "The blue set is not identical to the red set."
            if argv[1]=="hard":
                assert connected(answer), "The red set is not connected."
except BaseException as error_message:
    print error_message
    exit(1)

print "OK"
