#include <algorithm>
#include <cassert>
#include <chrono>
#include <cstdint>
#include <deque>
#include <iostream>
#include <memory>
#include <random>
#include <unordered_set>
#include <vector>
using namespace std;

using int64 = int64_t;
using Clock = chrono::steady_clock;
using Ms = chrono::milliseconds;

struct Order {
  int64 time;
  int64 dest;
};

class Problem {
  struct Edge {
    int64 u;
    int64 v;
    int64 dist;
  };

 public:
  struct State {
    State(const Problem& p) : problem(p) {
      initialize();
      actionBefore();
    }

    bool moveTo(int64 dest);
    void checkOrder();
    void updateOrder();

    const Problem& problem;
    int64 time = 0;
    int64 car_location = 0;
    int64 score = 0;
    deque<Order> orders_in_shop;
    deque<Order> orders; // on car

  private:
    void initialize();
    bool action(int64 dest);
    void actionBefore();
    void actionAfter();
  };

  Problem();

  int64 V() const { return V_; }
  int64 t_max() const { return t_max_; }
  const vector<int>& frequencies() const { return frequencies_; }
  const vector<vector<int>>& neighbors() const { return neighbors_; }
  const vector<vector<int>>& distance() const { return distance_; }
  const vector<vector<int>>& going_to() const { return going_to_; }

 private:
  void prepare();
  void inputFrequency();

  int64 V_;
  vector<Edge> edges_;
  int64 t_max_;
  vector<int> frequencies_;

  vector<vector<int>> neighbors_;
  vector<vector<int>> distance_;
  vector<vector<int>> going_to_;
};

class Solver {
 public:
  explicit Solver(const Problem& p) : problem_(p) {}

  void solve();

 private:
  const Problem& problem_;
  vector<int> answer_;
};
void Problem::inputFrequency() {
  frequencies_.resize(V_);
  for (int& f : frequencies_)
    cin >> f;
}

void Problem::State::initialize() {}

void Problem::State::actionBefore() {
  int64 N;
  cin >> N;
  for (int64 i = 0; i < N; ++i) {
    int64 id, dest;
    cin >> id >> dest;  // Ignore |id|
    orders_in_shop.emplace_back(Order{time, dest - 1});
  }

  // Do not use information about putting items.
  cin >> N;
  for (int64 i = 0; i < N; ++i) {
    int64 id;
    cin >> id;
  }
}

void Problem::State::actionAfter() {
  string verdict;
  cin >> verdict;
  assert(verdict == "OK");
  // Do not use information about deliveries.
  int64 N;
  cin >> N;
  for (int64 i = 0; i < N; ++i) {
    int64 id;
    cin >> id;
  }
}
Problem::Problem() {
  int64 E;
  cin >> V_ >> E;
  for (int64 i = 0; i < E; ++i) {
    int64 u, v, d;
    cin >> u >> v >> d;
    --u;
    --v;
    edges_.emplace_back(Edge{u, v, d});
  }
  inputFrequency();
  cin >> t_max_;

  prepare();
}

void Problem::prepare() {
  distance_.resize(V_, vector<int>(V_, -1));
  going_to_.resize(V_, vector<int>(V_, -1));
  for (int64 i = 0; i < V_; ++i) {
    distance_[i][i] = 0;
    going_to_[i][i] = -1;
  }

  neighbors_.resize(V_);
  for (const Edge& e : edges_) {
    int64 u = e.u;
    int64 v = e.v;
    neighbors_[u].push_back(v);
    neighbors_[v].push_back(u);
    distance_[u][v] = e.dist;
    distance_[v][u] = e.dist;
    going_to_[u][v] = v;
    going_to_[v][u] = u;
  }

  for (int64 k = 0; k < V_; ++k) {
    for (int64 i = 0; i < V_; ++i) {
      int64 ik = distance_[i][k];
      if (ik < 0)
        continue;
      for (int64 j = 0; j < V_; ++j) {
        int64 kj = distance_[k][j];
        if (kj < 0)
          continue;
        int64 ij = distance_[i][j];
        if (ij < 0 || ij > ik + kj) {
          distance_[i][j] = ik + kj;
          going_to_[i][j] = going_to_[i][k];
        }
      }
    }
  }
}

