#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target ("sse4.2")

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <functional>
#include <numeric>
#include <random>
#include <chrono>
#include <cstring>
#include <climits>
#include <cassert>

#include <x86intrin.h>

using namespace std;

typedef long long int ll;

struct xorshift64 {
  unsigned long long int x = 88172645463325252ULL;
  inline unsigned short nextUShort() {
    x = x ^ (x << 7);
    return x = x ^ (x >> 9);
  }
  inline unsigned int nextUShortMod(unsigned long long int mod) {
    x = x ^ (x << 7);
    x = x ^ (x >> 9);
    return ((x & 0x000000000000ffff) * mod) >> 48;
  }
  inline unsigned int nextUInt() {
    x = x ^ (x << 7);
    return x = x ^ (x >> 9);
  }
  inline unsigned int nextUIntMod(unsigned long long int mod) {
    x = x ^ (x << 7);
    x = x ^ (x >> 9);
    return ((x & 0x00000000ffffffff) * mod) >> 32;
  }
  inline unsigned long long int nextULL() {
    x = x ^ (x << 7);
    return x = x ^ (x >> 9);
  }
  inline double nextDouble() {
    x = x ^ (x << 7);
    x = x ^ (x >> 9);
    return (double)x * 5.42101086242752217e-20;
  }
};

struct timer {
  double t = 0.0;
  inline void restart() {
    t = now();
  }
  inline double time() {
    return now() - t;
  }
  inline double now() {
    unsigned long long l, h;
    __asm__ ("rdtsc" : "=a"(l), "=d"(h));
    return (double)(l | h << 32) * 3.5714285714285715e-10; // 1 / 2.8e9
  }
};

//#define DEBUG

xorshift64 theRandom;
timer theTimer;

constexpr short tMax = 10000;
constexpr short vMax = 400;

short tFirst = 0;

short V, E;

list<pair<short, short> > graphAdj[vMax];
short dist[vMax][vMax];
vector<short> route[vMax][vMax];

vector<vector<short> > order(vMax);
vector<short> orderList(tMax, -1);
short orderCount[vMax];
short totalOrderCount;

vector<short> visitOrder, bestVisitOrder;
short visitOrderLength;
short nowEnded, lastEnded;

ll calcScoreForInit(short tNow, short pNow, short lNow, vector<vector<short> >& orderInit, short (&orderCountInit)[vMax], vector<short> &visitOrderInit, short &totalOrderCountInit, short &visitOrderInitLength) {
  short t = tNow;
  ll score = 0;
  short now = pNow;
  short lastSupply = lNow;
  vector<short> delivered(V);
  short i = 0;
  for (i = 0; i < visitOrderInitLength; i++) {
    short next = visitOrderInit[i];
    for (short j = 1; j < route[now][next].size(); j++) {
      t += dist[route[now][next][j-1]][route[now][next][j]]; // move
      if (t > tMax) break; // out of time
      if (route[now][next][j] == 0) lastSupply = t; // resupply if arrived at store
      while (delivered[route[now][next][j]] < orderCountInit[route[now][next][j]]) {
        if (lastSupply >= orderInit[route[now][next][j]][delivered[route[now][next][j]]]) {
          score += (tMax * tMax) - ((t - orderInit[route[now][next][j]][delivered[route[now][next][j]]]) * (t - orderInit[route[now][next][j]][delivered[route[now][next][j]]]));
          delivered[route[now][next][j]]++; // fill an order
        }
        else break;
      }
    }
    if (t > tMax) break; // out of time
    now = next;
  }
  nowEnded = i;
  t = min(t, tMax);
  if (t > tNow) score /= t - tNow; // efficiency
  return score;
}

