#include <bits/stdc++.h>
const int GCD = 2520;
const int MAX_N = 1000;
int k[MAX_N];
int m[MAX_N];
int e[MAX_N][10];
int next[MAX_N * GCD];
bool visited[MAX_N * GCD];
int compCnx[MAX_N * GCD];
int compCnxCount = 0;
int compUnique[MAX_N * GCD];
bool visitedCycle[MAX_N];
void discoverCycle(int v, int cnx) {
  visitedCycle[v / GCD] = true;
  compUnique[cnx] = 1;
  int u = next[v];
  while (u != v) {
    if (!visitedCycle[u / GCD]) {
      compUnique[cnx]++;
      visitedCycle[u / GCD] = true;
    }
    u = next[u];
  }
  visitedCycle[v / GCD] = false;
  u = next[v];
  while (u != v) {
    visitedCycle[u / GCD] = false;
    u = next[u];
  }
}
void dag(int v) {
  if (visited[next[v]]) {
    if (compCnx[next[v]] == 0) {
      compCnxCount++;
      compCnx[v] = compCnxCount;
      discoverCycle(next[v], compCnxCount);
    } else {
      compCnx[v] = compCnx[next[v]];
    }
  } else {
    visited[v] = true;
    dag(next[v]);
    compCnx[v] = compCnx[next[v]];
  }
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &k[i]);
    k[i] %= GCD;
    k[i] += GCD;
    k[i] %= GCD;
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", &m[i]);
    for (int j = 0; j < m[i]; j++) {
      scanf("%d", &e[i][j]);
      e[i][j]--;
    }
  }
  for (int c = 0; c < GCD; c++) {
    for (int i = 0; i < n; i++) {
      int v = i * GCD + c;
      int nextC = (c + k[i]) % GCD;
      int nextI = e[i][nextC % m[i]];
      next[v] = nextI * GCD + nextC;
    }
  }
  for (int c = 0; c < GCD; c++) {
    for (int i = 0; i < n; i++) {
      int v = i * GCD + c;
      if (!visited[v]) {
        dag(v);
      }
    }
  }
  int q;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    x--;
    y %= GCD;
    y += GCD;
    y %= GCD;
    int v = x * GCD + y;
    int answer = compUnique[compCnx[v]];
    printf("%d\n", answer);
  }
  return 0;
}
