#include <bits/stdc++.h>
using namespace std;
inline int _r() {
  int o = 0, f = 0;
  char t = getchar();
  while (t < 48 || t > 57) f |= (t == '-'), t = getchar();
  while (t > 47 && t < 58) o = o * 10 + t - 48, t = getchar();
  return f ? -o : o;
}
void setIO() {
  ios::sync_with_stdio(false);
  cin.tie(0);
}
int A[1005], C[1005];
int V[1005 * 2521], P[1005 * 2521], O[1005 * 2521], Vis[1005];
int B[1005][15];
int o, tim;
int _cal(int x, int y) { return (x - 1) * 2520 + y; }
pair<int, int> _cal(int x) {
  int t = x / 2520;
  return make_pair(t + 1, x - t * 2520);
}
int main() {
  int n, M = (1000000000 + 2520) / 2520 * 2520;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> A[i], A[i] = (A[i] + M) % 2520;
  for (int i = 1; i <= n; i++) {
    int m;
    cin >> m;
    C[i] = m;
    for (int j = 0; j < m; j++) cin >> B[i][j];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 2520; j++) {
      int p = 0;
      tim++;
      int u = i, c = j;
      while (!V[_cal(u, c)]) {
        P[++p] = _cal(u, c);
        V[_cal(u, c)] = tim;
        u = B[u][c % C[u]];
        c = (c + A[u]) % 2520;
      }
      if (V[_cal(u, c)] == tim) {
        int s = 1, k = _cal(u, c), cnt = 0;
        while (P[s] != k) s++;
        o++;
        for (k = s; k <= p; k++)
          if (Vis[_cal(P[k]).first] != o) cnt++, Vis[_cal(P[k]).first] = o;
        for (k = 1; k <= p; k++) O[P[k]] = cnt;
      } else {
        int ans = O[_cal(u, c)];
        for (int k = 1; k <= p; k++) O[P[k]] = ans;
      }
    }
  }
  int q, x, y;
  cin >> q;
  while (q--) {
    cin >> x >> y;
    cout << O[_cal(x, ((y + M) % 2520 + A[x] + 2520) % 2520)] << endl;
  }
}
