#include <bits/stdc++.h>
using namespace std;
const int MN = 1005, MC = 2600, inf = 1000000005, mod = 1000000007;
const long long INF = 1000000000000000005LL;
const int dod = 2520;
int M[MN], k[MN];
pair<int, int> gdzie[MN][MC];
int ile[MN][MC], byl[MN], used[MN][MC];
vector<int> hop[MN];
vector<pair<int, int> > cyc;
int nowy, xd;
void go(int x, int c) {
  used[x][c] = 1;
  cyc.push_back({x, c});
  int nx = gdzie[x][c].first, nc = gdzie[x][c].second;
  pair<int, int> cv = {nx, nc};
  if (used[nx][nc] == 1) {
    ++nowy;
    int pos = cyc.size() - 1, cans = 0;
    for (int j = pos; j >= 0; --j) {
      pair<int, int> cur = cyc[j];
      if (byl[cur.first] != nowy) ++cans;
      byl[cur.first] = nowy;
      if (cur == cv) break;
    }
    for (auto v : cyc) ile[v.first][v.second] = cans;
  } else if (used[nx][nc] == 2) {
    for (auto v : cyc) ile[v.first][v.second] = ile[nx][nc];
  } else {
    go(nx, nc);
  }
  cyc.pop_back();
  used[x][c] = 2;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d", &k[i]);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &M[i]);
    for (int j = 0; j < M[i]; ++j) {
      int v;
      scanf("%d", &v);
      hop[i].push_back(v);
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int c = 0; c < dod; ++c) {
      int cc = (c + k[i]) % dod;
      if (cc < 0) cc += dod;
      gdzie[i][c] = {hop[i][cc % M[i]], cc};
    }
  }
  for (int i = 1; i <= n; ++i)
    for (int c = 0; c < dod; ++c)
      if (!used[i][c]) go(i, c);
  int q;
  scanf("%d", &q);
  while (q--) {
    int x, y;
    scanf("%d%d", &x, &y);
    y %= dod;
    if (y < 0) y += dod;
    printf("%d\n", ile[x][y]);
  }
}
