#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;
const double TIME_LIMIT = 0.4;
ll startCycle;

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, orderCount;
int cost[MAX_V][MAX_V];
ll 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++) {
                cost[i][j] = min(cost[i][j], cost[i][k] + cost[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;
    bool delivered;

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

struct PathInfo {
    int dist;
    vector<int> path;
};

struct Action {
    int dest;
    int waitCount;

    Action(int dest = -1, int waitCount = 0) {
        this->dest = dest;
        this->waitCount = waitCount;
    }
};

struct Node {
    int v;
    int dist;
    vector<int> path;

    Node(int v = -1, int dist = -1) {
        this->v = v;
        this->dist = dist;
    }

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

vector <Vertex> vertexList;
vector <Edge> edgeList;
Order orderTimeLine[MAX_T + 10];
vector <Order> orderList;
int nextOrderTime[MAX_T + 10];
int deliveredCount[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) {
                    cost[i][j] = 0;
                } else {
                    cost[i][j] = INF;
                }
            }
        }

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

    vector<int> run() {
        int time = 0;
        int before = 0;
        int updated_at = 0;
        int dc = 0;
        vector<int> path;
        // setupDeliveredCount(0);
        memset(deliveredCount, 0, sizeof(deliveredCount));

        while (time < T) {
            vector<int> packList = getPackageList(before, time);
            before = time + 1;

            if (packList.empty()) {
                time = nextOrderTime[time];

                for (int i = 0; i < time - before; ++i) {
                    path.push_back(WAIT_COMMAND);
                }

                updated_at = time;
            } else {
                vector<int> result = buildDeliveryPath(packList, time, before - 1);
                int current = STORE_ID;
                bool flg = true;
                int pathCost = 0;

                for (int i = 0; i < result.size() && flg; ++i) {
                    int to = result[i];

                    vector<int> stockOrder = getPackageList(before, time);
                    int stockSize = stockOrder.size() - packList.size();
                    double rate = stockSize / (max(1, cost[current][STORE_ID]));

                    if (stockSize > 2 && rate >= 0.0) {
                        flg = false;
                        to = STORE_ID;
                    }

                    PathInfo pathinfo = getPath(current, to);
                    pathCost += pathinfo.dist;

                    // fprintf(stderr, "Path Dist = %d\n", pathinfo.dist);

                    for (int j = 0; j < pathinfo.path.size(); ++j) {
                        int dst = pathinfo.path[j];

                        // fprintf(stderr, "Dist: %d -> %d\n", time, time + cost[current][dst]);
                        time += cost[current][dst];

                        // fprintf(stderr, "Move %d -> %d\n", current, dst);
                        path.push_back(dst);
                        current = dst;

                        if (current == STORE_ID) {
                            flg = false;
                            break;
                        }

                        // delivered order
                        int cnt = deliveredCount[current];
                        Vertex *vertex = &vertexList[current];

                        /*
                        if (current == to) {
                            fprintf(stderr, "Delivery Time: %d - %d - %d\n", cnt, vertex->deliver_times[cnt],
                                    updated_at);
                        }
                         */

                        while (cnt < vertex->packageCount && vertex->deliver_times[cnt] <= updated_at) {
                            /*
                            fprintf(stderr, "Delivery success (%d, %d <= %d)\n", current, vertex->deliver_times[cnt],
                                    updated_at);
                                    */
                            orderTimeLine[vertex->deliver_times[cnt]].delivered = true;
                            ++dc;
                            ++cnt;
                        }

                        deliveredCount[current] = cnt;
                    }
                }

                if (current != STORE_ID) {
                    pathCost += cost[current][STORE_ID];
                    time += cost[current][STORE_ID];
                    path.push_back(STORE_ID);
                }
                fprintf(stderr, "TotalCost %d, Distance = %d\n", pathCost, time - before + 1);
                updated_at = time;
            }

            fprintf(stderr, "time = %d\n", time);
        }

        setupDeliveredCount(0);
        ll score = calcScore(path, 0, true);

        fprintf(stderr, "Score = %lld, DC = %d\n", score, dc);

        return path2answer(path);
    }

    vector<int> getPackageList(int from, int to) {
        vector<int> result;
        bool checked[V];
        memset(checked, false, sizeof(checked));

        if (to >= T) return result;
        int cnt = 0;

        for (int t = 0; t <= to; ++t) {
            int dest = orderTimeLine[t].dest;

            if (dest == UNKNOWN) continue;
            if (orderTimeLine[t].delivered) continue;
            ++cnt;
            if (checked[dest]) continue;
            checked[dest] = true;

            result.push_back(dest);
        }

        // fprintf(stderr, "%d: Package Count = %d\n", to, cnt);

        return result;
    }

    void setupDeliveredCount(int time) {
        assert(time <= T);
        memset(deliveredCount, 0, sizeof(deliveredCount));

        for (int t = 0; t <= time; ++t) {
            int dest = orderTimeLine[t].dest;
            if (dest == UNKNOWN) continue;

            ++deliveredCount[dest];
        }
    }

    vector<int> greedyPath(vector<int> &packageList) {
        vector<int> result;

        map<int, bool> checked;
        int len = packageList.size();
        int current = STORE_ID;

        for (int i = 0; i < len; ++i) {
            int minDist = INF;
            int bestId = -1;

            for (int j = 0; j < len; ++j) {
                int dest = packageList[j];
                int dist = cost[current][dest];

                if (checked[dest]) continue;

                if (minDist > dist) {
                    minDist = dist;
                    bestId = dest;
                }
            }

            assert(bestId != -1);
            result.push_back(bestId);
            current = bestId;
            checked[bestId] = true;
        }

        return result;
    }

    vector<int> buildDeliveryPath(vector<int> &packageList, int started_at, int last_at) {
        if (packageList.size() <= 2) return packageList;

        packageList = greedyPath(packageList);

        // setupDeliveredCount(last_at);
        vector<int> bestPath = packageList;
        vector<int> path = bestPath;
        vector<int> currentPath = bestPath;
        ll bestScore = calcScore(bestPath, started_at);
        ll currentScore = bestScore;
        startCycle = getCycle();

        fprintf(stderr, "%5d: buildDeliveryPath (%d) - FirstScore = %lld\n", started_at, (int) packageList.size(),
                bestScore);

        double currentTime = getTime(startCycle);
        int tryCount = 0;
        int len = path.size();

        int R = 500000;
        double k = 0.5;
        double diffScore;

        while (currentTime < TIME_LIMIT) {
            currentTime = getTime(startCycle);
            double remainTime = (TIME_LIMIT - currentTime) / TIME_LIMIT;

            int type = xor128() % 3;

            int i, j, a, b, temp;

            do {
                i = xor128() % len;
                j = xor128() % len;
            } while (i == j);

            switch (type) {
                case 0:
                    swap(path[i], path[j]);
                    break;
                case 1:
                    temp = path[i];
                    path.erase(path.begin() + i);
                    path.insert(path.begin() + j, temp);
                    break;
                case 2:
                    if (i > j) {
                        temp = i;
                        i = j;
                        j = temp;
                    }

                    a = i;
                    b = j;

                    while (i < j) {
                        swap(path[i], path[j]);

                        ++i;
                        --j;
                    }
                    break;
            }

            ll score = calcScore(path, started_at);
            diffScore = (score - currentScore) / (double) BASE_SCORE;

            if (currentScore < score || (xor128() % R < R * exp(diffScore / (k * sqrt(remainTime))))) {
                currentPath = path;
                currentScore = score;

                if (bestScore < score) {
                    bestScore = score;
                    bestPath = path;
                }
            } else {
                switch (type) {
                    case 0:
                        swap(path[i], path[j]);
                        break;
                    case 1:
                        path = currentPath;
                        break;
                    case 2:
                        path = currentPath;
                        break;
                }
            }

            ++tryCount;
        }

        fprintf(stderr, "bestScore = %lld, tryCount = %d\n", bestScore, tryCount);

        return bestPath;
    }

    vector<int> path2answer(vector<int> &path) {
        int current = STORE_ID;
        int time = 0;
        vector<int> answer;

        for (int i = 0; i < path.size() && time < MAX_T; ++i) {
            int dest = path[i];
            if (dest == WAIT_COMMAND) {
                answer.push_back(WAIT_COMMAND);
                continue;
            }

            PathInfo pathInfo = getPath(current, dest);

            for (int j = 0; j < pathInfo.path.size(); ++j) {
                int to = pathInfo.path[j];
                int d = cost[current][to];

                assert(STORE_ID <= to && to <= V - 1);
                assert(d > 0);

                while (time < MAX_T && d > 0) {
                    answer.push_back(to + 1);
                    ++time;
                    --d;
                }

                current = to;
            }
        }

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

        return answer;
    }

    ll calcScore(vector<int> &path, int started_at, bool real = false) {
        ll totalScore = 0;
        int dcnt = 0;

        int time = started_at;
        int current = STORE_ID;

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

            if (current == STORE_ID) {
                started_at = time;
            }

            if (dest == WAIT_COMMAND) {
                assert(current == STORE_ID);
                ++time;
                if (real) {
                    fprintf(stderr, "%d: WAIT\n", time);
                }
                continue;
            }

            if (dest < 0 || V <= dest) {
                fprintf(stderr, "dest = %d\n", dest);
            }
            assert(STORE_ID <= current && current < V);
            assert(STORE_ID <= dest && dest < V);

            if (current != dest) {
                if (cost[current][dest] > MAX_T) {
                    fprintf(stderr, "%d -> %d cost %d\n", current, dest, cost[current][dest]);
                }

                assert(cost[current][dest] <= MAX_T);
                if (real) {
                    fprintf(stderr, "%d: Move %d -> %d, %d -> %d (%d)\n", time, current, dest, time, time + cost[current][dest], cost[current][dest]);
                }
                time += cost[current][dest];
                current = dest;
            }

            if (time > MAX_T) {
                // fprintf(stderr, "Time over: %d\n", time);
                break;
            }
            assert(time <= MAX_T);

            // delivered order
            int cnt = deliveredCount[current];
            Vertex *vertex = &vertexList[current];

            while (cnt < vertex->packageCount && vertex->deliver_times[cnt] <= started_at) {
                int delay = time - vertex->deliver_times[cnt];
                assert(0 <= delay && delay <= MAX_T);
                if (real) {
                    fprintf(stderr, "%d: Delivery %d - Score %lld -> %lld (delay: %d)\n", time, current, totalScore, totalScore + BASE_SCORE - penalty[delay], delay);
                }
                totalScore += BASE_SCORE - penalty[delay];
                ++dcnt;
                ++cnt;
            }

            if (real) {
                deliveredCount[current] = cnt;
            }
        }

        time += cost[current][STORE_ID];

        return totalScore;
    }

    PathInfo getPath(int from, int to) {
        PathInfo pathInfo;
        pathInfo.dist = 0;

        priority_queue <Node, vector<Node>, greater<Node>> pque;
        pque.push(Node(from, 0));

        bool visited[V];
        memset(visited, false, sizeof(visited));

        while (!pque.empty()) {
            Node node = pque.top();
            pque.pop();

            if (visited[node.v]) continue;
            visited[node.v] = true;

            if (node.v == to) {
                pathInfo.path = node.path;
                pathInfo.dist = node.dist;
                break;
            }

            for (int i = 0; i < vertexList[node.v].edges.size(); ++i) {
                Edge *edge = &vertexList[node.v].edges[i];

                assert(edge->from == node.v);

                Node next(edge->to, node.dist + edge->dist);
                next.path = node.path;
                next.path.push_back(edge->to);

                pque.push(next);
            }
        }

        return pathInfo;
    }
};

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;

        cost[u][v] = d;
        cost[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);
    }

    warshall_floyd(V);

    cin >> T;

    int N;
    int id, dst;
    orderCount = 0;

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

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

    fprintf(stderr, "Order Count = %d\n", orderCount);

    int nextTime = T;

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

        if (orderTimeLine[t].dest != UNKNOWN) {
            nextTime = t + 1;
        }
    }

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

    for (int t = 0; t < T; ++t) {
        cout << result[t] << endl;
    }

    return 0;
}

Measured memory_bytes: 1536