#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1010;
const int MAXK = 10;
const int MOD = 2520;
int n;
int k[MAXN];
int e[MAXN][MAXK];
int sz[MAXN];
int modaj(int x) {
  if (x < 0) {
    x += (-x / MOD * MOD);
  }
  while (x < 0) x += MOD;
  return x % MOD;
}
void load() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", k + i);
    k[i] = modaj(k[i]);
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", sz + i);
    for (int j = 0; j < sz[i]; j++) {
      scanf("%d", &e[i][j]);
      e[i][j]--;
    }
  }
}
int dp[MAXN][MOD];
set<int> cyc;
void calcLength(int p, int c) {
  c = modaj(c);
  if (dp[p][c] == -3) return;
  dp[p][c] = -3;
  cyc.insert(p);
  c = modaj(c + k[p]);
  int nxt = e[p][c % sz[p]];
  calcLength(nxt, c);
}
int rek(int p, int c) {
  c = modaj(c);
  int &ret = dp[p][c];
  if (ret >= 0) return ret;
  if (ret == -2) {
    cyc.clear();
    calcLength(p, c);
    return ret = cyc.size();
  }
  ret = -2;
  c = modaj(c + k[p]);
  int nxt = e[p][c % sz[p]];
  return ret = rek(nxt, c);
}
void solve() {
  int q;
  scanf("%d", &q);
  for (int i = 0; i < MAXN; i++)
    for (int j = 0; j < MOD; j++) dp[i][j] = -1;
  while (q--) {
    int p, c;
    scanf("%d%d", &p, &c);
    p--;
    printf("%d\n", rek(p, c));
  }
}
int main() {
  load();
  solve();
  return 0;
}
