#include <cstdio>
#include <cassert>
#include <set>
#include <vector>
#include <map>
using namespace std;

#ifdef LOCAL
    #define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
    #define eprintf(...) 42;
#endif

const int MAXN = 100100;
const int MAXM = 600200;

class Edge {
public:
    int from;
    int to;
    int c;

    Edge() {};

    Edge(int from, int to, int c) {
        this->from = from;
        this->to = to;
        this->c = c;
    }
};

int n, m;
int source, sink;
map < pair <int, int>, int > mp, mpr;
Edge edges[MAXM];

inline int findEdge(int from, int to, bool rev) {
    if (rev) {
        auto it = mpr.find(make_pair(from, to));
        return it == mp.end() ? -1 : it->second;
    } else {
        auto it = mp.find(make_pair(from, to));
        return it == mp.end() ? -1 : it->second;
    }
}

inline int go(int v, int id, bool rev) {
    if (rev) {
        return edges[id].to == v ? edges[id].from : -1;
    } else {
        return edges[id].from == v ? edges[id].to : -1;
    }
}

pair <int, int> solve(const vector < vector <int> > &paths) {
    vector <int> candidates;
    for (auto &path : paths) {
        #ifdef DEEP_DEBUG
        eprintf("path: ");
        for (auto i : path) {
            eprintf("%d ", i);
        }
        eprintf("\n");
        #endif
        int start = source;
        int end = sink;
        int startPtr = 0;
        int prevStart = -1;
        int lastStartEdge = -1;
        while (startPtr < (int)path.size()) {
            int next = go(start, path[startPtr], false);
            #ifdef DEEP_DEBUG
            eprintf("start = %d edge = %d next = %d\n", start, path[startPtr], next);
            #endif
            if (next == -1) {
                break;
            }
            prevStart = start;
            lastStartEdge = path[startPtr];
            start = next;
            startPtr++;
        }
        if (startPtr == (int)path.size()) {
            continue;
        }
        int prevEnd = -1;
        int lastEndEdge = -1;
        int endPtr = (int)path.size() - 1;
        while (true) {
            int next = go(end, path[endPtr], true);
            #ifdef DEEP_DEBUG
            eprintf("end = %d edge = %d next = %d\n", end, path[endPtr], next);
            #endif
            if (next == -1) {
                break;
            }
            prevEnd = end;
            lastEndEdge = path[endPtr];
            end = next;
            endPtr--;
        }
        #ifdef DEEP_DEBUG
        eprintf("startPtr = %d endPtr = %d\n", startPtr, endPtr);
        eprintf("start = %d prevStart = %d lastStartEdge = %d\n", start, prevStart, lastStartEdge);
        eprintf("end = %d prevEnd = %d lastEndEdge = %d\n", end, prevEnd, lastEndEdge);
        #endif
        if (endPtr - startPtr >= 1) {
            return make_pair(path[startPtr], path[endPtr]);
        } else if (endPtr - startPtr == 0) {
            candidates.push_back(path[startPtr]);
        } else {
            if (prevStart == -1) {
                candidates.push_back(path[startPtr]);
            } else {
                int anotherEdge = findEdge(end, prevStart, true);
                assert(anotherEdge != -1);
                return make_pair(anotherEdge, path[startPtr]);
            }
        }
    }
    assert((int)candidates.size() == 2);
    return make_pair(candidates[0], candidates[1]);
}

int main() {
    freopen("zoo.in", "r", stdin);
    freopen("zoo.out", "w", stdout);
    while (scanf("%d%d", &n, &m) == 2) {
        mp.clear();
        mpr.clear();
        for (int i = 0; i < m; i++) {
            int a, b, c;
            scanf("%d%d%d", &a, &b, &c);
            --a, --b, --c;
            mp[make_pair(a, b)] = c;
            mpr[make_pair(b, c)] =  c;
            edges[c] = Edge(a, b, c);
        }
        source = n + 1;
        sink = n + 2;
        for (int i = 0; i < n; i++) {
            edges[m + i] = Edge(source, i, m + i);
        }
        for (int i = 0; i < n; i++) {
            edges[m + n + i] = Edge(i, sink, m + n + i);
        }
        int k;
        scanf("%d", &k);
        vector < vector <int> > paths(k);
        for (int i = 0; i < k; i++) {
            int l;
            scanf("%d", &l);
            paths[i].resize(l + 2);
            scanf("%d", &paths[i][0]);
            scanf("%d", &paths[i][l + 1]);
            paths[i][0]--, paths[i][l + 1]--;
            for (int j = 1; j < l + 1; j++) {
                scanf("%d", &paths[i][j]);
                --paths[i][j];
            }
            paths[i][0] += m;
            paths[i].back() += m + n;
        }
        pair <int, int> solution = solve(paths);
        printf("%d %d\n", solution.first + 1, solution.second + 1);
    }
    return 0;
}
