#include "bits/stdc++.h"
#include "chrono"

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()

const int INF = 1 << 29;

auto start = chrono::system_clock::now();
double timelimit = 29800;

inline bool check_time() {
  auto end = chrono::system_clock::now();
  auto dur = end - start;
  auto msec = chrono::duration_cast<chrono::milliseconds>(dur).count();
  if (msec >= timelimit) return false;
  return true;
}

inline double get_time() {
  auto end = chrono::system_clock::now();
  auto dur = end - start;
  auto msec = chrono::duration_cast<chrono::milliseconds>(dur).count();
  return msec;
}

inline void now_time() {
  auto end = chrono::system_clock::now();
  auto dur = end - start;
  auto msec = chrono::duration_cast<chrono::milliseconds>(dur).count();
  cout << "time : " << msec << " msec" << endl;
}

unsigned long long seed128[4];  // x,y,z,wとして利用
inline void seed(unsigned long long s) {
  for (int i = 1; i <= 4; i++) {
    seed128[i - 1] = s = 1812433253U * (s ^ (s >> 30)) + i;
  }
}

inline unsigned long long Xor128() {
  unsigned long long t;
  t = (seed128[0] ^ (seed128[0] << 11));
  seed128[0] = seed128[1];
  seed128[1] = seed128[2];
  seed128[2] = seed128[3];
  seed128[3] = (seed128[3] ^ (seed128[3] >> 19)) ^ (t ^ (t >> 8));
  return seed128[3];
}

int n, m;  //頂点数, 辺数
vector<vector<pair<int, int>>> g;
const int T = 10000;
vector<pii> order;  //(id, v) ただし(-1, -1)は注文がないことを表す
int order_cnt = 0;  //注文数
vector<int> start_o;             // start_o[id]:= 注文idを開始した時刻
vector<vector<int>> order_info;  // order_info[v]:= 頂点vの注文リスト

vector<vector<int>> d;             // d[i][j]:= iからjへの最短経路距離
vector<vector<vector<int>>> path;  // d[i][j]:= iからjへの最短経路path
int delivery_cnt = 0;              //達成した注文数

vector<int> pri_v;
ll max_score = 0;
vector<vector<bool>> store_on_path;

void read() {
  // input graph
  cin >> n >> m;
  g.resize(n);
  rep(i, m) {
    int u, v, c;
    cin >> u >> v >> c;
    u--;
    v--;
    g[u].emplace_back(v, c);
    g[v].emplace_back(u, c);
  }

  // input order
  order_info.resize(n);
  int f;
  rep(i, n) cin >> f;
  int tmp_T;
  cin >> tmp_T;
}

vector<int> dijkstra(const int s, const int n, vector<int>& pv,
                     const vector<vector<pii>>& g) {
  priority_queue<pii, vector<pii>, greater<pii>> que;
  vector<int> d(n, INF);
  d[s] = 0;
  que.emplace(0, s);
  while (!que.empty()) {
    pii p = que.top();
    que.pop();
    int fr = p.second;
    if (d[fr] < p.first) continue;
    for (pair<int, int> e : g[fr]) {
      int to = e.fi;
      int cost = e.se;
      if (d[to] > d[fr] + cost) {
        d[to] = d[fr] + cost;
        pv[to] = fr;
        que.emplace(d[to], to);
      }
    }
  }
  return d;
}

vector<int> get_path(int t, const vector<int>& pv) {
  vector<int> path;
  while (t != -1) {
    path.push_back(t);
    t = pv[t];
  }
  reverse(path.begin(), path.end());
  return path;
}

void calc_graph() {
  d.resize(n);
  path.resize(n);
  store_on_path.resize(n);
  rep(i, n) {
    path[i].resize(n);
    store_on_path[i].resize(n, false);
  }

  rep(i, n) {
    vector<int> pv(n, -1);
    d[i] = dijkstra(i, n, pv, g);
    rep(j, n) {
      if (i == j) continue;
      path[i][j] = get_path(j, pv);
    }
  }
  store_on_path[0][0] = true;
  rep(i, n) {
    rep(j, n) {
      if (i == j) continue;
      rep2(k, 1, (int)path[i][j].size()) {
        if (path[i][j][k] == 0) store_on_path[i][j] = true;
      }
    }
  }
}

// nowtでidの注文を完了した時に、それによって得られるスコア
inline double calc_value(const int nowt, const int id) {
  double wait = (nowt - start_o[id]);
  return max(0.0, (double)T * T - wait * wait);
}

