/******************************************************************/
/* ACM ICPC 2017-2018, NEERC                                      */
/* Northern Subregional Contest                                   */
/* St Petersburg, November 4, 2017                                */
/******************************************************************/
/* Problem D. Dividing Marbles                                    */
/*                                                                */
/* Original idea         Gennady Korotkevich                      */
/* Problem statement     Gennady Korotkevich                      */
/* Test set              Gennady Korotkevich                      */
/******************************************************************/
/* Checker                                                        */
/*                                                                */
/* Author                Gennady Korotkevich                      */
/******************************************************************/

import ru.ifmo.testlib.Checker;
import ru.ifmo.testlib.InStream;
import ru.ifmo.testlib.Outcome;

import static ru.ifmo.testlib.Outcome.Type.OK;
import static ru.ifmo.testlib.Outcome.Type.WA;
import static ru.ifmo.testlib.Outcome.Type.FAIL;

import java.util.HashSet;

public class Check implements Checker {
    static final int INPUT_BITS = 4;

    public Outcome test(InStream inf, InStream ouf, InStream ans) {
        int tt = inf.nextInt();
        for (int qq = 1; qq <= tt; qq++) {
            long n = 0;
            for (int i = 0; i < INPUT_BITS; i++) {
                int d = inf.nextInt();
                n += (1L << d);
            }
            int user = ouf.nextInt();
            if (user < 0) {
                return new Outcome(WA, String.format("case %d: Found %d, a negative number", qq, user));
            }
            HashSet<Long> h = new HashSet<>();
            h.add(n);
            for (int i = 0; i < user; i++) {
                long m = ouf.nextLong();
                if (!h.contains(m)) {
                    return new Outcome(WA, String.format("case %d: Pile with %d marbles doesn't exist", qq, m));
                }
                h.remove(m);
                long m1 = ouf.nextLong();
                long m2 = ouf.nextLong();
                if (m1 <= 0 || m2 <= 0 || m1 + m2 != m) {
                    return new Outcome(WA, String.format("case %d: Invalid division: %d into %d and %d", qq, m, m1, m2));
                }
                h.add(m1);
                h.add(m2);
            }
            if (h.size() != 1) {
                return new Outcome(WA, String.format("case %d: More than one pile left in the end", qq));
            }
            if (!h.contains(1L)) {
                return new Outcome(WA, String.format("case %d: The final pile contains more than 1 marble", qq));
            }
            int jury = ans.nextInt();
            if (user > jury) {
                return new Outcome(WA, String.format("case %d: Suboptimal: expected %d, found %d", qq, jury, user));
            }
            if (user < jury) {
                return new Outcome(FAIL, String.format("case %d: Better than jury's: expected %d, found %d", qq, jury, user));
            }
            for (int i = 0; i < jury; i++) {
                ans.nextLong();
                ans.nextLong();
                ans.nextLong();
            }
        }
        return new Outcome(OK, tt + " tests");
    }
}

