#include <algorithm>
#include <array>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const double CPU_FREQ_ATCODER = 1.0 / (28 * 100 * 1000);
const double CPU_FREQ = CPU_FREQ_ATCODER;

static unsigned long long rdtsc()
{
  unsigned int low, high;
  __asm__ volatile("rdtsc"
                   : "=a"(low), "=d"(high));
  return ((unsigned long long int)low) | ((unsigned long long int)high << 32);
}

class Timer
{
  long long start;
  long long cycle;

public:
  Timer() { start = rdtsc(); }
  void reset() { start = rdtsc(); }

  inline double elapsed()
  {
    cycle = rdtsc();
    return (cycle - start) * CPU_FREQ;
  }
};

inline double calcTemp(double startTime, double endTime, double curTime, double startTemp, double endTemp)
{
  const double c = 1 - (curTime - startTime) / (endTime - startTime);
  return c * (startTemp - endTemp) + endTemp;
}

class XorShiftL
{
  static const int LOG_CACHE_SIZE = 0x10000;

  int logIndex = 0;
  double logCache[LOG_CACHE_SIZE];

  std::uint64_t x = 88172645463325252LL;

public:
  XorShiftL()
  {
    for (int i = 0; i < LOG_CACHE_SIZE; i++) {
      logCache[i] = log((i + 0.5) / 0x10000);
    }
    shuffle(logCache);
  }

  std::uint64_t xor64()
  {
    x = x ^ (x << 7);
    return x = x ^ (x >> 9);
  }

  // [0, n)
  int nextInt(int n) { return xor64() % n; }

  // [low, high)
  int nextInt(int low, int high) { return xor64() % (high - low) + low; }
  double nextDouble() { return xor64() / double(std::numeric_limits<std::uint64_t>::max()); }

  /**
   * 2つの相異なる数をランダムに取得する
   * get<0>(result) < get<1>(result) が保証されている
   */
  std::tuple<int, int> nextIntPair(int low, int high)
  {
    int i = nextInt(low, high);
    int j = nextInt(low, high - 1);
    if (j >= i)
      j++;
    else
      std::swap(i, j);
    return std::make_tuple(i, j);
  }

  /**
   * 2つの相異なる数をランダムに取得する
   * get<0>(result) < get<1>(result) は保証されない
   */
  std::tuple<int, int> nextIntUnorderedPair(int low, int high)
  {
    int i = nextInt(low, high);
    int j = nextInt(low, high - 1);
    if (j >= i)
      j++;
    return std::make_tuple(i, j);
  }

  /**
   * ログを取得する
   */
  double nextLog()
  {
    const double res = logCache[logIndex++];
    if (logIndex == LOG_CACHE_SIZE) {
      logIndex = 0;
    }
    return res;
  }

  template <typename T>
  void shuffle(T &s)
  {
    for (int i = 1; i < (int)s.size(); i++) {
      int j = nextInt(0, i + 1);
      if (i != j) {
        std::swap(s[i], s[j]);
      }
    }
  }

  template <typename T, int n>
  void shuffle(T (&ary)[n])
  {
    for (int i = 1; i < n; i++) {
      int j = nextInt(0, i + 1);
      if (i != j) {
        std::swap(ary[i], ary[j]);
      }
    }
  }
};

class Seq
{
  struct OpHistory {
    int i, j;

    OpHistory() {}
    OpHistory(int i, int j) : i(i), j(j) {}
  };

  vector<int> seq;
  vector<OpHistory> history;

public:
  void push(int v)
  {
    seq.push_back(v);
  }

  vector<int>::iterator begin()
  {
    return seq.begin();
  }

  vector<int>::iterator end()
  {
    return seq.end();
  }

  int size() const
  {
    return seq.size();
  }

  void commit()
  {
    history.clear();
  }

  void rollback()
  {
    while (!history.empty()) {
      auto h = history.back();
      history.pop_back();
      std::swap(seq[h.i], seq[h.j]);
    }
  }

  void swap(int i, int j)
  {
    std::swap(seq[i], seq[j]);
    history.emplace_back(i, j);
  }

  int operator[](int i) const { return seq[i]; }
};

#ifdef PROFILE
#include <gperftools/profiler.h>
#endif

using namespace std;

struct Edge {
  int to, d;
  Edge() {}
  Edge(int to_, int d_) : to(to_), d(d_){};
};

struct Order {
  int64_t t;
  int id, dst;
  Order() {}
  Order(int t_, int id_, int dst_) : t(t_), id(id_), dst(dst_){};
  bool operator<(const Order &o) const { return t < o.t; }
};

