# 2019/11/21 15:30
# Simulationを用いたDelivery cycleの最適化の導入。
# 次の目的地への効率がShopへ戻るrootの効率を下回る時は貪欲を下回る時はRerootする。
#
# 2019/11/21 11:00
# Simlation機能の導入：ただしBでは将来オーダーが分からないのでSimと実際の挙動はずれる。。。
#
# 2019/11/18 16:50 yorimichi wo tsuika p=1
# folk from submission 2019-11-19 16:23:40
# 2019/11/20 19:00 introduced initial wait_time(=3) but point decreased...
#
# no yorimichi if going back to the shop
# only short cut, no go&back drop
# wait at shop during the number of order < 3

import time
st_time=time.time()

from heapq import heappush, heappop, heapify
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
import math
import random
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
ts=time.time()
sys.setrecursionlimit(10**6)
#input=sys.stdin.readline

show_flg=False
#show_flg=True
drop_probability=1

def dijkstra(edge,st):
    # initialize: def: d=dist(st,i), prev=[previous vertex in minimum path], q[]
    n=len(edge)
    d=[(0 if st==i else inf,i) for i in range(n)]
    prev=[0]*n
    q=[i for i in d]
    heapify(q)
    
    # calc
    while q:
        dist,cur=heappop(q)
        for dst,dist in edge[cur]:
            alt=d[cur][0]+dist
            if alt<d[dst][0]:
                d[dst]=(alt,dst)
                prev[dst]=cur
                heappush(q,(alt,dst))
    dist=[i for i,j in d]
    return dist,prev

V,E=MI()
g=[[] for _ in range(V+1)]
c=[]
neighber=[set() for _ in range(V+1)]
d_map=[{} for _ in range(V+1)]
for i in range(E):
    u,v,d=MI()
    g[u].append((v,d))
    g[v].append((u,d))
    c.append((u,v,d))
    neighber[u].add(v)
    neighber[v].add(u)
    d_map[u][v]=d
    d_map[v][u]=d

for i in sorted(c):
    0#print(*i)

mins=[[] for _ in range(V+1)]
prevs=[[] for _ in range(V+1)]
for i in range(1,V+1):
    min_dist,prev=dijkstra(g,i)
    mins[i]=min_dist
    prevs[i]=prev

#show('mins',mins)
#show('prevs',prevs)

f=LI()
T=I()
order=[0]*T
accor=[0]*T
order_time=[]

#show('order',order)
order_at_shop=[] # stock (vertex, order_time)
item_at_car=[[] for _ in range(V+1)] # stock order time at each vertex
sim_item_at_car=[[] for _ in range(V+1)] # stock order time at each vertex
num_item_at_car=0
sim_num_item_at_car=0

def ship():
    global num_item_at_car
    while order_at_shop:
        v,t=order_at_shop.pop()
        item_at_car[v].append(t)
        num_item_at_car+=1
##    show('item_at_car',item_at_car)

def deliver(v,deliver_t):
    global num_item_at_car
    d_p=0
    while item_at_car[v]:
        or_t=item_at_car[v].pop()
        num_item_at_car-=1
        d_p+=T**2-(deliver_t-or_t)**2
##    show("delivered to",v,"deliverPoint",d_p)
    return d_p

def sim_init():
    global item_at_car
    global sim_item_at_car
    global sim_num_item_at_car
    global num_item_at_car
    sim_num_item_at_car=num_item_at_car
    sim_item_at_car=[[j for j in orders] for orders in item_at_car]

def sim_deliver(v,deliver_t):
    global sim_num_item_at_car
    d_p=0
    while sim_item_at_car[v]:
        or_t=sim_item_at_car[v].pop()
        sim_num_item_at_car-=1
        d_p+=T**2-(deliver_t-or_t)**2
##    show("[SIM]delivered to",v,"deliverPoint",d_p)
    return d_p


