#include <bits/stdc++.h>
using namespace std;
int nxt[3000005], ans[3000005], dis[3000005], b[3000005];
inline int id(int x, int y) { return x * 2520 + y; }
int a[1005], e[1005][20], m[1005];
int main(int argc, char** argv) {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] %= 2520;
    a[i] += 2520, a[i] %= 2520;
  }
  for (int i = 1; i <= n; i++) {
    cin >> m[i];
    for (int j = 0; j < m[i]; j++) cin >> e[i][j];
  }
  for (int i = 1; i <= n; i++)
    for (int j = 0; j < 2520; j++)
      nxt[id(i, j)] = id(e[i][(j + a[i]) % m[i]], (j + a[i]) % 2520);
  for (int i = id(1, 0); i <= id(n, 2519); i++) {
    int D = 1;
    if (!ans[i]) {
      int lst = 0;
      for (int k = i;; k = nxt[k]) {
        dis[k] = i;
        if (ans[k]) {
          lst = k;
          break;
        }
        if (dis[nxt[k]] == i) {
          lst = k;
          int X = nxt[k];
          for (int k = X;; k = nxt[k]) {
            if (!b[k / 2520]) ++ans[lst];
            b[k / 2520] = 1;
            if (k == lst) break;
          }
          break;
        }
      }
      for (int k = i;; k = nxt[k]) {
        b[k / 2520] = 0;
        ans[k] = ans[lst];
        if (k == lst) break;
      }
    }
  }
  int q;
  cin >> q;
  while (q--) {
    int a, b;
    cin >> a >> b;
    b = (b % 2520 + 2520) % 2520;
    cout << ans[id(a, b)] << "\n";
  }
  return 0;
}