int V, E;
vector<vector<Edge>> g;
vector<vector<int>> nxt;
vector<vector<int>> tim;
int64_t Tmax;
vector<Order> orders;
Timer timer;
XorShiftL rng;

template <typename T>
using pq = priority_queue<T, vector<T>, greater<T>>;

Seq state;
int64_t curCost = 1LL << 60;

vector<bool> visited;
vector<int64_t> cnt;
vector<int64_t> sum1;
vector<int64_t> sum2;

void greedy()
{
  int64_t cost = 0;

  int uo = 0;
  int v = 0;
  int64_t t = 0;
  vector<Order> curOrders;

  auto pick = [&]() {
    if (v != 0) return;
    for (; uo < (int)orders.size() && orders[uo].t <= t; uo++) {
      curOrders.push_back(orders[uo]);
      state.push(uo);
    }
  };

  for (;;) {
    int low = uo, high = (int)orders.size();
    while (high - low > 1) {
      int mid = (low + high) / 2;
      if (orders[mid].t <= t) low = mid;
      else high = mid;
    }

    bool moveStore = curOrders.empty();
    int restOrder = (int)curOrders.size();
    int unpickedOrder = high - uo;

    moveStore |= restOrder <= 50  && unpickedOrder >= 15;

    if (moveStore) {
      // cerr << "greedy1 : " << cost << " " << uo << " " << orders.size() << " " << t << " " << orders[uo].t << endl;
      t += tim[v][0];
      v = 0;
      if (uo >= (int)orders.size()) break;
      t = std::max(t, orders[uo].t);
      pick();
      continue;
    }

    int shortest = 0;
    for (int i = 1; i < (int)curOrders.size(); i++) {
      int d1 = curOrders[shortest].dst;
      int d2 = curOrders[i].dst;
      if (tim[v][d1] > tim[v][d2]) {
        shortest = i;
      }
    }

    t += tim[v][curOrders[shortest].dst];
    v = curOrders[shortest].dst;

    if (t <= Tmax) {
      const int p = curOrders[shortest].t;
      auto cur = Tmax * Tmax - (t - p) * (t - p);
      cost += cur;
    }
    
    state.push(curOrders[shortest].id);
    swap(curOrders[shortest], curOrders.back());
    curOrders.pop_back();
  }

  // cerr << "greedy=" << cost << endl;
}

int64_t simulate()
{
  fill(visited.begin(), visited.end(), false);
  fill(cnt.begin(), cnt.end(), 0LL);
  fill(sum1.begin(), sum1.end(), 0LL);
  fill(sum2.begin(), sum2.end(), 0LL);

  int64_t cost = 0;

  int uo = 0;
  int v = 0;
  int64_t t = 0;
  int64_t visit = 0;

  auto pick = [&]() {
    if (v != 0) return;
    for (; uo < (int)orders.size() && orders[uo].t <= t; uo++) {
      int d = orders[uo].dst;
      cnt[d]++;
      sum1[d] += orders[uo].t;
      sum2[d] += orders[uo].t * orders[uo].t;
    }
  };

  auto moveTo = [&](int dst) {
    while (v != dst) {
      const int u = nxt[v][dst];
      t += tim[v][u];
      if (t > Tmax) return;
      v = u;
      cost += cnt[v] * t * t - 2 * t * sum1[v] + sum2[v];
      visit += cnt[v];
      cnt[v] = sum1[v] = sum2[v] = 0;
      pick();
    }
  };

  for (int o : state) {
    auto &order = orders[o];
    // cerr << o << " " << visited.size() << " " << V << order.id << endl;
    if (visited[o]) {
      moveTo(order.dst);
      if (t > Tmax) break;
      // visit++;
    } else {
      moveTo(0);
      if (t > Tmax) break;
      t = std::max(t, order.t);
      visited[o] = true;
      pick();
    }
  }
  return -(visit * Tmax * Tmax - cost);
}