short updateAndGetArriveTimeForInit(short tNow, short &pNow, short &lNow, vector<vector<short> >& orderInit, short (&orderCountInit)[vMax], vector<short> &visitOrderInit, short &totalOrderCountInit, short &visitOrderInitLength) {
  short t = tNow;
  ll score = 0;
  short now = pNow;
  short lastSupply = lNow;
  vector<short> delivered(V);
  short i = 0;
  for (i = 0; i < visitOrderInitLength; i++) {
    short next = visitOrderInit[i];
    if (route[now][next].size() <= 1) continue;
    for (short j = 1; j < route[now][next].size(); j++) {
      t += dist[route[now][next][j-1]][route[now][next][j]]; // move
      if (t > tMax) break; // out of time
      if (route[now][next][j] == 0) lastSupply = t; // resupply if arrived at store
      if (route[now][next][j] == 0) {
        next = 0;
        break;
      }
      while (delivered[route[now][next][j]] < orderCountInit[route[now][next][j]]) {
        if (lastSupply >= orderInit[route[now][next][j]][delivered[route[now][next][j]]]) {
          score += (tMax * tMax) - ((t - orderInit[route[now][next][j]][delivered[route[now][next][j]]]) * (t - orderInit[route[now][next][j]][delivered[route[now][next][j]]]));
          orderInit[route[now][next][j]].erase(orderInit[route[now][next][j]].begin()); // remove filled order
          orderCountInit[route[now][next][j]]--;
          totalOrderCountInit--;
        }
        else break;
      }
    }
    if (t > tMax) break; // out of time
    now = next;
    visitOrder.push_back(now);
    if (now == 0) break;
  }
  if (t == tNow) {
    t++;
    if (now == 0) lastSupply = t;
  }
  lNow = lastSupply;
  pNow = now;
  nowEnded = i;
  for (short i = tNow+1; i <= min((short)(tMax-1), t); i++) {
    if (orderList[i] != -1) {
      orderInit[orderList[i]].push_back(i);
      orderCountInit[orderList[i]]++;
      totalOrderCountInit++;
    }
  }
  return t;
}

