#include <bits/stdc++.h>
using namespace std;
const int ITERS=140000;
const bool VISIT1=true;
/*const*/ bool UPD_ORDER=true;
const bool REVERSE=true;
const bool GREEDY_WIDE=true;
int n,m,tmax,tmax2,o,cnt,i,j,k,swaps,x[808],y[808],z[808],d[402][402],id[10100],dst[10100],av[10100],otm[10100],p[10100],b[10100],newp[10100],fi[402],freq[402];
vector<int> g[402],orders[10100],here[402];  // TODO: orders[i] can be just number, not vector
long long mided,cur,score;
char verdict[5];
int sqr(int x) { return x*x; }
long long deliver_here(int pos, int t) {
  long long score=0;
  for (; fi[pos]<here[pos].size(); fi[pos]++) {
    int i=here[pos][fi[pos]];
    if (av[i]==1) {
      av[i]=2;
      if (t<tmax) score+=tmax2-sqr(t-otm[i]);
      newp[cnt++]=i;
      //fprintf(stderr,"order dst=%d st=%d et=%d\n",pos,otm[i],t);  // i=%d,i
    } else if (av[i]==0) break;
  }
  return score;
}
void init() {
  // TODO: greedy by cnt first, score second
  long long score=cnt=0;
  int pos=1,lst=-1,t=0;
  for (int i=1; i<=n; i++) fi[i]=0;
  for (int i=0; i<o; i++) av[i]=0;
  for (int nxto=-1; cnt<o; ) {
    //fprintf(stderr,"#time=%d; position=%d; nxt=%d\n",t,pos,dst[p[cur]]);
    score+=deliver_here(pos,t);
    if (pos==1) {
      for (int i=lst+1; i<=t && i<tmax; i++) for (int j=0; j<orders[i].size(); j++) av[orders[i][j]]=1;
      lst=t;
    }
    if (nxto==-1 || av[nxto]==2) {
      nxto=-1;
      for (int i=0; i<o; i++) if (av[i]!=2) {
        if (nxto==-1) { nxto=i; continue; }
        int nlst_new=(av[i]==0)?max(t+d[pos][1],otm[i]):lst;
        int tm_i_new=(av[i]==0)?(nlst_new+d[1][dst[i]]):(t+d[pos][dst[i]]);
        int tm_nxto_new=(otm[nxto]>nlst_new)?(max(tm_i_new+d[dst[i]][1],otm[nxto])+d[1][dst[nxto]]):(tm_i_new+d[dst[i]][dst[nxto]]);
        int nlst_old=(av[nxto]==0)?max(t+d[pos][1],otm[nxto]):lst;
        int tm_nxto_old=(av[nxto]==0)?(nlst_old+d[1][dst[nxto]]):(t+d[pos][dst[nxto]]);
        int tm_i_old=(otm[i]>nlst_new)?(max(tm_nxto_old+d[dst[nxto]][1],otm[i])+d[1][dst[i]]):(tm_nxto_old+d[dst[nxto]][dst[i]]);
        if (tm_i_new>=tmax && tm_nxto_old>=tmax || GREEDY_WIDE) {
          if (sqr(tm_i_new-otm[i])+sqr(tm_nxto_new-otm[nxto])<sqr(tm_nxto_old-otm[nxto])+sqr(tm_i_old-otm[i])) nxto=i;
        } else {
          int score_new=(tm_i_new<tmax)?tmax2-sqr(tm_i_new-otm[i]):0;
          if (tm_nxto_new<tmax) score_new+=tmax2-sqr(tm_nxto_new-otm[nxto]);
          int score_old=(tm_nxto_old<tmax)?tmax2-sqr(tm_nxto_old-otm[nxto]):0;
          if (tm_i_old<tmax) score_old+=tmax2-sqr(tm_i_old-otm[i]);
          if (score_new>score_old) nxto=i;
        }
      }
      if (nxto==-1) nxto=1;
    }
    int ds=(av[nxto]==1)?dst[nxto]:1;
    if (VISIT1) {
      if (pos!=1 && ds!=1 && d[pos][ds]==d[pos][1]+d[1][ds]) ds=1;
    }
    if (pos!=ds) {
      for (int i=0; i<g[pos].size(); i++) {
        int e=g[pos][i];
        int nxt=x[e]+y[e]-pos;
        if (d[ds][pos]==d[ds][nxt]+z[e]) {
          ds=nxt;
          break;
        }
      }
      t+=d[pos][ds];
      pos=ds;
    } else t++;
  }
  for (i=0; i<o; i++) p[i]=newp[i];
  fprintf(stderr,"Greedy: score=%I64d; cnt=%d\n",score,cnt);
}
long long calc_score() {
  long long score=cnt=0;
  int pos=1,lst=-1;
  for (int i=1; i<=n; i++) fi[i]=0;
  for (int i=0; i<o; i++) av[i]=0;
  for (int cur=0, t=0; cur<o && t<tmax; ) {
    //fprintf(stderr,"#time=%d; position=%d; nxt=%d\n",t,pos,dst[p[cur]]);
    score+=deliver_here(pos,t);
    for (; cur<o && av[p[cur]]==2; cur++);
    if (cur>=o) break;
    if (pos==1) {
      for (int i=lst+1; i<=t; i++) for (int j=0; j<orders[i].size(); j++) av[orders[i][j]]=1;
      lst=t;
    }
    int ds=(av[p[cur]]==1)?dst[p[cur]]:1;
    if (VISIT1) {
      if (pos!=1 && ds!=1 && d[pos][ds]==d[pos][1]+d[1][ds]) ds=1;
    }
    if (pos!=ds) {
      for (int i=0; i<g[pos].size(); i++) {
        int e=g[pos][i];
        int nxt=x[e]+y[e]-pos;
        if (d[ds][pos]==d[ds][nxt]+z[e]) {
          ds=nxt;
          break;
        }
      }
      t+=d[pos][ds];
      pos=ds;
    } else t++;
  }
  int num=cnt;
  for (int i=0; i<o; i++) if (av[p[i]]!=2) newp[num++]=p[i];
  if (num!=o) puts("!!!!!");
  return score;
}
void print_result() {
  score=cnt=0;
  int cur=0,pos=-1,ed=0;
  for (int i=1; i<=n; i++) fi[i]=0;
  for (int i=0; i<tmax; i++) av[i]=0;
  for (int t=0; t<tmax; t++) {
    scanf("%d",&cnt);
    for (int j=0; j<cnt; j++, o++) {
      scanf("%d%d",&id[o],&dst[o]);
      b[id[o]]=o;
      here[dst[o]].push_back(o);
      orders[i].push_back(o);
      otm[o]=i;
      p[o]=o;  // TODO: logic of permutation creating instead of this
    }
    scanf("%d",&cnt);
    for (int j=0; j<cnt; j++) {
      scanf("%d",&k);
      av[b[k]]=1;
    }
    // printf("time %d: pos %d %d\n",t,pos,ed);
    if (pos<0) {
      //score+=deliver_here(-pos,t);
      for (; cur<o && av[p[cur]]==2; cur++);
    }
    int ds=(cur<o && av[p[cur]]==1)?dst[p[cur]]:1;
    // printf("pcur=%d ds=%d\n",p[cur],ds);
    if (pos<0) {
     if (pos==-ds) {
       puts("-1");
     } else {
      if (VISIT1) {
        if (pos!=-1 && ds!=1 && d[-pos][ds]==d[-pos][1]+d[1][ds]) ds=1;
      }
      for (int i=0; i<g[-pos].size(); i++) {
        int e=g[-pos][i];
        int nxt=x[e]+y[e]+pos;
        //printf(" nxt=%d; %d =?= %d + %d\n",nxt,d[ds][-pos],d[ds][nxt],z[e]);
        if (d[ds][-pos]==d[ds][nxt]+z[e]) {
          printf("%d\n",nxt);
          if (z[e]>1) {
            ed=(x[e]==-pos)?1:(z[e]-1);
            pos=e;
          } else pos=-nxt;
          break;
        }
      }
     }
    } else {
      if (d[ds][x[pos]]+ed<d[ds][y[pos]]+z[pos]-ed) {
        printf("%d\n",x[pos]);
        if (ed==1) pos=-x[pos]; else ed--;
      } else {
        printf("%d\n",y[pos]);
        if (ed+1==z[pos]) pos=-y[pos]; else ed++;
      }
    }
    fflush(stdout);
    scanf("%s",verdict);
    if (verdict[0]=='N') break;
    scanf("%d",&cnt);
    for (int j=0; j<cnt; j++) {
      scanf("%d",&k);
      av[b[k]]=2;
      score+=sqr(tmax2)-sqr(t+1-otm[b[k]]);
    }
  }
}
void upd_order() {
  for (int i=0; i<o; i++) p[i]=newp[i];
}
int main() {
  auto dur_beg=chrono::high_resolution_clock::now();
  srand(371);
  scanf("%d%d",&n,&m);
  memset(d,10,sizeof(d));
  for (i=1; i<=n; i++) d[i][i]=0;
  for (i=1; i<=m; i++) {
    scanf("%d%d%d",&x[i],&y[i],&z[i]);
    mided+=z[i];
    d[x[i]][y[i]]=d[y[i]][x[i]]=z[i];
    g[x[i]].push_back(i);
    g[y[i]].push_back(i);
  }
  mided/=m;
  for (i=1; i<=n; i++) scanf("%d",&freq[i]);
  for (k=1; k<=n; k++) for (i=1; i<=n; i++) for (j=1; j<=n; j++)
    d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
  //for (i=1; i<=n; i++, puts("")) for (j=1; j<=n; j++) printf("%d ",d[i][j]);
  scanf("%d",&tmax);
  tmax2=sqr(tmax);
  /*for (i=0; i<tmax; i++) {
    
  }
  init();
  score=calc_score();
  //if (UPD_ORDER) upd_order();
  fprintf(stderr,"score=%I64d; cnt=%d; o=%d\n",score,cnt,o);
  for (int it=0; it<ITERS; it++) {
    i=rand()%o;
    if (REVERSE) {
      //j=i+1+rand()%min(o/3,o-i);
      j=rand()%o;
      if (j>=i) j++;
      if (i>j) swap(i,j);
      reverse(p+i,p+j);
    } else {
      j=rand()%(o-1);
      if (j>=i) j++;
      swap(p[i],p[j]);
    }
    cur=calc_score();
    if (cur<score) {
      if (REVERSE) {
        reverse(p+i,p+j);
      } else {
        swap(p[i],p[j]);
      }
    } else {
      if (UPD_ORDER) upd_order();
      swaps++;
      //fprintf(stderr,"len=%d\n",j-i);
      score=cur;
    }
    if ((it&511)==0) {
      auto dur=chrono::high_resolution_clock::now()-dur_beg;
      auto ms=chrono::duration_cast<chrono::milliseconds>(dur).count();
      //fprintf(stderr,"iter %d; %d ms\n",it,ms);
      if (ms>22000) UPD_ORDER=false;
      if (ms>29450) break;
    }
  }
  fprintf(stderr,"%d swaps made; score=%I64d; cnt=%d\n",swaps,score,cnt);*/
  // TODO: type of rand_iterations + SA/HC
  print_result();  // TODO: print best
  fprintf(stderr,"%d swaps made; score=%I64d; cnt=%d\n",swaps,score,cnt);
  auto dur=chrono::high_resolution_clock::now()-dur_beg;
  auto ms=chrono::duration_cast<chrono::milliseconds>(dur).count();
  fprintf(stderr,"%d ms\n",ms);
  fflush(stderr);
  return 0;
}


Measured memory_bytes: 2512