def stat_init():
    global order_at_shop
    global item_at_car
    global num_item_at_car
    order_at_shop=[] # stock (vertex, order_time)
    item_at_car=[[] for _ in range(V+1)] # stock order time at each vertex
    num_item_at_car=0

def root_valuator(current,now,root,f_info=True):
    if len(root)==0:
        return 0,0
    global item_at_car
    cur=current
    point=0
    rt=now
    for next_dest,dist in root[::-1]:
        rt+=dist
        if len(item_at_car[next_dest])>0:
            for j in item_at_car[next_dest]:
                point+=T**2-(rt-j)**2
                #show(j,next_dest,dist,rt,point)
        cur=next_dest
    return point,rt-now
    

def neighber_search(cur,next_dest,effc,best_p,best_d,f_info=True):
    global item_at_car
    drop=[]
    n_item=0
    for nb,dist in g[cur]:
        if nb==next_dest:continue
        if len(item_at_car[nb])>n_item:
            n_item=len(item_at_car[nb])
            drop_effc=(best_p+n_item*(T**2))/(mins[cur][nb]*2+best_d)
            #if random.random()<drop_effc/effc:
            if drop_effc>=effc:
                drop=[(cur,mins[nb][cur]),(nb,mins[cur][nb])]
    return drop

def optimizer_drop(current,now,root,effc,best_p,best_d,f_info=True):
##    show('!Optimizing',root)
    global item_at_car
    opt_root=[]
    cur=current
    org_now=now
    org_effc=effc
    n=len(root)
    
    for i in range(1,n+1):
        next_dest,dist=root[-i]
        drop=[]
        alt=0
        n_item=0
        for nb,d_nb in g[cur]:
            if nb==next_dest:continue
            if len(item_at_car[nb])>n_item:
                #n_item=len(item_at_car[nb])
                rt=now+d_nb
                for j in item_at_car[nb]:
                    alt+=T**2-(rt-j)**2
                if prevs[nb][next_dest]==nb: # nb connected to next_dest ( possible enhancement mins[prevs[nb][next_dest]][next_dest]==mins[nb][next_dest] )
                    drop_time=mins[cur][nb]+mins[nb][next_dest]-dist
                    drop_effc=(best_p+alt)/(drop_time+best_d)
                    if drop_effc>=effc:
                        effc=drop_effc
                        #effc=drop_effc
                        drop.append((nb,mins[cur][nb]))
                        drop.append((next_dest,mins[nb][next_dest]))
                elif 0==1:
                    drop_time=mins[cur][nb]*2
                    drop_effc=(best_p+alt)/(drop_time+best_d)
                    if drop_effc>=effc:
                        effc=drop_effc
                        #effc=drop_effc
                        drop.append((nb,mins[cur][nb]))
                        drop.append((cur,mins[nb][cur]))
                        drop.append((next_dest,mins[cur][next_dest]))
        
        if drop:
            opt_root+=drop
            now+=drop_time
##            show('#yorimichi',drop)
        else:
            opt_root.append((next_dest,dist))
            now+=dist
        cur=next_dest
    
    # opt_root validation
    opt_valid=True
    cur=current
    for next_dest,dist in opt_root:
        if next_dest in neighber[cur]:
            if d_map[cur][next_dest]!=dist:
                opt_valid=False
            else:
                0 # validation ok
        else:
            opt_valid=False
        
        cur=next_dest
    
    if opt_valid:
        p,t=root_valuator(current,org_now,opt_root,f_info=False)
        opt_effc=p/t if t!=0 else 0
        if org_effc<=opt_effc:
            rt=opt_root[::-1]
        else:
            rt=root
    else:
##        show('!!optimization failed')
        rt=root
    
    return rt

def root_generator(cur,deliver_point):# generate root
    root=[]
    p=deliver_point
    while p!=cur:
        prev_p=prevs[cur][p]
        root.append((p,mins[p][prev_p]))
        p=prev_p
    return root


