Imports System.Collections.Generic

Module Module1

    Public MapOs As Grph

    Sub Main()

        Dim t As String = Console.ReadLine()
        Dim tmp As String() = Split(t, " ")
        Dim V As Integer = CInt(tmp(0))
        Dim E As Integer = CInt(tmp(1))
        Dim Map As New List(Of Grph)

        For i As Integer = 0 To E - 1
            Map.Add(New Grph(Console.ReadLine()))
        Next

        Dim Tmax As Integer = CInt(Console.ReadLine())
        Dim Odr As New List(Of Haccyu)

        For i As Integer = 0 To Tmax - 1

            Dim j As Integer = CInt(Console.ReadLine())
            For k As Integer = j To 1 Step -1
                Odr.Add(New Haccyu(i, j, k, Console.ReadLine()))
            Next
        Next

        MapOs = Map.Find(AddressOf OneStepMap)
        If IsNothing(MapOs) Then
            For i As Integer = 0 To Tmax - 1
                Console.WriteLine(-1)
            Next
            Exit Sub
        End If
        Dim OdrOs As Haccyu
        OdrOs = Odr.Find(AddressOf OneStepOdr)
        If IsNothing(OdrOs) Then
            For i As Integer = 0 To Tmax - 1
                Console.WriteLine(-1)
            Next
            Exit Sub
        End If

        Dim ti As Integer = 0
        ti = OdrOs.Timing
        Dim total As Integer = 0
        ' 休め
        For i As Integer = 1 To ti
            Console.WriteLine(-1)
            total += 1
            If total >= Tmax Then Exit Sub
        Next

        '' 進め
        'For i As Integer = 1 To MapOs.Distance
        '    Console.WriteLine(MapOs.EndPoint)
        '    total += 1
        '    If total >= Tmax Then Exit Sub
        'Next
        '' 止まれ
        'For i As Integer = MapOs.Distance To Tmax - 1
        '    Console.WriteLine(-1)
        '    total += 1
        '    If total >= Tmax Then Exit Sub
        'Next

        ' 時間内往復
        Do
            For i As Integer = 0 To MapOs.Distance - 1
                Console.WriteLine(MapOs.EndPoint)
                ti += 1
                If ti >= Tmax Then Exit Sub
            Next
            For i As Integer = 0 To MapOs.Distance - 1
                Console.WriteLine(MapOs.StartPoint)
                ti += 1
                If ti >= Tmax Then Exit Sub
            Next
        Loop

    End Sub

    Private Function OneStepOdr(ByVal o As Haccyu) As Boolean

        If o.Choten = MapOs.EndPoint Then
            Return True
        Else
            Return False
        End If

    End Function

    Private Function OneStepMap(ByVal m As Grph) As Boolean

        If m.StartPoint = 1 Then
            Return True
        Else
            Return False
        End If

    End Function

    Class Grph

        Private _StartPoint As Integer
        Private _EndPoint As Integer
        Private _Distance As Integer

        Public Property StartPoint() As Integer
            Get
                Return _StartPoint
            End Get
            Set(value As Integer)
                _EndPoint = value
            End Set
        End Property
        Public Property EndPoint() As Integer
            Get
                Return _EndPoint
            End Get
            Set(value As Integer)
                _EndPoint = value
            End Set
        End Property
        Public Property Distance() As Integer
            Get
                Return _Distance
            End Get
            Set(value As Integer)
                _Distance = value
            End Set
        End Property
        Sub New(inText As String)

            Dim tmp As String()

            tmp = Split(inText, " ")
            _StartPoint = CInt(tmp(0))
            _EndPoint = CInt(tmp(1))
            _Distance = CInt(tmp(2))

        End Sub

    End Class

    Class Haccyu
        Private _Timing As Integer
        Private _Hacyusu As Integer
        Private _Banme As Integer
        Private _OrderID As Integer
        Private _Choten As Integer

        Public Property Timing() As Integer
            Get
                Return _Timing
            End Get
            Set(value As Integer)
                _Timing = value
            End Set
        End Property
        Public Property Choten() As Integer
            Get
                Return _Choten
            End Get
            Set(value As Integer)
                _Choten = value
            End Set
        End Property
        Sub New(Timing As Integer, Hacyusu As Integer, Banme As Integer, Odr As String)

            Dim tmp As String()

            tmp = Split(Odr, " ")
            _Timing = Timing
            _Hacyusu = Hacyusu
            _OrderID = CInt(tmp(0))
            _Choten = CInt(tmp(1))

        End Sub
    End Class

End Module

