#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e4 + 5, MAXC = 2530, MOD = 2520;
int dp[MAXN][MAXC];
int n;
vector<int> Graph[MAXN];
int D[MAXN][MAXC];
int tme = 1;
int k[MAXN];
void dfs(int x, int y) {
  stack<pair<int, int> > s;
  if (y < 0) y = (y % MOD) + MOD;
  while (!D[x][y]) {
    D[x][y] = tme++;
    s.push(pair<int, int>(x, y));
    int b = (y + k[x]) % MOD;
    int m = Graph[x].size();
    if (b < 0) b += MOD;
    int a = b % m;
    a = Graph[x][a];
    x = a;
    y = b;
  }
  int answer;
  if (dp[x][y])
    answer = dp[x][y];
  else {
    set<int> temp;
    vector<pair<int, int> > t;
    while (s.top().first != x || s.top().second != y) {
      int cur = s.top().first;
      int ccur = s.top().second;
      t.push_back(s.top());
      s.pop();
      temp.insert(cur);
    }
    temp.insert(x);
    answer = temp.size();
    for (pair<int, int> x : t) dp[x.first][x.second] = answer;
  }
  while (!s.empty()) {
    int cur = s.top().first;
    int ccur = s.top().second;
    s.pop();
    dp[cur][ccur] = answer;
  }
}
void dfs() {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < MOD; j++)
      if (!D[i][j]) dfs(i, j);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) cin >> k[i];
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    for (int j = 0; j < x; j++) {
      int y;
      cin >> y;
      --y;
      Graph[i].push_back(y);
    }
  }
  dfs();
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    --a;
    b = b % MOD;
    if (b < 0) b += MOD;
    cout << dp[a][b] << '\n';
  }
  return 0;
}
