//{{{
#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 15.0
#define LIMIT 20.0
#else
#define LIMIT 25.0
#endif

// mt19937 mt(1);
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
// back at 9500
// back at 9400 and dismiss some???
// back at <last info> and dismiss some???
// remove orders of next

// 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_map<int, int>& orders){
    // vector<int> os(orders.begin(), orders.end());
    vector<int> os;
    for(auto& o : orders){
        os.push_back(o.first);
    }
    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_map<int, 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& p : orders){
            int o = p.first;
            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;
}


vector<pair<int, int>> makeGreedy(unordered_map<int, int>& orders){
    vector<pair<int, int>> os;
    int pos = 0, ret = 0;
    int num = orders.size();
    for(int i = 0; i < num; i++){
        int mini = M;
        pair<int, int> order(0, 0);
        for(auto& p : orders){
            int o = p.first;
            int len = lens[pos][o];
            if(len < mini){
                mini = len;
                order = p;
            }
        }
        ret += mini;
        pos = order.first;
        orders.erase(order.first);
        os.push_back(order);
    }
    return os;
}

//TODO run twice
int calcScore(vector<pair<int, int>>& os, int t0, bool calcOverflow = true){
    int ret = 0, pos = 0;
    int t = t0;
    int over = 0;
    for(auto& p : os){
        int o = p.first;
        t += lens[pos][o];
        if(t > tMax && !calcOverflow){
            break;
        }
        ret += lens[pos][o];
        pos = o;
        over += (t > tMax) * p.second;
        // over += (t > tMax || (t0 < 9400 && t > 9500)) * p.second;
    }
    t += lens[pos][0];
    ret += lens[pos][0];
    ret += over * 10000;
    return ret;;
}

int traverse2opt0_2(ALL_VARIABLES_PARAMETER, unordered_map<int, int>& orders, int t0){
    auto os = makeGreedy(orders);
    auto os1 = os;
    int mini;
    int lastTime = 9450;
    bool last = t0 >= lastTime;
    bool refine = t0 > 7000 && !last;
    // bool refine = t0 > 7000 && !last;
    for(int _ = 0; _ < 1 + refine; _++){
        mini = calcScore(os, t0);
        double limit = 0.5;
        if(last){
            limit = LIMIT - watch.elapsed();
        }else if(!refine){
        }else if(_ == 0){
            limit *= 1.5;
        }else{
            limit /= 1.5;
        }
        Watch watch;
        int score = mini;
        while(watch.elapsed() < limit){
            int i1 = mt() % (os.size() - 1), i2 = mt() % (os.size() - i1 - 1) + i1 + 1;
            if(i1 == 0 && os[i2].first == 0){
                continue;
            }
            reverse(os.begin() + i1, os.begin() + i2 + 1);
            int cand = calcScore(os, t0);

            static const double startT = 100000, endT = 0.000001;
            double diff = score - cand;
            double temp = startT + (endT - startT) * min(1.0, watch.elapsed() / LIMIT);
            trial++;
            bool changeCond = last ? exp(diff / temp) * (1ull << 32) > mt() : cand < mini;
            if(changeCond){
                accepted++;
                score = cand;
                if(cand < mini){
                    mini = cand;
                    os1 = os;
                    if(last){
                        printde(mini);
                    }
                }
            }else{
                reverse(os.begin() + i1, os.begin() + i2 + 1);
            }
        }

        if(refine && _ == 0 && problem == 1 && !last){
            vector<pair<int, int>> os2;
            auto updateO2 = [&](int t2){ 
                os2.clear();
                unordered_set<int> comingOrders;
                for(int i = t0 + 1; i <= t0 + t2 && i < tMax; i++){
                    if(infos[i].id != -1){
                        comingOrders.insert(infos[i].dest);
                    }
                }
                for(auto& p : os){
                    if(!comingOrders.count(p.first)){
                        os2.push_back(p);
                    }
                }
                printde(os.size(), os2.size());
            };
            updateO2(mini);
            for(int i = 0; i < 4; i++){
                int t2 = 0;
                {
                    int pos = 0;
                    for(auto& p : os2){
                        int o = p.first;
                        t2 += lens[pos][o];
                        pos = o;
                    }
                    t2 += lens[pos][0];
                }
                updateO2(t2);
            }
            os2.swap(os);
        }
    }

    int ret = 0, pos = 0;
    double coef = 0.01, base = 0;
    for(size_t i = 0; i < os1.size(); i++){
        auto& o = os1[i].first;
        if((!last && (lens[pos][0] < ret * coef + base || t0 + ret >= lastTime)) || t0 + ret > tMax){
            while(i < os1.size()){
                orders[os1[i].first]++;
                i++;
            }
            dests.push_back(0);
            break;
        }
        ret += lens[pos][o];
        pos = o;
        dests.push_back(o);
    }
    dests.push_back(0);
    return ret;
}

