#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("no-stack-protector")
using namespace std;
template <typename T>
void print(T a) {
  for (auto x : a) cout << x << ' ';
  cout << endl;
}
const long long lcm = 2520;
long long n;
vector<long long> go, used, dp;
unordered_set<long long> s;
long long current = 0;
long long stop_vert;
bool visited = true;
vector<long long> sorted;
void dfs1(long long v) {
  used[v] = 1;
  long long to = go[v];
  if (!used[to]) {
    dfs1(to);
    if (!visited) s.insert(v / lcm);
    if (v == stop_vert) visited = true;
    used[v] = 0;
    return;
  } else if (used[to] == 1) {
    s.insert(v / lcm);
    visited = false;
    stop_vert = to;
    if (v == stop_vert) visited = true;
    used[v] = 0;
    return;
  } else {
    current = dp[to];
    used[v] = 0;
    return;
  }
}
void dfs2(long long v) {
  used[v] = 1;
  long long to = go[v];
  if (!used[to]) {
    dfs2(to);
    if (current)
      dp[v] = current;
    else
      dp[v] = (long long)(s).size();
  } else {
    if (current)
      dp[v] = current;
    else
      dp[v] = (long long)(s).size();
  }
  used[v] = 2;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  go.resize(n * lcm);
  vector<long long> k(n);
  for (long long i = 0; i < n; i++) {
    cin >> k[i];
    k[i] %= lcm;
  }
  vector<vector<long long> > g(n);
  for (long long i = 0; i < n; i++) {
    long long m;
    cin >> m;
    for (long long j = 0; j < m; j++) {
      long long to;
      cin >> to;
      to--;
      g[i].push_back(to);
    }
  }
  go.resize(n * lcm);
  for (long long i = 0; i < n * lcm; i++) {
    long long v = i / lcm, c = i % lcm;
    c += k[v];
    c %= lcm;
    if (c < 0) c += lcm;
    long long m = (long long)(g[v]).size();
    long long to = g[v][c % m];
    go[i] = to * lcm + c;
  }
  used.assign(n * lcm, 0);
  dp.assign(n * lcm, 1);
  for (long long i = 0; i < n * lcm; i++) {
    if (!used[i]) {
      dfs1(i);
      dfs2(i);
      s.clear();
      current = 0;
    }
  }
  long long q;
  cin >> q;
  for (long long i = 0; i < q; i++) {
    long long v, c;
    cin >> v >> c;
    v--;
    c %= lcm;
    if (c < 0) c += lcm;
    cout << dp[v * lcm + c] << '\n';
  }
  return 0;
}
