#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T &x) {
  x = 0;
  int c = getchar(), f = 1;
  for (; !isdigit(c); c = getchar())
    if (c == 45) f = -1;
  for (; isdigit(c); c = getchar()) (x *= 10) += f * (c - '0');
}
int n, m, q, w[1000 + 5];
vector<int> G[1000 + 5];
stack<pair<int, int> > sta;
set<int> s;
int cyc[1000 + 5][2520 + 5];
bool mrk[1000 + 5][2520 + 5];
bool ins[1000 + 5][2520 + 5];
int DFS(int u, int c) {
  c = ((c + w[u]) % 2520 + 2520) % 2520;
  if (ins[u][c]) {
    s.insert(u);
    while (sta.top() != pair<int, int>(u, c))
      s.insert(sta.top().first), sta.pop();
    return s.size();
  }
  if (mrk[u][c]) return cyc[u][c];
  mrk[u][c] = true, ins[u][c] = true, sta.push(pair<int, int>(u, c));
  cyc[u][c] = DFS(G[u][c % G[u].size()], c), ins[u][c] = false;
  return cyc[u][c];
}
int main() {
  read(n);
  for (int i = 1; i <= n; i++) read(w[i]);
  for (int u = 1, v; u <= n; u++)
    for (read(m); m; m--) read(v), G[u].push_back(v);
  read(q);
  while (q--) {
    int u, c;
    read(u), read(c);
    printf("%d\n", DFS(u, c));
    while (!sta.empty()) sta.pop();
    s.clear();
  }
  return 0;
}