void print()
{
  fill(visited.begin(), visited.end(), false);
  int64_t cost = 0;
  int v = 0;
  int t = 0;

  int uo = 0;
  vector<vector<int64_t>> picked(V);

  auto pick = [&]() {
    if (v != 0) return;
    for (; uo < (int)orders.size() && orders[uo].t <= t; uo++) {
      picked[orders[uo].dst].push_back(orders[uo].t);
    }
  };

  auto moveTo = [&](int dst) {
    while (v != dst) {
      const int u = nxt[v][dst];
      for (int i = 0; i < tim[v][u]; i++) {
        t++;
        if (t > Tmax) return;
        cout << u + 1 << endl;
      }
      v = u;
      for (auto p : picked[v]) {
        // cerr << v << " " << p << endl;
        auto cur = Tmax * Tmax - (t - p) * (t - p);
        cost += cur;
      }
      picked[v].clear();
      pick();
    }
  };

  auto stay = [&](int ti) {
    while (t < ti) {
      t++;
      if (t > Tmax) break;
      cout << -1 << endl;
    }
  };

  for (int o : state) {
    auto &order = orders[o];
    if (visited[o]) {
      moveTo(order.dst);
      if (t > Tmax) break;
      // cerr << t << " "  << cost << endl;
    } else {
      moveTo(0);
      visited[o] = true;
      stay(orders[o].t);
      pick();
    }
  }
  cerr << "score " << simulate() << ", t=" << t << endl;
}

void solve()
{
  timer.reset();

  cin >> V >> E;
  g.resize(V);
  for (int i = 0; i < E; i++) {
    int u, v, d;
    cin >> u >> v >> d;
    u--, v--;
    g[u].emplace_back(v, d);
    g[v].emplace_back(u, d);
  }
  cin >> Tmax;
  for (int i = 0; i < Tmax; i++) {
    int n, id, dst;
    cin >> n;
    for (int j = 0; j < n; j++) {
      cin >> id >> dst;
      id--, dst--;
      // if (i<= 292) cerr << i << " " << id << " " << dst << endl;
      orders.emplace_back(i, id, dst);
    }
  }
  // cerr << V << " " << orders.size() << endl;

  cnt.resize(V);
  sum1.resize(V);
  sum2.resize(V);

  nxt.assign(V, vector<int>(V));
  tim.assign(V, vector<int>(V));
  for (int i = 0; i < V; i++) {
    pq<pair<int, int>> que;
    que.emplace(0, i);
    vector<pair<int, int>> dist(V, make_pair(1 << 29, -1));
    dist[i].first = 0;
    while (!que.empty()) {
      int v, d;
      tie(d, v) = que.top();
      que.pop();
      if (dist[v].first != d) continue;
      for (auto e : g[v]) {
        int c = d + e.d;
        if (dist[e.to].first <= c) continue;
        dist[e.to] = make_pair(c, v);
        que.emplace(c, e.to);
      }
    }
    for (int j = 0; j < V; j++) {
      if (i == j) {
        nxt[i][j] = -1;
        tim[i][j] = 0;
        continue;
      }
      int v;
      for (v = j; dist[v].second != i; v = dist[v].second) {
      }
      nxt[i][j] = v;
      tim[i][j] = dist[j].first;
    }
  }

  greedy();
  // for (auto order : orders) {
  //   state.push(order.id);
  //   state.push(order.id);
  // }

  visited.resize(orders.size());

  curCost = simulate();

  const int TIME_LIMIT = 29000;
  const int START_TIME = 0;
  const double T0 = 0;
  const double T1 = 0;
  // cerr << "ans=" << curCost << endl;
  int iter = 0;

  // if (false)
  while (true) {
    iter++;
    const double t = timer.elapsed();

    if (t >= TIME_LIMIT) {
      break;
    }

    // int i, j;
    // tie(i, j) = rng.nextIntUnorderedPair(0, state.size());
    // state.swap(i, j);

    int i, j;
    tie(i, j) = rng.nextIntUnorderedPair(0, state.size());
    if (std::abs(i-j) < V/5) continue;
    for (; i < j; i++) state.swap(i, i+1);
    for (; i > j; i--) state.swap(i, i-1);

    // int sz = std::min(5, state.size());
    // int m = rng.nextInt(sz) + 3;
    // int s = rng.nextInt(0, state.size() - m);
    // for (int i = 1; i < m; i++) {
    //   int j = rng.nextInt(i + 1);
    //   if (j != i) {
    //     state.swap(i+s, j+s);
    //   }
    // }

    const int64_t diff = simulate() - curCost;
    const double temp = calcTemp(START_TIME, TIME_LIMIT, t, T0, T1);

    // cerr << "test " << diff << " " << temp << " " << temp * rng.nextLog() << endl;
    if (diff <= -temp * rng.nextLog()) {
      state.commit();
      if (diff != 0) {
        cerr << "commit " << diff << " " << curCost << endl;
      }
      curCost += diff;
      continue;
    }
    state.rollback();
    // cerr << "rollback" << endl;
  }
  cerr << "iter=" << iter << ", ans=" << curCost << endl;
}

int main()
{
#ifdef PROFILE
  ProfilerStart("");
#endif

  solve();
  print();

#ifdef PROFILE
  ProfilerStop();
#endif
}

Measured memory_bytes: 2560