#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>
#include <utility>
#include <random>
#include <string.h>
#define inf 100000000
using namespace std;
typedef long long int ll;

const unsigned long long int cycle_per_sec = 2800000000;

unsigned long long int getCycle() {
    unsigned int low, high;
    __asm__ volatile ("rdtsc" : "=a" (low), "=d" (high));
    return ((unsigned long long int)low) | ((unsigned long long int)high << 32);
}

double getTime (unsigned long long int begin_cycle) {
    return (double)(getCycle() - begin_cycle) / cycle_per_sec;
}

class XorShift {
public:
    unsigned int x;
    unsigned int y;
    unsigned int z;
    unsigned int w;
    unsigned int t;
    
    XorShift(int tmp) {
        mt19937 rnd(tmp);
        x = rnd();
        y = rnd();
        z = rnd();
        w = rnd();
        t = 1;
    }
    
    int rand() {
        t = x ^ (x << 11);
        x = y;
        y = z;
        z = w;
        w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
        return w & 0x7fffffff;
    }
} engine(rand());

const int PEAK_UPDATE_SPAN = 100;

int V, E;
vector<pair<int,int>> EDGE[400];
int DIST[400][400];
const int Tmax = 10000;
int F[400];
int ORDER[Tmax];
int PEAK;
int ORDER_FREQ[PEAK_UPDATE_SPAN];

struct STATE {
    ll score;
    int tour_size;
    pair<int,int> tours[Tmax];
    int orders_si[400];
    int orders[400][100];
    int havings_si[400];
    int havings[400][100];
    int pos; // -1 if on edge

    STATE() {
        score = 0;
        tour_size = 0;
        pos = 0;
        memset(orders_si, 0, sizeof(orders_si));
        memset(havings_si, 0, sizeof(havings_si));
    }

    void copy (STATE& st) {
        score = st.score;
        tour_size = st.tour_size;
        pos = st.pos;
        for (int i = 0; i < V; i++) {
            orders_si[i] = st.orders_si[i];
            for (int j = 0; j < orders_si[i]; j++) {
                orders[i][j] = st.orders[i][j];
            }
            havings_si[i] = st.havings_si[i];
            for (int j = 0; j < havings_si[i]; j++) {
                havings[i][j] = st.havings[i][j];
            }
        }
    }
};

void init() {
    PEAK = 4750;
    for (int i = 0; i < V; i++) {
        for (int j = 0; j < V; j++) {
            DIST[i][j] = inf;
        }
        DIST[i][i] = 0;
    }
    for (int i = 0; i < V; i++) {
        for (pair<int, int>& e: EDGE[i]) {
            DIST[i][e.first] = min(DIST[i][e.first], e.second);
        }
    }
    for (int u = 0; u < V; u++) {
        for (int i = 0; i < V; i++) {
            for (int j = 0; j < V; j++) {
                DIST[i][j] = min(DIST[i][j], DIST[i][u] + DIST[u][j]);
            }
        }
    }
}

void input() {
    cin >> V >> E;
    for(int i = 0; i < E; i++) {
        // recieve edges
        int u, v, d; std::cin >> u >> v >> d;
        u--; v--;
        EDGE[u].push_back({v, d});
        EDGE[v].push_back({u, d});
    }
    for (int i = 0; i < V; i++) {
        cin >> F[i];
    }
    int _;
    cin >> _;
}

ll last_tsp(STATE& state, int now, int limit) {
    ll ret = 0;

    limit = limit-now;
    vector<int> points;
    for (int i = 0; i < V; i++) {
        if (state.havings_si[i]) {
            points.push_back(i);
        }
    }
    points.push_back(0);
    sort(points.begin(), points.end());

    vector<bool> saw((int)points.size(), false);
    int pre = 0;
    saw[pre] = true;
    for (int i = 0; i < (int)points.size()-1; i++) {
        int dis = inf;
        int to = -1;
        for (int j = 0; j < (int)points.size(); j++) {
            if (saw[j]) continue;
            if (dis > DIST[points[pre]][points[j]]) {
                dis = DIST[points[pre]][points[j]];
                to = j;
            }
        }
        limit -= dis;
        if (limit < 0) {
            return ret;
        }

        now += DIST[points[pre]][points[to]];
        for (int j = 0; j < state.havings_si[points[to]]; j++) {
            ll dif = now+1-state.havings[points[to]][j];
            ret += Tmax*Tmax-dif*dif;
        }

        pre = to;
        saw[to] = true;
    }
    return ret;
}