bool Problem::State::moveTo(int64 dest) {
  int64& from = car_location;
  if (from == dest) {
    return action(-1);
  }

  const auto& distance = problem.distance();
  const auto& going_to = problem.going_to();
  while (from != dest) {
    int64 next = going_to[from][dest];
    int64 d = distance[from][next];
    for (int64 i = 0; i < d; ++i) {
      if (!action(next + 1))
        return false;
    }
    from = next;
  }
  return true;
}

bool Problem::State::action(int64 dest) {
  // Action in 1 time
  cout << dest << endl;  // flush for B
  actionAfter();
  if (++time >= problem.t_max())
    return false;
  actionBefore();
  return true;
}

void Problem::State::updateOrder() {
  if (car_location == 0) {
    // Check order
    auto itr = orders_in_shop.begin();
    while (itr != orders_in_shop.end() && itr->time <= time)
      ++itr;
    orders.insert(orders.begin(), orders_in_shop.begin(), itr);
    orders_in_shop.erase(orders_in_shop.begin(), itr);
  } else {
    const int64 tm2 = problem.t_max() * problem.t_max();
    // Deliver
    for (int64 i = orders.size() - 1; i >= 0; --i) {
      const Order& order = orders[i];
      if (order.dest == car_location) {
        int64 dt = time - order.time;
        score += tm2 - dt * dt;
        orders.erase(orders.begin() + i);
      }
    }
  }
}

void Solver::solve() {
  mt19937 prg;
  const vector<vector<int>>& dist = problem_.distance();

  int64 prev_time = 0;
  int64 prev_size = 0;
  Problem::State state(problem_);
  while (true) {
    state.updateOrder();

    // Choose the closest destination, if have orders.
    // otherwise, back to 0
    int64 pos = state.car_location;
    if (state.orders.size() == 0) {
      if (!state.moveTo(0))
        break;
      continue;
    }
    int64 tdiff = state.time - prev_time;
    prev_time = state.time;
    prev_size = state.orders.size();

    // Make a list of destinations.
    unordered_set<int> id_set;
    for (auto order : state.orders) {
      id_set.insert(order.dest);
    }
    vector<int> dests(id_set.begin(), id_set.end());

    // Initialize the route: go to the closest target.
    int64 shortest = 0;
    dests.insert(dests.begin(), 0);
    for (int64 i = 0; i < dests.size() - 1; ++i) {
      int64 j = i + 1;
      int64 d = dist[dests[i]][dests[j]];
      for (int64 k = j + 1; k < dests.size(); ++k) {
        int64 dd = dist[dests[i]][dests[k]];
        if (dd < d) {
          d = dd;
          j = k;
        }
      }
      if (j != i + 1)
        swap(dests[i + 1], dests[j]);
      shortest += d;
    }
    shortest += dist[dests.back()][0];
    dests.push_back(0);

    auto time_limit = Clock::now() + Ms(200);
    while (Clock::now() < time_limit) {
      int64 i = prg() % (dests.size() - 2) + 1;
      int64 j = prg() % (dests.size() - 2) + 1;
      if (i == j)
        continue;
      if (i > j)
        swap(i, j);
      int64 diff = (dist[dests[i - 1]][dests[j]] + dist[dests[i]][dests[j + 1]])
        - (dist[dests[i - 1]][dests[i]] + dist[dests[j]][dests[j + 1]]);
      if (i + 1 < j) {
        diff += (dist[dests[j]][dests[i + 1]] + dist[dests[j - 1]][dests[i]])
          - (dist[dests[i]][dests[i + 1]] + dist[dests[j - 1]][dests[j]]);
      }
      if (diff < 0) {
        swap(dests[i], dests[j]);
        shortest += diff;
      }
    }

    bool end = false;
    for (int64 d : dests) {
      if (!state.moveTo(d)) {
        end = true;
        break;
      }
      state.updateOrder();
    }
    if (end)
      break;
  }
  state.updateOrder();

  // cerr << "Score:  " << state.score << "\n";
  // cerr << "Not pickup: " << state.orders_in_shop.size() << "\n";
  // cerr << "Not deliver: " << state.orders.size() << "\n";
}

int main() {
  auto problem = make_unique<Problem>();
  auto solver = make_unique<Solver>(*problem);

  solver->solve();

  return 0;
}


Measured memory_bytes: 2640