int update_ans(double& score, int& nowt, const vector<int>& TSP_path,
               vector<int>& ans, vector<int>& next_TSP_path) {
  assert(TSP_path[0] == 0);
  if (nowt < 9500) assert(TSP_path[(int)TSP_path.size() - 1] == 0);
  vector<pii> new_order;  //まだ商品を持っていないが注文は来ているid
  int order_num = -1;
  int order_id = -1;
  int order_v = -1;
  int n_achive = -1;
  int n_put = -1;

  rep(i, (int)TSP_path.size() - 1) {
    int now = TSP_path[i];
    int nx = TSP_path[i + 1];
    if (nowt + d[now][nx] > T) return 0;
    double rate = (double)(10001 - nowt) / 10001;
    int limd = 5 + Xor128() % 30;
    int limo = 50 + Xor128() % 200;
    if (now != 0 && (nowt < T && (int)new_order.size() >= limo &&
                     d[now][0] <= limd && d[now][0] + nowt <= T)) {
      assert(now != 0);
      rep(j, (int)path[now][0].size() - 1) {
        int pnow = path[now][0][j];
        int pnx = path[now][0][j + 1];
        rep(k, d[pnow][pnx]) {
          cout << pnx + 1 << endl;
          nowt++;
          string s;
          cin >> s;
          assert(s == "OK");
          if (k == d[pnow][pnx] - 1) break;
          cin >> n_achive;
          cin >> order_num;
          if (order_num == 1) {
            cin >> order_id >> order_v;
            new_order.push_back({--order_id, --order_v});
            start_o.push_back(nowt);
          }
          cin >> n_put;
        }
        cin >> n_achive;
        if ((int)order_info[pnx].size() != 0) {
          int tmp_achive;
          rep(_, n_achive) cin >> tmp_achive;
          assert(pnx != 0);
          next_TSP_path.push_back(pnx);
          assert(n_achive == (int)order_info[pnx].size());
          for (int id : order_info[pnx]) {
            delivery_cnt++;
            score += calc_value(nowt, id);
          }
          order_info[pnx].clear();
        }
        if (nowt == T) return -1;
        cin >> order_num;
        if (order_num == 1) {
          cin >> order_id >> order_v;
          new_order.push_back({--order_id, --order_v});
          start_o.push_back(nowt);
        }
        cin >> n_put;
      }
      for (auto p : new_order) {
        int tmp_id;
        cin >> tmp_id;
        int id = p.fi;
        int v = p.se;
        order_info[v].push_back(id);
      }
      next_TSP_path.push_back(0);
      new_order.clear();
      return -1;
    }
    rep(j, (int)path[now][nx].size() - 1) {
      int pnow = path[now][nx][j];
      int pnx = path[now][nx][j + 1];
      rep(k, d[pnow][pnx]) {
        cout << pnx + 1 << endl;
        nowt++;
        string s;
        cin >> s;
        assert(s == "OK");
        if (k == d[pnow][pnx] - 1) {
          break;
        }
        cin >> n_achive;
        cin >> order_num;
        if (order_num == 1) {
          cin >> order_id >> order_v;
          new_order.push_back({--order_id, --order_v});
          start_o.push_back(nowt);
        }
        cin >> n_put;
      }
      cin >> n_achive;
      if ((int)order_info[pnx].size() != 0) {
        int tmp_achive;
        rep(_, n_achive) cin >> tmp_achive;
        assert(pnx != 0);
        for (int id : order_info[pnx]) {
          delivery_cnt++;
          score += calc_value(nowt, id);
        }
        next_TSP_path.push_back(pnx);
        order_info[pnx].clear();
      }
      if (nowt == T) return -1;
      cin >> order_num;
      if (order_num == 1) {
        cin >> order_id >> order_v;
        new_order.push_back({--order_id, --order_v});
        start_o.push_back(nowt);
      }
      cin >> n_put;
      if (pnx == 0) {
        int tmp_id;
        for (auto p : new_order) {
          cin >> tmp_id;
          int id = p.fi;
          int v = p.se;
          order_info[v].push_back(id);
        }
        next_TSP_path.push_back(0);
        new_order.clear();
        return -1;
      }
    }
  }
  return -1;
}

