#pragma GCC optimize "O3"
#pragma GCC target "sse4.2"

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vs = vector<string>;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
using vpii = vector<pii>;
using vpll = vector<pll>;
#define rep(i, n) for(ll i = 0; i < (n); i++)
#define repb(i, n) for(ll i = (n)-1; i >= 0; i--)
#define repr(i, a, b) for(ll i = (a); i < (b); i++)
#define reprb(i, a, b) for(ll i = (b)-1; i >= (a); i--)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((ll)(x).size())
const int INF = 2000000000;
const ld EPS = 1e-12L;
const ld PI = 3.1415926535897932385L;
template<class S, class T>inline bool chmax(S &a, const T &b){ if(a<b) { a=b; return 1; } return 0; }
template<class S, class T>inline bool chmin(S &a, const T &b){ if(b<a) { a=b; return 1; } return 0; }
#ifdef OJ_LOCAL
#include "dump.hpp"
#else
#define dump(...) ((void)0)
#endif

// https://github.com/beet-aizu/library/blob/master/marathon/timer.cpp
const ll CPS = 2800000000;
struct Timer{
    ll start;
    Timer(){
        reset();
    }
    void reset(){
        start = getCycle();
    }
    inline double get(){
        return (double)(getCycle()-start)/CPS;
    }
	inline ll getCycle(){
		uint32_t low, high;
		__asm__ volatile ("rdtsc" : "=a" (low), "=d" (high));
		return ((ll)low)|((ll)high<<32);
	}
};
Timer timer;

random_device rnd;
mt19937 mt(rnd());

// result:
// AtCoder_codetest: CPS = 2800000000 cnt = 285970395(2980 ms)
// My_PC           : CPS = 3500000000 cnt = 310452127(2980 ms)
//                                    cnt = 310452127(2980 ms)

template<typename T> struct Edge {
    int from, to;
    T cost;
    Edge(int from, int to, T cost): from(from), to(to), cost(cost) {}
    Edge(int from, int to): from(from), to(to), cost(1) {}
    bool operator<(Edge<T> &r) {
        return cost < r.cost;
    }
};
template<typename T>
ostream &operator<<(ostream &os, Edge<T> edge) {
    os << edge.from << " -> " << edge.to << " (" << edge.cost << ")";
    return os;
}

// グラフテンプレート(隣接行列)
template<typename T = ll> struct GraphM {
    // 頂点数
    int n;
    // 無辺を表す
    const T NIL = INF;
    // 隣接行列
    vector<vector<T>> mat;
    GraphM(int n)
        : n(n), mat(n, vector<T>(n, NIL)) {}
    template<typename... Args>
    void add_edge(int from, int to, T cost = 1) {
        mat[from][to] = cost;
    }
    
    // 最短距離行列
    vector<vector<T>> dist;
    // nxt[i][j] = shortest_path(i -> j)[1]
    vector<vector<T>> nxt;
    void WarshallFloyd() {
        dist = mat;
        nxt.resize(n, vector<T>(n));
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
                nxt[i][j] = j;
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                if(i == j) dist[i][j] = 0;
                else if(dist[i][j] == NIL) dist[i][j] = INF;
            }
        }
        for(int k = 0; k < n; k++) {
            for(int i = 0; i < n; i++) {
                for(int j = 0; j < n; j++) {
                    if(dist[i][k] != INF && dist[k][j] != INF) {
                        if(dist[i][j] > dist[i][k] + dist[k][j]) {
                            dist[i][j] = dist[i][k] + dist[k][j];
                            nxt[i][j] = nxt[i][k];
                        }
                    }
                }
            }
        }
    }
    vector<int> shortest_path(int start, int goal) {
        if(start == goal) return {start};
        vector<int> ret;
        for(int i = start; i != goal; i = nxt[i][goal]) {
            ret.emplace_back(i);
        }
        ret.emplace_back(goal);
        return ret;
    }
};
template<typename T>
ostream &operator<<(ostream &os, GraphM<T> graph) {
    os << "V = " << graph.n << "\n";
    for(int i = 0; i < graph.n; i++) {
        for(int j = 0; j < graph.n; j++) {
            os << graph.mat[i][j] << " \n"[j == graph.n - 1];
        }
    }
    return os;
}

struct Delivery {
    int id, destination, ordered_time;
    Delivery(): id(0), destination(0), ordered_time(0) {}
    Delivery(int id, int d, int ot): id(id), destination(d), ordered_time(ot) {}
};

int main(){
    int V, E;
    cin >> V >> E;
    vi u(E), v(E), d(E);
    GraphM<int> G(V);
    // 0-indexed
    // 店舗：0
    rep(i, E){
        cin >> u[i] >> v[i] >> d[i];
        u[i]--;
        v[i]--;
        G.add_edge(u[i], v[i], d[i]);
        G.add_edge(v[i], u[i], d[i]);
    }
    G.WarshallFloyd();
    //dump(G.dist);
    
    int Tmax;
    cin >> Tmax;
    vector<Delivery> info(Tmax + 1);
    rep(i, Tmax){
        int Nnew;
        cin >> Nnew;
        if(Nnew > 0){
            int id, dest;
            cin >> id >> dest;
            dest--;
            info[i] = Delivery(id, dest, i);
        }else{
            info[i] = Delivery();
        }
    }
    info[Tmax] = Delivery();
    
    // 何も考えず1商品のみ
    int t = 0;
    // int ret_time = 0;
    int pos = 0;
    vector<Delivery> del;
    if(info[0].id != 0){
        del.emplace_back(info[0]);
    }
    vi ans(Tmax, -1);
    auto fin = [&]{
        rep(i, Tmax){
            cout << ans[i] << "\n";
        }
        exit(0);
    };
    auto update = [&](int a){
        //dump("update", a, t);
        if(a == -1) ans[t] = a;
        else ans[t] = a + 1;
        t++;
        if(info[t].id != 0){
            del.emplace_back(info[t]);
        }
        if(t >= Tmax) fin();
    };
    while(1){
        if(del.empty()){
            update(-1);
        }else{
            int goal = del[0].destination;
            vi path = G.shortest_path(pos, goal);
            //dump(path);
            int npath = SZ(path);
            // ret_time = t + G.dist[pos][goal] * 2;
            repr(i, 1, npath){
                rep(j, G.dist[path[i-1]][path[i]]){
                    update(path[i]);
                }
            }
            repb(i, npath-1){
                rep(j, G.dist[path[i]][path[i+1]]){
                    update(path[i]);
                }
            }
            del.erase(del.begin());
        }
    }
    
	/*
    ll cnt = 0;
    chrono::system_clock::time_point start, end;

    start = chrono::system_clock::now();
    while(timer.get() <= 29.98){
        cnt++;
    }
    cout << cnt << endl;
    end = chrono::system_clock::now();

    double time = static_cast<double>(chrono::duration_cast<chrono::microseconds>(end - start).count() / 1000.0);
    printf("time %lf[ms]\n", time);
    */
    return 0;
}

Measured memory_bytes: 2560