#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <map>
#include <queue>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

const ll CYCLE_PER_SEC = 2700000000;

unsigned long long xor128() {
    static unsigned long long rx = 123456789, ry = 362436069, rz = 521288629, rw = 88675123;
    unsigned long long rt = (rx ^ (rx << 11));
    rx = ry;
    ry = rz;
    rz = rw;
    return (rw = (rw ^ (rw >> 19)) ^ (rt ^ (rt >> 8)));
}

unsigned long long int getCycle() {
    unsigned int low, high;
    __asm__ volatile ("rdtsc" : "=a" (low), "=d" (high));
    return ((unsigned long long int) low) | ((unsigned long long int) high << 32);
}

double getTime(unsigned long long int begin_cycle) {
    return (double) (getCycle() - begin_cycle) / CYCLE_PER_SEC;
}

const int MAX_V = 401;
const int MAX_T = 10000;
const ll BASE_SCORE = pow(MAX_T, 2);
const int INF = 99999;

const int UNKNOWN = -1;
const int STORE_ID = 0;

const int WAIT_COMMAND = -1;

int V, E, T;
int dist[MAX_V][MAX_V];
int penalty[MAX_T + 10];

void warshall_floyd(int v = MAX_V) {
    for (int k = 0; k < v; k++) {
        for (int i = 0; i < v; i++) {
            for (int j = 0; j < v; j++) {
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
            }
        }
    }
}

struct Edge {
    int from;
    int to;
    int dist;

    Edge(int from = -1, int to = -1, int dist = INF) {
        this->from = from;
        this->to = to;
        this->dist = dist;
    }
};

struct Vertex {
    int id;
    int packageCount;
    vector<int> deliver_times;
    vector <Edge> edges;

    Vertex(int id = -1) {
        this->id = id;
        this->packageCount = 0;
    }
};

struct Order {
    int id;
    int dest;

    Order(int id = -1, int dest = UNKNOWN) {
        this->id = id;
        this->dest = dest;
    }
};

struct Node {
    int id;
    int cnt;

    Node(int id = -1, int cnt = -1) {
        this->id = id;
        this->cnt = cnt;
    }

    bool operator>(const Node &n) const {
        return cnt < n.cnt;
    }
};

vector <Vertex> vertexList;
vector <Edge> edgeList;
Order orderList[MAX_T + 10];
int nextOrderTime[MAX_T + 10];

class HokudaiHitachi {
public:
    void init() {
        for (int i = 0; i < MAX_V; ++i) {
            for (int j = 0; j < MAX_V; ++j) {
                if (i == j) {
                    dist[i][j] = 0;
                } else {
                    dist[i][j] = INF;
                }
            }
        }

        warshall_floyd();

        for (int t = MAX_T; t >= 0; --t) {
            nextOrderTime[t] = MAX_T;
            penalty[t] = pow(t, 2);
        }
    }

    vector<int> initSolver() {
        vector<int> result;

        return result;
    }

    vector<int> run() {
        vector<int> result = initSolver();

        return dests2answer(result);
    }

    vector<int> dests2answer(vector<int> &dests) {
        int current = STORE_ID;
        vector<int> answer;

        while (answer.size() < MAX_T) {
            answer.push_back(WAIT_COMMAND);
        }

        return answer;
    }

    ll calcScore(vector<int> &dests) {
        ll totalScore = 0;

        int time = 0;
        int current = STORE_ID;
        int updated_at = 0;
        int delivered_count[V];
        memset(delivered_count, 0, sizeof(delivered_count));
        vector<int> orders;

        for (int i = 0; i < dests.size() && time < MAX_T; ++i) {
            int dest = dests[i];

            if (current == STORE_ID) {
                updated_at = time;
            } else {
                int cnt = delivered_count[current];

                while (cnt < vertexList[dest].packageCount && vertexList[dest].deliver_times[cnt] <= time) {
                    int delay = time - vertexList[dest].deliver_times[cnt];
                    assert(0 <= delay && delay <= MAX_T);
                    totalScore += BASE_SCORE - penalty[delay];
                    ++cnt;
                }

                delivered_count[current] = cnt;
            }

            if (current != dest) {
                current = dest;
                time += dist[current][dest];
            } else if (dest == STORE_ID) {
                time = nextOrderTime[time];
            }

            assert(time <= MAX_T);
        }

        return totalScore;
    }
};

int main() {
    HokudaiHitachi hh;
    hh.init();
    cin >> V >> E;

    for (int vid = 0; vid < V; ++vid) {
        Vertex vertex(vid);

        vertexList.push_back(vertex);
    }

    int u, v, d;
    for (int i = 0; i < E; ++i) {
        cin >> u >> v >> d;

        --u;
        --v;

        dist[u][v] = d;
        dist[v][u] = d;

        Edge e1(u, v, d);
        Edge e2(v, u, d);

        edgeList.push_back(e1);

        vertexList[u].edges.push_back(e1);
        vertexList[v].edges.push_back(e2);
    }

    cin >> T;

    int N;
    int id, dst;

    for (int t = 0; t < T; ++t) {
        cin >> N;
        if (N == 0) continue;

        cin >> id >> dst;
        orderList[t] = Order(id, dst);
        vertexList[dst].deliver_times.push_back(t);
        ++vertexList[dst].packageCount;
    }

    int nextTime = UNKNOWN;

    for (int t = T; t >= 0; --t) {
        nextOrderTime[t] = nextTime;

        if (orderList[t].dest != UNKNOWN) {
            nextTime = t;
        }
    }

    vector<int> result = hh.run();

    for (int i = 0; i < result.size(); ++i) {
        cout << result[i] << endl;
    }

    return 0;
}

Measured memory_bytes: 1280