#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& a) {
  in >> a.first >> a.second;
  return in;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& out, pair<T1, T2> a) {
  out << a.first << " " << a.second;
  return out;
}
template <typename T, typename T1>
T amax(T& a, T1 b) {
  if (b > a) a = b;
  return a;
}
template <typename T, typename T1>
T amin(T& a, T1 b) {
  if (b < a) a = b;
  return a;
}
const long long INF = 1e18;
const int32_t M = 1e9 + 7;
const int32_t MM = 998244353;
const long long N = 1005;
long long k[N];
long long m[N];
long long ans[N][2 * 2520];
long long vis[N][2 * 2520];
vector<long long> e[N];
long long normalize(long long x, long long mod = 2520) {
  return ((x % mod) + mod) % mod;
}
long long used[N];
vector<long long> v;
long long dfs(long long x, long long y, long long tim) {
  if (ans[x][y] != -1) {
    return ans[x][y];
  }
  if (vis[x][y] != -1) {
    ans[x][y] = 0;
    for (long long i = vis[x][y]; i < tim; i++) {
      ans[x][y] += !used[v[i]];
      used[v[i]] = 1;
    }
    for (long long i = vis[x][y]; i < tim; i++) {
      used[v[i]] = 0;
    }
    if (ans[x][y] == -1) {
      exit(0);
    }
  }
  if (ans[x][y] == -1) {
    vis[x][y] = tim;
    v.push_back(x);
    long long c = normalize(y + k[x]);
    ans[x][y] = dfs(e[x][c % m[x]], c, tim + 1);
    v.pop_back();
    vis[x][y] = -1;
  }
  return ans[x][y];
}
void solve() {
  memset(ans, -1, sizeof(ans));
  memset(vis, -1, sizeof(vis));
  long long n;
  cin >> n;
  for (long long i = 1; i < n + 1; i++) {
    cin >> k[i];
  }
  for (long long i = 1; i < n + 1; i++) {
    cin >> m[i];
    for (long long j = 0; j < m[i]; j++) {
      long long x;
      cin >> x;
      e[i].push_back(x);
    }
  }
  long long q;
  cin >> q;
  while (q--) {
    long long x, y;
    cin >> x >> y;
    y = normalize(y);
    cout << dfs(x, y, 0) << "\n";
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) solve();
  return 0;
}
