#include <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef pair<int,int>P;

std::chrono::system_clock::time_point start;
std::chrono::system_clock::time_point owari;

unsigned long long xor128() {
    static unsigned long long rx = 411111789, ry = 9471111169, rz = 5212488629, rw = 886575123;
    unsigned long long rt = (rx ^ (rx << 11));
    rx = ry;
    ry = rz;
    rz = rw;
    return (rw = (rw ^ (rw >> 19)) ^ (rt ^ (rt >> 8)));
}
 
double rand128(double min = 0, double max = 1){
        return (double)(xor128() % 0xFFFF) / 0xFFFF * (max - min) + min;
}

double calc_time(){
	owari = std::chrono::system_clock::now();
	return std::chrono::duration_cast<std::chrono::milliseconds>(owari-start).count();
}
int XXX;
int MMM=10;
bool NO[5000];
int dp[1<<20][20];
P pre[1<<20][20];

struct Graph{
	int LEN=0;
	int CNT=0;
	int n,m,T_max;
	int dist[400][400];
	int query[10000];
	vector<int>id[10000];
	vector<int>ver[10000];
	vector<P>G[400];
	int nexT[400][400];
	vector<int>NEX[400][400];
	vector<int>TSP[4];
	unordered_set<int>have[400];
	unordered_set<int>store[400];
	unordered_map<int,int>id_conf;
	int TSP_N;
	int f[400];
	Graph(){}
	void init(){
		for(int i=0;i<400;i++){
			for(int j=0;j<400;j++){
				dist[i][j] = i==j?0:1e8;
				nexT[i][j] = j;
			}
		}
		cin>>n>>m;
		for(int i=0;i<m;i++){
			int a,b;
			double c;
			cin>>a>>b>>c; a--; b--;
			if(a==0)XXX=b;
			if(b==0)XXX=a;
			dist[a][b]=dist[b][a]= ceil(c);
		}
		r(i,n)cin>>f[i];
		cin>>T_max;
		/*
		r(i,T_max){
			cin>>query[i];
			r(j,query[i]){
				MMM=min(i,MMM);
				int a,b;
				cin>>a>>b; b--;
				id[i].emplace_back(a);
				ver[i].emplace_back(b);
				id_conf[a]=i;
			}
		}
		*/
	}
	void warshall(){
		for(int k=0;k<n;k++){
			for(int i=0;i<n;i++){
				for(int j=0;j<n;j++){
					if(dist[i][j] > dist[i][k]+dist[k][j]){
						dist[i][j] = dist[i][k]+dist[k][j];
						nexT[i][j] = nexT[i][k];
					}
				}
			}
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				for(int cur=i;cur!=j;cur=nexT[cur][j]){
					NEX[i][j].emplace_back(cur);
				}
				NEX[i][j].emplace_back(j);
				if( i==j )NEX[i][j].emplace_back(j);
			}
		}
		r(i,n){
			//cerr<<dist[0][i]<<' '<<i<<endl;
		}
	}


	void Aneeling_2(){
		vector<int>v,ANS;
		set<int>used;
		int now=0;
		v.emplace_back(0);
		used.insert(0);
		int MAX=1e9,cur=0;
		r(i,n-1){
			int MIN=1e9,ID;
			r(j,n)if(!used.count(j)){
				if(dist[now][j]<MIN){
					MIN=dist[now][j];
					ID=j;
				}
			}
			//ID=i+1;
			cur+=MIN;
			v.emplace_back(ID);
			if(n/3==i)v.emplace_back(0);
			if(n/3+n/3==i)v.emplace_back(0);
			used.insert(ID);
			now=ID;
		}
		v.emplace_back(0);
		cur+=dist[v[n-1]][0];


		double startTemp=0;
	    double endTemp=0;
	    int cnt=0;
		// 焼きなす
		const double saTimeStart    = calc_time();         // 焼きなまし開始時刻。getTimeは、時間を返す関数
	    const double saTimeEnd      = 15000;
		while(1){
			double ela = calc_time();
			if( ela > 20000 )break;
			vector<int>nex(n+3);
			int i=xor128()%(n+3);
			int j=xor128()%(n+3);
			int k=xor128()%(n+3);
			if(k<j)swap(k,j);
			if(j<i)swap(j,i);
			if(k<j)swap(k,j);
			if(!i)continue;
			if(i==j||j==k)continue;
			int e=xor128()%3,X=0;


			/*if(e==0){
				r(a,i) nex[X++]=(v[a]);
				for(int a=k;a>=j;a--) nex[X++]=(v[a]);
				for(int a=i;a<j;a++) nex[X++]=(v[a]);
				for(int a=k+1;a<n+3;a++) nex[X++]=(v[a]);
			}
			if(e==1){
				r(a,i) nex[X++]=(v[a]);
				for(int a=j-1;a>=i;a--) nex[X++]=(v[a]);
				for(int a=k;a>=j;a--) nex[X++]=(v[a]);
				for(int a=k+1;a<n+3;a++) nex[X++]=(v[a]);
			}
			if(e==2){
				r(a,i) nex[X++]=(v[a]);
				for(int a=j;a<k+1;a++) nex[X++]=(v[a]);
				for(int a=j-1;a>=i;a--) nex[X++]=(v[a]);
				for(int a=k+1;a<n+3;a++) nex[X++]=(v[a]);
			}*/
			if(e==0){
				r(a,i) nex[X++]=(v[a]);
				for(int a=k;a>=j;a--) nex[X++]=(v[a]);
				for(int a=j-1;a>=i;a--) nex[X++]=(v[a]);
				for(int a=k+1;a<n+3;a++) nex[X++]=(v[a]);
			}
			if(e==1){
				r(a,n+3)nex[a]=v[a];
				int L=xor128()%(n+3);
				int R=xor128()%(n+3);
				while(R==L)R=xor128()%(n+3);
				swap(nex[L],nex[R]);
			}
			if(e==2){
				r(a,n+3)nex[a]=v[a];
				int L=xor128()%(n+3);
				int R=xor128()%(n+3);
				while(R==L)R=xor128()%(n+3);
				int TMP=nex[L];
				nex.erase(nex.begin()+L);
				nex.insert(nex.begin()+R,TMP);

			}

			/*int AX=cur;
        	int BX=sco;
			double TT = saTimeEnd-saTimeStart;
	        double t = calc_time()-saTimeStart; 
	        double temp = startTemp + (endTemp - startTemp) * t / TT;
	        double prob = exp(( -BX+AX ) / temp);
	        bool FORCE_NEXT = prob > (double)rand128(0,1) ;*/

			int sco = 0,TRU;
			vector<int>vec,VEC;
			r(i,n+3){
				if(nex[i]==0)vec.emplace_back(i);
				if(i!=n+2)sco+=dist[nex[i]][nex[i+1]];
				else sco+=dist[nex[i]][0];
			}



			TRU=sco;
			vec.emplace_back(n+3);

			sco -= (long long)abs(vec[0]-vec[1])*abs(vec[1]-vec[2])*abs(vec[2]-vec[3])*abs(vec[3]-vec[4])/sco/n*100;
			if(abs(vec[0]-vec[1])<20)sco += 10000;
			if(abs(vec[1]-vec[2])<20)sco += 10000;
			if(abs(vec[2]-vec[3])<20)sco += 10000;
			if(abs(vec[3]-vec[4])<20)sco += 10000;

			if(cur >= sco){
				//if(X!=n)cout<<X<<endl;
	        	cur = sco;
				v = nex;

	        }

	        if(MAX > sco){
	        	MAX = sco;
	        	ANS = nex;
	        	cerr<<vec[0]<<' '<<vec[1]<<' '<<vec[2]<<' '<<vec[3]<<' '<<vec[4]<<' '<<e<<' '<<cnt<<endl;
	        	cerr<<TRU<<' '<<calc_time()<<endl;
	        	LEN = TRU;
	        	//cout<<i<<' '<<j<<' '<<k<<endl;
	        	//cout<<MAX<<' '<<n<<' '<<cnt++<<' '<<nex.size()<<endl;
	        }

	        cnt++;
		}
		vector<int>Z;
		ANS.emplace_back(0);
		int idd=0;
		r(i,(int)ANS.size()){
			if(ANS[i]==0){
				if(i){
					TSP[idd++]=Z;
					//cerr<<TSP[idd-1].size()<<endl;
				}
				Z.clear();
			}
			else{
				Z.emplace_back(ANS[i]);
			}
		}
		//exit(0);
		TSP_N=idd;
		//cerr<<idd<<endl;
		int sco=0;
		r(i,n){
			if(i!=n-1)sco+=dist[ANS[i]][ANS[i+1]];
			else sco+=dist[0][ANS[n-1]];
		}
		/*
		r(i,n){
			int cnt=0;
			if(i)r(j,NEX[TSP[i-1]][TSP[i]].size()){
				if(NEX[TSP[i-1]][TSP[i]][j]==0)cnt=1;
			}
			if(i)cerr<<dist[0][TSP[i]]<<' '<<dist[TSP[i-1]][TSP[i]]<<' '<<TSP[i-1]<<' '<<TSP[i]<<' '<<cnt<<endl;
		}
		*/
		//cout<<sco<<endl;
	}

	int next_vertex(int A,int B){
		int x=NEX[A][B][1];
		//cout<<A<<' '<<B<<' '<<x<<endl;
		return x;
	}

	int CNT2=0;

	int IDX(vector<int>&A){
		for(int k=1,l=0;l<4;l++,k++){
			//cerr<<k<<endl;
			int sum=0;
			r(j,TSP[(CNT2+k)%TSP_N].size()){
				r(i,(int)A.size()){
					if(TSP[(CNT2+k)%TSP_N][j]==A[i]) sum++;
				}
			}
			if(sum){
				//cerr<<sum<<endl;
				CNT2 = (CNT2+k)%TSP_N;
				return CNT2;
			}
		}
	}

	vector<int> NEX_TSP(vector<int>&A,int C){
		vector<int>nex;
		r(i,TSP[C].size()){
			int F=0;
			r(j,A.size()){
				if(TSP[C][i]==A[j]){
					F=1;
					break;
				}
			}
			if(F){
				nex.emplace_back(TSP[C][i]);
			}
		}
		return nex;
	}

	vector<int> YAMANOBORI(vector<int>&A,vector<int>&B,int K,vector<int>&C){

		map<int,int>M;
		r(i,(int)A.size()){
			M[A[i]]=B[i];
		}

		int N=min(n/4,(int)A.size());
		vector<int>v;
		vector<long long>vA,vB;
		vector<int>ANS,idx;
		set<int>used;
		int now=0;
		long long MAX=1e18,cur=0,sum=0;
		//cerr<<K<<endl;

		if(K<9200){
			int idx2 = IDX(A);
			v=NEX_TSP(A,idx2);
			N=v.size();
			//r(i,n)used.insert(v[i]);

			vector<int>idx=v;
			int now=0;
			r(i,N){
				cur+=dist[now][idx[i]];
				now=idx[i];
			}
			cur+=dist[now][0];
			MAX=cur;

			int TT=50000;
			//cerr<<MAX<<' '<<cur<<endl;
			if( N>2){
				
				//cerr<<K<<endl;
				for(int iteration = 0;iteration<TT;iteration++){
					//cerr<<iteration<<endl;
					//cerr<<MAX<<' '<<cur<<endl;

					int i=xor128()%(N);
					int j=xor128()%(N);
					int k=xor128()%(N);
					if(k<j)swap(k,j);
					if(j<i)swap(j,i);
					if(k<j)swap(k,j);
					//if(i==0)continue;
					//cerr<<i<<j<<k<<' '<<idx.size()<<endl;
					if(i==j||j==k)continue;

					int e=0,X=0;

					long long sco = cur,now=0,sum=0;
					if(e==0){
						if(i)sco -= dist[idx[i-1]][idx[i]];
						else sco -= dist[0][idx[i]];
						sco -= dist[idx[j-1]][idx[j]];
						if(k!=N-1)sco -= dist[idx[k+1]][idx[k]];
						else if(K<9500-n)sco -= dist[0][idx[k]];
						if(i)sco += dist[idx[i-1]][idx[k]];
						else sco += dist[0][idx[k]];
						sco += dist[idx[j]][idx[i]];
						if(k!=N-1)sco += dist[idx[j-1]][idx[k+1]];
						else sco += dist[0][idx[j-1]];
					}

					
					if(e==1){
						if(i)sco -= dist[idx[i-1]][idx[i]];
						else sco -= dist[0][idx[i]];
						sco -= dist[idx[j-1]][idx[j]];
						if(k!=N-1)sco -= dist[idx[k+1]][idx[k]];
						else if(K<9500-n)sco -= dist[0][idx[k]];
						if(i)sco += dist[idx[i-1]][idx[j-1]];
						else sco += dist[0][idx[j-1]];
						sco += dist[idx[i]][idx[k]];
						if(k!=N-1)sco += dist[idx[j]][idx[k+1]];
						else sco += dist[0][idx[j]];
					}
					
					if(e==2){
						if(i)sco -= dist[idx[i-1]][idx[i]];
						else sco -= dist[0][idx[i]];
						sco -= dist[idx[j-1]][idx[j]];
						if(k!=N-1)sco -= dist[idx[k+1]][idx[k]];
						else if(K<9500-n)sco -= dist[0][idx[k]];
						if(i)sco += dist[idx[i-1]][idx[j]];
						else sco += dist[0][idx[j]];
						sco += dist[idx[k]][idx[j-1]];
						if(k!=N-1)sco += dist[idx[i]][idx[k+1]];
						else sco += dist[idx[i]][0];
					}
					
					if(e==3){
						if(i)sco -= dist[idx[i-1]][idx[i]];
						else sco -= dist[0][idx[i]];
						sco -= dist[idx[j-1]][idx[j]];
						if(k!=N-1)sco -= dist[idx[k+1]][idx[k]];
						else if(K<9500-n)sco -= dist[0][idx[k]];
						if(i)sco += dist[idx[i-1]][idx[k]];
						else sco += dist[0][idx[k]];
						sco += dist[idx[j]][idx[j-1]];
						if(k!=N-1)sco += dist[idx[i]][idx[k+1]];
						else sco += dist[idx[i]][0];
					}

					if(cur >= sco){
						vector<int>nex(N);
						if(e==0){
							r(a,i) nex[X++]=(idx[a]);
							for(int a=k;a>=j;a--) nex[X++]=(idx[a]);
							for(int a=i;a<j;a++) nex[X++]=(idx[a]);
							for(int a=k+1;a<N;a++) nex[X++]=(idx[a]);
						}
						if(e==1){
							r(a,i) nex[X++]=(idx[a]);
							for(int a=j-1;a>=i;a--) nex[X++]=(idx[a]);
							for(int a=k;a>=j;a--) nex[X++]=(idx[a]);
							for(int a=k+1;a<N;a++) nex[X++]=(idx[a]);
						}
						if(e==2){
							r(a,i) nex[X++]=(idx[a]);
							for(int a=j;a<k+1;a++) nex[X++]=(idx[a]);
							for(int a=j-1;a>=i;a--) nex[X++]=(idx[a]);
							for(int a=k+1;a<N;a++) nex[X++]=(idx[a]);
						}
						if(e==3){
							r(a,i) nex[X++]=(idx[a]);
							for(int a=k;a>=j;a--) nex[X++]=(idx[a]);
							for(int a=j-1;a>=i;a--) nex[X++]=(idx[a]);
							for(int a=k+1;a<N;a++) nex[X++]=(idx[a]);
						}
			        	cur = sco;
						idx = nex;

			        }

			        if(MAX > sco){
			        	MAX = sco;
			        	cerr<<MAX<<endl;
			        	v = idx;
			        }
		        }
	    	}
	    	r(a,N){
	    		used.insert(v[a]);
	    	}

	    	int RANG=K;now=0;
	    	r(a,N){
	    		RANG += (long long)dist[now][v[a]];
				now = v[a];
	    	}
	    	RANG += dist[0][now];

	    	if(!((9500-LEN/3) <=RANG && RANG<=9500-n/3)){
				for(int p=1;p>=0;p--){
					for(int X=0;X<(int)A.size();X++){
						if(used.count(A[X]))continue;
						vector<P>v2;

						v2.emplace_back( P(-dist[0][v[0]]+(dist[0][A[X]]+dist[A[X]][v[0]]),0) );
						for(int i=0;i<N-1;i++){
							v2.emplace_back(P(-dist[v[i+1]][v[i]]+(dist[v[i+1]][A[X]]+dist[A[X]][v[i]]),i+1));
						}
						v2.emplace_back( P(-dist[0][v[N-1]]+(dist[0][A[X]]+dist[A[X]][v[N-1]]),N) );
						sort(v2.begin(),v2.end());

						if(v2[0].first <= p){
							v.insert(v.begin()+v2[0].second,A[X]);
							N++;
							used.insert(A[X]);
						}
					}
				}
			}

	    	while((9500-LEN/3+LEN/15) <=RANG && RANG<=9500-n/3){
	    		int flag=0;
	    		for(int p=0;p<=15;p++){
					for(int X=0;X<(int)A.size();X++){
						if(used.count(A[X]))continue;
						if((int)store[A[X]].size())continue;
						vector<P>v2;

						v2.emplace_back( P(-dist[0][v[0]]+(dist[0][A[X]]+dist[A[X]][v[0]]),0) );
						for(int i=0;i<N-1;i++){
							v2.emplace_back(P(-dist[v[i+1]][v[i]]+(dist[v[i+1]][A[X]]+dist[A[X]][v[i]]),i+1));
						}   
						v2.emplace_back( P(-dist[0][v[N-1]]+(dist[0][A[X]]+dist[A[X]][v[N-1]]),N) );
						sort(v2.begin(),v2.end());

						if(v2[0].first <= p){
							v.insert(v.begin()+v2[0].second,A[X]);
							N++;
							used.insert(A[X]);
							flag=1;
							goto L;
						}
					}
				}
				L:;
				if(!flag)break;

				RANG=K,now=0;
		    	r(a,N){
		    		RANG += (long long)dist[now][v[a]];
					now = v[a];
		    	}
		    	RANG += dist[0][now];
	    	}



			if(1){
					int sA=0,sB=0,suA=0,suB=0,AA=0,BB=0;
					now=0;
					r(a,N){
						sA += (long long)dist[now][v[a]];
						suA += (sA+M[v[a]])*(sA+M[v[a]]);
						now = v[a];
						AA++;
					}
					reverse(v.begin(),v.end());
					now=0;suA=0;
					r(a,N){
						sA += (long long)dist[now][v[a]];
						//if(K+sA>=10000)continue;
						suB += (sA+M[v[a]])*(sA+M[v[a]]);
						now = v[a];
						BB++;
					}
					if(BB<AA)reverse(v.begin(),v.end());
					else if(suB>suA)reverse(v.begin(),v.end());
			}

			return v;
		}
		else{
			cerr<<K<<endl;
			N=A.size();
			//cerr<<N<<endl;
			r(i,A.size()){
				vA.emplace_back(i);
				vB.emplace_back(B[i]);
				/*
				idx.emplace_back(i);
				ANS.emplace_back(i);
				*/
			}
			r(i,N){
				//cerr<<i<<endl;
				int MIN=1e9,ID;
				r(j,N)if(!used.count(j)){
					if(dist[now][A[vA[j]]]<MIN){
						MIN=dist[now][A[vA[j]]];
						ID=j;
					}
				}
				cur+=MIN;
				idx.emplace_back(ID);
				used.insert(ID);
				now=ID;
				ANS.emplace_back(ID);
			}
			//cerr<<N<<endl;

			now=0;
			cur=0;

			r(i,N){
				sum += dist[now][A[vA[idx[i]]]];
				if(sum+K<10000)cur += C[i];
				now = A[vA[idx[i]]];
			}
			MAX=cur;
			//cerr<<cur<<endl;
			//exit(1);
		}

		//cerr<<N<<' '<<v.size()<<' '<<A.size()<<endl;
		//cur += dist[0][A[vA[N-1]]]*10;
		//cur += (sum*sum);
		//cout<<cur<<' '<<A.size()<<' '<<N<<endl;
		vector<int>nex(N);

		int TT=3000000;
		//if(K>=9500-n)TT*=2;
		//if(N<13)TT/=10;


		if(A.size()>=3){
		if(K<9200)for(int iteration = 0;iteration<TT;iteration++){
			break;
			//cerr<<iteration<<endl;

			int i=xor128()%(N);
			int j=xor128()%(N);
			int k=xor128()%(N);
			if(k<j)swap(k,j);
			if(j<i)swap(j,i);
			if(k<j)swap(k,j);
			//if(i==0)continue;
			//cerr<<i<<j<<k<<' '<<idx.size()<<endl;
			if(i==j||j==k)continue;

			int e=0,X=0;

			long long sco = cur,now=0,sum=0;

			if(e==0){
				if(i)sco -= dist[A[vA[idx[i-1]]]][A[vA[idx[i]]]];
				else sco -= dist[0][A[vA[idx[i]]]];
				sco -= dist[A[vA[idx[j-1]]]][A[vA[idx[j]]]];
				if(k!=N-1)sco -= dist[A[vA[idx[k+1]]]][A[vA[idx[k]]]];
				else if(K<9500-n)sco -= dist[0][A[vA[idx[k]]]];
				if(i)sco += dist[A[vA[idx[i-1]]]][A[vA[idx[k]]]];
				else sco += dist[0][A[vA[idx[k]]]];
				sco += dist[A[vA[idx[j]]]][A[vA[idx[i]]]];
				if(k!=N-1)sco += dist[A[vA[idx[j-1]]]][A[vA[idx[k+1]]]];
				else sco += dist[0][A[vA[idx[j-1]]]];
			}

			
			if(e==1){
				if(i)sco -= dist[A[vA[idx[i-1]]]][A[vA[idx[i]]]];
				else sco -= dist[0][A[vA[idx[i]]]];
				sco -= dist[A[vA[idx[j-1]]]][A[vA[idx[j]]]];
				if(k!=N-1)sco -= dist[A[vA[idx[k+1]]]][A[vA[idx[k]]]];
				else if(K<9500-n)sco -= dist[0][A[vA[idx[k]]]];
				if(i)sco += dist[A[vA[idx[i-1]]]][A[vA[idx[j-1]]]];
				else sco += dist[0][A[vA[idx[j-1]]]];
				sco += dist[A[vA[idx[i]]]][A[vA[idx[k]]]];
				if(k!=N-1)sco += dist[A[vA[idx[j]]]][A[vA[idx[k+1]]]];
				else sco += dist[0][A[vA[idx[j]]]];
			}
			
			if(e==2){
				if(i)sco -= dist[A[vA[idx[i-1]]]][A[vA[idx[i]]]];
				else sco -= dist[0][A[vA[idx[i]]]];
				sco -= dist[A[vA[idx[j-1]]]][A[vA[idx[j]]]];
				if(k!=N-1)sco -= dist[A[vA[idx[k+1]]]][A[vA[idx[k]]]];
				else if(K<9500-n)sco -= dist[0][A[vA[idx[k]]]];
				if(i)sco += dist[A[vA[idx[i-1]]]][A[vA[idx[j]]]];
				else sco += dist[0][A[vA[idx[j]]]];
				sco += dist[A[vA[idx[k]]]][A[vA[idx[j-1]]]];
				if(k!=N-1)sco += dist[A[vA[idx[i]]]][A[vA[idx[k+1]]]];
				else if(K<9500-n)sco += dist[A[vA[idx[i]]]][0];
			}
			
			if(e==3){
				if(i)sco -= dist[A[vA[idx[i-1]]]][A[vA[idx[i]]]];
				else sco -= dist[0][A[vA[idx[i]]]];
				sco -= dist[A[vA[idx[j-1]]]][A[vA[idx[j]]]];
				if(k!=N-1)sco -= dist[A[vA[idx[k+1]]]][A[vA[idx[k]]]];
				else if(K<9500-n)sco -= dist[0][A[vA[idx[k]]]];
				if(i)sco += dist[A[vA[idx[i-1]]]][A[vA[idx[k]]]];
				else sco += dist[0][A[vA[idx[k]]]];
				sco += dist[A[vA[idx[j]]]][A[vA[idx[j-1]]]];
				if(k!=N-1)sco += dist[A[vA[idx[i]]]][A[vA[idx[k+1]]]];
				else if(K<9500-n)sco += dist[A[vA[idx[i]]]][0];
			}
/*

			if(e==0){
				r(a,i) nex[X++]=(idx[a]);
				for(int a=k;a>=j;a--) nex[X++]=(idx[a]);
				for(int a=i;a<j;a++) nex[X++]=(idx[a]);
				for(int a=k+1;a<N;a++) nex[X++]=(idx[a]);
			}
			if(e==1){
				r(a,i) nex[X++]=(idx[a]);
				for(int a=j-1;a>=i;a--) nex[X++]=(idx[a]);
				for(int a=k;a>=j;a--) nex[X++]=(idx[a]);
				for(int a=k+1;a<N;a++) nex[X++]=(idx[a]);
			}
			if(e==2){
				r(a,i) nex[X++]=(idx[a]);
				for(int a=j;a<k+1;a++) nex[X++]=(idx[a]);
				for(int a=j-1;a>=i;a--) nex[X++]=(idx[a]);
				for(int a=k+1;a<N;a++) nex[X++]=(idx[a]);
			}
			if(e==3){
				r(a,i) nex[X++]=(idx[a]);
				for(int a=k;a>=j;a--) nex[X++]=(idx[a]);
				for(int a=j-1;a>=i;a--) nex[X++]=(idx[a]);
				for(int a=k+1;a<N;a++) nex[X++]=(idx[a]);
			}
			*/

			//cerr<<i<<j<<k<<endl;
			//cerr<<idx[0]<<idx[1]<<idx[2]<<endl;
			if(cur >= sco){
				//cerr<<idx[0]<<idx[1]<<idx[2]<<endl;
				if(e==0){
					r(a,i) nex[X++]=(idx[a]);
					for(int a=k;a>=j;a--) nex[X++]=(idx[a]);
					for(int a=i;a<j;a++) nex[X++]=(idx[a]);
					for(int a=k+1;a<N;a++) nex[X++]=(idx[a]);
				}
				if(e==1){
					r(a,i) nex[X++]=(idx[a]);
					for(int a=j-1;a>=i;a--) nex[X++]=(idx[a]);
					for(int a=k;a>=j;a--) nex[X++]=(idx[a]);
					for(int a=k+1;a<N;a++) nex[X++]=(idx[a]);
				}
				if(e==2){
					r(a,i) nex[X++]=(idx[a]);
					for(int a=j;a<k+1;a++) nex[X++]=(idx[a]);
					for(int a=j-1;a>=i;a--) nex[X++]=(idx[a]);
					for(int a=k+1;a<N;a++) nex[X++]=(idx[a]);
				}
				if(e==3){
					r(a,i) nex[X++]=(idx[a]);
					for(int a=k;a>=j;a--) nex[X++]=(idx[a]);
					for(int a=j-1;a>=i;a--) nex[X++]=(idx[a]);
					for(int a=k+1;a<N;a++) nex[X++]=(idx[a]);
				}
	        	cur = sco;
				idx = nex;

	        }

	        if(MAX > sco){
	        	/*
	        	if(e==0){
					r(a,i) nex[X++]=(idx[a]);
					for(int a=k;a>=j;a--) nex[X++]=(idx[a]);
					for(int a=i;a<j;a++) nex[X++]=(idx[a]);
					for(int a=k+1;a<N;a++) nex[X++]=(idx[a]);
				}*/
	        	MAX = sco;
	        	ANS = nex;
	        	long long sco2=0;
			r(a,N){
				sco2 += (long long)dist[now][A[vA[nex[a]]]];
				now = A[vA[nex[a]]];
			}
			sco2 += (long long)dist[0][A[vA[nex[N-1]]]];
			//cerr<<sco<<' '<<sco2<<endl;
	        }
		}

		else{
			//cerr<<K<<endl;
			double TIM=29700;
			TIM+=CNT*200;
			CNT++;
			if(TIM>29900){
				TIM=29910;
			}
			TT=1000000000;
			//cur=MAX=0;
			//cerr<<cur<<' '<<MAX<<endl;
			//exit(1);
			int SUM=1e9;
			for(int iteration = 0;iteration<TT;iteration++){

				if(iteration%100==0){
					if(calc_time()>=TIM)break;
				}

			int i=xor128()%(N);
			int j=xor128()%(N);
			int k=xor128()%(N);
			if(k<j)swap(k,j);
			if(j<i)swap(j,i);
			if(k<j)swap(k,j);
			//if(i==0)continue;
			if(i==j||j==k)continue;

			int e=xor128()%4,X=0;

			long long sco =0,now=0,sum=0;

			if(e==0){
				r(a,i) nex[X++]=(idx[a]);
				for(int a=k;a>=j;a--) nex[X++]=(idx[a]);
				for(int a=i;a<j;a++) nex[X++]=(idx[a]);
				for(int a=k+1;a<N;a++) nex[X++]=(idx[a]);
			}
			if(e==1){
				r(a,i) nex[X++]=(idx[a]);
				for(int a=j-1;a>=i;a--) nex[X++]=(idx[a]);
				for(int a=k;a>=j;a--) nex[X++]=(idx[a]);
				for(int a=k+1;a<N;a++) nex[X++]=(idx[a]);
			}
			if(e==2){
				r(a,i) nex[X++]=(idx[a]);
				for(int a=j;a<k+1;a++) nex[X++]=(idx[a]);
				for(int a=j-1;a>=i;a--) nex[X++]=(idx[a]);
				for(int a=k+1;a<N;a++) nex[X++]=(idx[a]);
			}
			if(e==3){
				r(a,i) nex[X++]=(idx[a]);
				for(int a=k;a>=j;a--) nex[X++]=(idx[a]);
				for(int a=j-1;a>=i;a--) nex[X++]=(idx[a]);
				for(int a=k+1;a<N;a++) nex[X++]=(idx[a]);
			}
		//int p=0;
			r(a,N){
				sum+=dist[now][A[nex[a]]];
				if(sum+K<10000)sco+=C[nex[a]];
				//p+=C[a];
				now = A[nex[a]];
			}
			//cerr<<N<<' '<<sco<<' '<<p<<endl;

			if(cur < sco || cur==sco&&sum<SUM){
				//cerr<<sco<<' '<<sum+K<<' '<<A.size()<<endl;
	        	cur = sco;
	        	SUM=sum;
				idx = nex;

	        }

	        if(MAX < sco){
	        	//cerr<<MAX<<' '<<calc_time()<<endl;
	        	MAX = sco;
	        	ANS = nex;
	        	//cerr<<MAX<<' '<<calc_time()<<endl;
	        }

		}
		}

		}
		else return A;

		//if(A.size()==360)exit(1);

		vector<int>ret;
		r(i,N){
			ret.emplace_back(A[vA[ANS[i]]]);
		}
		if(K>=9200){
			return ret;
		}
		else ret=v;
		return ret;
		//cout<<A.size()<<' '<<ret.size()<<endl;
		//cout<<MAX<<endl<<endl;

		int X=N,TM=X;
		set<int>ss;

/*
		for(;X<(int)A.size();X++){
			vector<P>v;

			v.emplace_back( P(-dist[0][ret[0]]+(dist[0][A[vA[X]]]+dist[A[vA[X]]][ret[0]]),0) );
			for(int i=0;i<N-1;i++){
				v.emplace_back(P(-dist[ret[i+1]][ret[i]]+(dist[ret[i+1]][A[vA[X]]]+dist[A[vA[X]]][ret[i]]),i+1));
			}
			v.emplace_back( P(-dist[0][ret[N-1]]+(dist[0][A[vA[X]]]+dist[A[vA[X]]][ret[N-1]]),N) );
			sort(v.begin(),v.end());

			if(v[0].first <= 2){
				//cout<<v[0].first<<' '<<v[0].second<<endl;
				ret.insert(ret.begin()+v[0].second,A[vA[X]]);
				N++;
				ss.insert(X);
				//cout<<N<<' '<<A[X]<<endl;
			}
		}
*/
		

		return ret;
	}

