#include <bits/stdc++.h>
inline int read() {
  char ch = getchar();
  int x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return x * f;
}
const int MAXN = 1010, MOD = 2520;
int n, k[MAXN], a[MAXN][3000], e[MAXN][11], cnt[MAXN];
bool vis[MAXN][3000];
std::pair<int, int> pre[MAXN][3000];
int dfs(int u, int val) {
  if (a[u][val]) return a[u][val];
  vis[u][val] = true;
  int vval = (val + k[u]) % MOD, v = e[u][vval % e[u][0] + 1];
  if (a[v][vval]) return a[u][val] = a[v][vval];
  if (!vis[v][vval]) {
    pre[v][vval] = std::make_pair(u, val);
    return a[u][val] = dfs(v, vval);
  }
  std::pair<int, int> tmp = std::make_pair(u, val);
  memset(cnt, 0, sizeof(cnt));
  while (tmp != std::make_pair(v, vval)) {
    if (!cnt[tmp.first]) {
      cnt[tmp.first] = true;
      ++a[u][val];
    }
    tmp = pre[tmp.first][tmp.second];
  }
  if (!cnt[tmp.first]) {
    cnt[tmp.first] = true;
    ++a[u][val];
  }
  return a[u][val];
}
int main() {
  n = read();
  for (int i = 1; i <= n; ++i) k[i] = (read() % MOD + MOD) % MOD;
  for (int i = 1; i <= n; ++i) {
    int m = read();
    for (int j = 1; j <= m; ++j) e[i][j] = read();
    e[i][0] = m;
  }
  int q = read();
  while (q--) {
    int x = read(), y = (read() % MOD + MOD) % MOD;
    printf("%d\n", dfs(x, y));
  }
  return 0;
}