def best_deliver(cur,now,item_at_car,f_info=True,optimize_mode=True,sim_mode=False):
    global order_at_shop
    #global item_at_car
    global num_item_at_car
    if sim_mode:
        item_at_car=[[i for i in order] for orders in sim_item_at_car]
    
    effc=0
    best_p,best_d=0,inf
    deliver_point=1
    # direct go
    for dest in range(2,V+1):
        if dest==cur:continue
        d=mins[cur][dest]
        rt=now+d
        alt=0
        for j in item_at_car[dest]:
            alt+=T**2-(rt-j)**2
        if alt/d>effc:
            effc,best_p,best_d=alt/d,alt,d
            deliver_point=dest
    # turn to shop and go
    if cur!=1:
        db=mins[cur][1]
        nx_item_at_car=[[] for i in range(1+V)]
        for order_v,t in order_at_shop:
            nx_item_at_car[order_v].append(t)
        for i in range(2,1+V):
            for t in item_at_car[i]:
                nx_item_at_car[i].append(t)
        if f_info:
            for t in range(min(now+1,T-1),min(now+db+1,T)):
                v=order[t]
                if v!=0:
                    nx_item_at_car[v].append(t)
        for dest in range(2,V+1):
            if cur==1:break
            if dest==cur:continue
            dg=mins[1][dest]
            rt=now+db+dg
            d=db+dg
            alt=0
            for j in nx_item_at_car[dest]:
                alt+=T**2-(rt-j)**2
            if alt/d>effc:
                effc,best_p,best_d=alt/d,alt,d
                deliver_point=1
            
    # generate root
    root=root_generator(cur,deliver_point)
    
    # optimize
    if deliver_point==1:
        0
    else:
        if optimize_mode:
            root=optimizer_drop(cur,now,root,effc,best_p,best_d,f_info=False)
    # opt end

    return deliver_point,root,effc

def sim_cycle(current,now,f_info=True,optimize_mode=False):
    global sim_num_item_at_car
    global sim_item_at_car
    max_rooting_num=8
    
    sim_init()
    cur=1 #current #/
    state=0
    cycle=[]
    dv_cycle=[]
    reached=False # True if reached at final destination
    #
    final_dest=1
    dest=1
    cur=1
    point=0
    effc=0
    #

    margin=3
    st_time_root_calc=time.time()
    interaction_time=st_time_root_calc-st_time_interactive
    time_due=(30-(st_time_interactive - st_time) - margin)*(now/T)
    
    t=now
    # simulation move
    stop_count=0
    while stop_count<V+1:
        ##show('[SIM] cur dest',cur,dest,sim_num_item_at_car,[(i,j) for i,j in enumerate(sim_item_at_car) if j])
        stop_count+=1
        if False:
            cycle.append(-1)
        else:
            if state==0:
                point+=sim_deliver(dest,t)

                if cur==final_dest:
                    #[SIM] set final destination
                    final_dest,root,effc=best_deliver(cur,t,sim_item_at_car,optimize_mode=False)
                    dv_cycle.append(final_dest)
                    #if final_dest==1:break
##                    show('[SIM]root setting',root)
                else:
                    0
                dest,dist=root.pop()
                state=1
            else: #[SIM] on road at teh begining
                0
            cycle.append(dest)
            t+=dist
            dist=0
##            show('[SIM]moving to',dest,'root',root)
            if dist==0:
                state=0
                cur=dest
        if cur==1 and state==0:
            break
        if len(dv_cycle)>=max_rooting_num:
            break
    
    
##    show('[SIM] non-opt Cycle',cycle)
    # simulation move
    sz=len(dv_cycle)
    cycle_effc=-inf
    for perm in permutations(range(1,sz)):
        alt_root=root_generator(1,dv_cycle[0])[::-1]
        pp=dv_cycle[0]
        for j in perm:
            alt_root+=root_generator(pp,dv_cycle[j])[::-1] #(cycle[j],mins[pp][cycle[j]]))
            pp=dv_cycle[j]
        alt_p,alt_t=root_valuator(current,now,alt_root[::-1],f_info=False)
        alt_effc=0 if alt_t==0 else alt_p/alt_t
        if alt_effc>=cycle_effc:
            cycle_effc=alt_effc
            opt_root=[(i,j) for i,j in alt_root]
            opt_p,opt_t=alt_p,alt_t
            
