#include <bits/stdc++.h>
using namespace std;
const int N = 1005, M = 2520, Q = 100005;
int n, q, vis[N][M], ans[Q], kk[N], m[N];
stack<pair<int, int>> stk;
vector<int> e[N];
void dfs(int pos, int c, int num) {
  while (1) {
    if (vis[pos][c]) {
      if (vis[pos][c] != num) {
        ans[num] = ans[vis[pos][c]];
        return;
      }
      pair<int, int> tmp = make_pair(pos, c);
      set<int> st;
      st.insert(pos);
      while (stk.top() != tmp) {
        st.insert(stk.top().first);
        stk.pop();
      }
      ans[num] = st.size();
      return;
    }
    vis[pos][c] = num;
    stk.push(make_pair(pos, c));
    int nc = ((c + kk[pos]) % M + M) % M;
    pos = e[pos][nc % m[pos]];
    c = nc;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> kk[i];
  for (int i = 1; i <= n; i++) {
    cin >> m[i];
    e[i].resize(m[i]);
    for (auto &j : e[i]) cin >> j;
  }
  cin >> q;
  int x, y;
  for (int i = 1; i <= q; i++) {
    while (!stk.empty()) stk.pop();
    cin >> x >> y;
    dfs(x, (y % M + M) % M, i);
    cout << ans[i] << endl;
  }
  return 0;
}
