#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  char i = getchar();
  long long f = 1, res = 0;
  while (i < '0' || i > '9') {
    if (i == '-') f = -1;
    i = getchar();
  }
  while (i >= '0' && i <= '9') {
    res = res * 10 + i - '0';
    i = getchar();
  }
  return f * res;
}
const int N = 1005;
const int mod = 2520;
int n;
int son[N][11], cnts[N], ans[N][2530], vis[N][2530], dep[N][2530];
int used[N], st[N * mod], w[N];
inline int dfs(int x, int y) {
  vis[x][y] = 1;
  if (ans[x][y]) return ans[x][y];
  st[dep[x][y]] = x;
  int vx = son[x][y % cnts[x]], vy = (y + w[vx]) % mod;
  if (vis[vx][vy]) {
    if (ans[vx][vy]) return ans[x][y] = ans[vx][vy];
    int l = dep[vx][vy], r = dep[x][y];
    memset(used, 0, sizeof used);
    for (register int i = l; i <= r; ++i) {
      if (!used[st[i]]) {
        used[st[i]] = 1;
        ans[x][y]++;
      }
    }
    return ans[x][y];
  }
  dep[vx][vy] = dep[x][y] + 1;
  ans[x][y] = dfs(vx, vy);
  return ans[x][y];
}
int main() {
  n = read();
  for (register int i = 1; i <= n; ++i) {
    w[i] = read();
    w[i] = (w[i] % mod + mod) % mod;
  }
  for (register int i = 1; i <= n; ++i) {
    cnts[i] = read();
    for (register int j = 0; j < cnts[i]; ++j) {
      son[i][j] = read();
    }
  }
  int q = read();
  while (q--) {
    int x = read(), c = read();
    c = (c % mod + mod) % mod;
    printf("%d\n", dfs(x, (c + w[x]) % mod));
  }
  return 0;
}
