#include <bits/stdc++.h>


using namespace std;
typedef long long ll;
#define REP(i,n) for(int i=0; i<int(n); i++)
#define FOR(i,m,n) for(int i=int(m); i<int(n); i++)
#define ALL(obj) (obj).begin(),(obj).end()
#define VI vector<int>
#define VLL vector<long long>
#define VVI vector<vector<int>>
#define VVLL vector<vector<long long>>
#define VC vector<char>
#define VS vector<string>
#define VVC vector<vector<char>>
#define VB vector<bool>
#define VVB vector<vector<bool>>
#define fore(i,a) for(auto &i:a)
typedef pair <int, int> P;
template<class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; }
template<typename T> using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;

const int  INF = 1 << 29;
const ll INFL = 1LL << 60;
const ll mod = 998244353;



int V, E, T;
int deri[10500];
vector<pair<int, int>> G[405];
VVI kyori(401, VI(401, INF));
VVI prever(401, VI(401, -1));
vector<vector<vector<int>>> keiro(401, vector<vector<int>>(401));
int road[401][401];
int store_max = 500;

void nyuuryoku() {

	//const char *fileName1 = "nyuuryoku.txt";
	//std::ifstream ci(fileName1);


	cin >> V >> E;
	REP(i, E) {
		int u, v, d;
		cin >> u >> v >> d;
		u--; v--;
		G[u].push_back({ v,d });
		G[v].push_back({ u,d });
		road[u][v] = d;
		road[v][u] = d;
	}

	VI f(V);
	REP(i, V)cin >> f[i];
	cin >> T;
	/*
	int cnt = 0;

	REP(i, T) {
		int n;
		ci >> n;
		if (n) {
			int id, dst;
			cnt++;
			ci >> id >> dst;
			dst--;
			deri[i] = dst;
		}
	}
	*/
}

void dijkstra(int s) {
	int vis[405];
	REP(i, V) vis[i] = 0;

	min_priority_queue<pair<ll, int>> que;

	kyori[s][s] = 0;
	que.push({ 0, s });

	while (!que.empty()) {
		auto q = que.top();
		que.pop();

		int cst = q.first;
		int cu = q.second;

		if (vis[cu]) continue;
		vis[cu] = 1;

		fore(p, G[cu]) {
			int cst2 = cst + p.second;
			int to = p.first;

			if (chmin(kyori[s][to], cst2)) {
				que.push({ kyori[s][to], to });
				prever[s][to] = cu;
			}
		}
	}
}

void get_path(int s, int t) {
	int tt = t;
	vector<int> path;



	while (1) {
		t = prever[s][t];
		if (s == t)break;
		path.push_back(t);
	}

	reverse(path.begin(), path.end());

	path.push_back(tt);

	for (int i : path) {
		keiro[s][tt] = path;
	}
}

void saitankeiro() {
	REP(i, V) {
		dijkstra(i);
	}


	REP(i, V)REP(j, V) {
		if (i == j)continue;
		get_path(i, j);
	}
}

VI ans;
ll point = 0;
VI tmp;


//ansを更新する
void update(ll a) {
	if (a > point) {
		point = a;
		ans = tmp;
	}
}

//tmpに解答をいれて計算する
void calc() {

	ll cur = 0;
	ll cnt = 0;
	int now = 0;
	int dst = 0;

	VVI store(401);
	VVI car(401);
	VVI kara(401);
	VI kar;

	REP(i, T) {
		if (deri[i] != 0)store[deri[i]].push_back(i);
		if (tmp[i] == -1)continue;
		if (now == 0 && dst == 0) {
			REP(j, 401) {
				for (int k : store[j]) {
					car[j].push_back(k);
				}
			}
			store = kara;
		}

		if (now == tmp[i]) {
			dst--;
		}
		else {
			dst = kyori[now][tmp[i]] - 1;
			now = tmp[i];
		}
		if (dst == 0) {
			for (int j : car[now]) {
				cur -= (ll)((i - j + 1)*(i - j + 1));
			}
			cnt += car[now].size();
			car[now] = kar;

		}

	}
	//cout << cnt * 100000000LL + cur << endl;
	update(cnt * 100000000LL + cur);
}

