//{{{
#include<algorithm>
#include<cmath>
#include<deque>
#include<fstream>
#include<iomanip>
#include<iostream>
#include<map>
#include<numeric>
#include<queue>
#include<random>
#include<set>
#include<sstream>
#include<sys/time.h>
#include<unordered_map>
#include<unordered_set>
#include<vector>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include"rprint2.hpp"
#else
#define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ }
FUNC(printde) FUNC(printde4) FUNC(printdbe) FUNC(printdwe) FUNC(printdu);
#endif
template <class S, class T>
istream& operator >> (istream& in, pair<S, T>& p){ return in >> p.first >> p.second; }
template <template <class T, class = std::allocator<T>> class V, class E>
istream& operator >> (istream& in, V<E>& v){ for(auto& e : v){ in >> e; } return in; }
//}}}

#ifdef LOCAL
#define LIMIT 1.0
#else
#define LIMIT 8.0
#endif

mt19937 mt;

// structs
struct Edge{
    int cost, from, dest;
    Edge(){ }
    Edge(int cost, int dest): cost(cost), from(-1), dest(dest){ }
    Edge(int cost, int from, int dest): cost(cost), from(from), dest(dest){ }
    // bool operator == (const Edge& e) const { return cost == e.cost; }
    // bool operator < (const Edge& e) const { return cost < e.cost; }
    // bool operator == (const Edge& e) const { return from == e.from && dest == e.dest; }
    ostream& operator >> (ostream& out) const {
        return out << '(' << cost << ':' << from << ' ' << dest << ')';
    }
};
struct Graph{
    struct Node{
        vector<Edge> edges;
    };
    vector<Node> nodes;
    Graph(int num): nodes(num){ }
    int id(Node* n){ return n - &nodes[0]; }
    int id(Node& n){ return &n - &nodes[0]; }
    // // void print(){ for(auto& node : nodes){ prints(id(node), node.outEdges); } }
    void add1Edge(int from, int dest, int cost = 1){
        nodes[from].edges.emplace_back(cost, dest);
    }
    void add2Edge2(int from, int dest, int cost = 1){
        add1Edge(from, dest, cost);
        add1Edge(dest, from, cost);
    }
    void dfs(int idx, int from = -1){
        for(auto e : nodes[idx].edges){
            if(e.dest == from){ continue; }
            dfs(e.dest, idx);
        }
    }
};

struct Info {
    int id, dest;
    friend istream& operator >> (istream& in, Info& e){
        in >> e.id >> e.dest;
        e.dest--;
        return in;
    }
};

// constants
int problem = 1;
int n, m;
vector<int> fs;
int tMax;
vector<Info> infos;
int lens[400][400];
vector<int> routes[400][400];
vector<Edge> edges;

// variables
int maxScore;

#define ALL_VARIABLES_DECLARATION\
    vector<int> dests;

#define ALL_VARIABLES_PARAMETER\
    vector<int>& dests

#define ALL_VARIABLES_ARGUMENT\
    dests

template <class... T>
void ignoreUnused(T&&...){ }

// {{{
struct Counter{
#ifdef LOCAL
#define COUNTER(name) Counter name(#name);
    int cnt = 0;
    string name;
    Counter(string name): name(name){ }
    ~Counter(){ printde(name, cnt); }
    void operator ++ (int){ cnt++; }
    void operator += (int d){ cnt += d; }
#else
#define COUNTER(name) Counter name;
    void operator ++ (int){ }
    void operator += (int){ }
#endif
};
// }}}
// COUNTER(cnt)
// COUNTER(trial)
// COUNTER(accepted)

// {{{
struct Watch {
    double prev;
    double getPrev(){
        return prev;
    }
#ifdef LOCAL
    timeval start;
    Watch(){
        gettimeofday(&start, nullptr);
    }
    double elapsed(){
        timeval now;
        gettimeofday(&now, nullptr);
        return prev = (now.tv_sec - start.tv_sec) + (now.tv_usec - start.tv_usec) / 1e6;
    }
#else
    double start;
#define ticks_per_sec 2800000000.0
    Watch(){
        start = get_time();
    }
    double elapsed(){
        return prev = get_time() - start;
    }
private:
    double get_time(){
        uint32_t lo, hi;
        asm volatile ("rdtsc" : "=a" (lo), "=d" (hi));
        return (((uint64_t)hi << 32) | lo) / ticks_per_sec;
    }
#endif
} watch;
// }}}

int traverseBF(ALL_VARIABLES_PARAMETER, unordered_set<int>& orders){
    vector<int> os(orders.begin(), orders.end());
    auto os1 = os;
    sort(os.begin(), os.end());
    int mini = M;
    do{
        int cand = 0, pos = 0;
        for(auto& o : os){
            cand += lens[pos][o];
            pos = o;
        }
        cand += lens[pos][0];
        if(cand < mini){
            mini = cand;
            os1 = os;
        }
    }while(next_permutation(os.begin(), os.end()));
    os1.push_back(0);
    dests.insert(dests.end(), os1.begin(), os1.end());
    orders.clear();
    return mini;
}

int traverse(ALL_VARIABLES_PARAMETER, unordered_set<int>& orders){
    if(orders.size() < 10){
        return traverseBF(ALL_VARIABLES_ARGUMENT, orders);
    }
    int pos = 0, ret = 0;
    int num = orders.size();
    for(int i = 0; i < num; i++){
        int order = 0, mini = M;
        for(auto& o : orders){
            int len = lens[pos][o];
            if(len < mini){
                mini = len;
                order = o;
            }
        }
        ret += mini;
        pos = order;
        orders.erase(order);
        dests.push_back(order);
    }
    ret += lens[pos][0];
    orders.clear();
    dests.push_back(0);
    return ret;
}

