#include <bits/stdc++.h>
using namespace std;
const int M = 2520;
int e[1010][10], m[1010];
int n, k[1010];
int snod[1010 * M], cnt;
bool vis[1010][M + 10];
bool ins[1010][M + 10];
void dfs(int u, int c) {
  if (ins[u][c]) {
    snod[++cnt] = u * M + c;
    return;
  }
  if (vis[u][c]) return;
  ins[u][c] = true;
  vis[u][c] = true;
  int cc = ((c + k[u]) % M + M) % M;
  dfs(e[u][cc % m[u]], cc);
  ins[u][c] = false;
  return;
}
int val[1010][M + 10];
int tmp[1010];
void getval(int st) {
  int u = st / M, c = st % M;
  int curu = u, curc = c;
  int res = 0;
  do {
    if (!tmp[curu]) res++;
    tmp[curu] = 1;
    curc = ((curc + k[curu]) % M + M) % M;
    curu = e[curu][curc % m[curu]];
  } while (curu != u || curc != c);
  do {
    val[curu][curc] = res;
    tmp[curu] = 0;
    curc = ((curc + k[curu]) % M + M) % M;
    curu = e[curu][curc % m[curu]];
  } while (curu != u || curc != c);
  return;
}
void dfs2(int u, int c) {
  if (val[u][c]) return;
  int tc = ((c + k[u]) % M + M) % M;
  dfs2(e[u][tc % m[u]], tc);
  val[u][c] = val[e[u][tc % m[u]]][tc];
  return;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &k[i]);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &m[i]);
    for (int j = 0; j < m[i]; j++) scanf("%d", &e[i][j]);
  }
  for (int i = 1; i <= n; i++)
    for (int j = 0; j < M; j++) dfs(i, j);
  for (int i = 1; i <= cnt; i++) getval(snod[i]);
  for (int i = 1; i <= n; i++)
    for (int j = 0; j < M; j++) dfs2(i, j);
  int q;
  scanf("%d", &q);
  while (q--) {
    int x, y;
    scanf("%d %d", &x, &y);
    y = (y % M + M) % M;
    printf("%d\n", val[x][y]);
  }
  return 0;
}
