#include <bits/stdc++.h>
using namespace std;
long long v[1005];
vector<long long> AE[1005];
long long flg[1005][3005];
long long csz[1005][3005];
bool vis[1005][3005];
const long long MOD = 2520;
inline long long mod(long long a) { return (a % MOD + MOD) % MOD; }
stack<pair<long long, long long> > prod;
long long dfs(long long p, long long k) {
  k = mod(k + v[p]);
  if (flg[p][k]) {
    vector<long long> cnt;
    cnt.emplace_back(p);
    while (prod.top() != pair<long long, long long>{p, k})
      cnt.emplace_back(prod.top().first), prod.pop();
    sort(cnt.begin(), cnt.end()),
        cnt.erase(unique(cnt.begin(), cnt.end()), cnt.end());
    return cnt.size();
  }
  if (vis[p][k]) return csz[p][k];
  flg[p][k] = 1, vis[p][k] = 1, prod.push({p, k});
  long long x = AE[p][k % AE[p].size()];
  csz[p][k] = dfs(x, k), flg[p][k] = 0;
  return csz[p][k];
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) cin >> v[i];
  for (long long i = 0; i < n; i++) {
    long long xn;
    cin >> xn;
    for (long long j = 0, x; j < xn; j++) cin >> x, --x, AE[i].push_back(x);
  }
  long long q;
  cin >> q;
  while (q--) {
    long long x, y;
    cin >> x >> y, --x;
    cout << dfs(x, y) << '\n';
  }
}
