#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include<map>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include<fstream>
#include <unordered_map>
#include <cstdlib>
#include <complex>
#include <cctype>
#include <bitset>
using namespace std;
typedef string::const_iterator State;
#define Ma_PI 3.141592653589793
#define eps 1e-5
#define LONG_INF 2000000000000000000LL
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007LL
#define GYAKU 500000004LL
#define MOD 998244353LL
#define seg_size 262144
#define REP(a,b) for(long long a = 0;a < b;++a)
unsigned long xor128() {
	static unsigned long x = 123456789, y = 362436069, z = 521288629, w = 88675123;
	unsigned long t = (x^(x << 11));
	x = y; y = z; z = w;
	return (w = (w^(w >> 19))^(t^(t >> 8)));
}
vector<pair<int, int>> vertexs[500];
pair<int, int> go_move[500][500];

int is_problemb = 0;

void println(int now) {
	cout << now << endl;
	if (is_problemb == 1) {
		string s;
		cin >> s;
		int q;
		cin >> q;
		REP(i, q) {
			int a;
			cin >> a;
		}
	}
}
vector<int> inputln() {
	vector<int> tmp;
	int n;
	cin >> n;
	REP(q, n) {
		int a, b;
		cin >> a >> b;
		tmp.push_back(b);
	}
	if (is_problemb == 1) {
		int n;
		cin >> n;
		REP(q, n) {
			int a;
			cin >> a;
		}
	}
	return tmp;
}
int main() {
	int V, E;
	cin >> V >> E;
	REP(i, E) {
		int a, b, c;
		cin >> a >> b >> c;
		vertexs[a].push_back(make_pair(b, c));
		vertexs[b].push_back(make_pair(a, c));
	}
	REP(i, V) {
		REP(q, V) {
			go_move[i + 1][q + 1] = make_pair(10000000, 0);
		}
	}
	for (int i = 1; i <= V; ++i) {
		go_move[i][i] = make_pair(0, -1);
		priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> next;
		next.push(make_pair(0, i));
		while (next.empty() == false) {
			pair<int, int> now = next.top();
			next.pop();
			if (now.first != go_move[i][now.second].first) continue;
			REP(q, vertexs[now.second].size()) {
				int go = vertexs[now.second][q].first;
				int cost = vertexs[now.second][q].second + now.first;
				if (go_move[i][go].first > cost) {
					go_move[i][go] = make_pair(cost, now.second);
					next.push(make_pair(cost, go));
				}
			}
		}
	}
	if (is_problemb == 1) {
		REP(i, V) {
			int a;
			cin >> a;
		}
	}
	int T_max;
	cin >> T_max;
	vector<int> input_a, input_b;
	input_b.push_back(1);
	int now_time = 0;
	while (now_time < T_max) {
		vector<int> now = inputln();
		REP(q, now.size()) {
			input_b.push_back(now[q]);
		}
		int decide = input_b.size();
		if (decide <= 1) {
			now_time++;
			println(-1);
		}
		else {
			swap(input_a, input_b);
			set<int> now;
			for (int q = 0; q < input_a.size(); ++q) {
				if (now.find(input_a[q]) == now.end()) {
					now.insert(input_a[q]);
				}
				else {
					input_a.erase(input_a.begin() + q);
					q--;
				}
			}
			input_a.push_back(1);
			if (input_a.size() != 3) {
				for (int q = 0; q < 100000; ++q) {
					if (xor128() % 2 == 0) {
						int a = xor128() % (input_a.size() - 3) + 1;
						if (go_move[input_a[a - 1]][input_a[a]].first + go_move[input_a[a + 1]][input_a[a + 2]].first >
							go_move[input_a[a - 1]][input_a[a + 1]].first + go_move[input_a[a]][input_a[a + 2]].first) {
							swap(input_a[a], input_a[a + 1]);
						}
					}
					else {
						int a = xor128() % (input_a.size() - 2) + 1;
						int b = xor128() % (input_a.size() - 2) + 1;
						if (a > b) swap(a, b);
						if (a+1 >= b) {
							continue;
						}
						int cost = go_move[input_a[a - 1]][input_a[a]].first + go_move[input_a[a]][input_a[a + 1]].first;
						cost += go_move[input_a[b - 1]][input_a[b]].first + go_move[input_a[b]][input_a[b + 1]].first;
						swap(input_a[a], input_a[b]);
						cost -= go_move[input_a[a - 1]][input_a[a]].first + go_move[input_a[a]][input_a[a + 1]].first;
						cost -= go_move[input_a[b - 1]][input_a[b]].first + go_move[input_a[b]][input_a[b + 1]].first;
						if (cost < 0) {
							swap(input_a[a], input_a[b]);
						}
					}
				}
			}
			input_b.push_back(1);
			vector<int> going;
			for (int q = 1; q < input_a.size(); ++q) {
				vector<int> tea;
				int now = input_a[q];
				while (now != input_a[q - 1]) {
					int next_go = go_move[input_a[q - 1]][now].second;
					int hoge = go_move[input_a[q - 1]][now].first - go_move[input_a[q - 1]][next_go].first;
					REP(q, hoge) {
						tea.push_back(now);
					}
					now = next_go;
				}
				REP(q, tea.size()) {
					going.push_back(tea[tea.size() - q - 1]);
				}
			}
			println(going[0]);
			while (now_time + going.size() > T_max) {
				going.pop_back();
			}
			for (int q = 1; q < going.size(); ++q) {
				vector<int> now = inputln();
				REP(q, now.size()) {
					input_b.push_back(now[q]);
				}
				println(going[q]);
			}
			now_time += going.size();
			input_a.clear();
		}
	}
	return 0;
}


Measured memory_bytes: 2424