// name: react
import java.io.*;
import java.util.HashSet;
import java.util.Set;

public class React {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    static class TestCase {
        int n;
        String elem1;
        String elem2;
        Reaction[] reactions;
    }

    static class Reaction {
        String elem1;
        String elem2;
        boolean reacts;

        public Reaction(String e1, String e2, boolean r) {
            elem1 = e1;
            elem2 = e2;
            reacts = r;
        }
    }
    
    public static TestCase read() throws java.lang.Exception {
        TestCase testCase = new TestCase();
        String[] info = br.readLine().split(" ");
        testCase.elem1 = info[0];
        testCase.elem2 = info[2];
        int n = Integer.parseInt(br.readLine());
        testCase.n = n;
        testCase.reactions = new Reaction[n];
        for (int i = 0; i < n; i++) {
            info = br.readLine().split(" ");
            int lineLength = info.length;
            testCase.reactions[i] = new Reaction(info[0], info[lineLength - 1], lineLength == 4);
        }
        return testCase;
    }

    public static void logic(TestCase testCase) {
        int n = testCase.n;
        String elem1 = testCase.elem1;
        String elem2 = testCase.elem2;
        Reaction[] reactions = testCase.reactions;
        Set<String> sameElems = new HashSet<>();
        Set<String> diffElems = new HashSet<>();
        Set<String> explored = new HashSet<>();
        sameElems.add(elem1);
        explored.add(elem1);
        boolean newExplored = true;
        while (newExplored) {
            newExplored = false;
            for (Reaction r : reactions) {
                if (explored.contains(r.elem1) != explored.contains(r.elem2)) {
                    if (!explored.contains(r.elem1)) {
                        explored.add(r.elem1);
                        if (sameElems.contains(r.elem2) == r.reacts) sameElems.add(r.elem1);
                        else diffElems.add(r.elem1);
                    } else {
                        explored.add(r.elem2);
                        if (sameElems.contains(r.elem1) == r.reacts) sameElems.add(r.elem2);
                        else diffElems.add(r.elem2);
                    }
                    newExplored = true;
                }
            }
            if (explored.contains(elem2)) {
                System.out.println(elem1 + (sameElems.contains(elem2) ? " REACTS WITH " : " DOES NOT REACT WITH ") + elem2);
                return;
            }
        }
        System.out.println("UNKNOWN");
    }

	public static void main(String[] args) throws java.lang.Exception {
        int T = Integer.parseInt(br.readLine());
        for (int i = 0; i < T; i++) {
            logic(read());
            br.readLine(); // Don't forget the blank line in between test cases!
        }
    }
}