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

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

public class Check implements Checker {
    public Outcome test(InStream inf, InStream ouf, InStream ans) {
        if (!checkOne(inf, ouf, ans)) {
            return new Outcome(Outcome.Type.WA, "Group 1");
        }
        if (!checkOne(inf, ouf, ans)) {
            return new Outcome(Outcome.Type.WA, "Group 2");
        }
        if (!checkOne(inf, ouf, ans)) {
            return new Outcome(Outcome.Type.WA, "Group 3");
        }

        return new Outcome(Outcome.Type.OK, ":)");
    }

    private boolean checkOne(InStream inf, InStream ouf, InStream ans) {
        String jury = ans.nextToken().toLowerCase();
        String part = null;
        if (jury.equals("incorrect")) {
            part = ouf.nextToken().toLowerCase();
            if (!jury.equals(part)) {
                throw new Outcome(Outcome.Type.WA, "Incorrect expected, \"" + part + "\" given");
            } else {
                throw new Outcome(Outcome.Type.OK, "Incorrect data");
            }
        }
        int jury_num = Integer.parseInt(jury);
        int part_num;
        try {
            String tmp = ouf.nextToken().toLowerCase();
            if (tmp.equals("incorrect")) {
            	throw new Outcome(Outcome.Type.WA, "Contestant hadn't found answer");
            }
            part_num = Integer.parseInt(tmp);
        } catch (NumberFormatException e) {
            throw new Outcome(Outcome.Type.WA, "Number expected, \"" + part + "\" given");
        }

        if (jury_num != part_num) {
            return false;
        } else {
            List<Integer> jury_result = new ArrayList<Integer>();
            List<Integer> part_result = new ArrayList<Integer>();
            for (int i = 0; i < jury_num; i++) {
                jury_result.add(ans.nextInt());
                part_result.add(ouf.nextInt());
            }
            Collections.sort(jury_result);
            Collections.sort(part_result);
            for (int i = 0; i < jury_num; i++) {
                if (jury_result.get(i) != part_result.get(i)) {
                    return false;
                }
            }
            return true;
        }
    }
}
