#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> Path;

const int INF = (1<<28);

ll SCORE = 0;
clock_t START, END;

class Edge{
public:
  int from, to, cost;

  Edge() : cost(INF) {}
  Edge(int s, int t, int d) : from(s), to(t), cost(d) {}
};

class Graph {
public:
  int N, M;
  vector<vector<Edge>> edges;
  vector<vector<int>> dists;
  vector<vector<Path>> paths;

  Graph() {}
  Graph(int _N, int _M) : N(_N), M(_M),
                          edges(N, vector<Edge>(N, Edge())),
                          dists(N, vector<int>(N, INF)),
                          paths(N, vector<vector<int>>(N)) {}

  void input() {
    for (int i = 0; i < M; i++) {
      int s, t, d;
      scanf("%d %d %d", &s, &t, &d);
      s--; t--;
      edges[s][t] = Edge(s, t, d);
      edges[t][s] = Edge(t, s, d);
    }
  }

  void init() {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (edges[i][j].cost == INF) continue;
        dists[i][j] = dists[j][i] = edges[i][j].cost;

        Path p1, p2;
        p1.push_back(i); p1.push_back(j);
        p2.push_back(j); p2.push_back(i);
        paths[i][j] = p1;
        paths[j][i] = p2;
      }
      dists[i][i] = 0;
      paths[i][i] = Path(1, i);
    }
    for (int k = 0; k < N; k++) {
      for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
          int tmp = dists[i][k] + dists[k][j];
          if (tmp < dists[i][j]) {
            dists[i][j] = tmp;

            Path path = paths[i][k];
            path.insert(path.end(), paths[k][j].begin()+1, paths[k][j].end());
            paths[i][j] = path;
          }
        }
      }
    }
  }
};


class Order {
public:
  int ID, DST;
  int t;

  Order() {}
  Order(int _ID, int _DST, int _t) : ID(_ID), DST(_DST), t(_t) {}
};

class OrderList {
public:
  int T;
  vector<Order> list;

  OrderList() {}
  OrderList(int _T) : T(_T), list(T) {}
  void input() {
    for (int t = 0; t < T; t++) {
      int n; scanf("%d", &n);
      int id, dst;
      if (n == 0) {
        id = dst = INF;
      } else {
        scanf("%d %d", &id, &dst);
        dst--;
      }
      list[t] = Order(id, dst, t);
    }
  }
  void init() {}
};


class Solver {
public:
  const int SKIP_NUM = 3;
  const int MAX_Iter = 500;   // 温度変更回数(500?)
  double INI_TEMP = 100;
  Graph G;
  OrderList ol;
  Path ans;

  int time;
  vector<ll> pow2;

  Solver(Graph _G, OrderList _ol) : time(0), G(_G), ol(_ol), pow2(ol.T+1) {
    for (int i = 0; i < ol.T+1; i++) {
      pow2[i] = pow(i, 2);
    }
  }

  // TODO : 高速化
  Path make_path(int s, vector<int> dest_order, int t) {
    int now = s, next = dest_order[0];
    Path path(1, now);
    dest_order.push_back(t);

    vector<bool> visited(G.N, false);
    for (int i = 0; i < dest_order.size(); i++) {
      next = dest_order[i];
      if (visited[next]) continue;
      visited[next] = true;

      Path now_to_next = G.paths[now][next];
      for (int j = 1; j < now_to_next.size(); j++) {
        next = now_to_next[j];
        path.push_back(next);
        visited[next] = true;
      }
      now = next;
    }
    return path;
  }

  // TODO : 高速化
  pair<ll, ll> evaluate_path(Path path, vector<vector<Order>> orders) {
    ll score = 0, ori_score = 0;
    int len = path.size(), t = time;

    // 目的地に届けた注文
    for (int i = 1; i < len; i++) {
      int from = path[i-1], to = path[i];
      t += G.dists[from][to];
      if (to == 0) break;
      if (t >= ol.T) {
        t = ol.T-1;
        break;
      }
      for (int j = 0; j < orders[to].size(); j++) {
        ori_score += pow2[ol.T] - pow2[t - orders[to][j].t];

        score += pow2[ol.T] - pow2[t - orders[to][j].t];
      }
      orders[to].clear();
    }

    // 店舗に帰るまでの時間
    ll total_time = t - time;
    score -= pow((ll)total_time, 4);

    // 残した注文
    for (int i = 0; i < G.N; i++) {
      for (int j = 0; j < orders[i].size(); j++) {
        score -= pow((ll)2*t - orders[i][j].t, 2);
      }
    }
    return make_pair(score, ori_score);
  }

  double exchange_probability(double diff, double temp){
    return exp(-diff / temp);
  }

  double calc_temperature(int iteration){
    return (double)INI_TEMP - pow(INI_TEMP, double(iteration)/MAX_Iter);
  }

  // TODO : 最適化
  vector<int> init_order(vector<vector<Order>> orders) {
    int now = 0;
    vector<int> dest_order;
    vector<bool> go(G.N, false);
    for (int i = 0; i < G.N; i++) {
      go[i] = !orders[i].empty();
    }
    for (int i = 0; i < G.N; i++) {
      int mn = INF, next = -1;
      for (int j = 0; j < G.N; j++) {
        if (go[j] && G.dists[now][j] < mn) {
          mn = G.dists[now][j];
          next = j;
        }
      }
      if (next == -1) break;
      go[next] = false;
      dest_order.push_back(next);
      now = next;
    }
    return dest_order;
  }