##    show('[SIM] optized Cycle',opt_root,cycle_effc,opt_p,opt_t,effc)
##    show(dv_cycle)
    return opt_root[::-1],dv_cycle[::-1],cycle_effc


# recieve info
def interactive(wait_num):
    global item_at_car
    stat_init()
    move=[]
    state=0 # 0 on vertex, 1 on road
    reached=False # True if reached at final destination
    final_dest=1
    dest=1
    cur=1
    point=0
    effc=0
    
    for t in range(T):
        Nnew=I()
        v=0
        for j in range(Nnew):
            new_id,v=MI()
        Nput=I()
        for j in range(Nput):
            put_id=I()

        order[t]=v
        if v!=0:
            order_at_shop.append((v,t))
        if cur==1 and state==0:
            ship()
        if num_item_at_car<=wait_num and cur==1 and state==0:
            print(-1)
            sys.stdout.flush()
            ship()
        else:
            if state==0:
                if dest==1: # 店で荷物を積載 #/
                    ship()
                    opt_root,dv_cycle,opt_effc=sim_cycle(cur,t,f_info=True)
                    if opt_root:
                        root=opt_root #/
                else: # 配達完了
                    point+=deliver(dest,t)

                if cur==final_dest:
                    next_effc=0
                    if dv_cycle:
                        if dv_cycle[-1]==final_dest:
                            dv_cycle.pop()
                            if dv_cycle:
                                next_p,next_t=root_valuator(cur,t,root_generator(cur,dv_cycle[-1]),f_info=True)
                                next_effc=next_p/next_t if next_t!=0 else 0
                    # set final destination
                    latest_final_dest,latest_root,latest_effc=best_deliver(cur,t,item_at_car,optimize_mode=False) #/
                    if next_effc<latest_effc or (latest_final_dest==1 or len(root)==0): #/
                        final_dest=1 #/
                        root=latest_root #/
##                    show('root setting',root)
                else:
                    0
                if len(root)==0: final_dest,root,latest_effc=best_deliver(cur,t,item_at_car,optimize_mode=False) #/
                dest,dist=root.pop()
                state=1
            else: # on road at teh begining
                0
            print(dest)
            sys.stdout.flush()
            dist-=1
##            show('moving to',dest,'root',root)
            if dist==0:
                state=0
                cur=dest

        vardict = input()
        if vardict == 'NG':
            for i in range(10**13):
                aa=[1,2]
                bb=aa.pop()+1
            sys.exit()

        Nachive = int(input())
        for j in range(Nachive):
            achive_id=I()

    return point


mode='rand'
mode='greed'
if mode=='rand':
    time_limit=29.5 # 1 = 1 sec
    time_limit=0.07
    now=time.time()
    p=-1
    s=T//2
    c=1
    while True:
        c+=1
        now=time.time()
        if now - st_time > time_limit:
            break
        cur_p,cur_st,cur_move=rand_move(s,c)
        if cur_p>p or p==-1:
            p=cur_p
            s=cur_st
            move=cur_move
elif mode=='greed':
    st_time_interactive=time.time()
    point=interactive(wait_num=3)
    show(point)
    #cycle  241565046   cycle_optimization without rerooting
    #cycle  262859674   cycle_optimization with rerooting
    
    #opt    254486710   wait=1 ikidake yorimichi 1 node
    #no opt 245213637   wait=1
    #opt    262210368   wait=3 ikidake yorimichi 1 node
    #no opt 262393179   wait=3


##    show(point)
    #opt    254486710   wait=1 ikidake yorimichi 1 node
    #no opt 245213637   wait=1
    #opt    262210368   wait=3 ikidake yorimichi 1 node
    #no opt 262393179   wait=3



Measured memory_bytes: 126164