#include <bits/stdc++.h>
using namespace std;
const int p = 2520;
int n, m, k[1010], q;
vector<vector<int> > G;
int f[1010 * 2560];
int gao(int x, int y) {
  int now = x * p + y;
  if (f[now] != -1) {
    return f[now];
  }
  set<int> se;
  stack<int> st;
  se.insert(now);
  st.push(now);
  while (true) {
    x = now / p, y = now % p;
    int ny = ((y + k[x]) % p + p) % p;
    int nx = G[x][ny % G[x].size()];
    int nxt = nx * p + ny;
    if (f[nxt] != -1) {
      while (!st.empty()) {
        f[st.top()] = f[nxt];
        st.pop();
      }
      return f[nxt];
    }
    if (se.find(nxt) != se.end()) {
      set<int> tmp;
      while (true) {
        int Top = st.top();
        tmp.insert(Top / p);
        st.pop();
        if (Top == nxt) {
          break;
        }
      }
      for (auto it : se) {
        f[it] = tmp.size();
      }
      return tmp.size();
    }
    now = nxt;
    st.push(now);
    se.insert(now);
  }
  return 0;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
  while (cin >> n) {
    G.clear();
    G.resize(n + 1);
    for (int i = 1; i <= n; ++i) {
      cin >> k[i];
    }
    for (int i = 1; i <= n; ++i) {
      cin >> m;
      G[i].resize(m);
      for (auto &it : G[i]) {
        cin >> it;
      }
    }
    memset(f, -1, sizeof f);
    cin >> q;
    while (q--) {
      int x, y;
      cin >> x >> y;
      y = (y % p + p) % p;
      cout << gao(x, y) << "\n";
    }
  }
  return 0;
}
