"use strict";
var fs=require("fs");
var input=fs.readFileSync("/dev/stdin","utf8");
var cin=input.split(/ |\n/),cx=0;
function next(){return +cin[cx++];}
function nexts(){return cin[cx++];}
function xArray(v){var a=arguments,l=a.length,r="Array(a["+--l+"]).fill(0).map(x=>{return "+v+";})";while(--l)r="Array(a["+l+"]).fill(0).map(x=>"+r+")";return eval(r);}
function unionFind(n){this.a=Array(n+1).fill(-1);}
unionFind.prototype={
  root:function(i){return this.a[i]<0?i:this.a[i]=this.root(this.a[i]);},
  same:function(x,y){return this.root(x)===this.root(y);},
  unite:function(x,y){x=this.root(x);y=this.root(y);if(x===y)return 1;if(this.a[y]<=this.a[x])this.a[y]+=this.a[x],this.a[x]=y;else this.a[x]+=this.a[y],this.a[y]=x;},
  size:function(i){return -this.a[this.root(i)];},
}



var leng = 0, nextV = 0, day = 0, takeday = -1;
var score = 0;

const INF = 1e9;
const V = next(); //200-400
const E = next(); //1.5V-2V
const dist = xArray(INF,V,V);
const directDist = xArray(INF,V,V);
const e = xArray("[]",V);
getE();
const tmax = next(); //10000
const tmax2 = tmax*tmax;
const ans = Array(tmax);
const has = new Set(), ready = new Set();
var round = [];
const orderTime = xArray("[]",V);

const out = main();
try{
  fs.writeFileSync("/dev/stdout", out);
  console.log(score);
}catch(er){
  console.log(out);
}

function main(){
  for(var k = 0; k < V; k++)
  for(var i = 0; i < V; i++)
  for(var j = 0; j < V; j++){
    if(dist[i][k]+dist[k][j]<dist[i][j])dist[i][j]=dist[i][k]+dist[k][j];
  }
  while(day < tmax){
    ans[day] = solve()+1;
    if(leng < 0){
      console.log(day,leng,nextV);
      return;
    }
    day++;
  }
  return ans.join("\n");
}

//頂点0+setの最小全域木
function kruskal(set){
  var vs = Array.from(set).concat(0);
  var n = vs.length;
  var es = [];
  for(var i = 0; i < n; i++)for(var j = i+1; j < n; j++){
    es.push([vs[i],vs[j],dist[vs[i]][vs[j]]]);
  }
  es.sort((a,b)=>a[2]-b[2]);
  var uf = new unionFind(V);
  var res = [];
  for(var i = 0; uf.size(0) !== n; i++){
    if(uf.unite(es[i][0],es[i][1]))continue;
    res.push([es[i][0],es[i][1],es[i][2]]);
  }
  return res;
}

//最小全域木に頂点を追加して再構築
function treeAdd(tree,set,addV){
  var vset = Array.from(set).concat(0);
  var vadd = Array.from(addV);
  var ns = vset.length, na = vadd.length;
  var es = [];
  for(var i = 0; i < ns; i++)for(var j = 0; j < na; j++){
    es.push([vset[i],vadd[j],dist[vset[i]][vadd[j]]]);
  }
  es.sort((a,b)=>a[2]-b[2]);
  es.push([0,0,INF]);
  var uf = new unionFind(V);
  var res = [];
  for(var i = 0, j = 0; uf.size(0) !== n;){
    if(i === ns || es[j][2] < tree[i][2]){
      if(!uf.unite(es[j][0],es[j][1]))
        res.push([es[j][0],es[j][1],es[j][2]]);
      j++;
    }else{
      if(!uf.unite(tree[i][0],tree[i][1]))
        res.push([tree[i][0],tree[i][1],tree[i][2]]);
      i++;
    }
  }
  return res;
}

function getE(){
  for(var i = 0; i < V; i++)dist[i][i] = 0;
  for(var i = 0; i < E; i++){
    var v1 = next()-1, v2 = next()-1, d = next();
    dist[v1][v2] = dist[v2][v1] = directDist[v1][v2] = directDist[v2][v1] = d;
    e[v1].push(v2);
    e[v2].push(v1);
  }
}

function deliver(){
  if(has.size === 0){
    if(nextV === 0)return -2;
    return minPath(0);
  }
  if(nextV === round[round.length-1])round.pop();
  if(round.length === 0)return minPath(0);
  return minPath(round[round.length-1]);
}

function minPath(to){
  for(var v of e[nextV]){
    if(dist[nextV][to] === directDist[nextV][v]+dist[v][to]){
      leng = dist[nextV][v]-1;
      return nextV = v;
    }
  }
  return -2;
}

function solve(){
  if(leng){
    leng--;
    return nextV;
  }
  if(nextV === 0)take();
  else give();
  return deliver();
}

function take(){
  round = [];
  while(takeday < day){
    takeday++;
    var n = next();
    for(var i = 0; i < n; i++){
      var id = next()-1, d = next()-1;
      has.add(d);
      orderTime[d].push(takeday);
    }
  }
  if(has.size === 0)return;
  var tree = kruskal(has);
  var road = xArray("[]",V);
  for(var i = 0; i < tree.length; i++){
    var a = tree[i][0], b = tree[i][1];
    road[a].push(b);
    road[b].push(a);
  }
  dfs(0,-1,road);
  round.reverse();
  round.pop();
}

function dfs(v,p,road){
  round.push(v);
  for(var i of road[v]){
    if(i === p)continue;
    dfs(i,v,road);
  }
}

function give(){
  has.delete(nextV);
  while(orderTime[nextV].length){
    var t = day - orderTime[nextV].pop();
    score += tmax2 - t*t;
  }
}

Measured memory_bytes: 53968