#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    def dijkstra(s):
        d = [float("inf")]*n
        pre = [None]*n
        d[s] = 0
        q = [(0,s)]
        while q:
            dx,x = heappop(q)
            for y,w in v[x]:
                nd = dx+w
                if nd < d[y]:
                    d[y] = nd
                    pre[y] = x
                    heappush(q,(nd,y))
        path = [[i] for i in range(n)]
        for i in range(n):
            while path[i][-1] != s:
                path[i].append(pre[path[i][-1]])
            path[i] = path[i][::-1][1:]
        return d,path

    n,m = LI()
    v = [[] for i in range(n)]
    for i in range(m):
        a,b,d = LI()
        a -= 1
        b -= 1
        v[a].append((b,d))
        v[b].append((a,d))
    d = [None]*n
    path = [None]*n
    for s in range(n):
        d[s],path[s] = dijkstra(s)
    f = LI()
    t = I()
    x = 0
    dx = 0
    dy = 0
    query = deque()
    dst = 0
    go = []
    f = 0
    time = [None]*n
    fl = [0]*n
    for i in range(t):
        while go and not fl[go[0]]:
            go.pop(0)
        N = I()
        if N == 1:
            k,l = LI()
            l -= 1
            query.append(l)
            if time[l] == None:
                time[l] = i
        N = I()
        for _ in range(N):
            input()
        if not f:
            if x == 0:
                if not query:
                    print(-1,flush=True)
                    input()
                    N = I()
                    for _ in range(N):
                        input()
                else:
                    f = 1
                    while query:
                        dst = query.popleft()
                        go.append(dst)
                        fl[dst] = 1
                    go = list(set(go))
                    go.sort(key = lambda a:time[a])
                    time = [None]*n
                    dst = go.pop(0)
                    p = [i for i in path[x][dst]]
                    y = p.pop(0)
                    print(y+1,flush=True)
                    input()
                    N = I()
                    for _ in range(N):
                        input()
                    dx = 1
                    dy = d[x][y]
                    if dx == dy:
                        fl[y] = 0
                        x = y
                        dx = 0
                        if x == dst:
                            if go:
                                dst = go.pop(0)
                                p = [i for i in path[x][dst]]
                                y = p.pop(0)
                                dy = d[x][y]
                            else:
                                f = 0
                                dst = 0
                                p = [i for i in path[x][dst]]
                                y = p.pop(0)
                                dy = d[x][y]
                        else:
                            y = p.pop(0)
                            dy = d[x][y]

            else:
                print(y+1,flush=True)
                input()
                N = I()
                for _ in range(N):
                    input()
                dx += 1
                if dx == dy:
                    fl[y] = 0
                    x = y
                    dx = 0
                    if p:
                        y = p.pop(0)
                        dy = d[x][y]
        else:
            print(y+1,flush=True)
            input()
            N = I()
            for _ in range(N):
                input()
            dx += 1
            if dx == dy:
                fl[y] = 0
                x = y
                dx = 0
                if x == dst:
                    if go:
                        dst = go.pop(0)
                        p = [i for i in path[x][dst]]
                        y = p.pop(0)
                        dy = d[x][y]
                    else:
                        f = 0
                        dst = 0
                        p = [i for i in path[x][dst]]
                        y = p.pop(0)
                        dy = d[x][y]
                else:
                    y = p.pop(0)
                    dy = d[x][y]
    return

#Solve
if __name__ == "__main__":
    solve()


Measured memory_bytes: 39472