int traverse2opt0(ALL_VARIABLES_PARAMETER, unordered_map<int, int>& orders, int t0){
    vector<pair<int, int>> os;
    {
        int pos = 0, ret = 0;
        int num = orders.size();
        for(int i = 0; i < num; i++){
            int mini = M;
            pair<int, int> order(0, 0);
            for(auto& p : orders){
                int o = p.first;
                int len = lens[pos][o];
                if(len < mini){
                    mini = len;
                    order = p;
                }
            }
            ret += mini;
            pos = order.first;
            orders.erase(order.first);
            os.push_back(order);
        }
    }

    auto os1 = os;
    auto calc = [&](){ 
        int ret = 0, pos = 0;
        int t = t0;
        int over = 0;
        for(auto& p : os){
            int o = p.first;
            t += lens[pos][o];
            ret += lens[pos][o];
            pos = o;
            over += (t > tMax) * p.second;
            // over += (t > tMax || (t0 < 9400 && t > 9500)) * p.second;
        }
        t += lens[pos][0];
        ret += lens[pos][0];
        ret += over * 10000;
        return ret;
    };
    int mini;
    // bool refine = t0 < 7000;
    bool refine = t0 > 7000;
    for(int _ = 0; _ < 1 + refine; _++){
        mini = calc();
        // double limit = 1;
        double limit = 0.5;
        // double limit = 0.1;
        bool last = t0 >= 9500 && mini > 10000;
        if(last){
            limit = LIMIT - watch.elapsed();
        }
        if(!refine || last){
        }else if(_ == 0){
            limit *= 1.5;
        }else{
            limit /= 1.5;
        }
        Watch watch;
        // int score = mini;
        while(watch.elapsed() < limit){
            int i1 = mt() % (os.size() - 1), i2 = mt() % (os.size() - i1 - 1) + i1 + 1;
            if(i1 == 0 && os[i2].first == 0){
                continue;
            }
            for(int i = 0; i1 + i < i2 - i; i++){
                swap(os[i1 + i], os[i2 - i]);
            }
            int cand = calc();

            // static const double startT = 0.01, endT = 0.000001;
            // double diff = score - cand;
            // double temp = startT + (endT - startT) * min(1.0, watch.elapsed() / LIMIT);
            trial++;
            // if(exp(diff / temp) * (1ull << 32) > mt()){
            if(cand < mini){
                accepted++;
                // score = cand;
                // if(cand < mini){
                mini = cand;
                os1 = os;
                if(t0 >= 9500 && mini > 10000){
                    printde(mini);
                }
                // }
                // }
            }else{
                for(int i = 0; i1 + i < i2 - i; i++){
                    swap(os[i1 + i], os[i2 - i]);
                }
            }
        }
        if(refine && _ == 0 && problem == 1 && mini < 10000){
            vector<pair<int, int>> os2;
            unordered_set<int> comingOrders;
            for(int i = t0 + 1; i <= t0 + mini && i < tMax; i++){
                if(infos[i].id != -1){
                    comingOrders.insert(infos[i].dest);
                }
            }
            for(auto& p : os){
                if(!comingOrders.count(p.first)){
                    os2.push_back(p);
                }
            }
            printde(os.size(), os2.size());
            os2.swap(os);
        }
    }
    int ret = 0, pos = 0;
    double coef = 0.01, base = 0;
    // double coef = 0.1, base = 0;
    for(size_t i = 0; i < os1.size(); i++){
        auto& o = os1[i].first;
        if((o && lens[o][0] < ret * coef + base && mini < 10000)
           || (t0 < 9400 && t0 + ret >= 9500)
           || t0 + ret > tMax){
            while(i < os1.size()){
                orders[os1[i].first]++;
                i++;
            }
            dests.push_back(0);
            break;
        }
        ret += lens[pos][o];
        pos = o;
        dests.push_back(o);
    }
    dests.push_back(0);
    return ret;
}

int traverse(ALL_VARIABLES_PARAMETER, unordered_map<int, int>& orders, int t0 = -1){
    if(orders.size() < 12){
        return traverseBF(ALL_VARIABLES_ARGUMENT, orders);
    }
    if(watch.elapsed() < 25){
        return traverse2opt0_2(ALL_VARIABLES_ARGUMENT, orders, t0);
        // return traverse2opt0(ALL_VARIABLES_ARGUMENT, orders, t0);
    }else{
        return traverseGreedy(ALL_VARIABLES_ARGUMENT, orders);
    }
}

void makeInit(ALL_VARIABLES_PARAMETER){
    unordered_map<int, int> orders;
    auto calcOrder = [&](){ 
        int ret = 0;
        for(auto& o : orders){
            ret += o.second;
        }
        return ret;
    };
    // int cnt = 0, cnt2 = 0;
    for(int i = 0; i < tMax; i++){
        if(infos[i].id != -1){
            // cnt2++;
            // cnt += orders.count(infos[i].dest);
            orders[infos[i].dest]++;
        }
        if(orders.empty()){ continue; }
        //TODO take infos into account even for B
        printde(i, calcOrder(), watch.elapsed());
        int t = traverse(ALL_VARIABLES_ARGUMENT, orders, i);
        printde(i, calcOrder(), watch.elapsed());
        for(int j = 0; j < t - 1; j++){
            i++;
            if(i < tMax && infos[i].id != -1){
                // cnt2++;
                // cnt += orders.count(infos[i].dest);
                orders[infos[i].dest]++;
            }
        }
    }
    // printde(cnt, cnt2);
}

// void sa(ALL_VARIABLES_PARAMETER){
// }

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

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

void solveB(ALL_VARIABLES_PARAMETER){
    unordered_map<int, 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[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]){
                        // printde(t, i, dests);
                        for(int i = 0; i < lens[pos][route]; i++){
                            if(cnt == tMax){
                                printde("over tMax");
                                return;
                            }
                            cnt++;
                            cout << route + 1 << endl;
                            // printde(route + 1);
                            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;
    int visited = 0;
    printde(dests.size());
    for(auto& dest : dests){
        if(dest < 0){
            for(int i = 0; i < -dest; i++){
                if(cnt == tMax){
                    printde("over tMax", visited);
                    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", visited);
                        return;
                    }
                    cnt++;
                    cout << route + 1 << '\n';
                }
                pos = route;
            }
            visited++;
        }
    }
    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: 32384