#include <bits/stdc++.h>
int inp() {
  char c = getchar();
  int neg = 1;
  while (c < '0' || c > '9') {
    if (c == '-') neg = -1;
    c = getchar();
  }
  int sum = 0;
  while (c >= '0' && c <= '9') {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  return sum * neg;
}
int k[1010];
int f[1010][3000];
int genx[1010][3000], geny[1010][3000];
int e[1010][15], c[1010];
bool ins[1010][3000], used[1010];
int gx, gy, cnt;
std::vector<int> vec;
void dfs0(int x, int y) {
  if (genx[x][y]) return;
  if (ins[x][y]) {
    gx = x;
    gy = y;
    used[x] = true;
    cnt = 1;
    vec.push_back(x);
    return;
  }
  ins[x][y] = true;
  dfs0(e[x][y % c[x] + 1], (y + k[e[x][y % c[x] + 1]] + 2520) % 2520);
  if (gx == x && gy == y) {
    f[x][y] = cnt;
    gx = gy = -1;
    cnt = 0;
    for (int i = 0; i < vec.size(); i++) used[vec[i]] = false;
    vec.clear();
  } else if (gx != -1) {
    if (!used[x]) {
      used[x] = true;
      cnt++;
      vec.push_back(x);
    }
  }
  genx[x][y] = gx;
  geny[x][y] = gy;
  ins[x][y] = false;
}
int dfs(int x, int y) {
  if (f[x][y]) return f[x][y];
  return f[x][y] =
             dfs(e[x][y % c[x] + 1], (y + k[e[x][y % c[x] + 1]] + 2520) % 2520);
}
int main() {
  int n = inp();
  for (int i = 1; i <= n; i++) k[i] = (inp() % 2520 + 2520) % 2520;
  for (int i = 1; i <= n; i++) {
    c[i] = inp();
    for (int j = 1; j <= c[i]; j++) e[i][j] = inp();
  }
  for (int i = 1; i <= n; i++)
    for (int j = 0; j < 2520; j++) dfs0(i, j);
  for (int i = 1; i <= n; i++)
    for (int j = 0; j < 2520; j++)
      if (genx[i][j] != -1) f[i][j] = f[genx[i][j]][geny[i][j]];
  int q = inp();
  while (q--) {
    int x = inp();
    int y = ((inp() + k[x]) % 2520 + 2520) % 2520;
    printf("%d\n", dfs(x, y));
  }
}
