class Log
  def initialize(enabled)
    @enabled = enabled
    @log = File.open("logA.txt", "wb") if @enabled
  end
  def close
    @log.close if @enabled
  end
  def print(str)
    @log.print(str) if @enabled
  end
end

#log = Log.new(true)
log = Log.new(false)

n_vertex, n_edge = gets.split.map(&:to_i)
edges = n_vertex.times.map { Array.new(n_vertex, -1) }
for i in 0...n_edge do
  u, v, d = gets.split.map(&:to_i)
  edges[u - 1][v - 1] = d
  edges[v - 1][u - 1] = d
end
log.print "[edges]\n"
for i in 0...n_vertex do
  log.print edges[i].join(" ") + "\n"
end
log.print "\n"

order_list = []
depot_order = []
load = []
curr_v = 0
next_v = 0
target_v = 0
len = 0
distance = 0
visited = {}
visited[0] = true

t_max = gets.to_i
t_last = (t_max.to_f * 0.95).to_i
log.print "[t_max] #{t_max} [t_last] #{t_last}\n\n"

for t in 0...t_max do
  log.print "[t=] #{t}\n"
  log.print "curr_v=#{curr_v} next_v=#{next_v} len=#{len}\n"
  
  # Read new order
  n_new = gets.to_i
  for i in 0...n_new do
    new_id, dst = gets.split.map(&:to_i)
    if order_list[new_id].nil?
      order_list[new_id] = [dst, t, -1]
      depot_order.push(order_list[new_id])
      log.print "[new order] new_id=#{new_id} dst=#{dst}\n"
    else
      log.print "Panic! Invalid new_id!\n"
    end
  end
  
  # Simulate depot
  if curr_v == 0 && distance == 0
    depot_order.each do |o|
      load.push(o)
    end
    depot_order = []
  end
  log.print "depot_order.size=#{depot_order.size} load.size=#{load.size}\n"
  
  if t < (t_max / 2)
    command = -1
  else
    if distance == 0
      target_v = -1
      len = -1
      for i in 0...n_vertex do
        if edges[curr_v][i] > 0 && !visited.include?(i)
          target_v = i
          len = edges[curr_v][i]
          break
        end
      end
      if target_v < 0
        for i in 0...n_vertex do
          if edges[curr_v][i] > 0
            target_v = i
            len = edges[curr_v][i]
            break
          end
        end
      end
      log.print "[target_v] #{target_v} [len] #{len}\n\n"
    end

    if target_v >= 0 && distance < len
      command = target_v + 1
    else
      command = -1
    end
  end
  puts command
  
  # Simulate move
  if command != -1
    if distance == 0
      next_v = target_v
    else
      if target_v != next_v
        log.print "Panic! Invalid target_v!\n"
      end
    end
    distance += 1
    if distance == len
      curr_v = next_v
      next_v = -1
      distance = 0
      visited[curr_v] = true
    end
  end
end

log.close