/*
ll calc_tour_score (STATE& state, vector<int>& path, int now) {
    int now_pos = 0;
    ll ret = 0;
    for (int i = 0; i < (int)path.size(); i++) {
        now += DIST[now_pos][path[i]];
        for (int j = 0; j < state.havings_si[path[i]]; j++) {
            ll dif = now+1-state.havings[path[i]][j];
            ret += Tmax*Tmax-dif*dif;
        }
        now_pos = path[i];
    }
    return ret;
}
*/

// score, len
pair<ll,int> cancel_path (STATE& origin_state, vector<int>& path, int now, int limit) {
    STATE state;
    state.copy(origin_state);
    state.pos = 0;

    int now_pos = 0;
    ll best_score = 0;
    ll sum = 0;
    int best_len = path.size()-1;
    for (int i = 0; i < (int)path.size()-1; i++) {
        for (int t = 0; t < DIST[now_pos][path[i]]; t++) {
            if (ORDER[now+t] != -1) {
                state.orders[ORDER[now+t]][state.orders_si[ORDER[now+t]]++] = now+t;
            }
        }
        now += DIST[now_pos][path[i]];
        now_pos = path[i];
        for (int j = 0; j < state.havings_si[now_pos]; j++) {
            ll dif = now+1-state.havings[now_pos][j];
            sum += Tmax*Tmax-dif*dif;
        }
        state.havings_si[now_pos] = 0;

        STATE new_state;
        new_state.copy(state);
        int zero_t = now+DIST[now_pos][0];
        for (int t = 0; t < DIST[now_pos][0]; t++) {
            if (ORDER[now+t] != -1) {
                new_state.orders[ORDER[now+t]][new_state.orders_si[ORDER[now+t]]++] = now+t;
            }
        }
        for (int v = 0; v < V; v++) {
            for (int j = 0; j < new_state.orders_si[v]; j++) {
                new_state.havings[v][new_state.havings_si[v]++] = new_state.orders[v][j];
            }
        }

        ll now_score = sum + last_tsp(new_state, zero_t, limit);
        if (best_score < now_score) {
            best_score = now_score;
            best_len = i+1;
        }
    }
    return pair<ll,int>(best_score, best_len);
}

pair<ll,int> skip_path (STATE& origin_state, vector<int> path, int now, int limit) {
    STATE state;
    state.copy(origin_state);
    state.pos = 0;

    if (path.size() <= 2) return pair<ll,int>(-1,-1);

    int most_cost = DIST[0][path[0]]+DIST[path[0]][path[1]]-DIST[0][path[1]];
    int most_pos = 0;
    for (int i = 0; i < (int)path.size()-2; i++) {
        int c = DIST[path[i]][path[i+1]]+DIST[path[i+1]][path[i+2]]-DIST[path[i]][path[i+2]];
        if (c > most_cost) {
            most_cost = c;
            most_pos = i+1;
        }
    }
    if (most_cost == 0) return pair<ll,int>(-1,-1);

    path.erase(path.begin()+most_pos);

    int now_pos = 0;
    ll sum = 0;
    for (int i = 0; i < (int)path.size(); i++) {
        for (int t = 0; t < DIST[now_pos][path[i]]; t++) {
            if (ORDER[now+t] != -1) {
                state.orders[ORDER[now+t]][state.orders_si[ORDER[now+t]]++] = now+t;
            }
        }
        now += DIST[now_pos][path[i]];
        now_pos = path[i];
        for (int j = 0; j < state.havings_si[now_pos]; j++) {
            ll dif = now+1-state.havings[now_pos][j];
            sum += Tmax*Tmax-dif*dif;
        }
        state.havings_si[now_pos] = 0;
    }
    for (int v = 0; v < V; v++) {
        for (int j = 0; j < state.orders_si[v]; j++) {
            state.havings[v][state.havings_si[v]++] = state.orders[v][j];
        }
    }

    ll now_score = sum + last_tsp(state, now, limit);
    return pair<ll,int>(now_score, most_pos);
}

