#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]);
}

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

const int INF = INT_MAX / 2;

class Solver
{
private:
    TicToc tt;
    int n, tMax, tCurr;
    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;

        long long score = 0;
        int t = tCurr;
        for(int i=0; i<m-1; ++i){
            int k = i + 1;
            for(int j=i+1; j<m; ++j){
                if(dist[node[i]][node[j]] < dist[node[i]][node[k]])
                    k = j;
            }
            swap(node[i+1], node[k]);
            t += dist[node[i]][node[i+1]];
            if(t <= tMax){
                for(int x : customer[node[i+1]]){
                    long long dt = t - x;
                    score += tMax * (long long)tMax - dt * dt;
                }
            }
        }

        double limit = 29.0 * tCurr / tMax;
        while(tt.toc() < limit){
            int a = xorshift(m - 1) + 1;
            int b = xorshift(m - 1) + 1;
            if(a == b)
                continue;
            if(b < a)
                swap(a, b);
            reverse(node.begin() + a, node.begin() + b + 1);

            long long score2 = 0;
            int t = tCurr;
            for(int i=0; i<m-1; ++i){
                t += dist[node[i]][node[i+1]];
                if(t <= tMax){
                    for(int x : customer[node[i+1]]){
                        long long dt = t - x;
                        score2 += tMax * (long long)tMax - dt * dt;
                    }
                }
            }
            if(score < score2)
                score = score2;
            else
                reverse(node.begin() + a, node.begin() + b + 1);
        }

        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;
        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){
            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;
            }
        }
    }
};

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

    return 0;
}


Measured memory_bytes: 2048