//距離の近いところに行く、k個店に荷物がたまったら戻る
//効果なし
vector<int> f(int k) {
	VB store(405, false);
	VB car(405, false);
	int now = 0;
	VI an;
	VI st(405, 0);
	VI ca(405, 0);
	REP(_, T) {

		store[deri[_]] = true;
		store[0] = false;

		st[deri[_]]++;
		st[0] = 0;

		int c = 0;
		REP(i, V)c += st[i];
		if (c > k && _ < 9000 && now != 0) {
			while (now != 0) {
				int ti = kyori[now][0];
				for (int j : keiro[now][0]) {
					REP(k, kyori[now][j]) {
						an.push_back(j);
					}
					car[now] = false;
					ca[now] = 0;
					now = j;
				}
				REP(j, ti - 1) {
					_++;
					if (deri[_] != 0) {
						store[deri[_]] = true;
						st[deri[_]]++;
					}
				}
			}
			continue;
		}
		if (now == 0) {
			REP(i, V) {
				if (store[i]) car[i] = true;
				store[i] = false;
				ca[i] += st[i];
				st[i] = 0;
			}
		}

		bool ch = false;
		REP(i, V) {
			if (car[i]) ch = true;
		}

		if (ch) {
			vector<pair<int, int>> v;
			REP(i, V) {
				if (car[i]) {
					v.push_back({ kyori[now][i],i });
				}
			}
			sort(ALL(v));
			int to = keiro[now][v[0].second][0];
			bool b = false;
			REP(i, kyori[now][to]) {

				an.push_back(to);

				if (!b)b = true;
				else {
					_++;
					if (deri[_] != 0) {
						store[deri[_]] = true;
						st[deri[_]]++;
					}
				}
			}
			now = to;
			car[to] = false;
			ca[to] = 0;
		}
		else if (now == 0)an.push_back(-1);
		else {
			int ti = kyori[now][0];
			for (int j : keiro[now][0]) {
				REP(k, kyori[now][j]) {
					an.push_back(j);
				}
				car[now] = false;
				ca[now] = 0;
				now = j;
			}
			REP(j, ti - 1) {
				_++;
				if (deri[_] != 0) {
					store[deri[_]] = true;
					st[deri[_]]++;
				}
			}
		}
	}
	while (an.size() < 10005)an.push_back(-1);

	return an;
}

//距離に対して他の荷物がどのくらい減るかによって順序決め
void g(int kkk) {

	VI a;
	VVI car(401);
	VVI store(401);
	VVI kara(401);
	VI kar;
	int number_car = 0;
	int number_store = 0;
	int now = 0;
	REP(i, T) {
		if (deri[i] != 0) {
			number_store++;
			store[deri[i]].push_back(i);
		}
		if (number_car == 0 && number_store == 0 && now == 0) {
			a.push_back(-1);
			continue;
		}

		if (now == 0) {
			REP(j, 401) {
				for (int k : store[j])car[j].push_back(k);
			}
			store = kara;
			number_car += number_store;
			number_store = 0;
		}
		VI c;
		REP(j, 401) {
			if (car[j].size() != 0)c.push_back(j);
		}

		if (c.size() == 0 || number_store > kkk) {
			int tim = kyori[now][0];
			VI d = keiro[now][0];
			for (int j : d) {
				REP(k, kyori[now][j]) {
					a.push_back(j);
				}
				now = j;
				number_car -= car[j].size();
				car[j] = kar;
			}

			REP(j, tim - 1) {
				i++;
				if (deri[i] != 0) {
					number_store++;
					store[deri[i]].push_back(i);
				}

			}
		}
		else if (c.size() == 1) {
			bool b = false;
			for (int j : keiro[now][c[0]]) {
				REP(k, kyori[now][j]) {
					a.push_back(j);
					if (!b)b = true;
					else {
						i++;
						if (deri[i] != 0) {
							number_store++;
							store[deri[i]].push_back(i);
						}
					}
				}
				number_car -= car[j].size();
				car[j] = kar;
				now = j;
				if (j == 0)break;
			}
		}
		else {
			int cc;
			ll cur = INFL;
			for (int j : c) {
				ll aa = 0;
				ll ti = kyori[now][j];
				for (int k : c) {
					if (j == k)continue;
					for (int l : car[k]) {
						aa += (i + ti - l)*(i + ti - l) - (i - l)*(i - l);
					}
				}
				if (aa < cur) {
					cur = aa;
					cc = j;
				}
			}
			bool b = false;
			for (int j : keiro[now][cc]) {
				REP(k, kyori[now][j]) {
					a.push_back(j);
					if (!b)b = true;
					else {
						i++;
						if (deri[i] != 0) {
							number_store++;
							store[deri[i]].push_back(i);
						}
					}
				}
				number_car -= car[j].size();
				car[j] = kar;
				now = j;

			}
		}

	}

	tmp = a;
}

