#include <bits/stdc++.h>
using namespace std;
const int ITERS=2000;
const bool VISIT1=false;
int n,m,tmax,tmax2,o,cnt,scnt,i,j,k,swaps,x[808],y[808],z[808],d[402][402],id[10100],dst[10100],av[10100],otm[10100],p[10100],fi[402];
vector<int> g[402],orders[10100],here[402];  // TODO: orders[i] can be just number, not vector
long long cur,score;
int sqr(int x) { return x*x; }
void init() {
  for (i=0; i<o; i++) p[i]=i;
  // TODO: some greedy?
}
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;
      score+=tmax2-sqr(t-otm[i]);
      scnt++;
      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;
}
long long calc_score() {
  long long score=scnt=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++;
  }
  return score;
}
void print_result() {
  score=0;
  long long scnt=0;
  int cur=0,pos=-1,ed=0,lst=-1;
  for (int i=1; i<=n; i++) fi[i]=0;
  for (int i=0; i<o; i++) av[i]=0;
  for (int t=0; t<tmax; t++) {
    //if (t>75 && t<82) 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++);
      if (cur>=o) {
        puts("-1");
        continue;
      }
      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;
          //printf("%d is available\n",orders[i][j]);
        }
        lst=t;
      }
    }
    int ds=(av[p[cur]]==1)?dst[p[cur]]:1;
    //if (t>75 && t<82) printf("pcur=%d ds=%d\n",p[cur],ds);
    if (pos<0) {
      if (pos==-ds) {
        puts("-1");
        continue;
      }
      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++;
      }
    }
  }
}
int main() {
  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]);
    d[x[i]][y[i]]=d[y[i]][x[i]]=z[i];
    g[x[i]].push_back(i);
    g[y[i]].push_back(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++) {
    scanf("%d",&cnt);
    for (j=0; j<cnt; j++, o++) {
      scanf("%d%d",&id[o],&dst[o]);
      here[dst[o]].push_back(o);
      orders[i].push_back(o);
      otm[o]=i;
    }
  }
  init();
  score=calc_score();
  for (int it=0; it<ITERS; it++) {
    i=rand()%o;
    j=rand()%(o-1);
    if (j>=i) j++;
    swap(p[i],p[j]);
    cur=calc_score();
    if (cur<score) swap(p[i],p[j]); else {
      swaps++;
      score=cur;
    }
  }
  fprintf(stderr,"%d swaps made; score=%I64d; scnt=%d\n",swaps,score,scnt);
  // TODO: fixed amount of rand_iterations + calc_score(best) + SA/HC
  print_result();
  fprintf(stderr,"%d swaps made; score=%I64d; scnt=%d\n",swaps,score,scnt);
  return 0;
}


Measured memory_bytes: 1536