// InterestingLSY
// 2659723130
// 《幽灵公主》是真的好看！
#ifdef intLSY
#include <intlsy/stdc++.h>
#else
#include <bits/stdc++.h>
#endif
#include <bits/c++0x_warning.h>
using namespace std;
#define il inline
#define elif else if

// Type
#define ld double
#define ll long long
#define ull unsigned ll

// Vector
#define vc vector
#define Pb push_back
#define Pf push_front
#define Eb emplace_back
#define All(x) x.begin(),x.end()
#define AllRev(x) x.rbegin(),x.rend()

// Memory
#define Ms(_data) memset(_data,0,sizeof(_data))
#define Msn(_data,_num) memset(_data,_num,sizeof(_data))

// Template
#define _cl class
#define _tp template
#define _tyn typename

// Pair
#define Mp make_pair
#define F first
#define S second
#define pii pair<int,int>
#define pli pair<ll,int>
#define pil pair<int,ll>
#define pll pair<ll,ll>

// Loop
#define For(i,j) for( int (i) = 1 ; (i) <= (j) ; ++(i) )
#define For0(i,j) for( int (i) = 0 ; (i) < (j) ; ++(i) )
#define Forx(i,j,k) for( int (i) = (j) ; (i) <= (k) ; ++(i) )
#define Forstep(i,j,k,st) for( int (i) = (j) ; (i) <= (k) ; (i) += (st) )
#define fOR(i,j) for( int (i) = (j) ; (i) >= 1 ; --(i) )
#define fOR0(i,j) for( int (i) = (j)-1 ; (i) >= 0 ; --(i) )
#define fORx(i,j,k) for( int (i) = (k) ; (i) >= (j) ; --(i) )

// Read
struct InputReader{
	#define bs 1048576
	char buf[bs]; int p;
	il InputReader(){ p = bs; }
	il void Flush(){ p = 0; fread(buf,1,bs,stdin); }
	il char C(){ if(p >= bs) Flush(); return buf[p++]; }
	il char Readnum(){ char ch = C(); while( !isdigit(ch) && ch != '-' ) ch = C(); return ch; }
	il void Readalpha( char &c ){ c = C(); while( !isalpha(c) ) c = C(); }
	int operator() (){
		int ans = 0, fu = 1; char ch = Readnum();
		if( ch == '-' ) fu = -1, ch = C();
		while( ch >= '0' && ch <= '9' ){
			ans = ans*10 + ch-'0';
			ch = C();
		}
		return ans * fu;
	}
	ll Readll(){
		ll ans = 0LL, fu = 1LL; char ch = Readnum();
		if( ch == '-' ) fu = -1LL, ch = C();
		while( ch >= '0' && ch <= '9' ){
			ans = ans*10LL + ch-'0';
			ch = C();
		}
		return ans * fu;
	}
	il void Readstring( string &x ){
		x.clear(); char ch = C();
		while( !isdigit(ch)  &&  !isalpha(ch)  &&  ch != '#'  &&  ch != '.' ) ch = C();
		while( isdigit(ch)  ||  isalpha(ch)  ||  ch == '#'  ||  ch == '.' ){
			x += ch;
			ch = C();
		}
	}
	il void Readchstring( char s[] ){
		int len = 0; char ch = C();
		while( !isdigit(ch)  &&  !isalpha(ch)  /*&&  ch != '*'  &&  ch != '?'*/ ) ch = C();
		while( isdigit(ch)  ||  isalpha(ch)  /*||  ch == '*'  ||  ch == '?'*/ ){
			s[len++] = ch;
			ch = C();
		}
		s[len] = '\0';
	}
	il void Specialread( char &c ){
		c = C();
		while( !isdigit(c) && !isalpha(c) && c != '#'  &&  c != '.'  &&  c != '='  &&  c != 'B' ) c = C();
	}
	#undef bs
}In;
il void Read( int &x ){ x = In(); }
il void Read( int &x, int &y ){ x = In(); y = In(); }
il void Read( int &x1 , int &x2 , int &x3 ){ x1 = In(); x2 = In(); x3 = In(); }
il void Read( int &x1 , int &x2 , int &x3 , int &x4 ){ x1 = In(); x2 = In(); x3 = In(); x4 = In(); }
il void Read( ll &x ){ x = In.Readll(); }
il void Read( ll &x, ll &y ){ x = In.Readll(); y = In.Readll(); }
il void Read( ll &x1 , ll &x2 , ll &x3 ){ x1 = In.Readll(); x2 = In.Readll(); x3 = In.Readll(); }
il void Read( ll &x1 , ll &x2 , ll &x3 , ll &x4 ){ x1 = In.Readll(); x2 = In.Readll(); x3 = In.Readll(); x4 = In.Readll(); }
_tp<_tyn T> void Read( T a[] , int st , int ed ){ Forx(i,st,ed) Read(a[i]); }
#define iRead(k) int k; Read(k);
#define iRead2(a,b) iRead(a); iRead(b);
#define iRead3(a,b,c) iRead2(a,b); iRead(c);
#define iRead4(a,b,c,d) iRead2(a,b); iRead2(c,d);
#define lRead(k) ll k; Read(k);
#define lRead2(a,b) lRead(a); lRead(b);
#define lRead3(a,b,c) lRead2(a,b); lRead(c);
#define lRead4(a,b,c,d) lRead2(a,b); lRead2(c,d);

