Buggy python submission (verdict: Time Limit Exceeded):
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 = True

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
returning = False
last_return_time = 0
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:
            # 到着した途中経由地にたまたま配達できたらオーダー一覧から消しておく
            for i, o in enumerate(order):
                if i > last_return_time:
                    break
                jdx = []
                for j, dst in enumerate(o):
                    if dst == v_next:
                        jdx.append(j)
                # 後ろからオーダーを消す(インデックスがずれないように)
                for j in jdx[::-1]:
                    o.pop(j)
            next_temp = predecessors[distination, v_next]
            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:
                returning = False
                delivering = False
            # 店に戻らずに配達した方が良いこともある
            # 面倒くさいので一度戻り始めたら戻ってもらう方針
            elif not returning:
                # 今持ってるオーダーが戻るよりいい感じに運べるならそうする
                idx = jdx = -1
                dist_temp_min_possession = 10**18
                for i, o in enumerate(order[:last_return_time + 1]):
                    if not o:
                        continue
                    for j, dst in enumerate(o):
                        # 現在地から配達場所までの時間
                        # Waiting time とかいうのがあるのでなんか重みを良い感じにする
                        dist_temp = dist[v_next, dst] + abs(time - i) ** 1
                        if dist_temp < dist_temp_min_possession:
                            dist_temp_min_possession = dist_temp
                            idx = i
                            jdx = j
                dist_temp_min_not_possession = 10**18
                for i, o in enumerate(order[last_return_time + 1:], last_return_time + 1):
                    if not o:
                        continue
                    for dst in o:
                        # 現在地から店 + 店から配達場所までの時間
                        # Waiting time とかいうのがあるのでなんか重みを良い感じにする
                        dist_temp = dist[v_next, 1] + dist[1, dst] + abs(time - i) ** 9
                        if dist_temp < dist_temp_min_not_possession:
                            dist_temp_min_not_possession = dist_temp
                if dist_temp_min_possession < dist_temp_min_not_possession:
                    # 目的地等を設定
                    distination = order[idx].pop(jdx)
                    remaining_last = dist[v_next, distination]
                    next_temp = predecessors[distination, v_next]
                    remaining_next = dist[v_next, next_temp]
                    v_next = next_temp
                # 店に戻る
                else:
                    returning = True
                    remaining_last = dist[distination, 1]
                    v_next = predecessors[1, distination]
                    remaining_next = dist[distination, v_next]
                    distination = 1
        time_next()
    # 配達する目的地を決める(一番近いところ)
    else:
        last_return_time = time
        idx = jdx = -1
        remaining_last = 0
        dist_temp_min = 10**18
        for i, o in enumerate(order):
            if not o:
                continue
            for j, dst in enumerate(o):
                # 店から配達場所までの時間
                # Waiting time とかいうのがあるのでなんか重みを良い感じにする
                dist_temp = dist[1, dst] + abs(time - i) ** 2
                if dist_temp < dist_temp_min:
                    dist_temp_min = dist_temp
                    idx = i
                    jdx = j
        # オーダーが無ければ何もせずに次の時間へ
        if idx == -1:
            print_flush(-1)
            time_next()
            continue
        # 目的地等を設定
        distination = order[idx].pop(jdx)
        remaining_last = dist[1, distination]
        v_next = predecessors[distination, 1]
        # print(distination, v_next)
        remaining_next = dist[1, v_next]
        delivering = True

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

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)
# 発生頻度？無視です……
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()))])
    # 車に積まれた荷物の情報は捨てる(えー……)
    [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)
    # 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