//
// ver 1.3
//

#include <iostream>
#include <fstream>
#include <vector>
#include <deque>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <cassert>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, a, n) for (int i = (a); i <= (n); i++)
#define all(a) (a).begin(), (a).end()

#define PROBLEM_TYPE 'A'

using namespace std;

const int INF = 12345678;

class Action {
public:
    enum class Type {
        STAY,
        MOVE
    };

    Type type;
    int target;

    Action(Type type, int target = -1) : type(type), target(target) {}

    void output() {
        if (type == Type::STAY) {
            cout << -1 << endl;
        } else if (type == Type::MOVE) {
            cout << target + 1 << endl;
        }
    }
};

class Solver {
private:
    class Edge {
    public:
        int next;
        int distance;

        Edge(int next, int distance) : next(next), distance(distance) {}
    };

    class Node {
    public:
        int frequency = -1;
        vector<Edge> edges;

        void addEdge(int next, int distance) {
            edges.emplace_back(next, distance);
        }
    };

    int V, E;
    int turn;
    int maxTurn;
    int currentNode;
    vector<Node*> nodes;
    map<int, int> orderDestination;
    set<int> carry, stock;

    int distanceTable[400][400];
    deque<int> reservedRoute;
    deque<int> reservedPath;

    int targetNode;
    int delay;

    deque<int> calcRoute(const set<int> &carry, int start = 0, int goal = 0) {
        deque<int> path;
        path.push_back(start);
        for (auto orderId : carry) {
            path.push_back(orderDestination[orderId]);
        }
        path.push_back(goal);

        // swap node
        while (true) {
            bool swapped = false;
            REP(i, 1, path.size() - 2) REP(j, i + 1, path.size() - 2) {
                int beforeDist, afterDist;
                if (j - i == 1) {
                    beforeDist =
                            distanceTable[path[i]][path[i - 1]] +
                            distanceTable[path[j]][path[j + 1]];
                    afterDist =
                            distanceTable[path[j]][path[i - 1]] +
                            distanceTable[path[i]][path[j + 1]];
                } else {
                    beforeDist =
                            distanceTable[path[i]][path[i - 1]] +
                            distanceTable[path[i]][path[i + 1]] +
                            distanceTable[path[j]][path[j - 1]] +
                            distanceTable[path[j]][path[j + 1]];
                    afterDist =
                            distanceTable[path[j]][path[i - 1]] +
                            distanceTable[path[j]][path[i + 1]] +
                            distanceTable[path[i]][path[j - 1]] +
                            distanceTable[path[i]][path[j + 1]];
                }
                if (beforeDist > afterDist) {
                    swap(path[i], path[j]);
                    swapped = true;
                }
            }
            if (!swapped) break;
        }

        return path;
    }

    deque<int> calcPath(int start, int goal) {
        int prev[400], dist[400];
        rep(i, V) {
            prev[i] = -1;
            dist[i] = INF;
        }
        priority_queue<pair<int, int>> q;
        q.push(make_pair(0, start));
        dist[start] = 0;

        while (!q.empty()) {
            auto pr = q.top(); q.pop();
            int nowDist = -pr.first;
            int nowNode = pr.second;
            if (nowDist > dist[nowNode]) continue;
            if (nowNode == goal) {
                break;
            }
            for (const auto &edge : nodes[nowNode]->edges) {
                int nextNode = edge.next;
                int nextDist = nowDist + edge.distance;
                if (nextDist >= dist[nextNode]) continue;
                dist[nextNode] = nextDist;
                prev[nextNode] = nowNode;
                q.push(make_pair(-nextDist, nextNode));
            }
        }

        deque<int> path;
        int nowNode = goal;
        while (nowNode != start) {
            path.push_front(nowNode);
            nowNode = prev[nowNode];
        }

        return path;
    }

public:
    bool finished;

    void initialize(char problemType) {
        cin >> V >> E;

        rep(_, V) {
            nodes.push_back(new Node());
        }

        rep(_, E) {
            int u, v, d;
            cin >> u >> v >> d;
            u--; v--;
            nodes[u]->addEdge(v, d);
            nodes[v]->addEdge(u, d);
        }

        if (problemType == 'B') {
            rep(i, V) {
                cin >> nodes[i]->frequency;
            }
        }

        cin >> maxTurn;

        turn = 0;
        currentNode = 0;
        delay = 0;

        rep(i, V) rep(j, V) distanceTable[i][j] = INF;
        rep(i, V) distanceTable[i][i] = 0;
        rep(i, V) {
            for (const auto &edge : nodes[i]->edges) {
                distanceTable[i][edge.next] = edge.distance;
            }
        }
        rep(k, V) rep(i, V) rep(j, V)
            distanceTable[i][j] = min(distanceTable[i][j], distanceTable[i][k] + distanceTable[k][j]);
    }

    void input1(char problemType) {
        int newN;
        cin >> newN;

        rep(_, newN) {
            int id, dst;
            cin >> id >> dst;
            dst--;
            orderDestination[id] = dst;
            stock.insert(id);
        }

        if (problemType == 'A') {
            if (currentNode == 0) {
                carry.insert(all(stock));
                stock.clear();
            }
        } else {
            int putN;
            cin >> putN;

            rep(_, putN) {
                int id;
                cin >> id;
                carry.insert(id);
                stock.erase(id);
            }
        }
    }

    Action think() {
        if (delay > 0) {
            delay--;
            return Action(Action::Type::MOVE, targetNode);
        }

        int destination;
        int minDist = INF;

        for (auto orderId : carry) {
            int dest = orderDestination[orderId];
            int dist = distanceTable[currentNode][dest];
            if (dist < minDist) {
                minDist = dist;
                destination = dest;
            }
        }

        for (auto orderId : stock) {
            int dest = orderDestination[orderId];
            int dist = distanceTable[currentNode][0] + distanceTable[0][dest];
            if (dist < minDist) {
                minDist = dist;
                destination = 0;
            }
        }

        if (stock.size() >= 300) {
            minDist = 0;
            destination = 0;
        }

        if (minDist == INF) {
            if (currentNode == 0) {
                return Action(Action::Type::STAY);
            } else {
                destination = 0;
            }
        }

        auto path = calcPath(currentNode, destination);

        delay = distanceTable[currentNode][path[0]];
        currentNode = path[0];
        targetNode = path[0];

        return think();
    }

    void input2(char problemType) {
        if (problemType == 'A') {
            vector<int> achieveOrderIds;
            for (auto orderId : carry) {
                if (currentNode == orderDestination[orderId]) {
                    achieveOrderIds.push_back(orderId);
                }
            }
            for (auto orderId : achieveOrderIds) {
                carry.erase(orderId);
            }
        } else {
            string verdict;
            cin >> verdict;

            if (verdict != "OK") {
                finished = true;
                return;
            }

            int achieveN;
            cin >> achieveN;

            rep(_, achieveN) {
                int id;
                cin >> id;
                carry.erase(id);
            }
        }
    }

    void finalizeTurn() {
        turn++;
        if (turn == maxTurn) finished = true;
    }
};

int main() {
    auto solver = Solver();

    solver.initialize(PROBLEM_TYPE);

    while (!solver.finished) {
        solver.input1(PROBLEM_TYPE);

        auto action = solver.think();
        action.output();

        solver.input2(PROBLEM_TYPE);

        solver.finalizeTurn();
    }

    return 0;
}


Measured memory_bytes: 1280