using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


class A1
{
  public void Do()
  {

    var V = Split.Int();
    var E = Split.Int();

    var wf = new WFX(V + 1);

    for (int i = 0; i < E; i++)
    {
      var a = Split.Int();
      var b = Split.Int();
      var d = Split.Int();

      wf.AddE(a, b, d);
      wf.AddE(b, a, d);

      Debug.WriteLine($"AddE t{i:d04} {a} {b} {d}");
    }

    // wf
    wf.V3();

    // road
    wf.all();

    var tmax = Split.Int();

    var q = new int[10];

    _rand = new Random(0);
    
    // 10000
    var wait = new List<int>();
    var run = new List<int>();
    for (int t = 1; ; t++)
    {
      // null
      var r = Console.ReadLine();
      if (r == null)
        break;

      // o or 1
      var i = int.Parse(r);
      for (var ii = 0; ii < i; ii++)
      {
        // 1
        var new_id_i = Split.Int();
        var dst_i = Split.Int();

        wait.Add(dst_i);

        //Console.WriteLine($"{t} {new_id_i} {dst_i}");

        q[t / 1000]++;
      }

      //
      if (run.Count >= 1)
      {
        // 走行中
        Console.WriteLine(run[0]);
        run.RemoveAt(0);
      }
      else if (wait.Count >= 1)
      {
        Debug.WriteLine($"t{t} {wait.Count}");

        // 待ち
        var nodes = new List<int>();
        nodes.Add(1);
        nodes.AddRange(wait);
        wait.Clear();

        // TSP
        IDs(nodes,wf);

        // 分解
        for (int idx = 0; idx < nodes.Count; idx++)
        {
          //
          int n0 = nodes[idx];
          int n1 = nodes[(idx + 1) % nodes.Count];
          var links = wf.E_ids[n0, n1];

          // サブ
          for (int l = 0; l < links.Count - 1; l++)
          {
            int s0 = links[l];
            int s1 = links[l + 1];
            for (int cc = 0; cc < wf.E_dist[s0, s1]; cc++)
              run.Add(links[l + 1]);
          }
        }

        // 走行中
        Console.WriteLine(run[0]);
        run.RemoveAt(0);

      }
      else
      {
        // 停止
        Console.WriteLine("-1");
      }

    }
    Debug.WriteLine($"wait:{wait.Count} run:{run.Count}");
    //
    for (var i = 0; i < 10; i++)
    {
      Debug.WriteLine(q[i]);
    }
  }

  Random _rand;
  
  int IDs(List<int> nodes,WFX wf)
  {
    var r = 0;
    for (int idx = 0; idx < nodes.Count; idx++)
    {

      // ノード
      int n0 = nodes[idx];
      int n1 = nodes[(idx + 1) % nodes.Count];
      var links = wf.E_ids[n0, n1];

      // サブ
      for (int l = 0; l < links.Count - 1; l++)
      {
        int s0 = links[l];
        int s1 = links[l + 1];
        r += wf.E_dist[s0, s1].Value;
      }
    }

    Debug.WriteLine($"初期r:{r}");

    //
    int tm = 0;
    for (int t=0; ;t++ )
    {
      if (tm == 100000)
        break;
      tm++;
      
      // idx
      int idx1 = _rand.Next(nodes.Count);
      int idx1n = (idx1 + 1) % nodes.Count;
      int idx2 = _rand.Next(nodes.Count);
      int idx2n = (idx2 + 1) % nodes.Count;

      if (idx1==idx2)
        continue;

      // nodes
      int node1 = nodes[idx1];
      int node1n = nodes[idx1n];
      int node2 = nodes[idx2];
      int node2n = nodes[idx2n];

      
      //
      var len1 = len(wf.E_ids[node1, node1n],wf);
      var len2 = len(wf.E_ids[node2, node2n], wf);
      //
      var newLen1 = len(wf.E_ids[node1, node2], wf);
      var newLen2 = len(wf.E_ids[node1n, node2n], wf);

      //
      if ( newLen1 + newLen2 - len1 - len2 >= 0)
        continue;

      tm = 0;

      r = r + newLen1 + newLen2 - len1 - len2;
      /*
      Debug.WriteLine(
        $"{node1}-{node2}:{newLen1} " +
        $"{node1n}-{node2n}:{newLen2} " + 
        $"{node1}-{node1n}:{len1} " + 
        $"{node2}-{node2n}:{len2}");
      */
      //Debug.WriteLine($"更新r:{r}");
      
      // idx2-idx1
      var kosuu = ((nodes.Count + idx2) - idx1) % nodes.Count;

      // 交換半分
      var loop = kosuu / 2;

      // 交換
      for (int i = 0; ;i = (i + 1) % nodes.Count)
      {
        //
        if (loop == 0)
          break;
        loop -= 1;

        // 交換
        int a = (idx1+1+i)%nodes.Count;
        int b = (idx2 - i+nodes.Count) % nodes.Count;
        var tmp = nodes[a];
        nodes[a] = nodes[b];
        nodes[b] = tmp;
      }
    }

    // 
    for (; ; )
    {
      if (nodes[0] == 1)
        break;
      var t = nodes[0];
      nodes.Add(t);
      nodes.RemoveAt(0);
    }

    Debug.WriteLine($"r:{r}\n");
    return r;
  }
 
