#include <bits/stdc++.h>
using namespace std;

constexpr int INF = INT_MAX;

struct edge{
    int from, to, d;
    edge(){}
    edge(int from, int to, int d):from(from),to(to),d(d){}
};

struct Graph{
    int V;
    vector<vector<edge> > g;
    vector<vector<int> > dist;
    void init(int _V){
        V = _V;
        g.resize(V);
    }
    void add_edge(int u, int v, int d){
        g[u].push_back(edge(u,v,d));
        g[v].push_back(edge(v,u,d));
    }
    void wf(){
        dist.resize(V, vector<int>(V, INF/3));
        for(auto &v : g){
            for(auto &e : v){
                int u = e.from, v = e.to, d = e.d;
                dist[u][v] = d;
                dist[v][u] = d;
            }
        }
        for(int i=0;i<V;i++){
            dist[i][i] = 0;
        }
        for(int k=0;k<V;k++){
            for(int i=0;i<V;i++){
                for(int j=0;j<V;j++){
                    dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j]);
                }
            }
        }
    }
    vector<edge> operator [](int i) {
        return g[i];
    }
};

int main(void){
    int V, E, Tmax;
    cin >> V >> E;
    Graph graph;
    graph.init(V);
    for(int i=0;i<E;i++){
        int u, v, d;
        cin >> u >> v >> d;
        u--; v--;
        graph.add_edge(u,v,d);
    }
    vector<int> f(V);
    for(int i=0;i<V;i++){
        cin >> f[i];
    }
    cin >> Tmax;
    graph.wf();
    set<int> req;
    queue<int> reqque, que;
    vector<bool> remain(V);
    int now = 0, cnt = 0, nxt = 0;
    for(int t=0;t<Tmax;t++){
        // 情報の受け取り
        int n;
        cin >> n;
        for(int i=0;i<n;i++){
            int id, dst;
            cin >> id >> dst;
            if(req.count(dst-1)==0){
                req.insert(dst-1);
                reqque.push(dst-1);
            }
        }
        cin >> n;
        for(int i=0;i<n;i++){
            int id;
            cin >> id;
        }
        if(now == 0){
            if(!reqque.empty()){
                while(!reqque.empty()){
                    int dst = reqque.front();
                    que.push(dst);
                    remain[dst] = true;
                    reqque.pop();
                }
                que.push(0);
                remain[0] = true;
                req.clear();
            }
        }
        if(!que.empty()){
            if(cnt == 0){
                int dst;
                while(!que.empty()){
                    dst = que.front();
                    if(remain[dst]) break;
                    que.pop();
                }
                for(auto &e : graph[now]){
                    if(now == e.to) continue;
                    if(graph.dist[now][e.to] + graph.dist[e.to][dst] == graph.dist[now][dst] && graph.dist[now][e.to] == e.d){
                        nxt = e.to;
                        cnt = e.d;
                        break;
                    }
                }
            }
            cout << nxt+1 << endl;
            now = -1;
            cnt--;
            if(cnt == 0){
                now = nxt;
                remain[now] = false;
                if(now == que.front()) que.pop();
            }
        }
        else{
            cout << -1 << endl;
        }
        string verdict;
        cin >> verdict;
        if(verdict == "NG"){
            return 0;
        }
        cin >> n;
        for(int i=0;i<n;i++){
            int id = 0;
            cin >> id;
        }
    }
    return 0;
}

Measured memory_bytes: 1992