void optimize_path(vector<int>& path) {
    if (path.size() <= 3) return;
    int n = path.size();
    double start_temp = 5.0, end_temp = 1.0;
    unsigned long long int start_cycle = getCycle();

    double TIME_LIMIT = 0.5;

    double now_t = 0.0;
    for (int loop = 0;; loop++) {
        if (loop%1000 == 0) {
            now_t = getTime(start_cycle);
            if (now_t > TIME_LIMIT) break;
        }

        if (engine.rand()%2) {
            int i = engine.rand()%n;
            int j = engine.rand()%n;
            if (i >= j) swap(i,j);
            if (j-i <= 1 || j-i == n-1) continue;


            int j2 = (j+1)%n;
            int ori_dist = DIST[path[i]][path[i+1]] + DIST[path[j]][path[j2]];
            int new_dist = DIST[path[i]][path[j]] + DIST[path[i+1]][path[j2]];

            double temp = start_temp + (end_temp - start_temp) * now_t / TIME_LIMIT;
            double prob = exp((ori_dist-new_dist)/temp);

            if (prob > (engine.rand()%inf)/(double)inf) {
                int swap_st = i+1, swap_en = j;
                while (swap_st < swap_en) {
                    swap(path[swap_st], path[swap_en]);
                    swap_st++;
                    swap_en--;
                }
            }
        } else {
            int i = engine.rand()%(n-1)+1;
            int j = engine.rand()%n;
            if (i == j || i-1 == j) continue;

            int i2 = (i+1)%n;
            int j2 = (j+1)%n;
            int ori_dist = DIST[path[i-1]][path[i]] + DIST[path[i]][path[i2]] + DIST[path[j]][path[j2]];
            int new_dist = DIST[path[i-1]][path[i2]] + DIST[path[j]][path[i]] + DIST[path[i]][path[j2]];

            double temp = start_temp + (end_temp - start_temp) * now_t / TIME_LIMIT;
            double prob = exp((ori_dist-new_dist)/temp);

            if (prob > (engine.rand()%inf)/(double)inf) {
                if (i > j) {
                    int tmp = path[i];
                    for (int k = i-1; k > j; k--) path[k+1] = path[k];
                    path[j+1] = tmp;
                } else {
                    int tmp = path[i];
                    for (int k = i+1; k <= j; k++) path[k-1] = path[k];
                    path[j] = tmp;
                }
            }
        }
    }
    /*
    int n = path.size();
    while (true) {
        bool end = true;
        for (int i = 0; i < n; i++) {
            for (int j = i+2; j < n; j++) {
                if (i == 0 && j == n-1) continue;
                int j2 = (j+1)%n;
                int ori_dist = DIST[path[i]][path[i+1]] + DIST[path[j]][path[j2]];
                int new_dist = DIST[path[i]][path[j]] + DIST[path[i+1]][path[j2]];
                if (ori_dist > new_dist) {
                    end = false;
                    int swap_st = i+1, swap_en = j;
                    while (swap_st < swap_en) {
                        swap(path[swap_st], path[swap_en]);
                        swap_st++;
                        swap_en--;
                    }
                }
            }
        }
        if (end) break;
    }
    int loop = 0;
    while (true) {
        loop++;
        bool end = true;
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (i == j || i-1 == j) continue;
                int i2 = (i+1)%n;
                int j2 = (j+1)%n;
                int ori_dist = DIST[path[i-1]][path[i]] + DIST[path[i]][path[i2]] + DIST[path[j]][path[j2]];
                int new_dist = DIST[path[i-1]][path[i2]] + DIST[path[j]][path[i]] + DIST[path[i]][path[j2]];
                if (ori_dist > new_dist) {
                    end = false;
                    if (i > j) {
                        int tmp = path[i];
                        for (int k = i-1; k > j; k--) path[k+1] = path[k];
                        path[j+1] = tmp;
                    } else {
                        int tmp = path[i];
                        for (int k = i+1; k <= j; k++) path[k-1] = path[k];
                        path[j] = tmp;
                    }
                }
            }
        }
        cerr << loop << endl;
        if (end) break;
    }
    */
}


