/******************************************************************/
/* ACM ICPC 2017-2018, NEERC                                      */
/* Northern Subregional Contest                                   */
/* St Petersburg, November 4, 2017                                */
/******************************************************************/
/* Problem G. Grand Test                                          */
/*                                                                */
/* Original idea         Andrew Stankevich                        */
/* Problem statement     Borys Minaiev                            */
/* Test set              Borys Minaiev                            */
/******************************************************************/
/* Checker                                                        */
/*                                                                */
/* Author                Georgiy Korneev                          */
/******************************************************************/

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

import java.util.HashSet;
import java.util.Set;

import static ru.ifmo.testlib.Outcome.Type.*;

public class Check implements Checker {
    public Outcome test(InStream inf, InStream ouf, InStream ans) {
        int t = inf.nextInt();
        for (int i = 0; i < t; i++) {
            int n = inf.nextInt();
            int m = inf.nextInt();
            Set<Road> roads = new HashSet<>();
            for (int j = 0; j < m; j++) {
                int u = inf.nextInt();
                int v = inf.nextInt();
                roads.add(new Road(u, v));
            }

            boolean ja = check(i + 1, n, roads, ans, FAIL);
            boolean pa = check(i + 1, n, roads, ouf, WA);
            if (ja != pa) {
                if (ja) {
                    throw new Outcome(WA, String.format("Country %d: Routes exist, but not found", i + 1));
                } else {
                    throw new Outcome(FAIL, String.format("Country %d: Routes found by contestant, but not jury", i + 1));
                }
            }
        }
        return new Outcome(OK, "");
    }

    private boolean check(final int country, final int n, final Set<Road> roads, final InStream ouf, final Outcome.Type type) {
        int s = ouf.nextInt();
        if (s == -1) {
            return false;
        }

        int f = ouf.nextInt();
        if (f == s) {
        	throw new Outcome(type, String.format("Country %d: F = S = %d (n = %d)", country, f, n));
        }

        Set<Integer> cities = new HashSet<>();
        Set<Road> used = new HashSet<>();
        for (int route = 1; route <= 3; route++) {
            int k = ouf.nextInt();
            if (k < 2 || n < k) {
                throw new Outcome(type, String.format("Country %d, route %d: Invalid route length %d (n = %d)", country, route, k, n));
            }
            int prevC = 0;
            for (int q = 1; q <= k; q++) {
                int c = ouf.nextInt();
                if (c < 1 || n < c) {
                    throw new Outcome(type, String.format("Country %d, route %d, city %d: Invalid city no %d (n = %d)", country, route, q, c, n));
                }
                if (q == 1) {
                    if (c != s) {
                        throw new Outcome(type, String.format("Country %d, route %d, city %d: Expected %d, found %d", country, route, q, s, c));
                    }
                } else {
                    Road road = new Road(c, prevC);
                    if (!roads.contains(road)) {
                        throw new Outcome(type, String.format("Country %d, route %d, city %d: No road between cities %d and %d", country, route, q, prevC, c));
                    }
                    if (!used.add(road)) {
                        throw new Outcome(type, String.format("Country %d, route %d, city %d: Road between cities %d and %d is already used", country, route, q, prevC, c));
                    }
                    if (q == k) {
                        if (c != f) {
                            throw new Outcome(type, String.format("Country %d, route %d, city %d: Expected %d, found %d", country, route, q, f, c));
                        }
                    } else if (!cities.add(c)){
                        throw new Outcome(type, String.format("Country %d, route %d, city %d: City %d is already visited", country, route, q, c));
                    }
                }
                prevC = c;
            }
        }
        return true;
    }

    static class Road {
        final int u, v;

        Road(final int u, final int v) {
            this.u = Math.min(u, v);
            this.v = Math.max(u, v);
        }

        @Override
        public boolean equals(final Object o) {
            if (o instanceof Road) {
                final Road road = (Road) o;
                return u == road.u && v == road.v;
            }
            return false;
        }

        @Override
        public int hashCode() {
            return 31 * u + v;
        }
    }
}