long long TTT;
	vector<int> Inter_ract(){
		vector<int>ope;
		long long score = 0,S1=0,S2=0;
		long long SSS = 0;

		int tim=0,dir=0,now=0,nex=0,A=0,B,zero=0; 
// 99059416
// 98914443
// 99458389
// 
		vector<int> lis;

		r(i,T_max){
			if(i==8000)TTT=S1/S2;

			int iku=0;

			cin>>query[i];
			r(j,query[i]){
				//cout<<endl;
				MMM=min(i,MMM);
				int a,b;
				cin>>a>>b; b--;
				id[i].emplace_back(a);
				ver[i].emplace_back(b);
				id_conf[a]=i;
			}

			int get;
			cin>>get;
			r(i,get){
				int p;
				cin>>p;
			}
			string s;

			if( query[i] ) {
				store[ver[i][0]].insert(id[i][0]);
			}

			if( now == 0 && A == 0 &&(int)lis.size()==0 ){
				zero=0;
				//lis.clear();
				nex=0;
				r(j,400){
					for(auto &e:store[j]){
						have[j].insert(e);
					}
					store[j].clear();
				}
				int cnt = 0;
				vector<int>v,ID,NUM;
				//v.emplace_back(0);
				r(j,400)if((int)have[j].size()){
					cnt++;
					v.emplace_back(j);
					int p=0;
					for(auto &e:have[j]){
						if(i>=8000)p+=dist[0][j]/f[j];
						else p+=(i - id_conf[e])*(i - id_conf[e]);//*(f[j]);
					}
					//if(i>=8000)p/=f[j];
					ID.emplace_back(p);
					NUM.emplace_back((int)have[j].size());
				}
				//vector<int>VV(1);
				//return VV;
				//exit(1);
				//cout<<cnt<<endl;
				if( cnt==0 ){
					//cout<<i<<endl;
					cout<<-1<<endl;
					string ok;
					cin>>ok;
					if(ok=="NG"){
						vector<int>VV(1);
						return VV;
					}
					//cout<<ok<<endl;
					int get2;
					cin>>get2;
					r(i,get2){
						int p;
						cin>>p;
					}
					//ope.emplace_back(-1);
					continue;
				}
				else{
					v = YAMANOBORI(v,ID,i,NUM);
					//cerr<<v.size()<<endl;
					v.emplace_back(0);
				}
				if(1){
					lis = v;
					r(j,v.size()-1){
						if(v[j]==v[j+1])cout<<1111<<endl;
					}
					nex = next_vertex(now,lis[0]);
					A=0;
					B=dist[now][nex];
				}

			}

			else if( A==0 && zero==0 ){

				if(now==0){
					r(j,400){
						for(auto &e:store[j]){
							have[j].insert(e);
						}
						store[j].clear();
					}
				}
				else{
					for(auto &e:have[now]){
						int pre=id_conf[e];
						score += 100000000 - (i-pre)*(i-pre);
						if(i>=3000)S1+=100000000 - (i-pre)*(i-pre),S2++;
						SSS++;
					}
					have[now].clear();
				}
			}

			if(now==nex)cout<<(int)lis.size()<<endl;
			else cout<<nex+1<<endl;
			//cout<<endl;
			string ok;
			cin>>ok;
			if(ok=="NG"){
				vector<int>VV(1);
				return VV;
			}
			int get2;
			cin>>get2;
			r(i,get2){
				int p;
				cin>>p;
			}
			A++;
			B--;
			//cout<<now<<' '<<nex<<' '<<A<<' '<<B<<' '<<i<<' '<<lis.size()<<endl;

			if( B==0 ){
				//if((int)lis.size()>=2&&lis[0]==lis[1])cout<<lis[0]<<lis[1]<<endl;
				now = nex;
				//cout<<nex<<' '<<lis[0]<<' '<<lis[1]<<endl;
				if( (int)lis.size() && now == lis[0] )lis.erase(lis.begin());
				if( (int)lis.size() && now == lis[0] )lis.erase(lis.begin());
				//if( (int)lis.size() && now == lis[0] )lis.erase(lis.begin());
				if((int)lis.size())nex = next_vertex(now,lis[0]);
				//cout<<nex<<endl;
				//if( lis.size() && nex == lis[0] )lis.erase(lis.begin());
				A=0;
				B=dist[now][nex];
			}

			//cerr<<i<<endl;
			//cout<<now<<' '<<nex<<' '<<A<<' '<<B<<' '<<i<<endl;

		}
		//cout<<ope.size()<<endl;

		//return ope;
		//cerr<< score <<endl;
		//exit(0);
		/*
		r(i,T_max){
			if(ope[i]==-1)cout<<ope[i]<<endl;
			else cout<<ope[i]+1<<endl;
		}
		*/

		vector<int>V(n);
		return V;


	}

	long long simulation(vector<int> &A){

		long long score = 0;
		memset(NO,0,sizeof(NO));

		unordered_set<int>have[400];
		unordered_set<int>store[400];
		int tim=0,cnt=0,now=1;

		r(i,A.size()){
			//cerr<<i<<endl;
			//cout<<A[i]<<' '<<A[i+1]<<' '<<i<<' '<<A.size()<<endl;
			if(A[i]!=-1 && A[i+1]!=0&&have[A[i+1]].size()==0)NO[i+1]=1;

			r(j,NEX[A[i]][A[i+1]].size()-1){

				int s=NEX[A[i]][A[i+1]][j];
				int t=NEX[A[i]][A[i+1]][j+1];
				//cout<<'a'<<endl;
				r(k,dist[s][t]){
					if(cnt==T_max){
						//cerr<<i<<
						return score;
					}
					if(query[cnt]){
						if(id[cnt].size()==0)exit(0);
						store[ver[cnt][0]].insert(id[cnt][0]);
					}
					cnt++;
				}
				//cout<<'b'<<endl;
				
				for(auto &e:have[t]){
					//cout<<e<<endl;
					int pre=id_conf[e];
					score += 100000000 - (cnt-pre)*(cnt-pre);
				}
				have[t].clear();
				if(t==0){
					r(k,400){
						for(auto &e:store[k]){
							have[k].insert(e);
						}
						store[k].clear();
					}
				}
			}
			//cout<<score<<endl;
		}

		return score;
	}
	long long output(vector<int> &A){

		long long score = 0;

		unordered_set<int>have[400];
		unordered_set<int>store[400];
		int tim=0,cnt=0,now=1;

		if(query[cnt]){
			if(id[cnt].size()==0)exit(0);
			store[ver[cnt][0]].insert(id[cnt][0]);
		}

		r(i,A.size()){
			//cout<<A[i]<<' '<<A[i+1]<<' '<<i<<' '<<A.size()<<endl;

			r(j,NEX[A[i]][A[i+1]].size()-1){

				int s=NEX[A[i]][A[i+1]][j];
				int t=NEX[A[i]][A[i+1]][j+1];
				//cout<<'a'<<endl;
				r(k,dist[s][t]){
					if(cnt==T_max) return score;
					cout<<t+1<<endl;
					cnt++;
					if(query[cnt]){
						if(id[cnt].size()==0)exit(0);
						store[ver[cnt][0]].insert(id[cnt][0]);
					}
				}
				//cout<<'b'<<endl;
				
				for(auto &e:have[t]){
					//cout<<e<<endl;
					int pre=id_conf[e];
					score += 100000000 - (cnt-pre)*(cnt-pre);
				}
				have[t].clear();
				if(t==0){
					r(k,400){
						for(auto &e:store[k]){
							have[k].insert(e);
						}
						store[k].clear();
					}
				}
			}
			//cout<<score<<endl;
		}

		return score;
	}
};



int main(){
	start = std::chrono::system_clock::now();
	Graph G;
	G.init();
	G.warshall();

	//if(G.n>=260)G.Aneeling_2();
	G.Aneeling_2();
	//exit(0);
	vector<int>v=G.Inter_ract();
	cerr<<G.TTT<<endl;

	if(v.size()==1)return 1;

	return 0;


}

Measured memory_bytes: 22992