import sys
from numpy import argmin
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall
input = sys.stdin.buffer.readline

# 問題AとB両方同じコードだと嬉しいね
problem_b = False

V, E = map(int, input().split())

v_from = []
v_to = []
cost = []

for _ in range(E):
    u, v, d = map(int, input().split())
    v_from.append(u)
    v_to.append(v)
    cost.append(d)

graph = csr_matrix((cost, (v_from, v_to)), [V+1, V+1])
dist, predecessors = floyd_warshall(graph, directed=False, return_predecessors=True)
dist[dist == 0] = float('inf')

dist_min = list(map(argmin, dist))

# print(dist_min)


# print(dist, predecessors)
# 発生頻度？無視です……
if problem_b:
    input()

T_max = int(input())
# order = [[int(input().split()[1]) for _ in range(int(input()))] for _ in range(T_max)]

time = 0
delivering = False
distination = 1
v_next = 1

order = []

def take_order():
    global order
    order.append([int(input().split()[1]) for _ in range(int(input()))])

    if problem_b:
        # 車に積まれた荷物の情報は捨てる(えー……)
        [input() for _ in range(int(input()))]

# 最初のオーダーだけとっておく
take_order()

def time_next():
    global time, order
    time += 1
    # 時間を進めたらオーダーをとる
    if time < T_max - 1:
        take_order()

def print_flush(value):
    print(value, flush=True)
    
    if problem_b:
        # OKを読み飛ばす
        input()
        # 配達完了情報も読み飛ばします……
        [input() for _ in range(int(input()))]

while time < T_max:
    # 配達中なら配達作業
    if delivering:
        # 途中経由地へ進む
        print_flush(v_next)
        # 途中経由地までの残りの距離を減らす
        remaining_next -= 1
        # 到着したら次の途中経由地へ
        if remaining_next == 0:
            next_temp = predecessors[distination, v_next] # predecessors[v_next, distination]
            if next_temp != -9999:
                remaining_next = dist[v_next, next_temp]
                v_next = next_temp
        # 最終目的地までの距離を減らす
        remaining_last -= 1
        # 到着してる(はず)
        if remaining_last == 0:
            # 店に戻り終わった(目的地が店だった)
            if distination == 1:
                delivering = False
            # 店に戻る
            else:
                remaining_last = dist[distination, 1]
                v_next = predecessors[1, distination] # predecessors[distination, 1]
                remaining_next = dist[distination, v_next]
                distination = 1
        time_next()
    # 配達する目的地を決める(一番近いところ)
    else:
        idx = -1
        remaining_last = 0
        dist_temp_min = 10**18
        for i, o in enumerate(order):
            if not o:
                continue
            dst = o[0]
            # 店から配達場所までの時間
            # Waiting time とかいうのがあるのでなんか重みを良い感じにする
            dist_temp = dist[1, dst] + abs(time - i) ** 2
            if dist_temp < dist_temp_min:
                dist_temp_min = dist_temp
                idx = i
        # オーダーが無ければ何もせずに次の時間へ
        if idx == -1:
            print_flush(-1)
            time_next()
            continue
        # 目的地等を設定
        distination = order[idx].pop()
        remaining_last = dist[1, distination]
        v_next = predecessors[distination, 1] # predecessors[1, distination]
        # print(distination, v_next)
        remaining_next = dist[1, v_next]
        delivering = True

Measured memory_bytes: 19628