#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 10;
const int M = 1e9 + 7;
const int inf = 1e9 + 7;
const long long INF = 1e18;
int n;
int a[N];
int d[N];
vector<int> b[N];
int vis[N][2550];
pair<int, int> his[N * 2550];
long long ans[N][2550];
int num[N];
int cur, top;
int solve(int x, int y) {
  if (ans[x][y]) return ans[x][y];
  ++cur;
  his[top = 1] = {x, y};
  vis[x][y] = cur;
  while (1) {
    x = b[x][y % d[x]];
    y += a[x];
    if (y < 0) {
      y = -y;
      y %= 2520;
      y = 2520 - y;
    } else {
      y %= 2520;
    }
    if (vis[x][y]) break;
    vis[x][y] = cur;
    his[++top] = {x, y};
  }
  if (vis[x][y] != cur) {
    for (int k = 1; k <= top; k++) {
      ans[his[k].first][his[k].second] = ans[x][y];
    }
  } else {
    int sz = 0;
    for (int k = top; k >= 1; k--) {
      if (num[his[k].first] != cur) {
        num[his[k].first] = cur;
        sz++;
      }
      if (his[k].first == x && his[k].second == y) break;
    }
    for (int k = 1; k <= top; k++) {
      ans[his[k].first][his[k].second] = sz;
    }
  }
  return ans[x][y];
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    cin >> d[i];
    b[i].resize(d[i]);
    for (int j = 0; j < d[i]; j++) {
      cin >> b[i][j];
    }
  }
  for (int i = 1; i <= n; i++)
    for (int j = 0; j < 2520; j++) {
      vis[i][j] = 0;
    }
  int q;
  cin >> q;
  int x, y;
  cur = 0;
  while (q--) {
    cin >> x >> y;
    y += a[x];
    if (y < 0) {
      y = -y;
      y %= 2520;
      y = 2520 - y;
    } else {
      y %= 2520;
    }
    cout << solve(x, y) << "\n";
  }
}
