#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;

void travelingSalesmanProblem(const vector<vector<int> > dist, vector<int>& node, double timeLimit)
{
    TicToc tt;
    tt.tic();

    int n = node.size();
    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[n-1]][node[0]];

    while(tt.toc() < timeLimit){
        int i = xorshift(n - 1) + 1;
        int j = xorshift(n - 1) + 1;
        if(i == j)
            continue;
        if(j < i)
            swap(i, j);
        reverse(node.begin() + i, node.begin() + j + 1);

        int sum2 = dist[node[n-1]][node[0]];
        for(int k=0; k<n-1; ++k)
            sum2 += dist[node[k]][node[k+1]];
        if(sum2 < sum)
            sum = sum2;
        else
            reverse(node.begin() + i, node.begin() + j + 1);
    }
}

class Solver
{
private:
    int n, tmax;
    vector<vector<Edge> > edges;
    vector<vector<int> > graph, dist;
    vector<vector<pair<int, int> > > order;

    void decideRoot(const set<int>& nodeSet, queue<int>& ope, double timeLimit)
    {
        vector<int> node(nodeSet.begin(), nodeSet.end());
        travelingSalesmanProblem(dist, node, timeLimit);

        int m = node.size();
        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;
                    }
                }
            }
        }
    }

public:
    void readProblem()
    {
        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()
    {
        TicToc tt;
        tt.tic();

        int t = 0;
        set<int> node;
        queue<int> ope;
        for(int t=0; t<tmax; ++t){
            for(const auto& p : order[t])
                node.insert(p.second);

            if(ope.empty() && !node.empty()){
                node.insert(1);
                double timeLimit = 29.0 * t / tmax - tt.toc();
                decideRoot(node, ope, timeLimit);
                node.clear();
            }

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