#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2521, MOD = 2520;
long long dp[1010][MAXN], vis[1010][MAXN], v[1010], n;
pair<int, int> nxt[1010][MAXN];
int did[1010];
void go(int x, int y) {
  stack<pair<int, int> > st;
  while (1) {
    st.push({x, y});
    vis[x][y] = 1;
    int nx = nxt[x][y].first, ny = nxt[x][y].second;
    if (dp[nx][ny] >= 0) {
      while (int(st.size())) {
        dp[st.top().first][st.top().second] = dp[nx][ny];
        st.pop();
      }
      break;
    }
    if (vis[nx][ny]) {
      vector<pair<int, int> > all;
      int ans = 0, fl = 0;
      while (int(st.size())) {
        int ax = st.top().first, ay = st.top().second;
        all.push_back({ax, ay});
        if (!fl) ans += !did[ax], did[ax] = 1;
        if (nx == ax && ny == ay) fl = 1;
        st.pop();
      }
      for (auto x : all) dp[x.first][x.second] = ans, did[x.first] = 0;
      break;
    }
    x = nx;
    y = ny;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  memset(dp, -1, sizeof(dp));
  cin >> n;
  for (int i = 0, ThxDem = n; i < ThxDem; ++i)
    cin >> v[i], v[i] %= MOD, v[i] += MOD, v[i] %= MOD;
  for (int i = 0, ThxDem = n; i < ThxDem; ++i) {
    int am;
    cin >> am;
    vector<int> xx(am);
    for (int j = 0, ThxDem = am; j < ThxDem; ++j) cin >> xx[j], xx[j]--;
    for (int j = 0, ThxDem = MAXN; j < ThxDem; ++j) {
      int wh = xx[j % am];
      int vv = j + v[wh];
      if (vv >= MOD) vv -= MOD;
      nxt[i][j] = {wh, vv};
    }
  }
  for (int i = 0, ThxDem = n; i < ThxDem; ++i)
    for (int j = 0, ThxDem = MOD; j < ThxDem; ++j)
      if (dp[i][j] < 0) go(i, j);
  int q;
  cin >> q;
  while (q--) {
    int x, c;
    cin >> x >> c;
    x--;
    c += v[x];
    c %= MOD;
    c += MOD;
    c %= MOD;
    cout << dp[x][c] << "\n";
  }
}
