#include <bits/stdc++.h>
using namespace std;
const int N = 1001, M = 2521;
vector<int> E[N];
int k[N], ans[N][M], n;
bool vis[N][M];
pair<int, int> pre[N][M];
int dfs(int u, int val) {
  if (ans[u][val]) return ans[u][val];
  vis[u][val] = 1;
  int nex = (val + k[u]) % 2520;
  int v = E[u][nex % E[u].size()];
  if (ans[v][nex]) return ans[u][val] = ans[v][nex];
  if (!vis[v][nex]) {
    pre[v][nex] = make_pair(u, val);
    return ans[u][val] = dfs(v, nex);
  } else {
    set<int> s;
    pair<int, int> now = make_pair(u, val);
    while (now != make_pair(v, nex)) {
      s.insert(now.first);
      now = pre[now.first][now.second];
    }
    s.insert(now.first);
    return ans[u][val] = s.size();
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%d", &k[i]), k[i] = (k[i] % 2520 + 2520) % 2520;
  for (int i = 1; i <= n; i++) {
    int t;
    scanf("%d", &t);
    for (int j = 1; j <= t; j++) {
      int v;
      scanf("%d", &v);
      E[i].push_back(v);
    }
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int x, y;
    scanf("%d%d", &x, &y);
    printf("%d\n", dfs(x, (y % 2520 + 2520) % 2520));
  }
  return 0;
}