  int len(List<int> ss,WFX wf)
  {
    var l = 0;
    for (int i = 1; i < ss.Count; i++)
      l += wf.E_dist[ss[i-1],ss[i]].Value;
    return l;
  }

  static void Main()
  {
    new A1().Do();
  }
}
static class Split
{
  public static string String()
  {
    while (true)
    {
      if (_ss.Count == 0)
        ss();

      if (_ss.Count != 0)
      {
        var tmp = _ss[0];
        _ss.RemoveAt(0);

        if (tmp.Length != 0)
          return tmp;
      }
    }
  }
  public static int Int()
  {
    var str = String();
    return int.Parse(str);
  }
  public static long Long()
  {
    var str = String();
    return long.Parse(str);
  }
  public static string[] strings()
  {
    for (; ; )
    {
      var ss = Console.ReadLine().Split(_delim);
      if (ss.Length == 1 && ss[0] == "")
        continue;

      return ss;
    }
  }

  // [2.1] -2,147,483,648 ~ +2,147,483,647
  public static int[] ints()
  {
    var _s = strings();

    var x = new int[_s.Length];
    for (var i = 0; i < _s.Length; i++)
      x[i] = int.Parse(_s[i]);

    return x;
  }

  static List<string> _ss = new List<string>();
  static void ss()
  {
    var s = Console.ReadLine();
    var ss = s.Split(_delim);
    _ss = new List<string>(ss);
  }

  static char[] _delim = new[] { '\t', ' ' };
  // 1

}

class WFX
{
  //
  public int?[,] E_dist;
  int V_頂点;

  public void V3()
  {
    // V3
    for (int k = 0; k < V_頂点; k++)
      for (int i = 0; i < V_頂点; i++)
        for (int j = 0; j < V_頂点; j++)
        {
          var tmp = E_dist[i, k] + E_dist[k, j];

          if (tmp == null)
            continue;

          if (E_dist[i, j] == null || tmp.Value < E_dist[i, j])
          {
            E_dist[i, j] = tmp;
            _E_chain[i, j] = _E_chain[k, j];
          }

        }
  }


  public void AddE(int a, int b, int d)
  {
    E_dist[a, b] = d;
    E_dist[b, a] = d;

    //
    _E_chain[a, b] = a;
    _E_chain[b, a] = b;
  }

  public WFX(int V_)
  {
    V_頂点 = V_;

    this.E_dist = new int?[V_, V_];
    for (var i = 0; i < V_; i++)
      this.E_dist[i, i] = 0;

    //
    this._E_chain = new int[V_頂点, V_頂点];
    for (var i = 0; i < V_頂点; i++)
    {
      for (int ii = 0; ii < V_頂点; ii++)
      {
        this._E_chain[i, ii] = -1;
      }
    }
  }

  //--------------------------------------------------


  public List<int>[,] E_ids;

  public void all()
  {
    E_ids = new List<int>[V_頂点, V_頂点];

    for (int i = 0; i < V_頂点; i++)
    {
      for (int ii = 0; ii < V_頂点; ii++)
      {
        E_ids[i, ii] = _ids(i, ii);
      }
    }
  }

  List<int> _ids(int id_s, int id_e)
  {
    var ret = new List<int>();
    ret.Add(id_e);
    for (; ; )
    {
      if (id_s == id_e)
        break;

      id_e = _E_chain[id_s, id_e];

      if (id_e == -1)
      {
        ret.Clear();
        break;
      }

      ret.Add(id_e);
    }
    //
    ret.Reverse();
    return ret;
  }

  int[,] _E_chain;
}


Measured memory_bytes: 46348