#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const long long INF = mod * mod;
const long double eps = 1e-8;
pair<int, int> nex[1000][2520];
vector<int> G[1000];
int len[1000];
int ans[1000][2520];
bool used[1000][2520];
bool subused[1000][2520];
void solve() {
  int n;
  cin >> n;
  vector<int> c(n);
  for (int i = 0; i < n; i++) {
    cin >> c[i];
    c[i] = (c[i] % 2520 + 2520) % 2520;
  }
  for (int i = 0; i < n; i++) {
    int m;
    cin >> m;
    len[i] = m;
    for (int j = 0; j < m; j++) {
      int to;
      cin >> to;
      to--;
      G[i].push_back(to);
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 2520; j++) {
      int id = j % len[i];
      int to = G[i][id];
      nex[i][j] = {to, (j + c[to]) % 2520};
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 2520; j++) {
      if (used[i][j]) continue;
      bool pass = false;
      vector<pair<int, int> > memo;
      pair<int, int> cur = {i, j};
      memo.push_back(cur);
      subused[i][j] = true;
      while (true) {
        pair<int, int> to = nex[cur.first][cur.second];
        int id = to.first, jd = to.second;
        if (used[id][jd]) {
          int mem = ans[id][jd];
          for (int k = 0; k < memo.size(); k++) {
            ans[memo[k].first][memo[k].second] = mem;
            used[memo[k].first][memo[k].second] = true;
          }
          pass = true;
          break;
        } else if (subused[id][jd]) {
          pair<int, int> las = to;
          int chk = 0;
          set<int> ids;
          for (int k = (int)memo.size() - 1; k >= 0; k--) {
            ids.insert(memo[k].first);
            if (las == memo[k]) {
              chk = k;
              break;
            }
          }
          int mem = ids.size();
          for (int k = 0; k < memo.size(); k++) {
            used[memo[k].first][memo[k].second] = true;
            ans[memo[k].first][memo[k].second] = mem;
          }
          break;
        } else {
          cur = to;
          subused[id][jd] = true;
          memo.push_back(cur);
        }
      }
      if (pass) continue;
    }
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int x, val;
    cin >> x >> val;
    x--;
    val += c[x];
    val = (val % 2520 + 2520) % 2520;
    cout << ans[x][val] << endl;
  }
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  solve();
  return 0;
}