void findInitial() {
  short tNow = tFirst-1;
  short pNow = 0;
  short lNow = tFirst-1;

  vector<vector<short> > orderInit(vMax);
  short orderCountInit[vMax];
  for (int i = 0; i < V; i++) orderCountInit[i] = 0;
  short totalOrderCountInit = 0;

  vector<short> visitOrderInit = vector<short>(15);
  short visitOrderInitLength = visitOrderInit.size();
  for (int i = 0; i < visitOrderInitLength; i++) visitOrderInit[i] = theRandom.nextUIntMod(V);

  ll score = 1000000;
  ll lastScore = score;
  ll bestScore = score;

  while (tNow < tMax) {
    if (totalOrderCountInit == 0) { // no order yet
      tNow = updateAndGetArriveTimeForInit(tNow, pNow, lNow, orderInit, orderCountInit, visitOrderInit, totalOrderCountInit, visitOrderInitLength);
      continue;
    }
    vector<short> bestVisitOrderInit = visitOrderInit;

    double temperature = bestScore * 0.03;
    double decayRate = 1.8e-5;

    score = calcScoreForInit(tNow, pNow, lNow, orderInit, orderCountInit, visitOrderInit, totalOrderCountInit, visitOrderInitLength);
    lastScore = score;
    bestScore = score;

    double timeLimit = theTimer.time() + 0.300;

    int iterCount = 0;
    int unUpdated = 0;

    //while (theTimer.time() < timeLimit) {
    while (unUpdated < 100000) {
      double swapRoll = theRandom.nextDouble();
      if (swapRoll < 0.25) { // swap
        int v1 = theRandom.nextUIntMod(visitOrderInitLength-1);
        int v2 = theRandom.nextUIntMod(visitOrderInitLength-1);
        if (v1 == v2 || visitOrderInit[v1] == visitOrderInit[v2]) continue;
        swap(visitOrderInit[v1], visitOrderInit[v2]);

        score = calcScoreForInit(tNow, pNow, lNow, orderInit, orderCountInit, visitOrderInit, totalOrderCountInit, visitOrderInitLength);

        #ifdef DEBUG
        if (iterCount % 10000 == 0) cerr << iterCount << " " << theTimer.time() << " " << score << " " << lastScore << " " << bestScore << " " << lastEnded << " " << temperature << endl;
        #endif

        if (score >= lastScore) {
          lastScore = score;
          if (score > bestScore) {
            bestScore = score;
            bestVisitOrderInit = visitOrderInit;
            unUpdated = 0;
          }
        }
        else if (theRandom.nextDouble() < exp((score - bestScore) / temperature)) { // accept
          lastScore = score;
        }
        else { // rollback
          swap(visitOrderInit[v1], visitOrderInit[v2]);
        }
      }
      else if (swapRoll < 0.5){ // insert
        int v1 = theRandom.nextUIntMod(visitOrderInitLength-1);
        int v2 = theRandom.nextUIntMod(visitOrderInitLength-1);
        if (v1 == v2) continue;
        int tmp;
        tmp = visitOrderInit[v1];
        if (v1 < v2) {
          for (int i = v1; i < v2; i++) visitOrderInit[i] = visitOrderInit[i+1];
        }
        else {
          for (int i = v1; i > v2; i--) visitOrderInit[i] = visitOrderInit[i-1];
        }
        visitOrderInit[v2] = tmp;

        score = calcScoreForInit(tNow, pNow, lNow, orderInit, orderCountInit, visitOrderInit, totalOrderCountInit, visitOrderInitLength);

        #ifdef DEBUG
        if (iterCount % 10000 == 0) cerr << iterCount << " " << theTimer.time() << " " << score << " " << lastScore << " " << bestScore << " " << lastEnded << " " << temperature << endl;
        #endif

        if (score >= lastScore) {
          lastScore = score;
          if (score > bestScore) {
            bestScore = score;
            bestVisitOrderInit = visitOrderInit;
            unUpdated = 0;
          }
        }
        else if (theRandom.nextDouble() < exp((score - bestScore) / temperature)) { // accept
          lastScore = score;
        }
        else { // rollback
          tmp = visitOrderInit[v2];
          if (v2 < v1) {
            for (int i = v2; i < v1; i++) visitOrderInit[i] = visitOrderInit[i+1];
          }
          else {
            for (int i = v2; i > v1; i--) visitOrderInit[i] = visitOrderInit[i-1];
          }
          visitOrderInit[v1] = tmp;
        }
      }
      else {
        int v = theRandom.nextUIntMod(visitOrderInitLength-1);
        int d = theRandom.nextUIntMod(V);
        if (visitOrderInit[v] == d) continue;
        short dBefore = visitOrderInit[v];
        visitOrderInit[v] = d;

        score = calcScoreForInit(tNow, pNow, lNow, orderInit, orderCountInit, visitOrderInit, totalOrderCountInit, visitOrderInitLength);

        #ifdef DEBUG
        if (iterCount % 10000 == 0) cerr << iterCount << " " << theTimer.time() << " " << score << " " << lastScore << " " << bestScore << " " << lastEnded << " " << temperature << endl;
        #endif

        if (score >= lastScore) {
          lastScore = score;
          if (score > bestScore) {
            bestScore = score;
            bestVisitOrderInit = visitOrderInit;
            unUpdated = 0;
          }
        }
        else if (theRandom.nextDouble() < exp((score - bestScore) / temperature)) { // accept
          lastScore = score;
        }
        else { // rollback
          visitOrderInit[v] = dBefore;
        }
      }

      iterCount++;
      unUpdated++;
      temperature *= 1.0 - decayRate;
    }

    visitOrderInit = bestVisitOrderInit;
    tNow = updateAndGetArriveTimeForInit(tNow, pNow, lNow, orderInit, orderCountInit, visitOrderInit, totalOrderCountInit, visitOrderInitLength);
  }
}