void tsp(STATE& state, int now_t) {
    vector<int> points;
    for (int i = 0; i < V; i++) {
        if (state.havings_si[i]) {
            points.push_back(i);
        }
    }
    if (points.size() == 0) return;
    points.push_back(0);
    sort(points.begin(), points.end());

    vector<bool> saw((int)points.size(), false);
    vector<int> path((int)points.size());
    int pre = 0;
    path[0] = points[0];
    saw[pre] = true;
    for (int i = 0; i < (int)points.size()-1; i++) {
        int dis = inf;
        int to = -1;
        for (int j = 0; j < (int)points.size(); j++) {
            if (saw[j]) continue;
            if (dis > DIST[points[pre]][points[j]]) {
                dis = DIST[points[pre]][points[j]];
                to = j;
            }
        }
        pre = to;
        saw[to] = true;
        path[i+1] = points[to];
    }
    optimize_path(path);
    for (int i = 0; i < (int)path.size()-1; i++) {
        path[i] = path[i+1];
    }
    path[(int)path.size()-1] = points[0];

    int path_time = 0;
    {
        int now_pos = 0;
        for (int i = 0; i < (int)path.size()-1; i++) {
            path_time += DIST[now_pos][path[i]];
            now_pos = path[i];
        }
        /*
        if (now_t + path_time >= Tmax) {
            last_optimize_path(state, path);
        }
        */
    }

    pair<ll, int> best_path1, best_path2;
    best_path1 = cancel_path(state, path, now_t, now_t+path_time);
    //cerr << path.size() << endl;
    //cerr << best_path1.first << " " << best_path1.second << endl;
    reverse(path.begin(), path.end()-1);
    best_path2 = cancel_path(state, path, now_t, now_t+path_time);
    //cerr << best_path2.first << " " << best_path2.second << endl;
    //cerr << endl;

    pair<ll, int> skip = skip_path(state, path, now_t, now_t+path_time);

    if (best_path1.first > best_path2.first) {
        reverse(path.begin(), path.end()-1);
    } else {
        best_path1 = best_path2;
    }

///////////////////////////
    skip.first = -1; 

    if (best_path1.first > skip.first) {
        path[best_path1.second++] = points[0];

        int now_pos = points[0];
        for (int i = 0; i < best_path1.second; i++) {
            while (now_pos != path[i]) {
                for (pair<int,int> e : EDGE[now_pos]) {
                    if (DIST[now_pos][path[i]] == e.second + DIST[e.first][path[i]]) {
                        state.tours[state.tour_size++] = pair<int,int>(e.first, e.second);
                        now_pos = e.first;
                        break;
                    }
                }
            }
        }
    } else {
        cerr << "skip" << endl;
        int now_pos = points[0];
        for (int i = 0; i < (int)path.size(); i++) {
            if (i == skip.second) {
                continue;
            }
            while (now_pos != path[i]) {
                for (pair<int,int> e : EDGE[now_pos]) {
                    if (DIST[now_pos][path[i]] == e.second + DIST[e.first][path[i]]) {
                        state.tours[state.tour_size++] = pair<int,int>(e.first, e.second);
                        now_pos = e.first;
                        break;
                    }
                }
            }
        }

    }
    /*
    int now_pos = points[0];
    for (int i = 0; i < (int)path.size(); i++) {
        while (now_pos != path[i]) {
            for (pair<int,int> e : EDGE[now_pos]) {
                if (DIST[now_pos][path[i]] == e.second + DIST[e.first][path[i]]) {
                    state.tours[state.tour_size++] = pair<int,int>(e.first, e.second);
                    now_pos = e.first;
                    break;
                }
            }
        }
    }
    */
    reverse(state.tours, state.tours+state.tour_size);
}


