#include <iostream>
#include <vector>
#include <map>
#include <utility>
#include <deque>
#include <list>
#include <algorithm>

using namespace std;

struct Edge{
	unsigned u, v, dist;
	bool operator == (const Edge &e){ return (u == e.u && v == e.v) || (u == e.v && v == e.u); }
	bool operator != (const Edge &e){ return !(*this == e); }
	bool operator < (const Edge &e) const { return u < e.u || (u == e.u && v < e.v); }
};

ostream & operator << (ostream &dest, const Edge &e){
	dest << "(" << e.u << ", " << e.v << ") : " << e.dist;
	return dest;
}

struct Graph{
	const unsigned V, E;
	vector<unsigned> dist;
	vector<unsigned> col;
	vector<unsigned> row;
	

	Graph() : V(get_unsigned()), E(get_unsigned()), dist(2*E), col(2*E), row(V + 1)	
	{
		vector<Edge> edges(2*E);
		for(unsigned i = 0; i < E; ++i){
			cin >> edges[i].u >> edges[i].v >> edges[i].dist;
			edges[i + E].u = edges[i].v;
			edges[i + E].v = edges[i].u;
			edges[i + E].dist = edges[i].dist;
		}
		sort(edges.begin(), edges.end());
		size_t r = 0;
		for(size_t i = 0; i < 2*E; ++i){
			while(r != edges[i].u) row[r++] = i;
			col[i] = edges[i].v;
			dist[i] = edges[i].dist;
		}
		for(; r <= V; ++r)
			row[r] = 2*E;
	}

	unsigned distance(size_t i, size_t j) const{
		size_t I = min(i, j);
		size_t J = max(i, j);
		if(I == 0) return 0;
		for(size_t k = row[I - 1]; k < row[I]; ++k)
			if(J == col[k]) return dist[k];
		return 0;
	}
	unsigned neighbor_begin(size_t i) const{
		if(i == 0) return -1;
		return row[i - 1];
	}
	unsigned neighbor_end(size_t i) const{
		if(i == 0) return -1;
		return row[i];
	}

private:
	unsigned get_unsigned() const{
		unsigned tmp; cin >> tmp;
		return tmp;
	}
};

void WF(const Graph &G, vector<vector<unsigned>> &next, vector<vector<unsigned>> &distance){
	
	for(size_t from = 0; from < G.V; ++from){
		for(size_t to = 0; to < G.V; ++to){
			distance[from][to] = from == to ? 0 : 1'000'000'000;
			next[from][to] = from;
		}
	}
	for(size_t from = 0; from < G.V; ++from){
		for(size_t to_index = G.neighbor_begin(from+ 1); to_index < G.neighbor_end(from + 1); ++to_index){
			size_t to = G.col[to_index] - 1;
			distance[from][to] = G.dist[to_index];
			next[from][to] = to;
		}
	}
	for(size_t inter = 0; inter < G.V; ++inter){
		for(size_t from = 0; from < G.V; ++from){
			for(size_t to = 0; to < G.V; ++to){
				if(distance[from][to] > distance[from][inter] + distance[inter][to]){
					distance[from][to] = distance[from][inter] + distance[inter][to];
					next[from][to] = next[from][inter];
				}
			}
		}
	}
}

void DFS(size_t current_node, const vector<vector<unsigned>> &distance, const vector<unsigned> &tour_point, const vector<unsigned> &parent, const vector<list<unsigned>> &children, vector<unsigned> &rec_path){
	//if(find(rec_path.begin(), rec_path.end(), current_node) == rec_path.end()){
	//	
	//}
	rec_path.emplace_back(current_node);
	for(auto &child : children[current_node])
		DFS(child, distance, tour_point, parent, children, rec_path);
}


void TSP(const vector<vector<unsigned>> &distance, const vector<unsigned> &tour_point, vector<unsigned> &rec_path){
	rec_path.clear();

	// make MSP by Prim method
	vector<unsigned> weight(tour_point.size());
	vector<unsigned> nearest_neighbor(tour_point.size());
	list<unsigned> uncontained;
	vector<list<unsigned>> children(tour_point.size());

	nearest_neighbor[0] = 0;
	weight[0] = -1;
	for(size_t i = 1; i < weight.size(); ++i){
		weight[i] = distance[tour_point[0]][tour_point[i]];
		nearest_neighbor[i] = 0;
		uncontained.emplace_back(i);
	}

	while(!uncontained.empty()){
		auto min_iter = uncontained.begin();
		for(auto iter = uncontained.begin(); iter != uncontained.end(); ++iter)
			if(weight[*iter] < weight[*min_iter])
				min_iter = iter;
		const size_t min_index = *min_iter; uncontained.erase(min_iter);
		children[nearest_neighbor[min_index]].emplace_back(min_index);
		for(auto i : uncontained){
			double w = distance[tour_point[min_index]][tour_point[i]];
			if(w < weight[i])
				weight[i] = w, nearest_neighbor[i] = min_index;
		}
	}
	
	DFS(0, distance, tour_point, nearest_neighbor, children, rec_path);
}

int main(){
	Graph G;
	unsigned T_max;
	cin >> T_max;
	vector<unsigned> orders(T_max);
	for(unsigned t = 0; t < T_max; ++t){
		unsigned N_new;
		cin >> N_new;
		if(N_new == 0) orders[t] = 0;
		else{
			unsigned id;
			cin >> id >> orders[t];
		}
	}

	vector<vector<unsigned>> distance(G.V, vector<unsigned>(G.V));
	vector<vector<unsigned>> next(G.V, vector<unsigned>(G.V));

	WF(G, next, distance);

	auto put_command = [T_max](int command){
		static size_t line = 0;
		if(line++ == T_max) exit(0);
		cout << command << "\n";
	};

	auto move = [&](size_t from, size_t to, deque<pair<unsigned, unsigned>> &load){
		size_t t = 0;
		size_t current = from;
		size_t next_point = next[current][to];
		size_t distance_to_next = distance[current][next_point];
		while(current != to){
			for(size_t way = 0; way < distance_to_next; ++way)
				++t, put_command(next_point + 1);
			current = next_point;
			next_point = next[current][to];
			distance_to_next = distance[current][next_point];
			load.erase(remove_if(load.begin(), load.end(), [current](auto &p){return p.second == current;}), load.end());
		}
		return t;
	};

	size_t t = 0;
	size_t elapsed = 0;

	deque<pair<unsigned, unsigned>> loading;
	size_t current = 0;
	bool is_moving = false;
	size_t goal = 0;
	size_t last_load = 0;

	if(!orders[0]) put_command(-1);
	while(1){
		for(size_t i = last_load + 1; i < t; ++i)
			if(orders[i]) last_load = i, loading.emplace_back(make_pair(i, orders[i] - 1));
		if(loading.empty()){
			while(!orders[++t])
				put_command(-1);
			loading.emplace_back(make_pair(t, orders[t] - 1));
			last_load = t;
		}

		vector<unsigned> tour_point(loading.size() + 1);
		tour_point[0] = 0;
		for(size_t i = 0; i < loading.size(); ++i) tour_point[i + 1] = loading[i].second;
		vector<unsigned> rec_path;
		TSP(distance, tour_point, rec_path);
		for(size_t i = 0; i < rec_path.size(); ++i)
			rec_path[i] = tour_point[rec_path[i]];
	
		for(size_t i = 0; i < rec_path.size(); ++i){
			goal = rec_path[i];
			t += move(current, goal, loading);
			current = goal;
		}
		t += move(current, 0, loading);
		current = 0;
	}
	return 0;
}


Measured memory_bytes: 1792