ll calcScore() {
  short t = tFirst;
  ll score = 0;
  short now = 0;
  short lastSupply = tFirst;
  vector<short> delivered(V);
  short i = 0;
  for (i = 0; i < visitOrderLength; i++) {
    short next = visitOrder[i];
    for (short j = 1; j < route[now][next].size(); j++) {
      t += dist[route[now][next][j-1]][route[now][next][j]]; // move
      if (t > tMax) break; // out of time
      if (route[now][next][j] == 0) lastSupply = t; // resupply if arrived at store
      while (delivered[route[now][next][j]] < orderCount[route[now][next][j]]) {
        if (lastSupply >= order[route[now][next][j]][delivered[route[now][next][j]]]) {
          score += (tMax * tMax) - ((t - order[route[now][next][j]][delivered[route[now][next][j]]]) * (t - order[route[now][next][j]][delivered[route[now][next][j]]]));
          delivered[route[now][next][j]]++; // fill an order
        }
        else break;
      }
    }
    if (t > tMax) break; // out of time
    now = next;
  }
  nowEnded = i;
  return score;
}

vector<short> getAnswer() {
  short t = tFirst;
  ll score = 0;
  short now = 0;
  short lastSupply = tFirst;
  vector<short> delivered(V);
  vector<short> ans(tMax, -1);
  short i = 0;
  for (i = 0; i < visitOrderLength; i++) {
    short next = visitOrder[i];
    for (short j = 1; j < route[now][next].size(); j++) {
      short lastT = t;
      t += dist[route[now][next][j-1]][route[now][next][j]]; // move
      if (t > tMax) break; // out of time
      if (route[now][next][j] == 0) lastSupply = t; // resupply if arrived at store
      for (short k = lastT; k < t; k++) ans[k] = route[now][next][j] + 1;
      while (delivered[route[now][next][j]] < orderCount[route[now][next][j]]) {
        if (lastSupply >= order[route[now][next][j]][delivered[route[now][next][j]]]) {
          score += (tMax * tMax) - ((t - order[route[now][next][j]][delivered[route[now][next][j]]]) * (t - order[route[now][next][j]][delivered[route[now][next][j]]]));
          delivered[route[now][next][j]]++; // fill an order
        }
        else break;
      }
    }
    if (t > tMax) break; // out of time
    now = next;
  }
  return ans;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  theTimer.restart();

  for (int i = 0; i < vMax; i++) orderCount[i] = 0;

  cin >> V >> E;
  for (int i = 0; i < E; i++) {
    int u, v, d;
    cin >> u >> v >> d;
    graphAdj[u-1].emplace_back(d, v-1);
    graphAdj[v-1].emplace_back(d, u-1);
  }
  int _tMax;
  cin >> _tMax;
  for (int i = 0; i < tMax; i++) {
    int c;
    cin >> c;
    for (int j = 0; j < c; j++) {
      int _id, d;
      cin >> _id >> d;
      order[d-1].push_back(i);
      orderList[i] = d-1;
      orderCount[d-1]++;
      totalOrderCount++;
      if (totalOrderCount == 1) tFirst = i;
    }
  }

  // calculate dist
  for (int i = 0; i < V; i++) {
    priority_queue<pair<int, vector<short> >, vector<pair<int, vector<short> > >, greater<pair<int, vector<short> > > > que;
    vector<bool> visited(V);
    que.emplace(0, vector<short>({i}));
    while (!que.empty()) {
      auto now = que.top();
      que.pop();
      int v = now.second[now.second.size()-1];
      if (visited[v]) continue;
      visited[v] = true;
      dist[i][v] = now.first;
      route[i][v] = now.second;
      for (auto &adj: graphAdj[v]) {
        auto next = now;
        next.first += adj.first;
        next.second.push_back(adj.second);
        que.push(next);
      }
    }
  }

  theRandom.x = ((ll)(random_device()()) << 32) | random_device()();

  findInitial();
  visitOrderLength = visitOrder.size();
  bestVisitOrder = visitOrder;

  ll score = calcScore();
  ll lastScore = score;
  ll bestScore = score;
  lastEnded = nowEnded;

  double temperature = 1.0e7;
  double decayRate = 1.0e-5;

  double timeLimit = 29.900;

  int iterCount = 0;

  while (theTimer.time() < timeLimit) {
    double swapRoll = theRandom.nextDouble();
    if (swapRoll < 0.25) { // swap
      int v1 = theRandom.nextUIntMod(lastEnded);
      int v2 = theRandom.nextUIntMod(lastEnded);
      if (v1 == v2 || visitOrder[v1] == visitOrder[v2] || lastEnded < max(v1, v2)) continue;
      swap(visitOrder[v1], visitOrder[v2]);

      score = calcScore();

      #ifdef DEBUG
      if (iterCount % 10000 == 0) cerr << iterCount << " " << theTimer.time() << " " << score << " " << lastScore << " " << bestScore << " " << lastEnded << " " << temperature << endl;
      #endif

      if (score >= lastScore) {
        lastScore = score;
        if (score >= bestScore) {
          bestScore = score;
          bestVisitOrder = visitOrder;
        }
        lastEnded = nowEnded;
      }
      else if (theRandom.nextDouble() < exp((score - lastScore) / temperature)) { // accept
        lastScore = score;
        lastEnded = nowEnded;
      }
      else { // rollback
        swap(visitOrder[v1], visitOrder[v2]);
      }
    }
    else if (swapRoll < 0.5){ // insert
      int v1 = theRandom.nextUIntMod(visitOrderLength);
      int v2 = theRandom.nextUIntMod(visitOrderLength);
      if (v1 == v2 || lastEnded < min(v1, v2)) continue;
      int tmp;
      tmp = visitOrder[v1];
      if (v1 < v2) {
        for (int i = v1; i < v2; i++) visitOrder[i] = visitOrder[i+1];
      }
      else {
        for (int i = v1; i > v2; i--) visitOrder[i] = visitOrder[i-1];
      }
      visitOrder[v2] = tmp;

      score = calcScore();

      #ifdef DEBUG
      if (iterCount % 10000 == 0) cerr << iterCount << " " << theTimer.time() << " " << score << " " << lastScore << " " << bestScore << " " << lastEnded << " " << temperature << endl;
      #endif

      if (score >= lastScore) {
        lastScore = score;
        if (score >= bestScore) {
          bestScore = score;
          bestVisitOrder = visitOrder;
        }
        lastEnded = nowEnded;
      }
      else if (theRandom.nextDouble() < exp((score - lastScore) / temperature)) { // accept
        lastScore = score;
        lastEnded = nowEnded;
      }
      else { // rollback
        tmp = visitOrder[v2];
        if (v2 < v1) {
          for (int i = v2; i < v1; i++) visitOrder[i] = visitOrder[i+1];
        }
        else {
          for (int i = v2; i > v1; i--) visitOrder[i] = visitOrder[i-1];
        }
        visitOrder[v1] = tmp;
      }
    }
    else {
      int v = theRandom.nextUIntMod(visitOrderLength);
      int d = theRandom.nextUIntMod(V);
      if (visitOrder[v] == d || lastEnded < v) continue;
      short dBefore = visitOrder[v];
      visitOrder[v] = d;

      score = calcScore();

      #ifdef DEBUG
      if (iterCount % 10000 == 0) cerr << iterCount << " " << theTimer.time() << " " << score << " " << lastScore << " " << bestScore << " " << lastEnded << " " << temperature << endl;
      #endif

      if (score >= lastScore) {
        lastScore = score;
        if (score >= bestScore) {
          bestScore = score;
          bestVisitOrder = visitOrder;
        }
        lastEnded = nowEnded;
      }
      else if (theRandom.nextDouble() < exp((score - lastScore) / temperature)) { // accept
        lastScore = score;
        lastEnded = nowEnded;
      }
      else { // rollback
        visitOrder[v] = dBefore;
      }
    }

    iterCount++;
    temperature *= 1.0 - decayRate;
  }

  visitOrder = bestVisitOrder;

  vector<short> ans = getAnswer();
  for (auto &x: ans) cout << x << endl;

  cerr << "Final statistics:" << endl;
  cerr << "iterCount   = " << iterCount << endl;
  cerr << "bestScore   = " << bestScore << endl;
  cerr << "temperature = " << temperature << endl;
  cerr << "timeTaken   = " << theTimer.time() << endl;

  return 0;
}


Measured memory_bytes: 13952