inline vector<int> heulistic_TSP(const int nowt, const vector<int>& DS) {
  priority_queue<pii> pq;
  for (int v : DS) {
    pq.push({order_info[v].size(), v});
  }
  vector<int> VS;
  int lm = 200;
  while ((int)VS.size() < lm && !pq.empty()) {
    pii x = pq.top();
    pq.pop();
    VS.push_back(x.se);
  }

  vector<int> TSP_path;
  int now = 0;
  int tmp_nowt = nowt;
  TSP_path.push_back(0);
  vector<bool> visited(n, false);
  int cnt = 0;
  vector<pii> tmp_new_order(n);
  while (true) {
    double mn_score = INF;
    int nx = -1;
    for (auto v : VS) {
      if (visited[v]) continue;
      if (tmp_nowt + d[now][v] > T) continue;
      double ocnt = order_info[v].size();
      double tmp_score = d[now][v];
      double low = 20000;
      double up = 20000;
      if (-low <= nowt && nowt <= up) {
        tmp_score = d[now][v] * d[now][v] * d[now][v] / (ocnt);
      }
      if (tmp_score < mn_score) {
        mn_score = tmp_score;
        nx = v;
      }
    }
    if (nx == -1) break;  // TSP完了
    visited[nx] = true;
    bool via_store = false;
    rep2(i, 1, (int)path[now][nx].size()) {
      if (path[now][nx][i] == 0) {
        via_store = true;
        break;
      }
    }
    if (via_store) {
      TSP_path.push_back(0);
      return TSP_path;
    }
    for (auto v : VS) {
      if (visited[v]) continue;
      if (d[now][nx] >= d[now][v] + d[v][nx]) {
        visited[v] = true;
        TSP_path.push_back(v);
        break;
      }
    }
    TSP_path.push_back(nx);
    tmp_nowt += d[now][nx];
    now = nx;
    cnt++;
  }
  TSP_path.push_back(0);
  return TSP_path;
}

//最小単位で表現 (u->vをu->w->v)
vector<int> trans_minimum(const vector<int> TSP) {
  vector<int> ret;
  rep(i, (int)TSP.size() - 1) {
    int now = TSP[i];
    int nx = TSP[i + 1];
    rep(j, (int)path[now][nx].size() - 1) { ret.push_back(path[now][nx][j]); }
    if (i == (int)TSP.size() - 2) {
      ret.push_back(0);
    }
  }
  return ret;
}

int delivery(double& score, int& nowt, vector<int>& ans) {
  vector<int> DS;
  rep(i, n) {
    if ((int)order_info[i].size() == 0) continue;
    DS.push_back(i);
  }
  vector<int> TSP_path;
  TSP_path = heulistic_TSP(nowt, DS);
  if ((int)TSP_path.size() <= 2) return -2;
  vector<int> next_TSP_path = {0};
  int val = update_ans(score, nowt, TSP_path, ans, next_TSP_path);
  return val;
}

void solve() {
  double score = 0;
  int nowt = 0;
  vector<int> ans(T + 1, -1);

  delivery_cnt = 0;
  rep(i, n) order_info[i].clear();
  int order_num = -1;
  int order_id = -1;
  int order_v = -1;
  int n_put = -1;
  int n_achive = -1;
  while (nowt < T) {
    cin >> order_num;
    if (order_num == 1) cin >> order_id >> order_v;
    cin >> n_put;
    if (n_put == 1) {
      int tmp_id;
      cin >> tmp_id;
      order_info[--order_v].push_back(--order_id);
      start_o.push_back(nowt);
      break;
    }
    cout << -1 << endl;
    nowt++;
    string s;
    cin >> s;
    assert(s == "OK");
    cin >> n_achive;
  }
  while (nowt < T) {
    int update = delivery(score, nowt, ans);
    if (update == 0) {
      while (nowt < T) {
        cout << -1 << endl;
        nowt++;
        string s;
        cin >> s;
        assert(s == "OK");
        cin >> n_achive;
        if (nowt == T) return;
        cin >> order_num;
        if (order_num == 1) cin >> order_id >> order_v;
        cin >> n_put;
        int tmp_id;
        rep(_, n_put) cin >> tmp_id;
      }
      break;
    }
    if (update == -2) {
      cout << -1 << endl;
      string s;
      cin >> s;
      assert(s == "OK");
      nowt++;
      cin >> n_achive;
      while (nowt < T) {
        cin >> order_num;
        if (order_num == 1) cin >> order_id >> order_v;
        cin >> n_put;
        if (order_num == 1) {
          int tmp_id;
          cin >> tmp_id;
          order_info[--order_v].push_back(--order_id);
          start_o.push_back(nowt);
          break;
        }
        cout << -1 << endl;
        nowt++;
        cin >> s;
        assert(s == "OK");
        cin >> n_achive;
      }
    }
  }
}

signed main() {
  seed(37);
  read();
  calc_graph();
  solve();
}


Measured memory_bytes: 21456