#include <bits/stdc++.h>
using namespace std;
template <typename T, typename E>
T qpow(T x, E e) {
  if (e == 0) return 1;
  if (e % 2 == 0) return qpow(x * x, e / 2);
  return x * qpow(x * x, e / 2);
}
template <unsigned m>
struct Mint {
  Mint() : val(0) {}
  Mint(int raw) : val((raw % (int)m + (int)m) % (int)m) {}
  Mint(long long raw) : val((raw % m + m) % m) {}
  Mint(unsigned raw) : val(raw % m) {}
  Mint(unsigned long long raw) : val(raw % m) {}
  friend Mint operator+(Mint a, Mint b) { return a.val + b.val; }
  friend Mint operator-(Mint a, Mint b) { return m + a.val - b.val; }
  friend Mint operator*(Mint a, Mint b) { return 1ull * a.val * b.val; }
  friend Mint operator/(Mint a, Mint b) { return a * qpow(b, m - 2); }
  friend Mint& operator+=(Mint& a, Mint b) { return a = a + b; }
  friend Mint& operator-=(Mint& a, Mint b) { return a = a - b; }
  friend Mint& operator*=(Mint& a, Mint b) { return a = a * b; }
  friend Mint& operator/=(Mint& a, Mint b) { return a = a / b; }
  friend bool operator==(Mint a, Mint b) { return a.val == b.val; }
  friend bool operator!=(Mint a, Mint b) { return a.val != b.val; }
  friend ostream& operator<<(ostream& out, Mint x) { return out << x.val; }
  unsigned val;
};
template <typename T>
T load() {
  T r;
  cin >> r;
  return r;
}
template <typename T>
vector<T> loadMany(int n) {
  vector<T> rs(n);
  generate(rs.begin(), rs.end(), &load<T>);
  return rs;
}
using SM = Mint<2520>;
struct Medusa {
  const vector<int>& link;
  int n;
  vector<int> rep;
  Medusa(const vector<int>& link)
      : link(link), n((int)link.size()), rep(n, -1) {
    calcRep();
  }
  void calcRep() {
    auto visit = vector<bool>(n, false);
    for (auto v = 0; v < n; ++v)
      if (rep[v] == -1) {
        auto r = slowFindCycleRep(v, visit);
        auto u = v;
        while (rep[u] == -1) {
          rep[u] = r;
          u = link[u];
        }
      }
  }
  int slowFindCycleRep(int v, vector<bool>& visit) {
    while (rep[v] == -1 and visit[v] == false) {
      visit[v] = true;
      v = link[v];
    }
    return rep[v] == -1 ? v : rep[v];
  }
  template <typename T, typename F>
  vector<T> calcCycleProperty(T def, F f) {
    auto answer = vector<T>(n, def);
    for (auto v = 0; v < n; ++v) {
      if (answer[rep[v]] == def) {
        auto us = vector<int>({rep[v]});
        auto u = link[rep[v]];
        while (u != rep[v]) {
          us.push_back(u);
          u = link[u];
        }
        answer[rep[v]] = f(us);
      }
      answer[v] = answer[rep[v]];
    }
    return answer;
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  auto n = load<int>();
  auto rawAdditions = loadMany<int>(n);
  auto additions = vector<SM>(n);
  for (auto v = 0; v < n; ++v) additions[v] = rawAdditions[v];
  auto m = vector<int>(n);
  auto edges = vector<vector<int>>(n);
  for (auto v = 0; v < n; ++v) {
    m[v] = load<int>();
    edges[v].resize(m[v]);
    for (auto i = 0; i < m[v]; ++i) edges[v][i] = load<int>() - 1;
  }
  auto vid = [&](int v, SM s) { return 2520 * v + s.val; };
  auto next = vector<int>(n * 2520);
  for (auto v = 0; v < n; ++v) {
    for (auto s = 0; s < 2520; ++s) {
      auto s2 = s + additions[v];
      auto source = vid(v, s);
      auto dest = vid(edges[v][s2.val % m[v]], s2);
      next[source] = dest;
      assert(dest < (int)next.size());
    }
  }
  auto medusa = Medusa(next);
  auto answer = medusa.calcCycleProperty<int>(-1, [&](vector<int>& vs) {
    for (auto& v : vs) v /= 2520;
    sort(vs.begin(), vs.end());
    vs.erase(unique(vs.begin(), vs.end()), vs.end());
    return (int)vs.size();
  });
  auto q = load<int>();
  while (q-- > 0) {
    auto v = load<int>() - 1;
    auto c = SM(load<int>());
    cout << answer[vid(v, c)] << '\n';
  }
}
