#include <fstream>
#include <cstdio>
#include <vector>
#include <set>
#include <algorithm>

using namespace std;

const int MAXN = 1e5 + 10;

ifstream cin("zoo.in");
ofstream cout("zoo.out");

int n, m;

vector<pair<int, int> > edges;
vector<vector<int> > paths;
                     
bool check(int v, int u) {
    swap(edges[v], edges[u]);
    bool f = true;
    int ti = 0;
    vector<int> used(n);
    for (size_t q = 0; q < paths.size(); q++) {
        vector<int> path = paths[q];
        int cur = path[0];
        size_t i = 0;
        ti++;
        used[cur] = ti;
        
        while (++i < path.size() - 1) {
            pair<int, int> edge = edges[path[i]];
            if (cur != edge.first) {
                f = false;
                break;
            }
            cur = edge.second;
            if (used[cur] == ti) {
                f = false;
                break;
            }
            used[cur] = ti;
        }
        if (cur != path.back()) {
            f = false;
        }
        if (!f) break;
    }
    swap(edges[v], edges[u]);
    return f;
}

bool check(int m) {
    for (int i = 0; i < m; i++)
        for (int j = i + 1; j < m; j++) {
            if (check(i, j)) {
                cout << i + 1 << " " << j + 1 << endl;
                return true;
            }
        }
    return false;
}

int main(){
    cin >> n >> m;
    edges.resize(m);

    for (int i = 0; i < m; i++) {
        int to, from, num;
        cin >> from >> to >> num;
        num--, from--, to--;
        edges[num] = make_pair(from, to);
    }

    int k;
    cin >> k;
    paths.resize(k);
    for (int i = 0; i < k; i++) {
        int len;
        cin >> len;
        paths[i].resize(len + 2);
        cin >> paths[i][0] >> paths[i][1 + len];
        paths[i][0]--, paths[i][1 + len]--;
        for (int j = 1; j < len + 1; j++) {
            cin >> paths[i][j];
            paths[i][j]--;
        }
    }
    if (check(m)) return 0;
}
