#include <bits/stdc++.h>
using namespace std;
int n, m[1100], cnt, a[2520 * 1100], flag[2520 * 1100], flag2[2520 * 1100],
    flag3[2520 * 1100], k[2520 * 1100], size, sum, e[1100][20];
int f1, f2, t1, t2, t, q, mod, q1[2520 * 1100], last, flag4[1100];
struct node {
  int v;
  node *next;
} pool[2 * 2520 * 1100], *h[2520 * 1100];
void addedge(int u, int v) {
  node *p = &pool[++cnt];
  p->v = v;
  p->next = h[u];
  h[u] = p;
}
int id(int i, int j) { return (i - 1) * mod + j; }
int rid(int i) { return i / mod + 1; }
void find(int u) {
  flag2[u] = 1;
  flag4[rid(u)] = 1;
  int v = h[u]->v;
  if (flag2[v] == 1) return;
  find(v);
}
int dfs(int u) {
  q1[++last] = u;
  flag[u] = 1;
  int v = h[u]->v;
  if (flag[v] == 1 && flag3[v] == 0) {
    size = 0;
    find(v);
    while (last > 0) {
      if (flag4[rid(q1[last])]) {
        flag4[rid(q1[last])] = 0;
        size++;
      }
      last--;
    }
    return size;
  } else if (flag[v] == 1 && flag3[v] == 1) {
    while (last > 0) last--;
    return a[v];
  }
  return dfs(v);
}
void put(int u, int s) {
  a[u] = s;
  flag3[u] = 1;
  int v = h[u]->v;
  if (flag3[v] == 1)
    return;
  else
    put(v, s);
}
int main() {
  int u, v;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &k[i]);
  mod = 2520;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &m[i]);
    for (int j = 1; j <= m[i]; j++) scanf("%d", &e[i][j - 1]);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < mod; j++) {
      f1 = id(i, j);
      t1 = e[i][j % m[i]];
      t2 = j + k[t1];
      t2 %= mod;
      if (t2 < 0) t2 += mod;
      f2 = id(t1, t2);
      addedge(f1, f2);
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < mod; j++) {
      t = id(i, j);
      if (flag3[t] == 1) continue;
      int kk = dfs(t);
      put(t, kk);
    }
  }
  scanf("%d", &q);
  for (int i = 1; i <= q; i++) {
    scanf("%d%d", &u, &sum);
    sum += k[u];
    sum %= mod;
    if (sum < 0) sum += mod;
    printf("%d\n", a[id(u, sum)]);
  }
  return 0;
}
