#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int N = 2000 + 10, M = 2520;
int k[N], m[N], e[N][10], p[N * M], in[N * M], vis[N * M], f[N * M];
vector<int> g[N * M];
int id(int i, int j) { return (i - 1) * M + j; }
vector<int> get(int i) {
  queue<int> q;
  vector<int> res;
  q.push(i), vis[i] = 1, res.push_back(i);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    if (vis[p[u]]) continue;
    q.push(p[u]), vis[p[u]] = 1, res.push_back(p[u]);
  }
  return res;
}
void dfs(int u) {
  for (int v : g[u]) {
    if (f[v]) continue;
    f[v] = f[u], dfs(v);
  }
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &k[i]), k[i] = (k[i] % M + M) % M;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &m[i]);
    for (int j = 0; j < m[i]; j++) scanf("%d", &e[i][j]);
    for (int j = 0; j < M; j++) {
      p[id(i, j)] = id(e[i][(j + k[i]) % m[i]], (j + k[i]) % M);
      in[p[id(i, j)]]++;
      g[p[id(i, j)]].push_back(id(i, j));
    }
  }
  int tot = n * M;
  queue<int> q;
  for (int i = 0; i < tot; i++) {
    if (in[i] == 0) q.push(i);
  }
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    in[p[u]]--;
    if (in[p[u]] == 0) q.push(p[u]);
  }
  for (int i = 0; i < tot; i++) {
    if (in[i] && !f[i]) {
      vector<int> v = get(i);
      set<int> s;
      for (int j : v) s.insert(j / M);
      for (int j : v) f[j] = s.size();
      for (int j : v) dfs(j);
    }
  }
  int Q;
  scanf("%d", &Q);
  while (Q--) {
    int x, y;
    scanf("%d%d", &x, &y);
    y = (y % M + M) % M;
    printf("%d\n", f[id(x, y)]);
  }
}
