This page is still under construction.

Parts of this page are still being built. What you see may change.

Bus Riding

Time limit2sMemory limit1024 MB

Summary
Simulate people riding circular bus routes, tracking boarding order by smallest route number and each transfer's one-minute wait, to report the finish time and stop or 0 0.
Level

Hard8 of 10

Topics
Simulation, Implementation, Math, Array
Solved
No attempts yet

Problem

A city has nn bus stops, and kk circular bus routes pass through them. Each route is given as a list of the numbers of the stops it passes through, and the ii-th route passes through stops ai,1,ai,2,…,ai,lia_{i,1}, a_{i,2}, \ldots, a_{i,l_i} in that order. Exactly one bus runs on each route. At time 0 this bus is at stop ai,1a_{i,1}. The bus takes exactly one minute to reach the next stop on its route. The time the bus spends standing at a stop can be ignored. All routes are circular, meaning that one minute after leaving stop ai,lia_{i,l_i} the bus arrives at stop ai,1a_{i,1} and travels the route again.

Several people in this city decided to ride the buses. Each of them made a plan for their ride. The plan of the jj-th person consists of the stop bjb_j where the person starts riding and a sequence of numbers cj,1,cj,2,…,cj,mjc_{j,1}, c_{j,2}, \ldots, c_{j,m_j}. These numbers mean the following: at time 0 the person comes to stop bjb_j and waits for the nearest bus (if at that moment some bus is at stop bjb_j, the person boards it). On this bus they ride past cj,1c_{j,1} stops, then get off and wait for the next bus at the stop where they ended up. On that bus they ride past cj,2c_{j,2} stops, get off again, and wait for the next bus again. And so on. If at some moment several buses arrive at a stop at once, the person boards the bus with the smallest route number. When a person gets off a bus at some stop, they can leave that stop no earlier than one minute later.

For each person, determine how many minutes after the starting moment their ride ends and at which stop it ends.

Input

The input file first contains the number nn, then the number kk. Then kk lines follow, specifying the bus routes. Each line begins with the number lil_i, which gives the length of the route, followed by the list of stops the route passes through: ai,1,ai,2,…,ai,lia_{i,1}, a_{i,2}, \ldots, a_{i,l_i}. A route may pass through the same stop several times.

Then comes the number pp, the number of people, followed by pp lines specifying the people's plans. Each line contains first the number bjb_j, the starting stop number, and mjm_j, the number of numbers in the sequence. Then come the numbers cj,1,cj,2,…,cj,mjc_{j,1}, c_{j,2}, \ldots, c_{j,m_j}.

All numbers in the input file are positive integers not exceeding 50.

Output

For each person, output two numbers to the output file: the time in minutes when their ride ends, and the number of the stop where this happens. If the person cannot complete their plan (at some stop they will not wait for a bus), output two zeros for them.

Examples1

  1. Example 1

    Input
    6 4
    4  1 2 3 5
    2  3 4
    5  5 2 1 3 2
    2  4 3
    3
    1  4  1 2 3 4
    2  1  1
    6  3  1 2 3
    
    Expected output
    20 1
    2 3
    0 0