void update_peak(int si) {
    int bestp = PEAK;
    double bestval = (double)inf;
    for (int p = 0; p < 9500; p += 100) {
        double val = 0.0;
        for (int i = 0; i < si; i++) {
            double ratio = ORDER_FREQ[i]/(double)PEAK_UPDATE_SPAN;
            int mid = i*PEAK_UPDATE_SPAN + PEAK_UPDATE_SPAN/2;
            double ex = (p>=mid)?(mid/(double)p):((9500-mid)/(double)(9500-p));
            val += (ratio-ex)*(ratio-ex);
        }
        if (val < bestval) {
            bestval = val;
            bestp = p;
        }
    }
    PEAK = bestp;
}

void update_future(int t) {
    int sum = 0;
    int hoge[800];
    for (int i = 0; i < V; i++) {
        for (int j = 0; j < F[i]; j++) {
            hoge[sum++] = i;
        }
    }
    for (int i = t+1; i < Tmax; i++) {
        double order_prob = (PEAK>=i)?(i/(double)PEAK):((9500-i)/(double)(9500-PEAK));
        if ((engine.rand()%inf)/(double)inf < order_prob) {
            ORDER[i] = -1;
            continue;
        }
        ORDER[i] = hoge[engine.rand()%sum];
    }
}

void solve() {
    STATE now_state;
    for (int i = 0; i < Tmax; i++) {
        ORDER[i] = -1;
        int Nnew, trash;
        cin >> Nnew;
        if (Nnew == 1) {
            cin >> trash >> ORDER[i];
            ORDER[i]--;
        }
        int Nput;
        cin >> Nput;
        for (int j = 0; j < Nput; j++) cin >> trash;
        if (ORDER[i] != -1) {
            ORDER_FREQ[i*PEAK_UPDATE_SPAN/Tmax]++;
            now_state.orders[ORDER[i]][now_state.orders_si[ORDER[i]]++] = i;
        }
        if (i%PEAK_UPDATE_SPAN == PEAK_UPDATE_SPAN-1) {
            update_peak(i*PEAK_UPDATE_SPAN/Tmax+1);
            //update_future(i);
        }
        if (now_state.pos == 0) {
            for (int v = 0; v < V; v++) {
                for (int j = 0; j < now_state.orders_si[v]; j++) {
                    now_state.havings[v][now_state.havings_si[v]++] = now_state.orders[v][j];
                }
                now_state.orders_si[v] = 0;
            }
        }
        if (!now_state.tour_size) {
            tsp(now_state, i);
        }
        if (now_state.tour_size) {
            int to = now_state.tours[now_state.tour_size-1].first;
            cout << to+1 << endl;
            now_state.tours[now_state.tour_size-1].second--;
            if (!now_state.tours[now_state.tour_size-1].second) {
                for (int j = 0; j < now_state.havings_si[to]; j++) {
                    ll dif = i+1-now_state.havings[to][j];
                    now_state.score += Tmax*Tmax-dif*dif;
                }
                now_state.havings_si[to] = 0;
                now_state.tour_size--;
                /*
                if (now_state.tour_size == 0) {
                    cerr << now_state.score << endl;
                }
                */
                now_state.pos = to;
            } else {
                now_state.pos = -1;
            }
        } else {
            cout << -1 << endl;
        }
        string verdict; cin >> verdict;
        if (verdict == "NG") {
            exit(1);
        }

        // the number of orders that have been delivered at time t.
        int Nachive; cin >> Nachive;
        for(int j = 0; j < Nachive; j++) {
            int achive_id; cin >> achive_id;
        }
    }
}

int main() {
    input();

    init();

    solve();

    return 0;
}


Measured memory_bytes: 2772