#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define rep(i,n) for( ll (i)=0; (i)<(n); ++(i) )
#define rpv(d) for(auto& v:d)
#define co(n) cout << (n) << endl;
#define cn(n) cin >> n;
#define all(x) (x).begin(), (x).end()

int v_num, e_num, f_sum;
struct order{
  int dst;
  int t;
  bool sw = false;
};
struct {
  int from = 0;
  int to = 1;
  int dst = 0;
  bool isOnNode(){ return (!dst); }
  bool isOn( int x ){ return isOnNode()&&(from==x); }
  void set( int x ){ from=x; to=(x+1)%v_num; dst=0; }
  void move( int x, vector<pair<int,int>>& edge, bool sw = true ){
    if (isOnNode()){
      to=x;dst=1;
      if (edge.end()==find(all(edge),pair<int,int>(min(from,to),max(from,to)))){
        puts("-1");
        dst=0;
        return;
      }
    }else if (to==x){
      ++dst;
    }else if (from==x){
      --dst;
    }
    cout << (x+1) << endl;
  }
  bool chkIsReached( vector<vector<int>>& fx ){
    if (dst==fx[from][to]){
      from=to;dst=0;
      return true;
    }
    return false;
  }
} car;
array<order,9501> odr;
list<int> dst;
vector<int> waitingOrders;
vector<int> stackingOrders;

void getInputGraph( vector<vector<int>>& fx, vector<vector<int>>& fd, vector<pair<int,int>>& edge );
void initializeBuffer( vector<vector<int>>& fx, vector<vector<int>>& fd );
void calcGraph( vector<vector<int>>& fx, vector<vector<int>>& fd );
void getInputOrders( int t );
void getInputStacks();
bool getInputResult();

int main(){
  // Initialize
  cin >> v_num >> e_num;
  vector<int> f(v_num);
  vector<vector<int>> fx(v_num,vector<int>(v_num));
  vector<vector<int>> fd(v_num,vector<int>(v_num));
  vector<pair<int,int>> edge(e_num);
  initializeBuffer( fx, fd );
  getInputGraph( fx, fd, edge );
  rpv(f){cn(v)} //f_sum = accumulate(all(f),0);
  int t_max; cin >> t_max;

  // Pre processing
  calcGraph( fx, fd );

  // Each Steps
  rep(t,t_max){
    getInputOrders(t);
    getInputStacks();
    if (dst.empty()&&car.isOn(0)){
      puts("-1");
    }else{
      if (dst.empty())dst.push_back(0);
      car.chkIsReached(fx);
      if (!car.isOnNode()){
        if ( fx[car.from][dst.front()] < fx[car.to][dst.front()] ){
          car.move(car.from,edge);
        }else{
          car.move(car.to,edge);
        }
      }else{
        dst.remove_if([&](int x){return x==car.from;});
        car.move(fd[car.from][dst.front()],edge);
      }
    }
    if(getInputResult())break;
  }
}

bool getInputResult(){
  string s;cn(s)
  if (s=="NG")return true;
  int n;cn(n)rep(i,n){
    int id;cn(id)
    car.set(odr[id].dst);
  }
  return false;
}

void getInputStacks(){
  int n; cin >> n;
  if (!n)return;
  car.set(0);
  rep(i,n){
    int id;cn(id)
    dst.push_back(odr[id].dst);
    stackingOrders.push_back(id);
  }
  waitingOrders.erase(all(waitingOrders));
}

void getInputOrders( int t ){
  int n; cin >> n;
  if (!n)return;
  int id,dst; cin >> id >> dst;
  odr[id].dst = dst - 1;
  odr[id].t = t;
  waitingOrders.push_back(id);
}

void calcGraph( vector<vector<int>>& fx, vector<vector<int>>& fd ){
  rep(k,v_num){
    rep(i,v_num){
      rep(j,v_num){
        if ( fx[i][k] + fx[k][j] < fx[i][j] ){
          fx[i][j] = fx[i][k] + fx[k][j];
          fd[i][j] = fd[i][k]; 
        }
      }
    }
  }
}

void getInputGraph( vector<vector<int>>& fx, vector<vector<int>>& fd, vector<pair<int,int>>& edge ){
  rep(i,e_num){
    int v1,v2,d;
    cin >> v1 >> v2 >> d;
    --v1; --v2;
    edge[i].first = min(v1,v2);
    edge[i].second = max(v1,v2);
    fx[v1][v2] = d;
    fx[v2][v1] = d;
    fd[v1][v2] = v2;
    fd[v2][v1] = v1;
  }
}

void initializeBuffer( vector<vector<int>>& fx, vector<vector<int>>& fd ){
  rep(i,v_num){rep(j,v_num){if (i!=j){fx[i][j]=0x7fff;}else{fx[i][j]=0;}}}
  rep(i,v_num){rep(j,v_num){fd[i][j]=-1;}}
}

Measured memory_bytes: 2896