import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;

public class Validate {

    final int MAX_N = 100_000;
    final int MAX_M = 100_000;
    final int MAX_K = 100_000;
    final int CNT_EDGE_IN_PATH = 100_000;

    int n, m;
    ArrayList<Integer>[] g;

    public void run() {
        StrictScanner inf = new StrictScanner(System.in);
        n = inf.nextInt();
        m = inf.nextInt();
        ensureLimits(n, 1, MAX_N, "n");
        ensureLimits(m, 1, MAX_M, "m");
        inf.nextLine();
        
        HashSet<Long> edges = new HashSet<>();
        HashSet<Integer> id = new HashSet<>();
        g = new ArrayList[n];
        for (int i = 0; i < n; i++) {
            g[i] = new ArrayList<Integer>();
        }
        for (int i = 0; i < m; i++) {
            int from = inf.nextInt() - 1;
            int to = inf.nextInt() - 1;
            int num = inf.nextInt() - 1;
            ensureLimits(num, 0, m - 1, "Number of edge");
            ensureLimits(to, 0, n - 1, "Number of vertex");
            ensureLimits(from, 0, n - 1, "Number of vertex");
            ensure(to != from, "Graph doesn't have loop");
            ensure(!id.contains(num), "Dublicate number edge");
            id.add(num);
            ensure(!edges.contains(2L * n * from + to), "Dublicate edge");
            edges.add(2L * n * from + to);
            // ensure(!edges.contains(2L * n * to + from), "Dublicate edge");
            // edges.add(2L * n * to + from);
            inf.nextLine();
        }
        
        // ensure(checkConnective() != -1, "Graph must connectivity");
        
        int cntPath = inf.nextInt();
        ensureLimits(cntPath, 1, MAX_K, "k");
        inf.nextLine();
        
        int SUM = 0;
        int[] cntEdge = new int[m];
        for (int curPath = 0; curPath < cntPath; curPath++) {
            int len = inf.nextInt();
 
            int from = inf.nextInt() - 1;
            ensureLimits(from, 0, n - 1, "Start vertex");
            
            int to = inf.nextInt() - 1;
            ensureLimits(to, 0, n - 1, "End vertex");
            ensure(to != from, "Start must not equal End");
            
            SUM += len;
            ensureLimits(len, 1, n - 1, "Length path");
            inf.nextLine();

            boolean[] used = new boolean[m];
            for (int i = 0; i < len; i++) {
                int edge = inf.nextInt() - 1;
                ensureLimits(edge, 0, m - 1, "Number edge");
                cntEdge[edge]++;
                ensure(!used[edge], "Dublicate edge in path");
                used[edge] = true;
            }
            inf.nextLine();
        }
        ensureLimits(SUM, 1, CNT_EDGE_IN_PATH, "Count edge in paths = " + SUM + ", but");
        for (int i = 0; i < m; i++) {
            ensure(cntEdge[i] != 0, "Edge " + (i + 1) + " never used in paths.");
        }
        System.err.println(SUM);
        inf.close();
    }

    boolean[] used;

    int checkConnective() {
        used = new boolean[n];
        dfs(0);
        for (int i = 0; i < n; i++) {
            if (!used[i]) {
                return i;
            }
        }
        return -1;
    }

    void dfs(int v) {
        used[v] = true;
        for (int u : g[v]) {
            if (!used[u]) {
                dfs(u);
            }
        }
    }

    public static void main(String[] args) {
        new Validate().run();
    }

    public class StrictScanner {
        private final BufferedReader in;
        private String line = "";
        private int pos;
        private int lineNo;

        public StrictScanner(InputStream source) {
            in = new BufferedReader(new InputStreamReader(source));
            nextLine();
        }

        public void close() {
            ensure(line == null, "Extra data at the end of file");
            try {
                in.close();
            } catch (IOException e) {
                throw new AssertionError("Failed to close with " + e);
            }
        }

        public void nextLine() {
            ensure(line != null, "EOF");
            ensure(pos == line.length(), "Extra characters on line " + lineNo);
            try {
                line = in.readLine();
            } catch (IOException e) {
                throw new AssertionError("Failed to read line with " + e);
            }
            pos = 0;
            lineNo++;
        }

        public String next() {
            ensure(line != null, "EOF");
            ensure(line.length() > 0, "Empty line " + lineNo);
            if (pos == 0)
                ensure(line.charAt(0) > ' ', "Line " + lineNo
                        + " starts with whitespace");
            else {
                ensure(pos < line.length(), "Line " + lineNo + " is over");
                ensure(line.charAt(pos) == ' ', "Wrong whitespace on line "
                        + lineNo);
                pos++;
                ensure(pos < line.length(), "Line " + lineNo + " is over");
                ensure(line.charAt(0) > ' ', "Line " + lineNo
                        + " has double whitespace");
            }
            StringBuilder sb = new StringBuilder();
            while (pos < line.length() && line.charAt(pos) > ' ')
                sb.append(line.charAt(pos++));
            return sb.toString();
        }

        public int nextInt() {
            String s = next();
            ensure(s.length() == 1 || s.charAt(0) != '0',
                    "Extra leading zero in number " + s + " on line " + lineNo);
            ensure(s.charAt(0) != '+', "Extra leading '+' in number " + s
                    + " on line " + lineNo);
            try {
                return Integer.parseInt(s);
            } catch (NumberFormatException e) {
                throw new AssertionError("Malformed number " + s + " on line "
                        + lineNo);
            }
        }

        public long nextLong() {
            String s = next();
            ensure(s.length() == 1 || s.charAt(0) != '0',
                    "Extra leading zero in number " + s + " on line " + lineNo);
            ensure(s.charAt(0) != '+', "Extra leading '+' in number " + s
                    + " on line " + lineNo);
            try {
                return Long.parseLong(s);
            } catch (NumberFormatException e) {
                throw new AssertionError("Malformed number " + s + " on line "
                        + lineNo);
            }
        }

        public double nextDouble() {
            String s = next();
            ensure(s.length() == 1 || s.startsWith("0.") || s.charAt(0) != '0',
                    "Extra leading zero in number " + s + " on line " + lineNo);
            ensure(s.charAt(0) != '+', "Extra leading '+' in number " + s
                    + " on line " + lineNo);
            try {
                return Double.parseDouble(s);
            } catch (NumberFormatException e) {
                throw new AssertionError("Malformed number " + s + " on line "
                        + lineNo);
            }
        }
    }

    void ensure(boolean b, String message) {
        if (!b) {
            throw new AssertionError(message);
        }
    }

    void ensureLimits(long n, long from, long to, String name) {
        ensure(from <= n && n <= to, name + " must be from " + from + " to "
                + to);
    }

    void ensureLimits(int n, int from, int to, String name) {
        ensure(from <= n && n <= to, name + " must be from " + from + " to "
                + to);
    }

    void ensureString(String s, String alph, String text) {
        for (int i = 0; i < s.length(); i++) {
            ensure(alph.indexOf(s.charAt(i)) != -1, text);
        }
    }

}
