#include <bits/stdc++.h>
using namespace std;
using db = double;
using ll = long long;
using uint = unsigned int;
using pii = pair<int, int>;
template <class T>
void cmax(T& x, const T& y) {
  if (x < y) x = y;
}
template <class T>
void cmin(T& x, const T& y) {
  if (x > y) x = y;
}
template <class T>
void sort(vector<T>& v) {
  sort(v.begin(), v.end());
}
template <class T>
void reverse(vector<T>& v) {
  reverse(v.begin(), v.end());
}
template <class T>
void unique(vector<T>& v) {
  sort(v.begin(), v.end()), v.erase(unique(v.begin(), v.end()), v.end());
}
const int SZ = 1 << 23 | 233;
struct FILEIN {
  char qwq[SZ], *S = qwq, *T = qwq, ch;
  char GETC() {
    return (S == T) && (T = (S = qwq) + fread(qwq, 1, SZ, stdin), S == T)
               ? EOF
               : *S++;
  }
  FILEIN& operator>>(char& c) {
    while (isspace(c = GETC()))
      ;
    return *this;
  }
  FILEIN& operator>>(string& s) {
    while (isspace(ch = GETC()))
      ;
    s = ch;
    while (!isspace(ch = GETC())) s += ch;
    return *this;
  }
  template <class T>
  void read(T& x) {
    bool sign = 0;
    while ((ch = GETC()) < 48) sign ^= (ch == 45);
    x = (ch ^ 48);
    while ((ch = GETC()) > 47) x = (x << 1) + (x << 3) + (ch ^ 48);
    x = sign ? -x : x;
  }
  FILEIN& operator>>(int& x) { return read(x), *this; }
  FILEIN& operator>>(ll& x) { return read(x), *this; }
} in;
struct FILEOUT {
  const static int LIMIT = 1 << 22;
  char quq[SZ], ST[233];
  int sz, O;
  ~FILEOUT() { flush(); }
  void flush() {
    fwrite(quq, 1, O, stdout);
    fflush(stdout);
    O = 0;
  }
  FILEOUT& operator<<(char c) { return quq[O++] = c, *this; }
  FILEOUT& operator<<(string str) {
    if (O > LIMIT) flush();
    for (char c : str) quq[O++] = c;
    return *this;
  }
  template <class T>
  void write(T x) {
    if (O > LIMIT) flush();
    if (x < 0) {
      quq[O++] = 45;
      x = -x;
    }
    do {
      ST[++sz] = x % 10 ^ 48;
      x /= 10;
    } while (x);
    while (sz) quq[O++] = ST[sz--];
  }
  FILEOUT& operator<<(int x) { return write(x), *this; }
  FILEOUT& operator<<(ll x) { return write(x), *this; }
} out;
long long n, q;
const long long maxn = 1e5 + 51;
vector<long long> g[maxn];
long long fa[maxn], ans[maxn];
long long f[maxn][22];
vector<pair<long long, long long> > qr[maxn];
long long sz[maxn], son[maxn], dep[maxn];
void dfs(long long u) {
  sz[u] = 1;
  for (long long v : g[u]) {
    f[v][0] = u, dep[v] = dep[u] + 1;
    dfs(v);
    sz[u] += sz[v];
    if (sz[v] > sz[son[u]]) son[u] = v;
  }
}
long long vis[maxn], sum[maxn];
void upd(long long dep, long long qwq) { sum[dep] += qwq; }
void add(long long u, long long qwq) {
  upd(dep[u], qwq);
  for (long long v : g[u])
    if (!vis[v] && v ^ fa[u]) add(v, qwq);
}
void dfs(long long u, long long kep) {
  for (long long v : g[u])
    if (v ^ son[u]) dfs(v, 0);
  if (son[u]) {
    dfs(son[u], 1);
    vis[son[u]] = 1;
  }
  add(u, 1);
  vis[son[u]] = 0;
  for (auto x : qr[u]) ans[x.second] = sum[x.first] - 1;
  if (!kep) add(u, -1);
}
long long kth(long long x, long long k) {
  for (long long i = 20; ~i; i--)
    if (k & (1 << i)) x = f[x][i];
  return x;
}
signed main() {
  in >> n;
  for (register long long i = (1); i <= (n); ++i) {
    in >> fa[i];
    g[fa[i]].emplace_back(i);
  }
  for (register long long i = (1); i <= (n); ++i) {
    if (!fa[i]) dfs(i);
  }
  for (register long long j = (1); j <= (20); ++j)
    for (register long long i = (1); i <= (n); ++i)
      f[i][j] = f[f[i][j - 1]][j - 1];
  in >> q;
  for (register long long i = (1); i <= (q); ++i) {
    long long v, p;
    in >> v >> p;
    long long u = kth(v, p);
    qr[u].emplace_back(dep[v], i);
  }
  for (register long long i = (1); i <= (n); ++i)
    if (!fa[i]) dfs(i, 0);
  for (register long long i = (1); i <= (q); ++i) out << ans[i] << ' ';
  out << '\n';
  return 0;
}