void makeInit(ALL_VARIABLES_PARAMETER){
    unordered_set<int> orders;
    for(int i = 0; i < tMax; i++){
        if(infos[i].id != -1){
            orders.insert(infos[i].dest);
        }
        if(orders.empty()){ continue; }
        //TODO take infos into account even for B
        int t = traverse(ALL_VARIABLES_ARGUMENT, orders);
        for(int j = 0; j < t - 1; j++){
            i++;
            if(i < tMax && infos[i].id != -1){
                orders.insert(infos[i].dest);
            }
        }
    }
}

// void sa(ALL_VARIABLES_PARAMETER){
// }

void solve(ALL_VARIABLES_PARAMETER){
    makeInit(ALL_VARIABLES_ARGUMENT);
    // sa(ALL_VARIABLES_ARGUMENT);
}

// ----------------------------------------------------------------------

void solveB(ALL_VARIABLES_PARAMETER){
    unordered_set<int> orders;
    int cnt = 0;
    for(int i = 0; i <= tMax; ){
        auto input = [&](){ 
            if(i == tMax){
                exit(0);
            }
            i++;
            int nNew; cin >> nNew;
            if(nNew){
                Info info; cin >> info;
                orders.insert(info.dest);
            }
            int nPut; cin >> nPut;
            for(int i = 0; i < nPut; i++){
                int id; cin >> id;
            }
            // printde(i, nNew, nPut);
            cerr.flush();
        };

        if(!i){
            input();
        }
        int t = 0;
        if(orders.size()){
            t = traverse(ALL_VARIABLES_ARGUMENT, orders);
            // for(int j = 0; j < t - 1; j++){
            //     i++;
            //     if(i < tMax && info.id != -1){
            //         orders.insert(info.dest);
            //     }
            // }
        }

        auto procResult = [&](){ 
            string s; cin >> s;
            if(s == "NG"){
                exit(0);
            }
            int n; cin >> n;
            for(int i = 0; i < n; i++){
                int id; cin >> id;
            }
        };
        if(dests.size()){
            int pos = 0;
            for(auto& dest : dests){
                if(dest < 0){
                    for(int i = 0; i < -dest; i++){
                        if(cnt == tMax){
                            printde("over tMax");
                            return;
                        }
                        cnt++;
                        cout << -1 << endl;
                        procResult();
                        input();
                    }
                }else{
                    for(auto& route : routes[pos][dest]){
                        for(int i = 0; i < lens[pos][route]; i++){
                            if(cnt == tMax){
                                printde("over tMax");
                                return;
                            }
                            cnt++;
                            cout << route + 1 << endl;
                            procResult();
                            input();
                        }
                        pos = route;
                    }
                }
            }
            dests.clear();
        }else{
            cnt++;
            cout << -1 << endl;
            procResult();
            input();
        }
    }
}

// ----------------------------------------------------------------------

void input(ALL_VARIABLES_PARAMETER){
    ignoreUnused(dests);
    cin >> n >> m;
    Graph g(n);
    for(int i = 0; i < m; i++){
        int a, b, d;
        cin >> a >> b >> d;
        a--; b--;
        g.add2Edge2(a, b, d);
        edges.emplace_back(d, a, b);
        edges.emplace_back(d, b, a);
    }
    cin >> tMax;
    if(tMax != 10000){
        problem = 2;
        fs.resize(n);
        fs[0] = tMax;
        for(int i = 1; i < n; i++){
            cin >> fs[i];
        }
        cin >> tMax;
    }else{
        infos.resize(tMax);
        for(int i = 0; i < tMax; i++){
            int n; cin >> n;
            if(n){
                cin >> infos[i];
            }else{
                infos[i].id = -1;
            }
        }
    }

    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            lens[i][j] = M;
        }
        lens[i][i] = 0;
    }
    for(auto& edge : edges){
        lens[edge.from][edge.dest] = edge.cost;
        routes[edge.from][edge.dest].push_back(edge.dest);
    }
    for(int k = 0; k < n; k++){
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                if(lens[i][j] > lens[i][k] + lens[k][j]){
                    lens[i][j] = lens[i][k] + lens[k][j];
                    routes[i][j].clear();
                    routes[i][j].insert(routes[i][j].end(), routes[i][k].begin(), routes[i][k].end());
                    routes[i][j].insert(routes[i][j].end(), routes[k][j].begin(), routes[k][j].end());
                }
            }
        }
    }
}

void output(ALL_VARIABLES_PARAMETER){
    int pos = 0;
    // printde(dests);
    int cnt = 0;
    for(auto& dest : dests){
        if(dest < 0){
            for(int i = 0; i < -dest; i++){
                if(cnt == tMax){
                    printde("over tMax");
                    return;
                }
                cnt++;
                cout << -1 << '\n';
            }
        }else{
            // printde(routes[pos][dest]);
            for(auto& route : routes[pos][dest]){
                for(int i = 0; i < lens[pos][route]; i++){
                    if(cnt == tMax){
                        printde("over tMax");
                        return;
                    }
                    cnt++;
                    cout << route + 1 << '\n';
                }
                pos = route;
            }
        }
    }
    while(cnt < tMax){
        cnt++;
        cout << -1 << '\n';
    }
}

int main(){
    cin.tie(0); ios::sync_with_stdio(false);
    ALL_VARIABLES_DECLARATION;
    input(ALL_VARIABLES_ARGUMENT);

    if(problem == 1){
        solve(ALL_VARIABLES_ARGUMENT);
        output(ALL_VARIABLES_ARGUMENT);
    }else{
        solveB(ALL_VARIABLES_ARGUMENT);
    }
}


Measured memory_bytes: 30672