#P1200F. Graph Traveler
Graph Traveler
No submission language available for this problem.
Description
Gildong is experimenting with an interesting machine Graph Traveler. In Graph Traveler, there is a directed graph consisting of vertices numbered from to . The -th vertex has outgoing edges that are labeled as , , , , each representing the destination vertex of the edge. The graph can have multiple edges and self-loops. The -th vertex also has an integer written on itself.
A travel on this graph works as follows.
- Gildong chooses a vertex to start from, and an integer to start with. Set the variable to this integer.
- After arriving at the vertex , or when Gildong begins the travel at some vertex , add to .
- The next vertex is where is an integer satisfying . Go to the next vertex and go back to step 2.
It's obvious that a travel never ends, since the 2nd and the 3rd step will be repeated endlessly.
For example, assume that Gildong starts at vertex with , and , , , . Right after he starts at vertex , becomes . Since the only integer () where is , Gildong goes to vertex . After arriving at vertex again, becomes . The only integer satisfying the conditions is , so he goes to vertex , and so on.
Since Gildong is quite inquisitive, he's going to ask you queries. He wants to know how many distinct vertices will be visited infinitely many times, if he starts the travel from a certain vertex with a certain value of . Note that you should not count the vertices that will be visited only finite times.
The first line of the input contains an integer (), the number of vertices in the graph.
The second line contains integers. The -th integer is (), the integer written on the -th vertex.
Next lines describe the edges of each vertex. The -st line contains an integer (), the number of outgoing edges of the -th vertex. The -nd line contains integers , , , , each having an integer value between and , inclusive.
Next line contains an integer (), the number of queries Gildong wants to ask.
Next lines contains two integers and (, ) each, which mean that the start vertex is and the starting value of is .
For each query, print the number of distinct vertices that will be visited infinitely many times, if Gildong starts at vertex with starting integer .
Input
The first line of the input contains an integer (), the number of vertices in the graph.
The second line contains integers. The -th integer is (), the integer written on the -th vertex.
Next lines describe the edges of each vertex. The -st line contains an integer (), the number of outgoing edges of the -th vertex. The -nd line contains integers , , , , each having an integer value between and , inclusive.
Next line contains an integer (), the number of queries Gildong wants to ask.
Next lines contains two integers and (, ) each, which mean that the start vertex is and the starting value of is .
Output
For each query, print the number of distinct vertices that will be visited infinitely many times, if Gildong starts at vertex with starting integer .
Samples
Note
The first example can be shown like the following image:

Three integers are marked on -th vertex: , , and respectively. The outgoing edges are labeled with an integer representing the edge number of -th vertex.
The travel for each query works as follows. It is described as a sequence of phrases, each in the format "vertex ( after added)".
The second example is same as the first example, except that the vertices have non-zero values. Therefore the answers to the queries also differ from the first example.

The queries for the second example works as follows: