#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
#include <memory>
#include <regex>
using namespace std;

#ifdef _WIN32
#define NOMINMAX
#include <Windows.h>
#endif
class TicToc
{
private:
    double getCurrTime(){
    #ifdef _WIN32
        return GetTickCount() * 1e-3;
    #else
        uint32_t low, high;
        __asm__ volatile ("rdtsc" : "=a" (low), "=d" (high));
        uint64_t t = ((uint64_t)low) | ((uint64_t)high << 32);
        return t / (1250000000LL * 2.23);
    #endif
    }
    double startTime;
public:
    void tic(){
        startTime = getCurrTime();
    }
    double toc(){
        return getCurrTime() - startTime;
    }
};

class Xorshift
{
private:
    uint32_t x, y, z, w;
public:
    Xorshift(uint32_t seed=88675123, int32_t loop=50){
        x = 123456789;
        y = 362436069;
        z = 521288629;
        w = seed;
        while(--loop >= 0){
            (*this)();
        }
    }
    uint32_t operator()(){
        uint32_t t=(x^(x<<11));
        x=y; y=z; z=w;
        return w=(w^(w>>19))^(t^(t>>8));
    }
    uint32_t operator()(uint32_t size){
        return (*this)() % size;
    }
    uint64_t get64(){
        uint64_t a = (*this)();
        uint64_t b = (*this)();
        return (a << 32) | b;
    }
    uint64_t get64(uint64_t size){
        return get64() % size;
    }
    template <class T>
    void shuffle(vector<T>& v){
        uint32_t n = v.size();
        for(uint32_t i=0; i<n-1; ++i){
            uint32_t j = (*this)(n-i) + i;
            swap(v[i], v[j]);
        }
    }
};
Xorshift xorshift;

template <class T>
class EdgeBase
{
public:
    int to;
    T cost;
    EdgeBase(){};
    EdgeBase(int to0, T cost0){to = to0; cost = cost0;}
};
typedef EdgeBase<int> Edge;

template<class T>
void shortestPath(const vector<vector<EdgeBase<T> > >& edges, int start, vector<T>& dist)
{
    const T INF = numeric_limits<T>::max();
    const T EPS = static_cast<T>(1.0e-10);

    dist.assign(edges.size(), INF);
    dist[start] = 0;
    priority_queue<pair<T,int>, vector<pair<T,int> >, greater<pair<T,int> > > q;
    q.push(make_pair(0, start));

    while(!q.empty()){
        pair<T, int> p = q.top();
        q.pop();
        int v = p.second;
        if(dist[v] < p.first - EPS)
            continue;
        for(unsigned i=0; i<edges[v].size(); ++i){
            EdgeBase<T> e = edges[v][i];
            if(dist[v] + e.cost < dist[e.to] - EPS){
                dist[e.to] = dist[v] + e.cost;
                q.push(make_pair(dist[e.to], e.to));
            }
        }
    }
}

template <class T>
void shortestPath(const vector<vector<EdgeBase<T> > >& edges, vector<vector<T> >& dist)
{
    dist.resize(edges.size());
    for(unsigned i=0; i<edges.size(); ++i)
        shortestPath(edges, i, dist[i]);
}

void travelingSalesmanProblem(const vector<vector<int> > dist, vector<int>& node, double timeLimit)
{
    TicToc tt;
    tt.tic();
 
    int n = node.size();
    if(n <= 3)
        return;

    int sum = 0;
    for(int i=0; i<n-1; ++i){
        int k = i + 1;
        for(int j=i+1; j<n; ++j){
            if(dist[node[i]][node[j]] < dist[node[i]][node[k]])
                k = j;
        }
        swap(node[i+1], node[k]);
        sum += dist[node[i]][node[i+1]];
    }
    sum += dist[node[0]][node[n-1]];
 
    while(tt.toc() < timeLimit){
        int a = xorshift(n);
        int len = xorshift(n - 3) + 2;
        int b = (a + len) % n;
        int c = (b + xorshift(n - len)) % n;
        bool isReverse = true;

        int sum2 = sum;
        if(b == c){
            sum2 -= dist[node[(a-1+n)%n]][node[a]] + dist[node[(b-1+n)%n]][node[b]];
            sum2 += dist[node[(a-1+n)%n]][node[(b-1+n)%n]] + dist[node[b]][node[a]];
        }
        else{
            sum2 -= dist[node[(a-1+n)%n]][node[a]] + dist[node[(b-1+n)%n]][node[b]] + dist[node[(c-1+n)%n]][node[c]];
            isReverse = (xorshift(2) == 0);
            if(!isReverse)
                sum2 += dist[node[(a-1+n)%n]][node[b]] + dist[node[(c-1+n)%n]][node[a]] + dist[node[(b-1+n)%n]][node[c]];
            else
                sum2 += dist[node[(a-1+n)%n]][node[b]] + dist[node[(c-1+n)%n]][node[(b-1+n)%n]] + dist[node[a]][node[c]];
        }

        if(sum2 < sum){
            sum = sum2;
            cerr << sum << endl;
            rotate(node.begin(), node.begin() + a, node.end());
            b = (b - a + n) % n;
            c = (c - a + n) % n;
            if(isReverse)
                reverse(node.begin(), node.begin() + b);
            if(b != c)
                rotate(node.begin(), node.begin() + b, node.begin() + c);
        }
    }
}

