#include <bits/stdc++.h>
using namespace std;

constexpr int INF = 1e9;
constexpr int STORE = 0;

struct Order {
  int t, n, id, dst;
};

istream& operator>>(istream& is, Order& order) {
  is >> order.n;
  if (order.n) {
    is >> order.id >> order.dst;
    order.dst--;
  }
  return is;
}

struct Position {
  int u, v, du, dv;
};

int main() {
  // input
  int V, E, T;
  cin >> V >> E;
  vector<vector<int>> adjacency_matrix(V, vector<int>(V, INF));
  for (int i = 0; i < E; i++) {
    int u, v, d;
    cin >> u >> v >> d;
    u--, v--;
    adjacency_matrix[u][v] = adjacency_matrix[v][u] = d;
  }
  vector<int> frequencies(V);
  for (auto& e : frequencies) cin >> e;
  cin >> T;

  // solve
  // Warshall-Floyd algorithm
  vector<vector<int>> distance_matrix = adjacency_matrix;
  for (int i = 0; i < V; i++) distance_matrix[i][i] = 0;
  vector<vector<int>> next_node_matrix(V, vector<int>(V));
  for (int i = 0; i < V; i++) {
    for (int j = 0; j < V; j++) {
      next_node_matrix[i][j] = j;
    }
  }
  for (int k = 0; k < V; k++) {
    for (int i = 0; i < V; i++) {
      for (int j = 0; j < V; j++) {
        if (distance_matrix[i][k] + distance_matrix[k][j] < distance_matrix[i][j]) {
          distance_matrix[i][j] = distance_matrix[i][k] + distance_matrix[k][j];
          next_node_matrix[i][j] = next_node_matrix[i][k];
        }
      }
    }
  }

  vector<Order> orders;
  vector<int> ids;
  Position position{STORE, STORE, 0, 0};
  queue<int> destination_queue;
  for (int t = 0; t < T; t++) {
    // input
    Order order;
    cin >> order;
    if (order.n) {
      order.t = t;
      orders.push_back(order);
    }

    int n_put;
    cin >> n_put;
    vector<int> put_ids(n_put);
    for (auto& e : put_ids) cin >> e;
    for (auto e : put_ids) ids.push_back(e);

    // solve
    if (n_put > 0 && destination_queue.empty() && orders.size() >= 8) {
      int min_cost = INF;
      vector<int> min_cost_route(8);
      vector<int> p(8);
      for (int i = 0; i < p.size(); i++) {
        p[i] = orders[i].dst;
      }
      orders.erase(orders.begin(), orders.begin() + 8);

      do {
        int cost = 0;
        cost += distance_matrix[STORE][p.front()];
        cost += distance_matrix[p.back()][STORE];
        for (int i = 0; i + 1 < p.size(); i++) cost += distance_matrix[p[i]][p[i + 1]];
        if (cost < min_cost) {
          min_cost = cost;
          min_cost_route = p;
        }
      } while (next_permutation(p.begin(), p.end()));
      for (auto e : min_cost_route) destination_queue.push(e);
      destination_queue.push(STORE);
    }

    // output
    int command;

    if (position.du == 0) {
      while (!destination_queue.empty() && destination_queue.front() == position.u) destination_queue.pop();
    } else if (position.dv == 0) {
      while (!destination_queue.empty() && destination_queue.front() == position.v) destination_queue.pop();
    }
    if (destination_queue.empty()) {
      command = -1;
    } else if (position.du == 0 || position.dv == 0) {
      int i = position.du == 0 ? position.u : position.v;
      int j = next_node_matrix[i][destination_queue.front()];
      command = j;
      position = {i, j, 1, adjacency_matrix[i][j] - 1};
    } else {
      if (position.du + distance_matrix[position.u][destination_queue.front()] <
          position.dv + distance_matrix[position.v][destination_queue.front()]) {
        command = position.u;
        position.du--;
        position.dv++;
      } else {
        command = position.v;
        position.du++;
        position.dv--;
      }
    }

    cout << command + (command != -1) << endl;

    // input
    string verdict;
    cin >> verdict;
    if (verdict == "NG") break;

    int n_archive;
    cin >> n_archive;
    vector<int> archive_ids(n_archive);
    for (auto& e : archive_ids) cin >> e;
    for (auto e : archive_ids) ids.erase(find_if(ids.begin(), ids.end(), [e](int id) { return id == e; }));
  }
}


Measured memory_bytes: 3280