// File
#define Fin(a) freopen(a,"r",stdin)
#define Fout(a) freopen(a,"w",stdout)
il void FILEIO(){
	#ifdef intLSY
		Fin("in.in");
	#endif
}
il void FILEIO( string pname ){
	#ifndef intLSY
		Fin((pname+".in").c_str());
		Fout((pname+".out").c_str());
	#else
		Fin("in.in");
	#endif
}
il void FILEIO_OICONTEST( string pname ){
	Fin((pname+".in").c_str());
	#ifndef intLSY
		Fout((pname+".out").c_str());
	#endif
}
void Printtime(){
	#ifdef intLSY
		double _timeuse = clock()* 1000.0 / CLOCKS_PER_SEC;
		fprintf(stderr,"\n\nTime usage:\n%.0lf ms\n",_timeuse);
	#endif
}
void END(){ Printtime(); exit(0); }
_tp<_tyn T>void END( T mes ){ cout << mes << endl; END(); }

// Debug
#define B cerr << "BreakPoint" << endl;
#define O(x) cerr << #x << " " << (x) << endl;
#define o(x) cerr << #x << " " << (x) << "  ";
#if __cplusplus >= 201703L
template<typename T, typename... Ts>
void P( T t0 , Ts... ts ){
    cerr << t0 << " ";
    if constexpr(sizeof...(ts)>0) P(ts...);
    else cerr << endl;
}
#else
template<typename T, typename... Ts>
void P( T t0 , Ts... ts ){
    // I love cpp17.
    // Fuck you.
}
#endif
#define Msz(x) cerr << "Sizeof " << #x << " " << sizeof(x)/1024/1024 << " MB" << endl;
_tp<_tyn T>void Print( T a[] , int s , int t , char sp = ' ' , char ed = '\n' ){
	if( s > t ) return;
	for( int i = s ; i < t ; i++ )
		cout << a[i] << sp;
	cout << a[t] << ed;
	cout.flush();
}
_tp<_tyn T>void Print( T a , int s = 0 , int t = -1 , char sp = ' ' , char ed = '\n' ){
	if( t == -1 ) t = a.size()-1;
	for( int i = s ; i <= t ; i++ )
		cout << a[i] << sp;
	cout << ed;
	cout.flush();
}

// Optimize
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define Mymax(a,b) (a) = max((a),(b))
#define Mymin(a,b) (a) = min((a),(b))
#define MyMax(a,b) (a) = Max((a),(b))
#define MyMin(a,b) (a) = Min((a),(b))
#define INF (0x3f3f3f3f)
#define LINF ((long long)(0x3f3f3f3f3f3f3f3f))
#define Sqr(x) (1LL*(x)*(x))
#define Lowbit(a) ((a)&(-(a)))
vc<int> Range( int l , int r ){
	vc<int> ret{};
	Forx(i,l,r) ret.Pb(i);
	return ret;
}
const ll rand_seed = time(0);
// const ll rand_seed = 233;
mt19937 Rand(rand_seed);	// さん
#include <functional>
///////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////
constexpr int MAXN = 410;
constexpr int MAXT = 10010;

int n,m,tmax;

int dis[MAXN][MAXN];
int trans[MAXN][MAXN];
void Floyd(){
	For(k,n)
		For(i,n)
			For(j,n)
				if( dis[i][j] > dis[i][k] + dis[k][j] ){
					dis[i][j] = dis[i][k] + dis[k][j];
					trans[i][j] = k;
				}
}
vc<int> e[MAXN];

struct Order{
	int id, pos, ti;
};
vc<Order> orders;

int cur_time = 0;
int cur_pos = 1;

il void Move( int to ){
	printf("%d\n",to);
	++cur_time;
	if( cur_time == tmax ) END();
}
il void Stay(){
	printf("-1\n");
	++cur_time;
	if( cur_time == tmax ) END();
}
bool marked[MAXN];
void TravelTo( int f , int t ){
	marked[f] = 1;
	assert( cur_pos == f );
	if( f == t ) return;
	if(trans[f][t]){
		TravelTo(f,trans[f][t]);
		TravelTo(trans[f][t],t);
		return;
	}else{
		For(i,dis[f][t])
			Move(t);
		cur_pos = t;
		return;
	}
}

