/******************************************************************/
/* ACM ICPC 2012-2013                                             */
/* Northeastern European Regional Contest                         */
/* St Petersburg - Barnaul - Tashkent - Tbilisi, December 2, 2012 */
/******************************************************************/
/* Problem J. Jumping Around                                      */
/*                                                                */
/* Original idea         Andrey Stankevich                        */
/* Problem statement     Andrey Stankevich                        */
/* Test set              Andrey Stankevich                        */
/******************************************************************/
/* Checker                                                        */
/*                                                                */
/* Author                Andrey Stankevich                        */
/******************************************************************/

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

import java.util.ArrayList;
import java.util.List;

public class Check implements Checker {

	@SuppressWarnings("unchecked")
    public Outcome test(InStream inf, InStream ouf, InStream ans) {
        int testNo = inf.nextInt();
        // parse lines with numbers from output and check PE
        List<Integer>[] out = (List<Integer>[]) new List[testNo + 1];
        for (int test = 1; test <= testNo; test++) {
            out[test] = new ArrayList<>();
            while (!ouf.seekEoLn())
                out[test].add(ouf.nextInt());
            ouf.nextLine();
        }
        if (!ouf.seekEoF())
            throw new Outcome(Outcome.Type.PE, "Extra data in output file");
        // check for WA
        for (int test = 1; test <= testNo; test++) {
            int a = inf.nextInt();
            int b = inf.nextInt();
            int c = inf.nextInt();
            int n = a + b + c;
            if (out[test].size() != n + 1)
                return new Outcome(Outcome.Type.WA, String.format("Test %d - wrong number of planets %d, expected %d", test, out[test].size(), n + 1));
            int prev = -1;
            boolean[] vis = new boolean[n + 1]; 
            for (int i = 0; i < n + 1; i++) {
                int v = out[test].get(i);
                if (v < 0 || v > n) {
                    return new Outcome(Outcome.Type.WA, String.format("Test %d - invalid planet number %d", test, v));
                }
                if (vis[v]) {
                    return new Outcome(Outcome.Type.WA, String.format("Test %d - planet %d visited twice", test, v));
                }
                vis[v] = true;
                if (i == 0) {
                    if (v != 0) {
                        return new Outcome(Outcome.Type.WA, String.format("Test %d - jumper must start from 0, starting from %d", test, v));
                    }
                } else {
                    int d = Math.abs(v - prev);
                    if (d != 1 && d != 2 && d != 3) {
                        return new Outcome(Outcome.Type.WA, String.format("Test %d - invalid jump by %d", test, d));
                    }
                    if (d == 1) {
                        a--;
                    } else if (d == 2) {
                        b--;
                    } else {
                        c--;
                    }
                }
                prev = v;
            }

            if (c != 0) {
                return new Outcome(Outcome.Type.WA, String.format("Test %d - wrong number of jumps by 3", test));
            }
            if (b != 0) {
                return new Outcome(Outcome.Type.WA, String.format("Test %d - wrong number of jumps by 2", test));
            }
            if (a != 0) {
                return new Outcome(Outcome.Type.WA, String.format("Test %d - wrong number of jumps by 1", test));
            }
        }
        return new Outcome(Outcome.Type.OK, String.format("%d tests", testNo));
	}
}

