#include <bits/stdc++.h>

using namespace std;

constexpr int inf = 1e8;
constexpr double Param = 0;

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

int dd[1000][1000];

int main()
{
    // input graph
    int n, m;
    cin >> n >> m;
    int nn=n;
    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];
        nn+=f[i]-1;
    }
    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 target_list = [&](int now) {
        vector<pair<int, int>> tsp;
        vector<bool> visited(n + 1, 0);
        for (bool flag = 1; flag;)
        {
            visited[now] = 1;
            int ret = 1, d_min = inf;
            flag = 0;
            int x = 0;
            for (int j = 0; j < T_max; ++j)
            {
                if (order_list[j] && !visited[order[j]])
                {
                    if (d_min > dist[order[j]][now])
                    {
                        d_min = dist[order[j]][now];
                        ret = order[j];
                    }
                    flag = 1;
                }
            }
            tsp.emplace_back(now, ret);
            now = ret;
        }
        int N = tsp.size();
        for (int i = 0; i < N; ++i)
        {
            for (int j = i + 1; j < min(N,int(i+sqrt(nn))); ++j)
            {
                if (dist[tsp[i].first][tsp[i].second] + dist[tsp[j].first][tsp[j].second] > dist[tsp[i].first][tsp[j].first] + dist[tsp[i].second][tsp[j].second])
                {
                    swap(tsp[i].second, tsp[j].first);
                    for (int k = i + 1; k < j; ++k)
                    {
                        swap(tsp[k].first, tsp[k].second);
                    }
                    for (int k = 1; k < N; ++k)
                    {
                        if (i + k > j - k)
                            break;
                        swap(tsp[i + k], tsp[j - k]);
                        //swap(tsp[i+k].first,tsp[i+k].second);
                        //if(i+k==j-k)break;
                        //swap(tsp[j-k].first,tsp[j-k].second);
                    }
                }
            }
        }
        vector<int> ret(n + 1, 1);
        for (int i = 0; i < N; ++i)
        {
            ret[tsp[i].first] = tsp[i].second;
        }
        return ret;
    };
    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;
    vector<int> tar(n + 1, 1);
    for (int i = 0; i < T_max; ++i)
    {
        //if(i==1855)cout<<-target<<endl;
        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;
        }
        // target の更新
        if (i < T_max * Param)
        {
            if (n_ordered > 2 * n_item)
            {
                target = 1;
                potential = dist[target];
            }
        }
        else
        {
            if (n_ordered > 2 * n_item)
            {
                target = 1;
                potential = dist[target];
            }
        }
        // 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 の更新
            if (i < T_max * Param)
            {
                target = new_target(now);
                potential = dist[target];
            }
            else
            {
                tar = target_list(1);
                target = tar[now.u];
                potential = dist[target];
            }
            //
        }

        //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(i==1848)cout<<-target<<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;
            }
        }
        if (n_achieve)
        {
            if (i < T_max * Param)
            {
                target = new_target(now);
            }
            else
            {
                if (target != now.u)
                {
                    tar = target_list(now.u);
                    // target の更新
                    target = tar[now.u];
                    potential = dist[target];
                    //
                }
                else
                {
                    // target の更新
                    target = tar[now.u];
                    potential = dist[target];
                    //
                }
            }
        }
    }
}


Measured memory_bytes: 4052