  // TODO : 高速化、温度による許容の部分
  Path search_path(vector<Order> ordlist) {
    vector<vector<Order>> orders(G.N);
    for (int i = 0; i < ordlist.size(); i++) {
      Order ord = ordlist[i];
      orders[ord.DST].push_back(ord);
    }

    // 初期値の決定
    vector<int> dest_order = init_order(orders);

    // simulated annealing ???
// START = clock();
    Path res_path = make_path(0, dest_order, 0);
// END = clock();
// cerr << "time(make_path): " << (double)(END - START)/CLOCKS_PER_SEC << endl;
// START = clock();
    pair<ll, ll> score_oriscore = evaluate_path(res_path, orders);
// END = clock();
// cerr << "time(eval_path): " << (double)(END - START)/CLOCKS_PER_SEC << endl;

// return res_path;

    ll pre_score = score_oriscore.first;
    ll ori_score = score_oriscore.second;

    ll max_score = pre_score;
cerr << "parcel num : " << ordlist.size() << endl;
cerr << "first score: " << ori_score << endl;

    random_device seed;
    mt19937 mt(seed());
    uniform_real_distribution<> rand01(0.0, 1.0);

    int N = dest_order.size();
// cerr << "dest_order: ";
// for (int i = 0; i < N; i++) {
//   cerr << dest_order[i] << " \n"[i==N-1];
// }
    for (int iter = 0; iter < MAX_Iter; iter++) {
      double temp = calc_temperature(iter+1);
// cerr << iter << ", tmep: " << temp << endl;

      int CNT = N/10;
      for (int c = 0; c < CNT-1; c++) {
        uniform_int_distribution<> rand0X(0, N-2);
        int i = rand0X(mt);
        uniform_int_distribution<> randXN(i+1, N-1);
        int j = randXN(mt);
// cerr << "(i, j) = (" << i << ", " << j << ")\n";
        vector<int> new_order = dest_order;
        if (rand01(mt) <= 0.5) {
          swap(new_order[i], new_order[j]);
        } else {
          reverse(new_order.begin()+i, new_order.begin()+j+1);
        }

        Path new_path = make_path(0, new_order, 0);
        score_oriscore = evaluate_path(new_path, orders);
        ll new_score = score_oriscore.first;
        ll pre_ori   = score_oriscore.second;

        ll diff = new_score - pre_score;
        if (diff > 0) {
// cerr << "pre_score: " << pre_score << ", new_score: " << new_score << endl;
          dest_order = new_order;
          pre_score = new_score;
          if (new_score > max_score) {
            max_score = new_score;
            res_path = new_path;
            ori_score = pre_ori;
// cerr << "max_score: " << max_score << endl;
          }
        } else if (diff < 0) {
          double prob = exchange_probability(-diff, temp/INI_TEMP*ol.T*ol.T);
// cerr << "temp: " << temp << ", prob: " << prob << endl;
          if (prob > rand01(mt)) {
            dest_order = new_order;
            pre_score = new_score;
          }
        }
      }
    }
cerr << "last score : " << ori_score << endl;
cerr << "max score  : " << (ll)ordlist.size() * ol.T * ol.T << endl;
// cerr << "diff score : " << -((ll)ordlist.size() * ol.T * ol.T - ori_score) << endl;
SCORE += ori_score;
    return res_path;
  }

  vector<Order> execute(Path path, vector<Order> ordlist) {
    vector<bool> visited(G.N, false);
    vector<Order> new_ordlist;
    int len = path.size();
    int now_node = path[0];
    for (int i = 1; i < len; i++) {
      int next_node = path[i];
      int dt = G.edges[now_node][next_node].cost;
      for (int t = time; t < min(time+dt, ol.T); t++) {
        ans.push_back(next_node+1);
        Order ord = ol.list[t+1];
        if (ord.ID != INF) new_ordlist.push_back(ord);
      }
      time += dt;

      visited[next_node] = true;
      now_node = next_node;

      // 途中で店舗に戻った場合
      if (now_node == 0) break;
    }

    // old_orderlistのチェック
    for (int i = 0; i < ordlist.size(); i++) {
      if (!visited[ordlist[i].DST]) {
        new_ordlist.push_back(ordlist[i]);
      }
    }
    return new_ordlist;
  }

  void solve() {
    vector<Order> ordlist;
    while (time < ol.T) {
      while (ordlist.size() < SKIP_NUM && time < ol.T) {
        Order ord = ol.list[time];
        if (ord.ID != INF) ordlist.push_back(ord);
        ans.push_back(-1);
        time++;
      }
      if (ordlist.empty()) break;
      Path path = search_path(ordlist);
      ordlist = execute(path, ordlist);
      cerr << endl;
    }
    cerr << "    score = " << SCORE << endl;
    cerr << "est_score = " << 30*SCORE << endl;
  }

  void output() {
    int sz = ans.size();
    if (sz < ol.T) {
      for (int i = 0; i < sz; i++) cout << ans[i] << endl;
      for (int i = 0; i < ol.T-sz; i++) cout << -1 << endl;
    } else {
      for (int i = 0; i < ol.T; i++) cout << ans[i] << endl;
    }
  }
};


/* ==================== main ==================== */
int main() {
  START = clock();

  int N, M; scanf("%d %d", &N, &M);
  Graph G(N, M);
  G.input();
  G.init();

  int T; scanf("%d", &T);
  OrderList ol(T);
  ol.input();
  ol.init();

  Solver sl(G, ol);
  sl.solve();
  sl.output();

  END = clock();
  cerr << "time: " << (double)(END - START)/CLOCKS_PER_SEC << endl;
  return 0;
}


Measured memory_bytes: 64768