"""https://atcoder.jp/contests/hokudai-hitachi2019-1/tasks/hokudai_hitachi2019_1_a
Time Limit: 30 sec / Memory Limit: 1024 MB

# Score
score := time_max**2 * n_deleverd - sum((deleverd_time[i] - ordered_time[i])**2 for i in deleverd)

# Requirements
- time_max = 10_000
- 200 <= n_vertices <= 400
- 1.5*n_vertices <= n_edges <= 2*n_vertices
- 1 <= d <= ceil(4*sqrt(2*n_vertices)) <= 114

# Output
- stay ==> -1
- move x ==> x
"""

import collections
import sys

STAY = -1
BASE_POS = 0


class Order:
    def __init__(self, order_id, ordered_time, destination):
        self.order_id = order_id
        self.ordered_time = ordered_time
        self.destination = destination

    def __str__(self):
        return 'Order' + str((self.order_id, self.ordered_time, self.destination))


class Graph:
    def __init__(self, n_vertices, n_edges):
        self.n_vertices = n_vertices
        self.n_edges = n_edges
        self.adjacency_list = [[] for i in range(n_vertices)]
        self.distances = [[float('inf')] * (n_vertices)
                          for _ in range(n_vertices)]
        self.transits = [[j for j in range(n_vertices)]
                         for i in range(n_vertices)]

    def add_edge(self, u, v, d):
        self.adjacency_list[u].append((v, d))
        self.adjacency_list[v].append((u, d))
        self.distances[u][v] = d
        self.distances[v][u] = d
        self.transits[u][v] = v
        self.transits[v][u] = u

    def floyd_warshall(self):
        for i in range(self.n_vertices):
            self.distances[i][i] = 0
            self.transits[i][i] = i
        for k in range(self.n_vertices):
            for i in range(self.n_vertices):
                for j in range(self.n_vertices):
                    if self.distances[i][j] > self.distances[i][k] + self.distances[k][j]:
                        self.distances[i][j] = self.distances[i][k] + \
                            self.distances[k][j]
                        self.transits[i][j] = self.transits[i][k]


def get_orders(ordered_time):
    n_new_order = int(input())  # 0 <= n_new_order <= 1
    orders = []
    for _ in range(n_new_order):
        # 1 <= order_id <= time_max+1
        # 2 <= destination <= n_vertices
        order_id, destination = map(int, input().split())
        orders.append(Order(order_id, ordered_time, destination-1))
    return orders


def get_input():
    n_vertices, n_edges = map(int, input().split())
    graph = Graph(n_vertices, n_edges)

    for _ in range(n_edges):
        u, v, d = map(int, input().split())
        graph.add_edge(u-1, v-1, d)
    graph.floyd_warshall()

    # Floyd–Warshall algorithm

    time_max = int(input())
    orders = []
    for time_index in range(time_max):
        new_orders = get_orders(time_index)
        for order in new_orders:
            orders.append(order)
    return graph, time_max, orders


def solve(graph, time_max, orders):
    car_on_vertex = True
    car_pos = BASE_POS
    # edge_car_driving = (0, 0)
    next_pickup = 0
    commands = [-1] * time_max
    cargo = []
    t = 0
    while t < time_max:
        # pick up cargos
        if car_on_vertex and car_pos == BASE_POS:
            while next_pickup < len(orders) and orders[next_pickup].ordered_time <= t:
                cargo.append(orders[next_pickup])
                next_pickup += 1
        if cargo:
            cargo.sort(key=lambda x: (t-x.ordered_time +
                                      graph.distances[car_pos][x.destination]) ** 2)
            print(','.join([str(c) for c in cargo]), file=sys.stderr)
            dist = cargo[0].destination
            if time_max <= t + graph.distances[car_pos][dist]:
                cargo = cargo[1:]  # TODO: 高速化
                continue
            transit_point = graph.transits[car_pos][dist]
            need_time_for_move = graph.distances[car_pos][transit_point]
            for i in range(need_time_for_move):
                commands[t + i] = transit_point
            t += need_time_for_move
            car_pos = transit_point
            cargo = cargo[1:]  # TODO: 高速化
            continue
        else:
            if car_on_vertex:
                if car_pos == BASE_POS:
                    commands[t] = STAY
                else:
                    transit_point = graph.transits[car_pos][BASE_POS]
                    need_time_for_move = graph.distances[car_pos][transit_point]
                    if time_max <= t + need_time_for_move:
                        t = time_max
                        break
                    for i in range(need_time_for_move):
                        commands[t + i] = transit_point
                    t += need_time_for_move
                    car_pos = transit_point
                    continue
            else:
                pass

        commands[t] = STAY
        t += 1
    return commands


def simulate(graph, time_max, orders, commands):
    car_on_vertex = True
    car_pos = BASE_POS

    return 0


def main():
    graph, time_max, orders = get_input()
    commands = solve(graph, time_max, orders)
    for command in commands:
        if command == STAY:
            print(STAY)
        else:
            print(command+1)


if __name__ == "__main__":
    main()


Measured memory_bytes: 130372