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

constexpr int INF = INT_MAX;
constexpr int MAX_T = 10000;
constexpr int ROOT = 0;

struct Order{
    int id, dst, time;
    Order(){}
    Order(int id, int dst, int time):id(id),dst(dst),time(time){}
};

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;
    vector<bool> onTour;
    vector<vector<int> > orderIds;
    array<Order, MAX_T> orderList; 
    queue<int> orderQue;
    void init(int _V){
        V = _V;
        g.resize(V);
        onTour.resize(V);
        orderIds.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 add_order(int id, int dst, int time){
        orderList[id] = Order(id,dst,time);
        orderQue.push(id);
    }
    bool receive_order(){
        if(orderQue.empty()) return false;
        while(!orderQue.empty()){
            int id = orderQue.front();
            int dst = orderList[id].dst;
            orderIds[dst].emplace_back(id);
            orderQue.pop();
        }
        return true;
    }
    int count_order(int v){
        return orderIds[v].size();
    }
    void clear_order(int v){
        orderIds[v].clear();
    }
    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];
    }
};

struct Vehicle{
    int from, to;
    int cnt;
    bool onNode, onRoot;
    void init(){
        from = ROOT;
        to = -1;
        cnt = 0;
        onNode = true;
        onRoot = true;
    }
    void move(){
        if(cnt > 0){
            cnt--;
            onNode = false;
            onRoot = false;
        }
        if(cnt == 0){
            from = to;
            to = -1;
            onNode = true;
            onRoot = from == ROOT;
        }
    }
};

double randFloat(double r){
    return double(rand())/RAND_MAX*r;
}


vector<int> build_path(Graph &graph){
    vector<int> nn;

    vector<int> ls;
    for(int i=0;i<graph.V;i++){
        if(graph.count_order(i) > 0 && !graph.onTour[i]){
            ls.emplace_back(i);
        }
    }

    // nn = {0,0};
    // for(auto x : ls){
    //     int dmin = INF, j = 1;
    //     for(int i=1;i<int(nn.size());i++){
    //         int tmp = graph.dist[nn[i-1]][x] + graph.dist[x][nn[i]] - graph.dist[nn[i-1]][nn[i]];
    //         if(tmp < dmin){
    //             j = i;
    //             dmin = tmp;
    //         }
    //     }
    //     nn.insert(nn.begin()+j,x);
    // }

    nn.push_back(0);
    vector<bool> used(ls.size(),false);
    for(int i=0;i<ls.size();i++){
        int dmin = INF, k = -1;
        for(int j=0;j<ls.size();j++){
            if(used[j]) continue;
            if(dmin > graph.dist[nn.back()][ls[j]]){
                dmin = graph.dist[nn.back()][ls[j]];
                k = j;
            }
        }
        nn.emplace_back(ls[k]);
        used[k] = true;
    }
    nn.emplace_back(0);

    vector<int> bestls = nn;
    int dif = 0, dmin = 0;

    for(int loopCount = 0; loopCount < 100; loopCount++){
        for(int i=0;i<int(nn.size())-2;i++){
            for(int j=i+1;j<int(nn.size())-1;j++){
                int lhs = graph.dist[nn[i]][nn[i+1]] + graph.dist[nn[j]][nn[j+1]];
                int rhs = graph.dist[nn[i]][nn[j]] + graph.dist[nn[i+1]][nn[j+1]];
                if(lhs > rhs || exp(double(lhs-rhs)*1.3) > randFloat(1.0)){
                    dif += rhs-lhs; 
                    reverse(nn.begin()+i+1, nn.begin()+j+1);
                    if(dif < dmin){
                        dmin = dif;
                        bestls = nn;
                    }
                }
            }
        }
    }

    bestls.erase(bestls.begin());
    bestls.pop_back();
    return bestls;
}

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<int> ls;
    // vector<bool> remain(V);
    // int now = 0, cnt = 0, nxt = 0;

    Vehicle vehicle;
    vehicle.init();
    queue<int> tourQue;

    for(int t=0;t<Tmax;t++){
        // 情報の受け取り
        int n;
        cin >> n;
        for(int i=0;i<n;i++){
            int id, dst;
            cin >> id >> dst;
            dst--;
            graph.add_order(id, dst, t);

            // if(req.count(dst)==0){
            //     req.insert(dst);
            //     reqque.push(dst);
            // }
        }
        cin >> n;
        for(int i=0;i<n;i++){
            int id;
            cin >> id;
        }

        bool turnEnd = false;
        if(vehicle.onRoot){
            if(graph.receive_order()){
                vector<int> nxtTour = build_path(graph);
                for(auto &v : nxtTour){
                    tourQue.push(v);
                    graph.onTour[v] = true;
                }
            }
            else{
                cout << -1 << endl;
                turnEnd = true;
            }
        }

        if(!turnEnd){
            if(vehicle.onNode){
                int dst;
                while(!tourQue.empty()){
                    int v = tourQue.front();
                    if(graph.count_order(v) == 0) tourQue.pop();
                    else break;
                }
                if(!tourQue.empty()){
                    dst = tourQue.front();
                }else{
                    dst = ROOT;
                }
                for(auto &e : graph[vehicle.from]){
                    if(vehicle.from == e.to) continue;
                    if(graph.dist[vehicle.from][e.to] + graph.dist[e.to][dst] == graph.dist[vehicle.from][dst] && graph.dist[vehicle.from][e.to] == e.d){
                        vehicle.to = e.to;
                        vehicle.cnt = e.d;
                        break;
                    }
                }
            }
            if(vehicle.to != -1) cout << vehicle.to + 1 << endl;
            else cout << -1 << endl;
            vehicle.move();
            if(vehicle.onNode){
                graph.clear_order(vehicle.from);
                graph.onTour[vehicle.from] = false;
            }
        }

        // if(now == 0){
        //     if(!reqque.empty()){
        //         vector<int> tmp;
        //         while(!reqque.empty()){
        //             int dst = reqque.front();
        //             assert(dst != 0);
        //             if(!remain[dst]){
        //                 tmp.push_back(dst);
        //                 remain[dst] = true;
        //             }
        //             reqque.pop();
        //         }
        //         // ls.push_back(0);
        //         if(!tmp.empty()){
        //             tmp = build_path(tmp, graph);
        //             ls.insert(ls.end(), tmp.begin(), tmp.end());
        //         }
        //         req.clear();
        //     }
        //     remain[0] = true;
        // }

        // if(!ls.empty()){
        //     if(cnt <= 0){
        //         int dst = 0;
        //         while(!ls.empty()){
        //             dst = ls.front();
        //             if(remain[dst] && (now != dst)) break;
        //             ls.erase(ls.begin());
        //         }
        //         assert(!ls.empty());
        //         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;
        //         while(!ls.empty() && now == ls.front()) ls.erase(ls.begin());
        //     }
        // }
        // 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: 2004