import java.util.Scanner;
import java.util.Arrays;
import java.io.File;
import java.io.PrintWriter;
import java.io.FileNotFoundException;

public class hot_pm {

    private static final int MAX_T = 100000;

    public static void main(String[] args) throws FileNotFoundException {
        Scanner in = new Scanner(new File("hot.in"));
        PrintWriter out = new PrintWriter("hot.out");

        int n = in.nextInt();

        myAssert(n >= 1 && n <= 100);

        int[] l = new int[n];
        int[][] a = new int[n][];

        for (int i = 0; i < n; i++) {
            l[i] = in.nextInt();
            myAssert(l[i] >= 1 && l[i] <= 8);
            a[i] = new int[l[i]];
            for (int j = 0; j < l[i]; j++) {
                a[i][j] = in.nextInt() - 1;
                myAssert(a[i][j] >= 0 && a[i][j] < n);
            }
        }

        int p = in.nextInt();
        myAssert(p >= 1 && p <= 100);

        int[] s = new int[p];
        int[] d = new int[p];
        int[] t = new int[p];
        for (int j = 0; j < p; j++) {
            s[j] = in.nextInt() - 1;
            d[j] = in.nextInt() - 1;
            t[j] = in.nextInt();
            myAssert(s[j] >= 0 && s[j] < n);
            myAssert(d[j] >= 0 && d[j] < n);
            myAssert(s[j] != d[j]);
            myAssert(t[j] >= 0 && t[j] <= 1000);
        }

        int[][] v = new int[MAX_T + 1][p];
        for (int[] ints : v) {
            Arrays.fill(ints, -1);
        }
        for (int j = 0; j < p; j++) {
            int tt = t[j];
            v[tt][j] = s[j];
            while (tt < MAX_T && v[tt][j] != d[j]) {
                v[tt + 1][j] = a[v[tt][j]][tt % l[v[tt][j]]];
                tt++;
            }
        }

        int[][] u = new int[MAX_T + 1][p];
        int[][] w = new int[MAX_T + 1][n];
        for (int[] ints : u) {
            Arrays.fill(ints, -1);
        }
        int[][] bc = new int[MAX_T + 1][n];
        for (int[] ints : bc) {
            Arrays.fill(ints, -1);
        }

        for (int tt = 0; tt < MAX_T; tt++) {
            // Sources
            for (int j = 0; j < p; j++) {
                if (t[j] == tt) {
                    u[tt][j] = w[tt][s[j]] + 1;
                }
            }

            // Destinations
            for (int j = 0; j < p; j++) {
                if (v[tt][j] == d[j]) {
                    if (u[tt][j] > w[tt][d[j]]) {
                        w[tt][d[j]] = u[tt][j];
                        bc[tt][d[j]] = j;
                    }
                }
            }

            for (int i = 0; i < n; i++) {
                int ii = a[i][tt % l[i]];
                w[tt + 1][ii] += w[tt][i];
            }


            for (int j = 0; j < p; j++)
                if (u[tt][j] >= 0 && v[tt][j] != d[j]) {
                    u[tt + 1][j] =
                            w[tt + 1][v[tt + 1][j]] -
                                    w[tt][v[tt][j]] +
                                    u[tt][j];
                }
        }

        int res = 0;
        for (int i = 0; i < n; i++) {
            res += w[MAX_T][i];
        }

        out.println(res);

        boolean[] pu = new boolean[p];
        boolean[][] pw = new boolean[MAX_T + 1][n];
        boolean[][] puw = new boolean[MAX_T + 1][n];

        for (int i = 0; i < n; i++) {
            pw[MAX_T][i] = true;
        }

        for (int tt = MAX_T - 1; tt >= 0; tt--) {

            for (int i = 0; i < n; i++) {
                int ii = a[i][tt % l[i]];
                pw[tt][i] = pw[tt + 1][ii];
            }

            for (int j = 0; j < p; j++) if (pu[j] && v[tt][j] >= 0) puw[tt][v[tt][j]] = true;

            for (int i = 0; i < n; i++) {
                if (pw[tt][i] && !puw[tt][i]) {
                    if (bc[tt][i] >= 0) {
                        pu[bc[tt][i]] = true;
                    }
                }
            }
        }

        for (int i = 0; i < p; i++) {
            if (pu[i]) out.println(i + 1);
        }

        out.close();

    }

    private static void myAssert(boolean b) {
        if (!b)
            throw new RuntimeException("Assertion Failed");
    }

}