vc<int> GetBestOrder_Less( const vc<Order> &pending ){
	// O(pending.size());
	auto GetProfit = [&]( const vc<pii> &poses ){
		int token = Rand();
		static int vis_time[MAXN], last_token[MAXN];

		int tim = cur_time, pos = cur_pos;
		function<void(int,int)> Go = [&]( int f , int t ){
			if( tim > tmax ) return;
			if( last_token[f] != token ){
				last_token[f] = token;
				vis_time[f] = tim;
			}
			if( f == t ) return;
			if(trans[f][t]){
				Go(f,trans[f][t]); if( tim > tmax ) return;
				Go(trans[f][t],t);
			}else{
				tim += dis[f][t];
				Go(t,t);
			}
		};
		for( auto p : poses ){
			int v = p.F;
			if( last_token[v] == token ) continue;
			Go(pos,v);
			pos = v;
			if( tim > tmax ) break;
		}

		ll profit = 0;
		for( auto order : pending ){
			if( last_token[order.pos] != token ) continue;
			profit += Sqr(tmax) - Sqr(vis_time[order.pos]-order.ti);
		}

		return profit;
	};

	static vc<pii> best_order;
	best_order.clear();
	ll max_profit = -LINF;

	static vc<pii> tmp_order;
	tmp_order.clear();
	for( auto order : pending ){
		tmp_order.Eb(order.pos,order.ti);
	}
	sort(All(tmp_order));
	tmp_order.resize( unique(All(tmp_order),[]( pii x , pii y ){ return x.F == y.F; }) - tmp_order.begin() );
	sort(All(tmp_order),[]( pii x , pii y ){ return x.S < y.S; });

	For(ti,200000){
		ll t_profit = GetProfit(tmp_order);
		if( t_profit > max_profit ){
			max_profit = t_profit;
			best_order = tmp_order;
		}
		random_shuffle(All(tmp_order));
	}

	static vc<int> ret_order;
	ret_order.clear();
	for( auto p : best_order )
		ret_order.Eb(p.F);

	return ret_order;
}

vc<int> fullorder;
void GetFullOrder(){
	static bool vis[MAXN];
	int p = 1;
	fullorder = {1};
	vis[1] = 1;
	For(_i,n-1){
		vc<int> neighbours{};
		for( auto v : e[p] )
			if(!vis[v])
				neighbours.Eb(v);
		int v;
		if( not neighbours.empty() ){
			v = neighbours[uniform_int_distribution<>(0,neighbours.size()-1)(Rand)];
		}else{
			vc<int> notvis{};
			For(i,n){
				if(!vis[i])
					notvis.Eb(i);
			}
			sort(All(notvis),[p]( int a , int b ){ return dis[p][a] < dis[p][b]; });
			v = notvis[0];
		}
		vis[v] = 1;
		fullorder.Eb(v);
		p = v;
	}
}

vc<int> GetBestOrder_More( const vc<Order> &pending ){
	static bool have_order[MAXN];
	Ms(have_order);
	for( auto order : pending )
		have_order[order.pos] = 1;
	vc<int> ans{};
	for( auto p : fullorder ){
		if(have_order[p])
			ans.Eb(p);
	}
	return ans;
}

void Solve(){
	vc<Order>::iterator nxt_order = orders.begin();
	static vc<Order> pending_delivery;

	auto Pack = [&](){
		assert( cur_pos == 1 );
		while( nxt_order != orders.end() and nxt_order->ti <= cur_time ){
			if( cur_time+dis[cur_pos][nxt_order->pos] <= 10000 ){
				pending_delivery.Pb(*nxt_order);
			}
			++nxt_order;
		}
	};

	while( cur_time < tmax ){
		assert( cur_pos == 1 );
		Pack();
		while( 	nxt_order != orders.end() and
				pending_delivery.size() < 50 and
				( pending_delivery.empty() or nxt_order->ti - pending_delivery.begin()->ti <= tmax/100 )
			){
				Stay();
				Pack();
				// O(cur_time);
			}

		static vc<int> delivery_order; delivery_order.clear();
		if( pending_delivery.size() <= 60 )
			delivery_order = GetBestOrder_Less(pending_delivery);
		else
			delivery_order = GetBestOrder_More(pending_delivery);

		Ms(marked);
		for( auto v : delivery_order ){
			if(marked[v]) continue;
			TravelTo(cur_pos,v);
		}
		TravelTo(cur_pos,1);

		pending_delivery.clear();

		if( nxt_order == orders.end() ){
			while(1)
				Stay();
			break;
		}
	}
}

int main(){
	FILEIO();
	srand(rand_seed);
	// Fout("temp/out.out");

	Read(n,m);
	Msn(dis,INF);
	For(i,n) dis[i][i] = 0;
	For(i,m){
		iRead3(a,b,c);
		if( dis[a][b] > c ){
			dis[a][b] = c;
			trans[a][b] = 0;
		}
		if( dis[b][a] > c ){
			dis[b][a] = c;
			trans[b][a] = 0;
		}
		e[a].Eb(b);
		e[b].Eb(a);
	}
	Floyd();

	GetFullOrder();

	Read(tmax);
	For0(ti,tmax){
		iRead(cnt);
		For(i,cnt){
			iRead2(id,pos);
			orders.Pb({id,pos,ti});
		}
	}
	sort(All(orders),[]( const Order &x , const Order &y ){ return x.ti < y.ti; });

	Solve();

	END();
}


Measured memory_bytes: 1792