#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
const long long maxn = 2520, mod = 2520;
long long tot, n, k[maxn], dp[maxn][mod], m[maxn], vis[maxn][mod], c, q, x, st,
    f[maxn];
vector<long long> vec[maxn];
pair<long long, long long> v[maxn * maxn];
long long dfs(long long x, long long c, long long tim) {
  c = (c % mod + k[x] % mod + mod) % mod;
  v[++tot] = {x, c};
  if (dp[x][c]) return dp[x][c];
  if (vis[x][c] == tim) {
    long long res = 0;
    do {
      if (f[v[tot].first] != tim) res++;
      f[v[tot].first] = tim;
    } while (v[--tot] != pair<long long, long long>{x, c});
    return dp[x][c] = res;
  }
  vis[x][c] = tim;
  return dp[x][c] = dfs(vec[x][c % m[x]], c, tim);
}
int main() {
  scanf("%lld", &n);
  for (long long i = 1; i <= n; i++) scanf("%lld", &k[i]);
  for (long long i = 1; i <= n; i++) {
    scanf("%lld", &m[i]);
    for (long long j = 1; j <= m[i]; j++) {
      scanf("%lld", &x);
      vec[i].push_back(x);
    }
  }
  scanf("%lld", &q);
  for (long long i = 1; i <= q; i++) {
    scanf("%lld%lld", &x, &c);
    printf("%lld\n", dfs(x, (c % mod + mod) % mod, i));
  }
  return 0;
}
