package main

import (
	"bufio"
	"fmt"
	"log"
	"math"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)

func main() {
	log.SetFlags(log.Lshortfile)
	sc.Split(bufio.ScanWords)
	solver()
}

var V, E int

const T int = 10000

type Edge struct {
	u, v, d int
}

var edges []Edge

type Order struct {
	time    int
	item_id int
	dst     int // お届け先
	end     int
}

func (o Order) String() string {
	s := fmt.Sprintf("time=%d item=%d dst=%d end=%d",
		o.time, o.item_id, o.dst, o.end)
	return s
}

var orders []*Order

func checkOrder(now, lastShipTime, pos int) {
	for i := 0; i < len(orders); i++ {
		if orders[i].time <= lastShipTime && orders[i].dst == pos {
			orders[i].end = now
		}
	}
}

func solver() {
	// exPlan is plan path all place
	// exDist has number of need for go to next
	// exIndex is now place
	// exSub is number of moved on the edge
	var rawPlan []int
	var exPlan []int
	var exDist []int
	var exIndex int
	var exSub int
	var lastShipTime int
	input()
	preprocessing()
	ans := make([]int, T)
	for i := 0; i < T; i++ {
		if len(exPlan) == 0 || exIndex == len(exPlan) {
			exPlan = make([]int, 0)
			exDist = make([]int, 0)
			rawPlan, exPlan, exDist = makePlan(i)
			//　有効なOrderがないとき
			if exPlan == nil {
				ans[i] = -1
				continue
			}
			exIndex = 1
			exSub = exDist[exIndex]
		}
		// log.Println(i, exIndex, exPlan[exIndex], exSub)
		ans[i] = exPlan[exIndex]
		exSub--
		if exSub == 0 {
			checkOrder(i, lastShipTime, exPlan[exIndex])
			if exPlan[exIndex] == 1 {
				lastShipTime = i
			}
			exIndex++
			if exIndex < len(exPlan) {
				for exDist[exIndex] == 0 {
					exIndex++
				}
				exSub = exDist[exIndex]
			}
		}
		// log.Println(exIndex, exSub)
	}
	output(ans)
	// fmt.Println(ans)
	log.Println(Score(orders))
	log.Println(dist[77][268])
	log.Println(path[77][268])
	log.Println(rawPlan)
	log.Println(exPlan)
	log.Println(exDist)
}
func input() {
	V = nextInt()
	E = nextInt()
	edges = make([]Edge, E)
	for i := 0; i < E; i++ {
		u := nextInt()
		v := nextInt()
		d := nextInt()
		edges[i].u, edges[i].v, edges[i].d = u, v, d
	}
	N := nextInt()
	for i := 0; i < N; i++ {
		n := nextInt()
		for j := 0; j < n; j++ {
			var o Order
			o.item_id = nextInt()
			o.dst = nextInt()
			o.time = i
			o.end = 0
			orders = append(orders, &o)
		}
	}
}

func output(ans []int) {
	for _, a := range ans {
		fmt.Println(a)
	}
}

func Score(ord []*Order) (score int) {
	for _, o := range ord {
		if o.end != 0 {
			score += T*T - (o.end-o.time)*(o.end-o.time)
		}
	}
	return score
}

// *************************************************************************

func makePlan(t int) (rawPlan []int, explan []int, exdist []int) {
	time := t
	actOrders := make([]*Order, 0)
	plan := make([]*Order, 0)
	for i := range orders {
		if orders[i].end == 0 && orders[i].time <= time {
			actOrders = append(actOrders, orders[i])
		}
	}
	// 注文の選択方法は後で再考する
	for i := range actOrders {
		plan = append(plan, actOrders[i])
		rawPlan = append(rawPlan, actOrders[i].dst)
		if i > 14 {
			break
		}
	}
	if len(plan) == 0 {
		return nil, nil, nil
	}
	explan = orderToPath(plan)
	exdist = make([]int, len(explan))
	// exdist[0] = dist[1][explan[0]]
	for i := 1; i < len(explan); i++ {
		exdist[i] = dist[explan[i-1]][explan[i]]
	}
	// log.Println(explan)
	// log.Println(exdist)
	return
}

// **************************************************************************
// preprocessing library 前処理用

