#include <bits/stdc++.h>
using namespace std;
long long int INF = 3e18;
double Pi = 3.1415926535897932384626;
priority_queue<long long> pql;
priority_queue<pair<long long, long long> > pqp;
priority_queue<long long, vector<long long>, greater<long long> > pqls;
priority_queue<pair<long long, long long>, vector<pair<long long, long long> >,
               greater<pair<long long, long long> > >
    pqps;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, -1, 1};
char dir[] = "DRUL";
long long mod = 1000000007;
long long rui(long long number1, long long number2) {
  if (number2 == 0) {
    return 1;
  } else {
    long long number3 = rui(number1, number2 / 2);
    number3 *= number3;
    number3 %= mod;
    if (number2 % 2 == 1) {
      number3 *= number1;
      number3 %= mod;
    }
    return number3;
  }
}
long long gcd(long long number1, long long number2) {
  if (number1 > number2) {
    swap(number1, number2);
  }
  if (number1 == 0 || number1 == number2) {
    return number2;
  } else {
    return gcd(number2 % number1, number1);
  }
}
void YES(bool condition) {
  if (condition) {
    cout << "YES" << endl;
    ;
  } else {
    cout << "NO" << endl;
    ;
  }
  return;
}
void Yes(bool condition) {
  if (condition) {
    cout << "Yes" << endl;
    ;
  } else {
    cout << "No" << endl;
    ;
  }
  return;
}
long long n, num, sum, ans, a, b, c, d, e, g, h, w, i, j, q;
long long k[2005], m[15];
long long tests;
bool flag, dame;
long long go[4530000];
long long table[4530000];
long long ans_table[4530000];
long long ans_ref[4530000];
long long q_table[2005];
vector<long long> hukuro;
void dfs(long long from_v, long long q) {
  long long to_v = go[from_v];
  num++;
  if (n == 1000 && (num - 1) % 252000 == 0) {
  }
  if (ans_ref[to_v] != -1 && ans_ref[to_v] < q) {
    long long ans_num = ans_ref[to_v];
    ans = ans_table[ans_num];
    if (ans == -1 && n == 45) {
    }
    ans_table[q] = ans;
    return;
  }
  if (table[to_v] >= 10 * q + 2) {
    num = 0;
    for (int k = 1; k <= n; k++) {
      if (q_table[k] == q) {
        num++;
      }
    }
    ans_table[q] = num;
    ans = num;
    return;
  } else if (table[to_v] == 10 * q + 1) {
    table[to_v]++;
    ans_ref[to_v] = q;
    q_table[to_v / 2520] = q;
    dfs(to_v, q);
  } else {
    table[to_v] = 10 * q + 1;
    ans_ref[to_v] = q;
    dfs(to_v, q);
  }
  return;
}
long long solve(long long from, long long startnum, long long q) {
  startnum += k[from];
  startnum %= 2520ll;
  long long from_v = from * 2520ll + startnum;
  dfs(from_v, q);
  return ans;
}
void add_edge(long long from) {
  for (int i = 0; i < 2520; i++) {
    long long from_v = 2520ll * from + i;
    long long to = m[i % a];
    long long num1 = (i + k[to]) % 2520ll;
    long long to_v = 2520ll * to + num1;
    go[from_v] = to_v;
  }
}
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> k[i];
    k[i] = (k[i] % 2520ll + 2520ll) % 2520ll;
  }
  for (int i = 1; i <= n; i++) {
    cin >> a;
    for (int j = 0; j < a; j++) {
      cin >> m[j];
    }
    add_edge(i);
  }
  long long query;
  cin >> query;
  for (i = 0; i < 2800000; i++) {
    ans_table[i] = -1;
    table[i] = -1;
    ans_ref[i] = -1;
  }
  for (i = 1; i <= 1000; i++) {
    q_table[i] = -1;
  }
  for (int i = 0; i < query; i++) {
    cin >> a >> b;
    b = ((b % 2520ll) + 2520ll) % 2520ll;
    cout << solve(a, b, i + 1) << endl;
    ;
  }
  return 0;
}
