#include <iostream>
#include <vector>
#include <utility>
#include <queue>
#include <chrono>
#include <random>

auto start = std::chrono::system_clock::now();
auto msec() {
	auto end = std::chrono::system_clock::now();
	auto dur = end - start;
	auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(dur).count();
	return ms;
}

const int TIMELIMIT = 10000;

typedef std::pair<int, int> PII;
const int INF = 999999999;

std::vector<std::vector<int>> D;
std::vector<std::vector<int>> prever;

//std::mt19937 engine(0);

int main() {
	// recieve |V| and |E|
	int V, E; std::cin >> V >> E;
	std::vector<std::vector<PII>> G(V);
	
	for(int i = 0; i < E; i++) {
		// recieve edges
		int u, v, d; std::cin >> u >> v >> d;
		u--; v--;
		G[u].push_back({v, d});
		G[v].push_back({u, d});
	}
	D.assign(V,std::vector<int>(V,INF));
	prever.assign(V,std::vector<int>(V,-1));
	for(int i = 0; i < V; i++){
		std::vector<bool> flag(V);
		D[i][i] = 0;
		std::priority_queue<PII, std::vector<PII>, std::greater<PII> > que;
		que.push(PII(0,i));
		while(!que.empty()){
			PII p = que.top();
			que.pop();
			int v = p.second;
			if(D[i][v] < p.first){
				continue;
			}
			for(int j=0;j<G[v].size();j++){
				int e = G[v][j].first;
				if(D[i][e] > D[i][v] + G[v][j].second){
					D[i][e] = D[i][v] + G[v][j].second;
					prever[i][e] = v;
					que.push(PII(D[i][e],e));
				}
			}
		}
	}
	int Tmax; std::cin >> Tmax;
	std::vector<int> NnewArray;
	std::vector<std::vector<int>> dstArray(Tmax);

	// recieve info
	for(int t = 0; t < Tmax; t++) {
		int Nnew; std::cin >> Nnew;
		NnewArray.push_back(Nnew);
		for(int j = 0; j < Nnew; j++) {
			int new_id, dst; std::cin >> new_id >> dst;
			dstArray[t].push_back(dst);
		}
    }
	long long maxScore = 0;
	std::vector<int> maxAnsArray;
	std::vector<PII> maxTargetResult;
	int maxLoop = -1;
	for(int loop = 0; loop < 100; loop++){
		if(msec()>=TIMELIMIT){
			break;
		}
		long long score = 0;
		std::vector<int> orderFlag;
		std::vector<PII> ORDER;
		int nowNode = 0;
		int targetNode = 0;
		int remainDist = 0;
		int unloadedBaggageSum = 0;
		std::vector<int> unloadedBaggageCount(V,0);
		int loadedBaggageSum = 0;
		std::vector<int> loadedBaggageCount(V,0);
		std::vector<std::vector<int>> nodeToOrder(V);
    	std::vector<int> ansArray;
		std::vector<PII> targetResult;
		int beforeTarget = -1;
		for(int t = 0; t < Tmax; t++) {
			int Nnew = NnewArray[t];
			for(int j = 0; j < Nnew; j++) {
				int dst = dstArray[t][j];
				dst--;
				orderFlag.push_back(0);
				ORDER.push_back({t, dst});
				unloadedBaggageSum++;
				unloadedBaggageCount[dst]++;
				nodeToOrder[dst].push_back(orderFlag.size()-1);
			}
			// insert your code here to get more meaningful output
			int ans = -1;
			if(remainDist == 0){
				nowNode = targetNode;
				if(nowNode == 0){
					for(int j = 0; j < ORDER.size(); j++){
						if(orderFlag[j] == 0){
							orderFlag[j] = 1;
							unloadedBaggageSum--;
							loadedBaggageSum++;
							unloadedBaggageCount[ORDER[j].second]--;
							loadedBaggageCount[ORDER[j].second]++;
						}
					}
				} else {
					for(int j = 0; j < nodeToOrder[nowNode].size(); j++){
						int id = nodeToOrder[nowNode][j];
						if(orderFlag[id] == 1){
							score += Tmax * Tmax - (t - ORDER[id].first) * (t - ORDER[id].first);
							orderFlag[id] = 2;
							loadedBaggageSum--;
							loadedBaggageCount[ORDER[id].second]--;
						}
					}
				}
				int mintarget = -1;
				int mincost = INF;
				for(int j = 1; j < V; j++){
					if(unloadedBaggageCount[j] > 0){
						int coe = 50 + loop;
						int cost = 10 * (D[nowNode][0]+D[0][j]) * coe / (coe + unloadedBaggageSum) / (unloadedBaggageCount[j] + loadedBaggageCount[j]);
						if(cost <= mincost){
							mintarget = 0;
							mincost = cost;
						}
					}
					if(loadedBaggageCount[j] > 0){
   						int coe = 1 + loop % 5;
						int cost = 10 * D[nowNode][j] * (coe + unloadedBaggageCount[j]) * (coe + unloadedBaggageCount[j]) / coe / coe / loadedBaggageCount[j];
						if(cost < mincost){
							mintarget = j;
							mincost = cost;
						}
					}
				}
				if(mintarget == -1 && nowNode > 0){
					mintarget = 0;
				}
				if(beforeTarget != mintarget){
					targetResult.push_back({t,mintarget});
					beforeTarget = mintarget;
                }
				int minNextCost = INF;
				if(mintarget >= 0){
					for(int j = 0; j < G[nowNode].size(); j++){
						if(D[G[nowNode][j].first][mintarget] < minNextCost){
							ans = G[nowNode][j].first + 1;
							targetNode = ans - 1;
							remainDist = G[nowNode][j].second - 1;
							minNextCost = D[G[nowNode][j].first][mintarget];
						}
					}
				}
			} else {
				ans = targetNode + 1;
				remainDist--;
			}
			ansArray.push_back(ans);
	    }
		if(score > maxScore){
			maxScore = score;
			maxAnsArray = ansArray;
			maxTargetResult = targetResult;
			maxLoop = loop;
		}
	}
	int changeCountReturn = 0;
	int changeCountNotReturn = 0;
	int changeResult = 1;
	while(changeResult < maxTargetResult.size()){
		if(msec()>=TIMELIMIT){
			break;
		}
		long long score = 0;
		int scoreFlag = 0;
		std::vector<int> orderFlag;
		std::vector<PII> ORDER;
		int nowNode = 0;
		int targetNode = 0;
		int remainDist = 0;
		int unloadedBaggageSum = 0;
		std::vector<int> unloadedBaggageCount(V,0);
		int loadedBaggageSum = 0;
		std::vector<int> loadedBaggageCount(V,0);
		std::vector<std::vector<int>> nodeToOrder(V);
    	std::vector<int> ansArray;
		std::vector<PII> targetResult;
		int beforeTarget = -1;
		for(int t = 0; t < Tmax; t++) {
			int Nnew = NnewArray[t];
			for(int j = 0; j < Nnew; j++) {
				int dst = dstArray[t][j];
				dst--;
				orderFlag.push_back(0);
				ORDER.push_back({t, dst});
				unloadedBaggageSum++;
				unloadedBaggageCount[dst]++;
				nodeToOrder[dst].push_back(orderFlag.size()-1);
			}
			int ans = -1;
			if(remainDist == 0){
				nowNode = targetNode;
				if(nowNode == 0){
					for(int j = 0; j < ORDER.size(); j++){
						if(orderFlag[j] == 0){
							orderFlag[j] = 1;
							unloadedBaggageSum--;
							loadedBaggageSum++;
							unloadedBaggageCount[ORDER[j].second]--;
							loadedBaggageCount[ORDER[j].second]++;
						}
					}
				} else {
					for(int j = 0; j < nodeToOrder[nowNode].size(); j++){
						int id = nodeToOrder[nowNode][j];
						if(orderFlag[id] == 1){
							score += Tmax * Tmax - (t - ORDER[id].first) * (t - ORDER[id].first);
							orderFlag[id] = 2;
							loadedBaggageSum--;
							loadedBaggageCount[ORDER[id].second]--;
						}
					}
				}
				int mintarget = -1;
				int mincost = INF;
				if(maxTargetResult[changeResult].first > t){
					if(maxAnsArray[t]==-1){
						mintarget = -1;
					} else {
						mintarget = maxAnsArray[t]-1;
					}
				} else {
					int returnBaseCost = 10;
					if(maxTargetResult[changeResult].first == t){
						if(maxTargetResult[changeResult].second == 0){
							returnBaseCost = 100;
						} else {
							returnBaseCost = 1;
						}
					}
					for(int j = 1; j < V; j++){
						if(unloadedBaggageCount[j] > 0){
							int coe = 50 + maxLoop;
							int cost = returnBaseCost * (D[nowNode][0]+D[0][j]) * coe / (coe + unloadedBaggageSum) / (unloadedBaggageCount[j] + loadedBaggageCount[j]);
							if(cost <= mincost){
								mintarget = 0;
								mincost = cost;
							}
						}
						if(loadedBaggageCount[j] > 0){
  							int coe = 1 + maxLoop % 5;
							int cost = 10 * D[nowNode][j] * (coe + unloadedBaggageCount[j]) * (coe + unloadedBaggageCount[j]) / coe / coe / loadedBaggageCount[j];
							if(cost < mincost){
								mintarget = j;
								mincost = cost;
							}
						}
					}
                }
				if(mintarget == -1 && nowNode > 0){
					mintarget = 0;
				}
				if(maxTargetResult[changeResult].first == t){
					if(maxTargetResult[changeResult].second == mintarget){
						changeResult++;
						break;
					}
				}
				if(beforeTarget != mintarget){
					targetResult.push_back({t,mintarget});
					beforeTarget = mintarget;
                }
				int minNextCost = INF;
				if(mintarget >= 0){
					for(int j = 0; j < G[nowNode].size(); j++){
						if(D[G[nowNode][j].first][mintarget] < minNextCost){
							ans = G[nowNode][j].first + 1;
							targetNode = ans - 1;
							remainDist = G[nowNode][j].second - 1;
							minNextCost = D[G[nowNode][j].first][mintarget];
						}
					}
				}
			} else {
				ans = targetNode + 1;
				remainDist--;
			}
			ansArray.push_back(ans);
	    }
		if(score > maxScore){
			if(targetResult[changeResult].second == 0){
				changeCountReturn++;
			} else {
				changeCountNotReturn++;
			}
			maxScore = score;
			maxAnsArray = ansArray;
			maxTargetResult = targetResult;
		}
		changeResult++;
	}
	for(int t = 0; t < Tmax; t++) {
		std::cout << maxAnsArray[t] << std::endl;
	}
	std::cerr << changeResult << " " << changeCountReturn << " " << changeCountNotReturn << std::endl;
	std::cerr << msec() << " " << maxScore << " " << maxLoop << std::endl;
	std::cerr << maxScore * 30 << std::endl;
	return 0;
}


Measured memory_bytes: 2432