#include <bits/stdc++.h>
using namespace std;
const int N = 1005 * 2520;
int now, vis[N], flag[N], n, Stack[N], cnt, from[N], k[N], m[N], to[1005][10],
    Ans[N], Q, x, y, nxt[N];
vector<int> G[N], v[N];
int main() {
  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++) scanf("%d", &to[i][j]);
  }
  for (int i = 1; i <= n; i++)
    for (int j = 0; j < 2520; j++) {
      int c = ((j + k[i]) % 2520 + 2520) % 2520;
      nxt[(i - 1) * 2520 + j] = (to[i][c % m[i]] - 1) * 2520 + c;
    }
  memset(flag, -1, sizeof flag);
  for (int i = 0; i < n * 2520; i++)
    if (vis[i] != -1) {
      int tot = 0;
      int j = i;
      while (!vis[j]) {
        Stack[++tot] = j;
        vis[j] = tot;
        j = nxt[j];
      }
      if (vis[j] == -1) {
        for (int k = 1; k <= tot; k++)
          Ans[Stack[k]] = Ans[j], vis[Stack[k]] = -1;
        continue;
      }
      int len = 0;
      for (int k = vis[j]; k <= tot; k++)
        len += flag[Stack[k] / 2520] != i, flag[Stack[k] / 2520] = i;
      for (int k = 1; k <= tot; k++) Ans[Stack[k]] = len, vis[Stack[k]] = -1;
    }
  scanf("%d", &Q);
  while (Q--) {
    scanf("%d%d", &x, &y);
    printf("%d\n", Ans[(x - 1) * 2520 + (y % 2520 + 2520) % 2520]);
  }
}
