#include "testlib.h"
#include <bits/stdc++.h>

using namespace std;


int main(int argc, char* argv[]) {
    registerValidation(argc, argv);

    int t = inf.readInt(1, 10, "t");
    inf.readEoln();
    for (int i = 0; i < t; i++) {
        int n = inf.readInt(2, 100);
        inf.readSpace();
        int s = inf.readInt(1, 100);
        inf.readSpace();
        int q = inf.readInt(1, 100);
        inf.readEoln();

        for (int j = 1; j <= n; j++) {
            int cargoes = inf.readInt(0, q, "cargoes");
            for (int k = 1; k <= cargoes; k++) {
                inf.readSpace();
                int destination = inf.readInt(1, n, "destination");
                ensuref(j != destination, "Cargoe cannot have its station as its destination");
            }
            inf.readEoln();
        }
    }
    inf.readEof();

    return 0;
}