#include <bits/stdc++.h>
using namespace std;
const long long base = 2520;
long long visit[(long long)(1005)][base + 100];
long long check[(long long)(1005)], k[(long long)(1005)], n;
vector<long long> m[(long long)(1005)];
long long mo(long long a) { return (a % base + base) % base; }
signed main() {
  scanf("%lld", &n);
  for (long long i = 1; i <= n; i++) scanf("%lld", &k[i]);
  for (long long i = 1; i <= n; i++) {
    long long num;
    scanf("%lld", &num);
    long long tmp;
    for (long long j = 1; j <= num; j++) {
      scanf("%lld", &tmp);
      m[i].push_back(tmp);
    }
  }
  long long q;
  scanf("%lld", &q);
  for (long long i = 1; i <= q; i++) {
    long long x, c;
    scanf("%lld%lld", &x, &c);
    if (visit[x][mo(c)]) {
      printf("%lld\n", visit[x][mo(c)]);
      continue;
    }
    long long fx, fc;
    long long cnt = 0, times = 0;
    long long bk = x, bc = mo(c);
    c = mo(c);
    fx = bk;
    fc = bc;
    while (visit[x][c] == 0) {
      visit[x][c] = -10;
      times++;
      c += k[x];
      c = mo(c);
      long long to = (c % m[x].size() + m[x].size()) % m[x].size();
      x = m[x][to];
    }
    if (visit[x][c] != -10) {
      long long num = visit[x][c];
      x = fx, c = fc;
      while (visit[x][c] == -10) {
        visit[x][c] = num;
        c += k[x];
        c = mo(c);
        long long to = (c % m[x].size() + m[x].size()) % m[x].size();
        x = m[x][to];
      }
      printf("%lld\n", num);
      continue;
    }
    bk = x, bc = c;
    bool flag = 1;
    while ((bk != x || bc != c) || (flag)) {
      if (flag) flag = 0;
      if (check[x] != i) check[x] = i, cnt++;
      c += k[x];
      c = mo(c);
      long long to = (c % m[x].size() + m[x].size()) % m[x].size();
      x = m[x][to];
    }
    long long mem = 0;
    x = fx, c = fc;
    while (visit[x][c] == -10) {
      visit[x][c] = cnt;
      mem++;
      c += k[x];
      c = mo(c);
      long long to = (c % m[x].size() + m[x].size()) % m[x].size();
      x = m[x][to];
    }
    printf("%lld\n", cnt);
  }
  return 0;
}
