#P1200F. Graph Traveler

    ID: 2315 Type: RemoteJudge 2000ms 512MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>brute forcedata structuresdfs and similardpgraphsimplementationmathnumber theory*2300

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 nn vertices numbered from 11 to nn. The ii-th vertex has mim_i outgoing edges that are labeled as ei[0]e_i[0], ei[1]e_i[1], \ldots, ei[mi1]e_i[m_i-1], each representing the destination vertex of the edge. The graph can have multiple edges and self-loops. The ii-th vertex also has an integer kik_i written on itself.

A travel on this graph works as follows.

  1. Gildong chooses a vertex to start from, and an integer to start with. Set the variable cc to this integer.
  2. After arriving at the vertex ii, or when Gildong begins the travel at some vertex ii, add kik_i to cc.
  3. The next vertex is ei[x]e_i[x] where xx is an integer 0xmi10 \le x \le m_i-1 satisfying xc(modmi)x \equiv c \pmod {m_i}. 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 11 with c=5c = 5, and m1=2m_1 = 2, e1[0]=1e_1[0] = 1, e1[1]=2e_1[1] = 2, k1=3k_1 = -3. Right after he starts at vertex 11, cc becomes 22. Since the only integer xx (0x10 \le x \le 1) where xc(modmi)x \equiv c \pmod {m_i} is 00, Gildong goes to vertex e1[0]=1e_1[0] = 1. After arriving at vertex 11 again, cc becomes 1-1. The only integer xx satisfying the conditions is 11, so he goes to vertex e1[1]=2e_1[1] = 2, and so on.

Since Gildong is quite inquisitive, he's going to ask you qq 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 cc. Note that you should not count the vertices that will be visited only finite times.

The first line of the input contains an integer nn (1n10001 \le n \le 1000), the number of vertices in the graph.

The second line contains nn integers. The ii-th integer is kik_i (109ki109-10^9 \le k_i \le 10^9), the integer written on the ii-th vertex.

Next 2n2 \cdot n lines describe the edges of each vertex. The (2i+1)(2 \cdot i + 1)-st line contains an integer mim_i (1mi101 \le m_i \le 10), the number of outgoing edges of the ii-th vertex. The (2i+2)(2 \cdot i + 2)-nd line contains mim_i integers ei[0]e_i[0], ei[1]e_i[1], \ldots, ei[mi1]e_i[m_i-1], each having an integer value between 11 and nn, inclusive.

Next line contains an integer qq (1q1051 \le q \le 10^5), the number of queries Gildong wants to ask.

Next qq lines contains two integers xx and yy (1xn1 \le x \le n, 109y109-10^9 \le y \le 10^9) each, which mean that the start vertex is xx and the starting value of cc is yy.

For each query, print the number of distinct vertices that will be visited infinitely many times, if Gildong starts at vertex xx with starting integer yy.

Input

The first line of the input contains an integer nn (1n10001 \le n \le 1000), the number of vertices in the graph.

The second line contains nn integers. The ii-th integer is kik_i (109ki109-10^9 \le k_i \le 10^9), the integer written on the ii-th vertex.

Next 2n2 \cdot n lines describe the edges of each vertex. The (2i+1)(2 \cdot i + 1)-st line contains an integer mim_i (1mi101 \le m_i \le 10), the number of outgoing edges of the ii-th vertex. The (2i+2)(2 \cdot i + 2)-nd line contains mim_i integers ei[0]e_i[0], ei[1]e_i[1], \ldots, ei[mi1]e_i[m_i-1], each having an integer value between 11 and nn, inclusive.

Next line contains an integer qq (1q1051 \le q \le 10^5), the number of queries Gildong wants to ask.

Next qq lines contains two integers xx and yy (1xn1 \le x \le n, 109y109-10^9 \le y \le 10^9) each, which mean that the start vertex is xx and the starting value of cc is yy.

Output

For each query, print the number of distinct vertices that will be visited infinitely many times, if Gildong starts at vertex xx with starting integer yy.

Samples

Sample Input 1

4
0 0 0 0
2
2 3
1
2
3
2 4 1
4
3 1 2 1
6
1 0
2 0
3 -1
4 -2
1 1
1 5

Sample Output 1

1
1
2
1
3
2

Sample Input 2

4
4 -5 -3 -1
2
2 3
1
2
3
2 4 1
4
3 1 2 1
6
1 0
2 0
3 -1
4 -2
1 1
1 5

Sample Output 2

1
1
1
3
1
1

Note

The first example can be shown like the following image:

Three integers are marked on ii-th vertex: ii, kik_i, and mim_i respectively. The outgoing edges are labeled with an integer representing the edge number of ii-th vertex.

The travel for each query works as follows. It is described as a sequence of phrases, each in the format "vertex (cc after kik_i added)".

  • 1(0)2(0)2(0)1(0) \to 2(0) \to 2(0) \to \ldots
  • 2(0)2(0)2(0) \to 2(0) \to \ldots
  • 3(1)1(1)3(1)3(-1) \to 1(-1) \to 3(-1) \to \ldots
  • 4(2)2(2)2(2)4(-2) \to 2(-2) \to 2(-2) \to \ldots
  • 1(1)3(1)4(1)1(1)1(1) \to 3(1) \to 4(1) \to 1(1) \to \ldots
  • 1(5)3(5)1(5)1(5) \to 3(5) \to 1(5) \to \ldots

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:

  • 1(4)2(1)2(6)1(4) \to 2(-1) \to 2(-6) \to \ldots
  • 2(5)2(10)2(-5) \to 2(-10) \to \ldots
  • 3(4)1(0)2(5)2(10)3(-4) \to 1(0) \to 2(-5) \to 2(-10) \to \ldots
  • 4(3)1(1)3(2)4(3)4(-3) \to 1(1) \to 3(-2) \to 4(-3) \to \ldots
  • 1(5)3(2)1(6)2(1)2(4)1(5) \to 3(2) \to 1(6) \to 2(1) \to 2(-4) \to \ldots
  • 1(9)3(6)2(1)2(4)1(9) \to 3(6) \to 2(1) \to 2(-4) \to \ldots