← Home
package main

import (
	"bufio"
	"fmt"
	"os"
)

// LCM of 1 to 10 is 2520
const L = 2520

func main() {
	// Use buffered I/O for speed
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n int
	fmt.Fscan(reader, &n)

	k := make([]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &k[i])
	}

	m := make([]int, n)
	adj := make([][]int, n)
	for i := 0; i < n; i++ {
		fmt.Fscan(reader, &m[i])
		adj[i] = make([]int, m[i])
		for j := 0; j < m[i]; j++ {
			fmt.Fscan(reader, &adj[i][j])
			adj[i][j]-- // Convert to 0-based index
		}
	}

	// State is defined by (vertex_index * L + c % L)
	totalStates := n * L
	nextState := make([]int, totalStates)

	// Precompute the functional graph of states
	for u := 0; u < n; u++ {
		// Calculate effective k modulo L
		effK := k[u] % L
		if effK < 0 {
			effK += L
		}
		modM := m[u]
		baseState := u * L

		for r := 0; r < L; r++ {
			// Calculate new value modulo L
			val := r + effK
			if val >= L {
				val -= L
			}
			
			// Calculate outgoing edge index
			// The condition is x == c % m_i.
			// Since m_i divides L, (c % L) % m_i == c % m_i.
			// Here val is (c_old + k_u) % L.
			// The updated c is passed to the next vertex, and edge is chosen based on it.
			idx := val % modM
			v := adj[u][idx]
			
			// The next state corresponds to vertex v and value c (where c % L == val)
			nextState[baseState + r] = v*L + val
		}
	}

	ans := make([]int, totalStates)
	vis := make([]byte, totalStates) // 0: unvisited, 1: visiting, 2: visited
	onStack := make([]int, totalStates)
	for i := range onStack {
		onStack[i] = -1
	}

	// Buffers for DFS/Cycle detection
	path := make([]int, 0, 4096)
	seen := make([]bool, n)
	seenList := make([]int, 0, n)

	for i := 0; i < totalStates; i++ {
		if vis[i] != 0 {
			continue
		}

		curr := i
		path = path[:0]
		
		// Traverse until we hit a node that is visiting or visited
		for vis[curr] == 0 {
			vis[curr] = 1
			onStack[curr] = len(path)
			path = append(path, curr)
			curr = nextState[curr]
		}

		var result int
		if vis[curr] == 1 {
			// Cycle detected within the current path
			// The cycle starts from the first occurrence of 'curr' in the current path
			startIdx := onStack[curr]
			
			// Count distinct vertices in the cycle
			cnt := 0
			// Iterate over the cycle portion of the path
			for j := startIdx; j < len(path); j++ {
				node := path[j]
				u := node / L
				if !seen[u] {
					seen[u] = true
					seenList = append(seenList, u)
					cnt++
				}
			}
			result = cnt
			
			// Reset seen array for next usage
			for _, u := range seenList {
				seen[u] = false
			}
			seenList = seenList[:0]
		} else {
			// Merged into a previously visited component
			result = ans[curr]
		}

		// Assign the result to all nodes in the current path
		for _, node := range path {
			ans[node] = result
			vis[node] = 2
			onStack[node] = -1
		}
	}

	var q int
	fmt.Fscan(reader, &q)
	for i := 0; i < q; i++ {
		var x, y int
		fmt.Fscan(reader, &x, &y)
		u := x - 1
		r := y % L
		if r < 0 {
			r += L
		}
		// Output the answer for the starting state
		fmt.Fprintln(writer, ans[u*L+r])
	}
}