#include <bits/stdc++.h>
#pragma warning(disable : 4996 6031)
using namespace std;
const long long mod = 2520, mn = 1005;
struct ed {
  long long to, ne;
} e[mn * 2];
long long n, m, d[mn], dp[mn][mod], no, vis[mn][mod], f[mod];
vector<long long> mp[mn];
long long mo(long long x) {
  x %= mod;
  x += mod;
  x %= mod;
  return x;
}
stack<pair<long long, long long> > st;
long long dfs(long long x, long long v) {
  v = mo(v + d[x]);
  st.push(make_pair(x, v));
  if (dp[x][v]) {
    return dp[x][v];
  }
  if (vis[x][v] == no) {
    long long ret = 0;
    do {
      if (f[st.top().first] != no) {
        ret++;
      }
      f[st.top().first] = no;
      st.pop();
    } while (st.top() != make_pair(x, v));
    return dp[x][v] = ret;
  }
  vis[x][v] = no;
  return dp[x][v] = dfs(mp[x][v % mp[x].size()], v);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> d[i];
  }
  for (long long i = 1; i <= n; i++) {
    cin >> m;
    for (long long ii = 1, a; ii <= m; ii++) {
      cin >> a;
      mp[i].push_back(a);
    }
  }
  cin >> m;
  for (long long i = 1, x, v; i <= m; i++) {
    no++;
    cin >> x >> v;
    cout << dfs(x, v) << endl;
  }
  return 0;
}