//荷物が多いところ（max-kk個以内ならOK）に行く
vector<int> h(int k, int kk) {
	VB store(405, false);
	VB car(405, false);
	int now = 0;
	VI an;
	VI st(405, 0);
	VI ca(405, 0);
	REP(_, T) {

		store[deri[_]] = true;
		store[0] = false;

		st[deri[_]]++;
		st[0] = 0;

		int c = 0;
		REP(i, V)c += st[i];
		if (c > k && _ < 9000 && now != 0) {
			while (now != 0) {
				int ti = kyori[now][0];
				for (int j : keiro[now][0]) {
					REP(k, kyori[now][j]) {
						an.push_back(j);
					}
					car[now] = false;
					ca[now] = 0;
					now = j;
				}
				REP(j, ti - 1) {
					_++;
					if (deri[_] != 0) {
						store[deri[_]] = true;
						st[deri[_]]++;
					}
				}
			}
			continue;
		}
		if (now == 0) {
			REP(i, V) {
				if (store[i]) car[i] = true;
				store[i] = false;
				ca[i] += st[i];
				st[i] = 0;
			}
		}

		bool ch = false;
		REP(i, V) {
			if (car[i]) ch = true;
		}

		if (ch) {

			int ma = 0;
			REP(i, V) {
				ma = max(ma, ca[i]);
			}

			vector<pair<int, int>> v;

			REP(i, V) {
				if (ca[i] >= max(1, ma - kk)) {
					v.push_back({ kyori[now][i],i });
				}
			}
			sort(ALL(v));
			int to = keiro[now][v[0].second][0];
			bool b = false;
			REP(i, kyori[now][to]) {

				an.push_back(to);

				if (!b)b = true;
				else {
					_++;
					if (deri[_] != 0) {
						store[deri[_]] = true;
						st[deri[_]]++;
					}
				}
			}
			now = to;
			car[to] = false;
			ca[to] = 0;
		}
		else if (now == 0)an.push_back(-1);
		else {
			int ti = kyori[now][0];
			for (int j : keiro[now][0]) {
				REP(k, kyori[now][j]) {
					an.push_back(j);
				}
				car[now] = false;
				ca[now] = 0;
				now = j;
			}
			REP(j, ti - 1) {
				_++;
				if (deri[_] != 0) {
					store[deri[_]] = true;
					st[deri[_]]++;
				}
			}
		}
	}
	while (an.size() < 10005)an.push_back(-1);

	return an;
}

void solve() {

	/*
	for (int i = 100; i <= 1000; i += 50) {
		cout << i << endl;
		tmp = f(i);
		calc();
	}



	for (int i = 200; i <= 500; i += 100) {
		g(i);
		calc();
	}
	*/
	for (int i = 100; i <= 500; i += 25) {
		FOR(j, 0, 10) {
			tmp = h(i, j);
			calc();
		}
	}
}


