#include<bits/stdc++.h>

using namespace std;

constexpr int inf=1e8;

struct pos{
    int u,v;
    int d_u,d_v;
};

int dd[1000][1000];

int main(){
    // input graph
    int n,m;
    cin>>n>>m;
    vector<vector<int>>dist(n+1,vector<int>(n+1,inf));
    vector<vector<int>>edge(n+1,vector<int>(n+1,inf));
    for(int i=1;i<=n;++i){
        dist[i][i]=0;
    }
    vector<vector<int>>g(n+1);
    for(int i=0;i<m;++i){
        int u,v;
        cin>>u>>v;
        dd[u][v]=dd[v][u]=i;
        cin>>dist[u][v];
        dist[v][u]=dist[u][v];
        edge[u][v]=edge[v][u]=dist[u][v];
        g[u].push_back(v);
        g[v].push_back(u);
    }
    vector<int>f(n);
    for(int i=0;i<n;++i){
        cin>>f[i];
    }
    int T_max;
    cin>>T_max;

    //warshall-floyd
    for(int k=1;k<=n;++k){
        for(int i=1;i<=n;++i){
            for(int j=1;j<=n;++j){
                dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
            }
        }
    }
    int target=1;
    double E_shop=0;
    vector<int>potential(n+1);
    vector<int>order(T_max);
    vector<bool>order_list(T_max);
    auto new_target = [&](pos now){
        int ret=1,d_min=inf;
        for(int j=0;j<T_max;++j){
            if(order_list[j]){
                if(d_min>min(dist[order[j]][now.u]+now.d_u,dist[order[j]][now.v]+now.d_v)){
                    d_min=min(dist[order[j]][now.u]+now.d_u,dist[order[j]][now.v]+now.d_v);
                    ret=order[j];
                }
            }
        }
        potential=dist[ret];
        return ret;
    };
    pos now;
    now.u=now.v=1;
    now.d_u=now.d_v=0;
    int n_ordered=0,n_item=0;
    for(int i=0;i<T_max;++i){
        int n_order;
        cin>>n_order;
        n_ordered+=n_order;
        for(int j=0;j<n_order;++j){
            int id,dst;
            cin>>id>>dst;
            order[id]=dst;
        }
        // shop-enelgy の更新
        if(n_ordered>2*n_item){
            target=1;
            potential=dist[1];
        }
        // to do
        int n_put;
        cin>>n_put;
        n_item+=n_put;
        if(n_put){
            E_shop=0;
            for(int j=0;j<n_put;++j){
                int put_id;
                cin>>put_id;
                order_list[put_id]=true;
            }
            // target の更新
            target=new_target(now);
            //
        }

        //cout<<target<<endl;
        // 行動決め(本質)
        if(target==-1){
            cout<<-2<<endl;
        }else{
            int ret=-1;
            // 頂点上にいる場合
            if(now.u==now.v){
                double min_potential_div=0;
                for(auto dst:g[now.u]){
                    if(min_potential_div>double(potential[dst]-potential[now.u])/edge[now.u][dst]){
                        min_potential_div=double(potential[dst]-potential[now.u])/edge[now.u][dst];
                        ret=dst;
                    }
                }
            }
            // 辺上にいる場合
            else{
                if(potential[now.u]+now.d_u<potential[now.v]+now.d_v){
                    ret=now.u;
                }else{
                    ret=now.v;
                }
            }
            cout<<ret<<endl;

            // 現在位置の更新
            if(ret!=-1){
                // 頂点上にいる場合
                if(now.u==now.v){
                    now.v=ret;
                    now.d_u=1;
                    now.d_v=dist[now.u][now.v]-now.d_u;
                    if(now.d_v==0){
                        now.u=now.v;
                        now.d_u=0;
                    }
                }
                // 辺上にいる場合
                else{
                    if(ret==now.u){
                        now.d_u--;
                        now.d_v++;
                        if(now.d_u==0){
                            now.v=now.u;
                            now.d_v=0;
                        }
                    }
                    if(ret==now.v){
                        now.d_u++;
                        now.d_v--;
                        if(now.d_v==0){
                            now.u=now.v;
                            now.d_u=0;
                        }
                    }
                }
            }
            //if(ret==7)cout<<dist[232][224]<<endl;
        }
        //
        // to do

        string judge;
        cin>>judge;
        if(judge=="NG")return 0;
        int n_achieve;
        cin>>n_achieve;
        n_ordered-=n_achieve;
        n_item-=n_achieve;
        for(int j=0;j<n_achieve;++j){
            int achieve_id;
            cin>>achieve_id;
            if(order_list[achieve_id]){
                order_list[achieve_id]=false;
                // target の更新
                target=new_target(now); 
                //
            }
        }
    }
}

Measured memory_bytes: 4052