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

public class Check implements Checker {
    public Outcome test(InStream inf, InStream ouf, InStream ans) {
        int n = inf.nextInt();
        int m = inf.nextInt();
        int[] fromVertex = new int[m];
        int[] toVertex = new int[m];

        for (int i = 0; i < m; i++) {
            int from = inf.nextInt() - 1;
            int to = inf.nextInt() - 1;
            int id = inf.nextInt() - 1;
            fromVertex[id] = from;
            toVertex[id] = to;
        }
        int v = ouf.nextInt() - 1;
        int u = ouf.nextInt() - 1;
        if (v < 0 || v >= m || u < 0 || u >= m) {
            return new Outcome(Type.WA, "Incorrect number");
        }
        if (v == u) {
            return new Outcome(Type.WA, "Number must differents.");
        }
        int tmp = toVertex[v];
        toVertex[v] = toVertex[u];
        toVertex[u] = tmp;

        tmp = fromVertex[v];
        fromVertex[v] = fromVertex[u];
        fromVertex[u] = tmp;

        int cntPath = inf.nextInt();
        int[] used = new int[n];
        int time = 0;
        for (int curPath = 0; curPath < cntPath; curPath++) {
            time++;
            int lenPath = inf.nextInt();
            int start = inf.nextInt() - 1;
            used[start] = time;
            int end = inf.nextInt() - 1;
            int cur = start;
            for (int i = 0; i < lenPath; i++) {
                int num = inf.nextInt() - 1;
                if (cur != fromVertex[num]) {
                    return new Outcome(Type.WA, "Incorrect path # "
                            + (curPath + 1) + " in edge: " + (i + 1));
                }
                cur = toVertex[num];
                if (used[cur] == time) {
                    return new Outcome(Type.WA, "Path #" + (curPath + 1) 
                            + " doesn't simple path");
                }
                used[cur] = time;
            }
            if (cur != end) {
                return new Outcome(Type.WA, "Inccorect end in path # "
                        + (curPath + 1));
            }
        }
        return new Outcome(Type.OK, (u + 1) + " " + (v + 1));
    }
}
