#!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 score(ans):
        res = 0
        x = 0
        st = 1
        dx = 0
        f = [[] for i in range(n)]
        j = 0
        query = []
        for i in range(t):
            while j < len(q) and q[j][0] == i:
                k,l = q[j]
                query.append((l,k))
                j += 1
            if st:
                if x == 0:
                    while query:
                        l,k = query.pop()
                        f[l].append(k)
                else:
                    while f[x]:
                        res += T-(i-f[x].pop())**2
            if ans[i] == -1:
                continue
            else:
                y = ans[i]-1
                st = 0
                dx += 1
                if d[x][y] == dx:
                    dx = 0
                    x = y
                    st = 1
        if st:
            if x != 0:
                while f[x]:
                    res += T-(i-f[x].pop())**2
        return res

    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)
    t = I()
    q = []
    for i in range(t):
        N = I()
        if N == 0:
            continue
        a,y = LI()
        y -= 1
        q.append((i,y))
    x = 0
    dx = 0
    dy = 0
    query = deque()
    dst = 0
    go = []
    f = 0
    time = [None]*n
    fl = [0]*n
    ans = []
    j = 0
    T = t**2
    for i in range(t):
        while go and not fl[go[0]]:
            go.pop(0)
        while j < len(q) and q[j][0] == i:
            k,l = q[j]
            query.append(l)
            if time[l] == None:
                time[l] = k
            j += 1
        if not f:
            if x == 0:
                if not query:
                    ans.append(-1)
                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]+d[x][a])
                    time = [None]*n
                    dst = go.pop(0)
                    p = [i for i in path[x][dst]]
                    y = p.pop(0)
                    ans.append(y+1)
                    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:
                ans.append(y+1)
                dx += 1
                if dx == dy:
                    fl[y] = 0
                    x = y
                    dx = 0
                    if p:
                        y = p.pop(0)
                        dy = d[x][y]
        else:
            ans.append(y+1)
            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]
    x = 0
    dx = 0
    dy = 0
    query = deque()
    dst = 0
    go = []
    f = 0
    time = [None]*n
    fl = [0]*n
    ans_ = []
    j = 0
    T = t**2
    for i in range(t):
        while go and not fl[go[0]]:
            go.pop(0)
        while j < len(q) and q[j][0] == i:
            k,l = q[j]
            query.append(l)
            if time[l] == None:
                time[l] = k
            j += 1
        if not f:
            if x == 0:
                if not query:
                    ans_.append(-1)
                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)
                    ans_.append(y+1)
                    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:
                ans_.append(y+1)
                dx += 1
                if dx == dy:
                    fl[y] = 0
                    x = y
                    dx = 0
                    if p:
                        y = p.pop(0)
                        dy = d[x][y]
        else:
            ans_.append(y+1)
            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]
    if score(ans) > score(ans_):
        for i in ans:
            print(i)
    else:
        for i in ans_:
            print(i)
    return

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


Measured memory_bytes: 41356