var dist [401][401]int
var pred [401][401]int
var path [401][401][]int

func preprocessing() {
	wf()                       // construct dist, pred
	constructAllShortestPath() // construct path
}

// wfで全点間の距離を計算する
// predで経路を記録する
func wf() {
	for i := 0; i <= V; i++ {
		for j := 0; j <= V; j++ {
			pred[i][j] = -1
		}
	}
	for i := 0; i <= V; i++ {
		for j := 0; j <= V; j++ {
			if i == j {
				dist[i][j] = 0
			} else {
				dist[i][j] = MAX
			}
		}
	}
	for _, e := range edges {
		u := e.u
		v := e.v
		d := e.d
		dist[u][v] = d
		pred[u][v] = u
		// 有向グラフのとき以下を消す
		dist[v][u] = d
		pred[v][u] = v
	}
	for k := 1; k <= V; k++ {
		for i := 1; i <= V; i++ {
			if dist[i][k] == MAX {
				continue
			}
			for j := 1; j <= V; j++ {
				if dist[k][j] == MAX {
					continue
				}
				if dist[i][j] > dist[i][k]+dist[k][j] {
					dist[i][j] = dist[i][k] + dist[k][j]
					pred[i][j] = pred[k][j]
				}
			}
		}
	}
	log.Println("wf end")
}

// constructShortestPath is return path to t for s
// call from constructAllShortestPath
func constructShortestPath(s, t int) (path []int) {
	path = make([]int, 0, 100)
	if t < 0 || t > V || s < 0 || s > V || s == t {
		return
	}
	if dist[s][t] == MAX {
		return []int{-1}
	}
	for s != t {
		t = pred[s][t]
		if t == -1 {
			panic(fmt.Sprintf("pred[%d][%d]=%d", s, t, pred[s][t]))
		}
		if s == t {
			break
		}
		path = append(path, t)
	}
	// 逆順に構築したのでリバースする
	for i := 0; i < len(path)/2; i++ {
		path[i], path[len(path)-i-1] = path[len(path)-i-1], path[i]
	}
	return
}

// constructAllShortestPath is 全点間の経路を構築する
func constructAllShortestPath() {
	for i := 1; i <= V; i++ {
		for j := 1; j <= V; j++ {
			path[i][j] = constructShortestPath(i, j)
		}
	}
	log.Println("constructAllShortestPath end")
}

// extendPath is return all pathing path
// 頂点の経路から途中の経路を生成する
// [a, d, f] -> [a,b,c,d,e,f]
func extendPath(p []int) (exp []int) {
	if len(p) <= 1 {
		log.Println("return no path")
		return
	}
	exp = make([]int, 1)
	exp[0] = p[0]
	for i := 1; i < len(p); i++ {
		s := p[i-1]
		e := p[i]
		exp = append(exp, path[s][e]...)
		exp = append(exp, e)
	}
	return
}

func orderToPath(ord []*Order) []int {
	var p []int
	p = append(p, 1) // スタート地点
	for _, o := range ord {
		p = append(p, o.dst)
	}
	p = append(p, 1) // スタートに戻る
	ep := extendPath(p)
	return ep
}

// ***********************************************************************
// common library

func nextString() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, err := strconv.Atoi(sc.Text())
	if err != nil {
		panic(err)
	}
	return i
}

var dy = []int{0, 1, 0, -1}
var dx = []int{1, 0, -1, 0}
var MAX = math.MaxInt64

func pow(a, b int) int {
	return int(math.Pow(float64(a), float64(b)))
}

func lcm(a, b int) int {
	return a * b / gcd(a, b)
}
func gcd(a, b int) int {
	if a < b {
		a, b = b, a
	}
	for b != 0 {
		a, b = b, a%b
	}
	return a
}
func max(a ...int) int {
	r := a[0]
	for i := 0; i < len(a); i++ {
		if r < a[i] {
			r = a[i]
		}
	}
	return r
}
func min(a ...int) int {
	r := a[0]
	for i := 0; i < len(a); i++ {
		if r > a[i] {
			r = a[i]
		}
	}
	return r
}
func sum(a []int) (r int) {
	for i := range a {
		r += a[i]
	}
	return r
}
func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}


Measured memory_bytes: 157136