import java.io.*;
import java.util.*;

public class zoo_nn {

  static class Edge {
    int from;
    int to;
    int id;

    public Edge(int from, int to, int id) {
      this.from = from;
      this.to = to;
      this.id = id;
    }

  }

  static List<Edge>[] edges;

  public static void main(String[] args) throws IOException {
    Scanner sc = new Scanner(new File("zoo.in"));
    PrintWriter out = new PrintWriter("zoo.out");
    int n = sc.nextInt();
    int m = sc.nextInt();
    Edge[] allEdges = new Edge[m];
    for (int i = 0; i < m; i++) {
      int v = sc.nextInt() - 1;
      int u = sc.nextInt() - 1;
      int id = sc.nextInt() - 1;
      allEdges[id] = new Edge(v, u, id);
    }
    int k = sc.nextInt();
    int[][] paths = new int[k][];
    int[] pathStart = new int[k];
    int[] pathFinish = new int[k];
    for (int i = 0; i < k; i++) {
      int len = sc.nextInt();
      pathStart[i] = sc.nextInt() - 1;
      pathFinish[i] = sc.nextInt() - 1;
      paths[i] = new int[len];
      for (int j = 0; j < len; j++) {
        paths[i][j] = sc.nextInt() - 1;
      }
    }
    int[] id = new int[m];
    for (int i = 0; i < m; i++) {
      id[i] = i;
    }
    bad: {
      Set<Integer> change1 = new HashSet<>();
      Set<Integer> change2 = new HashSet<>();
      for (int i = 0; i < k; i++) {
        List<Integer> ids = new ArrayList<>();
        ids.add(pathStart[i]);
        for (int e : paths[i]) {
          ids.add(allEdges[e].from);
          ids.add(allEdges[e].to);
        }
        ids.add(pathFinish[i]);
        int countBad = 0;
        for (int j = 0; j < ids.size(); j += 2) {
          if (ids.get(j).intValue() != ids.get(j + 1).intValue()) {
            countBad++;
          }
        }
        if (countBad > 1)
          break bad;
        if (countBad == 0)
          continue;
        for (int j = 0; j < ids.size(); j += 2) {
          if (ids.get(j).intValue() != ids.get(j + 1).intValue()) {
            int f1 = j / 2 - 1;
            int f2 = j / 2;
            if (f1 >= 0)
              change1.add(paths[i][f1]);
            if (f2 < paths[i].length)
              change2.add(paths[i][f2]);
          }
        }
      }
      int[] ans = check(change1, pathStart, pathFinish, paths, allEdges);
      if (ans != null) {
        out.println((1 + ans[0]) + " " + (1 + ans[1]));
        out.close();
        sc.close();
        return;
      }
      ans = check(change2, pathStart, pathFinish, paths, allEdges);
      if (ans != null) {
        out.println((1 + ans[0]) + " " + (1 + ans[1]));
        out.close();
        sc.close();
        return;
      }
    }
    {
      Set<Integer> change = new HashSet<>();
      for (int i = 0; i < k; i++) {
        List<Integer> ids = new ArrayList<>();
        ids.add(pathStart[i]);
        for (int e : paths[i]) {
          ids.add(allEdges[e].from);
          ids.add(allEdges[e].to);
        }
        ids.add(pathFinish[i]);
        for (int j = 0; j < paths[i].length; j++) {
          int left = 2 * j;
          int right = 2 * j + 2;
          if (ids.get(left).intValue() != ids.get(left + 1)
              && ids.get(right).intValue() != ids.get(right + 1)) {
            change.add(paths[i][j]);
          }
        }
      }
      int[] ans = check(change, pathStart, pathFinish, paths, allEdges);
      if (ans != null) {
        out.println((1 + ans[0]) + " " + (1 + ans[1]));
        out.close();
        sc.close();
        return;
      }
    }
    throw new AssertionError();
    // out.close();
    // sc.close();
  }

  static String print(Set<Integer> change) {
    Iterator<Integer> it = change.iterator();
    int v = it.next();
    int u = it.next();
    return (v + 1) + " " + (u + 1);
  }

  static int[] check(Set<Integer> change, int[] pathStart, int[] pathFinish,
      int[][] paths, Edge[] edges) {
    for (int v : change) {
      all: for (int u : change) {
        if (v == u)
          break;
        int m = edges.length;
        int[] id = new int[m];
        for (int i = 0; i < m; i++) {
          id[i] = i == v ? u : i == u ? v : i;
        }
        for (int i = 0; i < paths.length; i++) {
          List<Integer> ids = new ArrayList<>();
          ids.add(pathStart[i]);
          for (int j : paths[i]) {
            ids.add(edges[id[j]].from);
            ids.add(edges[id[j]].to);
          }
          ids.add(pathFinish[i]);
          int countBad = 0;
          for (int j = 0; j < ids.size(); j += 2) {
            if (ids.get(j) != ids.get(j + 1).intValue()) {
              countBad++;
            }
          }
          if (countBad > 0)
            continue all;
        }
        return new int[] { v, u };
      }
    }
    return null;
  }

  static class Scanner extends BufferedReader {

    StringTokenizer st;

    Scanner(File f) throws IOException {
      super(new FileReader(f));
    }

    String next() throws IOException {
      while (st == null || !st.hasMoreTokens()) {
        String line = readLine();
        if (line == null)
          return null;
        st = new StringTokenizer(line);
      }
      return st.nextToken();
    }

    int nextInt() throws IOException {
      return Integer.parseInt(next());
    }
  }
}
