"use strict";
var GET=(function(){function f(s){return new g(s);}function g(s){this._s=s.trim().split("\n");this._y=0;}g.prototype.a=function(f){var s=this._s, y=this._y, r;if(typeof s[y]==="string")s[y]=s[y].split(" ").reverse();r=s[y].pop();if(!s[y].length)this._y++;return f?r:+r;};g.prototype.l=function(f){var s=this._s[this._y++].split(" ");return f?s:s.map(a=>+a);};g.prototype.m=function(n,f){var r=this._s.slice(this._y,this._y+=n).map(a=>a.split(" "));return f?r:r.map(a=>a.map(a=>+a));};g.prototype.r=function(n,f){var r=this._s.slice(this._y,this._y+=n);return f?r:r.map(a=>+a);};return f;})();
var o=GET(require("fs").readFileSync("/dev/stdin","utf8"));
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);}

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

const INF = 1e9;
const V = o.a(); //200-400
const E = o.a(); //1.5V-2V
const vv = o.m(E);
const tmax = o.a(); //10000
const dist = xArray(INF,V,V);
const e = xArray("[]",V);
const ans = Array(tmax);

console.log(main());
function main(){
  for(var i = 0; i < V; i++)dist[i][i] = 0;
  for(var i = 0; i < E; i++){
    var v1 = vv[i][0]-1, v2 = vv[i][1]-1, d = vv[i][2];
    dist[v1][v2] = dist[v2][v1] = d;
    e[v1].push(v2);
    e[v2].push(v1);
  }
  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();
    day++;
  }
  return ans.join("\n");
}

function solve(){
  if(leng){
    leng--;
    return next;
  }
  if(next === 0)take();
}

function take(){
  while(takeday < day){
    var n = o.a();
    var d = o.m(n);
    takeday++;
  }
}