//荷物が多いところ（max-kk個以内ならOK）に行く
vector<int> hh(int k, int kk) {


	//const char *fileName = "syuturyoku.txt";
	//std::ofstream cou(fileName);
	VB store(405, false);
	VB car(405, false);
	int now = 0;
	VI an;
	VI st(405, 0);
	VI ca(405, 0);
	int nn, id, dst;
	string verdict;
	REP(_, T) {
		//cou << _ << endl;
		cin >> nn;
		if (nn) {
			cin >> id >> dst;
			deri[_] = dst;
		}
		cin >> nn;
		REP(ii, nn) {
			cin >> id;
		}

		store[deri[_]] = true;
		store[0] = false;

		st[deri[_]]++;
		st[0] = 0;

		int c = 0;
		REP(i, V)c += st[i];
		if (c > k && now != 0) {
			VI vv;
			while (now != 0) {
				int ti = kyori[now][0];
				for (int j : keiro[now][0]) {
					REP(k, kyori[now][j]) {
						vv.push_back(j);
					}
					car[now] = false;
					ca[now] = 0;
					now = j;
				}
				REP(j, ti) {
					cout << vv[j] + 1 << endl;
					cin >> verdict;
					cin >> nn;
					REP(i, nn)cin >> id;
					if (j != ti - 1)continue;
					if (j != ti - 1) {
						_++;
						if (_ == T)return tmp;
						cin >> nn;
						if (nn) {
							cin >> id >> dst;
							deri[_] = dst;
						}
						cin >> nn;
						REP(i, nn)cin >> id;
						if (deri[_] != 0) {
							store[deri[_]] = true;
							st[deri[_]]++;
						}
					}
				}
			}
			continue;
		}
		if (now == 0) {
			REP(i, V) {
				if (store[i]) car[i] = true;
				store[i] = false;
				ca[i] += st[i];
				st[i] = 0;
			}
		}

		bool ch = false;
		REP(i, V) {
			if (car[i]) ch = true;
		}

		if (ch) {

			int ma = 0;
			REP(i, V) {
				ma = max(ma, ca[i]);
			}

			vector<pair<int, int>> v;

			REP(i, V) {
				if (ca[i] >= max(1, ma - kk)) {
					v.push_back({ kyori[now][i],i });
				}
			}
			sort(ALL(v));
			int to = keiro[now][v[0].second][0];
			bool b = false;
			REP(i, kyori[now][to]) {

				cout << to + 1 << endl;
				cin >> verdict;
				cin >> nn;
				REP(i, nn)cin >> id;
				if (i != kyori[now][to] - 1) {
					_++;
					if (_ == T)return tmp;
					cin >> nn;
					if (nn) {
						cin >> id >> dst;
						deri[_] = dst;
					}
					cin >> nn;
					REP(i, nn)cin >> id;
					if (deri[_] != 0) {
						store[deri[_]] = true;
						st[deri[_]]++;
					}
				}

				if (deri[_] != 0) {
					store[deri[_]] = true;
					st[deri[_]]++;
				}

			}
			now = to;
			car[to] = false;
			ca[to] = 0;
		}
		else if (now == 0) {
			cout << -1 << endl;
			cin >> verdict;
			cin >> nn;
			REP(i, nn)cin >> id;
		}
		else {
			int ti = kyori[now][0];
			VI vv;
			for (int j : keiro[now][0]) {
				REP(k, kyori[now][j]) {
					vv.push_back(j);
				}
				car[now] = false;
				ca[now] = 0;
				now = j;
			}
			REP(j, ti) {
				cout << vv[j] + 1 << endl;
				cin >> verdict;
				cin >> nn;
				REP(i, nn)cin >> id;
				if (j != ti - 1)continue;
				if (j != ti - 1) {
					_++;
					if (_ == T)return tmp;
					cin >> nn;
					if (nn) {
						cin >> id >> dst;
						deri[_] = dst;
					}
					cin >> nn;
					REP(i, nn)cin >> id;
					if (deri[_] != 0) {
						store[deri[_]] = true;
						st[deri[_]]++;
					}
				}
			}
		}
	}

	return an;
}

int main() {



	nyuuryoku();

	saitankeiro();

	tmp = hh(200, 5);



	return 0;

	/*
	solve();

	const char *fileName = "syuturyoku.txt";
	std::ofstream cou(fileName);
	REP(i, 10000) {
		if (ans[i] != -1)cou << ans[i] + 1 << endl;
		else cou << -1 << endl;
	}

	*/

}


Measured memory_bytes: 18000