#include <bits/stdc++.h>
using namespace std;
const int MOD = 2520;
int n;
vector<int> k;
vector<vector<int>> e;
vector<vector<int>> ans;
vector<vector<int>> was;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  k.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> k[i];
    k[i] %= MOD;
    if (k[i] < 0) {
      k[i] += MOD;
    }
  }
  e.resize(n);
  for (int i = 0, m; i < n; ++i) {
    cin >> m;
    e[i].resize(m);
    for (int j = 0; j < m; ++j) {
      cin >> e[i][j];
      --e[i][j];
    }
  }
  was = vector<vector<int>>(n, vector<int>(MOD));
  ans = vector<vector<int>>(n, vector<int>(MOD));
  for (int v = 0; v < n; ++v) {
    for (int c = 0; c < MOD; ++c) {
      int vv = v;
      int cc = c;
      while (!was[vv][cc]) {
        was[vv][cc] = 1;
        cc = (cc + k[vv]) % MOD;
        vv = e[vv][cc % e[vv].size()];
      }
      int anss;
      if (was[vv][cc] == 1) {
        bitset<1000> vert;
        while (was[vv][cc] != 2) {
          was[vv][cc] = 2;
          vert[vv] = 1;
          cc = (cc + k[vv]) % MOD;
          vv = e[vv][cc % e[vv].size()];
        }
        anss = vert.count();
      } else {
        anss = ans[vv][cc];
      }
      vv = v;
      cc = c;
      while (was[vv][cc] != 3) {
        was[vv][cc] = 3;
        ans[vv][cc] = anss;
        cc = (cc + k[vv]) % MOD;
        vv = e[vv][cc % e[vv].size()];
      }
    }
  }
  int q;
  cin >> q;
  for (int tc = 1; tc <= q; ++tc) {
    int x, y;
    cin >> x >> y;
    --x;
    y %= MOD;
    if (y < 0) {
      y += MOD;
    }
    cout << ans[x][y] << '\n';
  }
  return 0;
}
