//{{{
#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;
    }
};

//TODO
// add 0 when tsp, remove after 0

// 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;
vector<int> orderedTime;

// 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 traverseGreedy(ALL_VARIABLES_PARAMETER, unordered_set<int>& 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;
}

int traverse2opt(ALL_VARIABLES_PARAMETER, unordered_set<int>& orders, int t0){
    vector<int> os;
    {
        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);
            os.push_back(order);
        }
        // os.push_back(0);
    }

    //TODO if(t0 == -1)
    auto os1 = os;
    auto calc = [&](){ 
        int ret = 0, pos = 0;
        int t = t0;
        for(auto& o : os){
            t += lens[pos][o];
            // ret += (t - orderedTime[o]) * (t - orderedTime[o]);
            // printde(t, orderedTime[o]);
            ret += lens[pos][o];
            pos = o;
        }
        t += lens[pos][0];
        ret += lens[pos][0];
        return ret;
    };
    Watch watch;
    int mini = calc();
    int m1 = mini;
    while(watch.elapsed() < 1){
        int i1 = mt() % (os.size() - 1), i2 = mt() % (os.size() - i1 - 1) + i1 + 1;
        for(int i = 0; i1 + i < i2 - i; i++){
            swap(os[i1 + i], os[i2 - i]);
        }
        int cand = calc();
        if(cand < mini){
            mini = cand;
            os1 = os;
        }else{
            for(int i = 0; i1 + i < i2 - i; i++){
                swap(os[i1 + i], os[i2 - i]);
            }
        }
    }
    // if(t0 != -1 && t0 + mini > tMax){
    //     auto calc2 = [&](){ 
    //         int ret = 0, pos = 0;
    //         int t = t0;
    //         for(auto& o : os){
    //             if(t + lens[pos][o] > tMax){
    //                 return ret;
    //             }
    //             ret++;
    //             t += lens[pos][o];
    //             pos = o;
    //         }
    //         if(t + lens[pos][0] > tMax){
    //             return ret;
    //         }
    //         ret++;
    //         return ret;
    //     };
    //     os = os1;
    //     int maxi = -1;
    //     while(watch.elapsed() < 2){
    //         int i1 = mt() % (os.size() - 1), i2 = mt() % (os.size() - i1 - 1) + i1 + 1;
    //         for(int i = 0; i1 + i < i2 - i; i++){
    //             swap(os[i1 + i], os[i2 - i]);
    //         }
    //         int cand = calc2();
    //         if(maxi < cand){
    //             maxi = cand;
    //             os1 = os;
    //         }else{
    //             for(int i = 0; i1 + i < i2 - i; i++){
    //                 swap(os[i1 + i], os[i2 - i]);
    //             }
    //         }
    //     }
    //     printde(maxi);
    // }else{
        os1.push_back(0);
    // }
    dests.insert(dests.end(), os1.begin(), os1.end());
    printde(m1, mini);
    // orders.clear();
    return mini;
}

int traverseDismiss(ALL_VARIABLES_PARAMETER, unordered_set<int>& orders){
    int num = orders.size();
    vector<int> orders0(orders.begin(), orders.end());
    vector<int> best;
    int mini = M;
    for(int h = -1; h < num; h++){
        vector<int> cand;
        int pos = 0, ret = 0;
        unordered_set<int> used;
        for(int i = 0; i < num; i++){
            if(h != -1 && i + 1 == num){ break; }
            int order = 0, mini = M;
            for(int j = 0; j < (int)orders0.size(); j++){
                auto& o = orders0[j];
                if(h == j || used.count(o)){ continue; }
                int len = lens[pos][o];
                if(len < mini){
                    mini = len;
                    order = o;
                }
            }
            ret += mini;
            pos = order;
            used.insert(order);
            cand.push_back(order);
        }
        ret += lens[pos][0];
        if(h != -1){
            ret = 1.1 * ret * num / (num - 1);
            // ret = .1 * ret * num / (num - 1);
        }
        if(ret < mini){
            mini = ret;
            best = cand;
        }
    }
    // orders.clear();
    for(auto& b : best){
        orders.erase(b);
    }
    best.push_back(0);
    dests.insert(dests.end(), best.begin(), best.end());
    return mini;
}

int traverse2opt0(ALL_VARIABLES_PARAMETER, unordered_set<int>& orders, int t0){
    vector<int> os;
    {
        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);
            os.push_back(order);
        }
        // os.push_back(0);
    }

    auto os1 = os;
    auto calc = [&](){ 
        int ret = 0, pos = 0;
        int t = t0;
        for(auto& o : os){
            t += lens[pos][o];
            // ret += (t - orderedTime[o]) * (t - orderedTime[o]);
            // printde(t, orderedTime[o]);
            ret += lens[pos][o];
            pos = o;
        }
        t += lens[pos][0];
        ret += lens[pos][0];
        return ret;
    };
    Watch watch;
    int mini = calc();
    // int m1 = mini;
    while(watch.elapsed() < 1){
        int i1 = mt() % (os.size() - 1), i2 = mt() % (os.size() - i1 - 1) + i1 + 1;
        if(i1 == 0 && os[i2] == 0){
            continue;
        }
        for(int i = 0; i1 + i < i2 - i; i++){
            swap(os[i1 + i], os[i2 - i]);
        }
        int cand = calc();
        if(cand < mini){
            mini = cand;
            os1 = os;
        }else{
            for(int i = 0; i1 + i < i2 - i; i++){
                swap(os[i1 + i], os[i2 - i]);
            }
        }
    }
    os1.push_back(0);
    int ret = 0, pos = 0;
    double coef = 0.1, base = 0;
    for(size_t i = 0; i < os1.size(); i++){
        auto& o = os1[i];
        if(!o && lens[o][0] < ret * coef + base){
        // if(!o && i + 1 < os1.size()){
        // }
            while(i < os1.size()){
                orders.insert(os1[i]);
                i++;
            }
            break;
        }
        ret += lens[pos][o];
        pos = o;
        dests.push_back(o);
    }
    // printde(mini, ret, os1);
    return ret;
    // dests.insert(dests.end(), os1.begin(), os1.end());
    // printde(m1, mini);
    // return mini;
}
int traverse(ALL_VARIABLES_PARAMETER, unordered_set<int>& orders, int t0 = -1){
    if(orders.size() < 12){
        return traverseBF(ALL_VARIABLES_ARGUMENT, orders);
    }
    // return traverseGreedy(ALL_VARIABLES_ARGUMENT, orders);
    // return traverseDismiss(ALL_VARIABLES_ARGUMENT, orders);
    if(watch.elapsed() < 25){
        return traverse2opt0(ALL_VARIABLES_ARGUMENT, orders, t0);
        // return traverse2opt(ALL_VARIABLES_ARGUMENT, orders, t0);
    }else{
        return traverseGreedy(ALL_VARIABLES_ARGUMENT, orders);
    }
}

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, i);
        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);
                orderedTime[info.dest] = i;
            }
            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;
    orderedTime.resize(n);
    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);
        // int cnt = 0;
        for(int i = 0; i < tMax; i++){
            int n; cin >> n;
            if(n){
                // cnt++;
                cin >> infos[i];
                //TODO id
                orderedTime[infos[i].dest] = i;
            }else{
                infos[i].id = -1;
            }
        }
        // printde(cnt);
    }

    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: 28288