/***************************************************************************/

const int INF = INT_MAX / 2;

class Solver
{
private:
    TicToc tt;
    int n, tMax, tCurr;
    bool isInteractive;
    vector<int> freq;
    vector<vector<Edge> > edges;
    vector<vector<int> > graph, dist, customer;
    vector<vector<pair<int, int> > > order;

    void decideRoot(queue<int>& ope)
    {
        vector<int> node(1, 1);
        for(int i=2; i<=n; ++i){
            if(!customer[i].empty())
                node.push_back(i);
        }
        int m = node.size();
        if(m == 1)
            return;

        double limit = 29.0 * tCurr / tMax - tt.toc();
        travelingSalesmanProblem(dist, node, limit);
        rotate(node.begin(), find(node.begin(), node.end(), 1), node.end());

        for(int i=0; i<m; ++i){
            int curr = node[i];
            int target = node[(i+1)%m];
            while(curr != target){
                for(Edge& e : edges[curr]){
                    int next = e.to;
                    if(dist[curr][target] - e.cost == dist[next][target]){
                        for(int j=0; j<e.cost; ++j)
                            ope.push(next);
                        curr = next;
                        break;
                    }
                }
            }
        }

        for(int i=1; i<=n; ++i)
            customer[i].clear();
    }

public:
    void readProblem()
    {
        tt.tic();

        int m;
        cin >> n >> m;
        edges.assign(n+1, vector<Edge>());
        graph.assign(n+1, vector<int>(n+1, INF));
        for(int i=0; i<m; ++i){
            int u, v, d;
            cin >> u >> v >> d;
            edges[u].push_back(Edge(v, d));
            edges[v].push_back(Edge(u, d));
            graph[u][v] = graph[v][u] = d;
        }
        shortestPath(edges, dist);

        cin >> tMax;
        if(tMax != 10000){
            isInteractive = true;
            freq.resize(n);
            freq[0] = tMax;
            for(int i=1; i<n; ++i)
                cin >> freq[i];
            cin >> tMax;
            order.assign(tMax, vector<pair<int, int> >());
        }
        else{
            isInteractive = false;
            order.assign(tMax, vector<pair<int, int> >());
            for(int i=0; i<tMax; ++i){
                int a;
                cin >> a;
                order[i].resize(a);
                for(int j=0; j<a; ++j){
                    int id, to;
                    cin >> id >> to;
                    order[i][j] = make_pair(id, to);
                }
            }
        }
    }

    void solve()
    {
        queue<int> ope;
        customer.assign(n+1, vector<int>());
        for(tCurr=0; tCurr<tMax; ++tCurr){
            if(isInteractive){
                int a;
                cin >> a;
                order[tCurr].resize(a);
                for(int i=0; i<a; ++i){
                    int id, to;
                    cin >> id >> to;
                    order[tCurr][i] = make_pair(id, to);
                }
                cin >> a;
                for(int i=0; i<a; ++i){
                    int tmp;
                    cin >> tmp;
                }
            }

            for(const auto& p : order[tCurr])
                customer[p.second].push_back(tCurr);

            if(ope.empty()){
                decideRoot(ope);
            }

            if(!ope.empty()){
                cout << ope.front() << endl;
                ope.pop();
            }
            else{
                cout << -1 << endl;
            }

            if(isInteractive){
                string s;
                int a;
                cin >> s >> a;
                for(int i=0; i<a; ++i){
                    int tmp;
                    cin >> tmp;
                }
            }
        }
    }
};

int main()
{
    Solver solver;
    solver.readProblem();
    solver.solve();

    return 0;
}


Measured memory_bytes: 5476