#include <bits/stdc++.h>
using namespace std;

#ifdef _DEBUG
#include "_DEBUG.hpp"
#endif
#define int long long
const long long inf = 2e18;
const int mod = 1e9 + 7;

template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
  for (T &in : v) is >> in;
  return is;
}

template <class T>
vector<T> make_vec(size_t a) {
  return vector<T>(a);
}

template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

template <class T, class V>
typename enable_if<is_class<T>::value == 0>::type fill(T &t, const V &v) {
  t = v;
}

template <class T, class V>
typename enable_if<is_class<T>::value != 0>::type fill(T &t, const V &v) {
  for (auto &e : t) fill(e, v);
}

template <class T>
struct SegmentTree {
  using F = function<T(T, T)>;
  F f;
  T inf;
  int n;
  vector<T> node;

  SegmentTree() {}
  SegmentTree(vector<T> v, T inf, F f) : inf(inf), f(f) {
    n = 1;
    while (n < v.size()) n *= 2;
    node.resize(2 * n - 1, inf);
    for (int i = 0; i < v.size(); i++) node[i + n - 1] = v[i];
    for (int i = n - 2; i >= 0; i--)
      node[i] = f(node[2 * i + 1], node[2 * i + 2]);
  }

  void update(int k, T val) {
    k += n - 1;
    node[k] = val;
    while (k > 0) {
      k = (k - 1) / 2;
      node[k] = f(node[2 * k + 1], node[2 * k + 2]);
    }
  }

  void add(int k, T val) {
    k += n - 1;
    node[k] += val;
    while (k > 0) {
      k = (k - 1) / 2;
      node[k] = f(node[2 * k + 1], node[2 * k + 2]);
    }
  }

  //区間[a, b)の値を返す
  T operator[](int x) { return getval(x, x + 1); }
  T getval(int a, int b) { return getval(a, b, 0, 0, n); }
  T getval(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l) return inf;
    if (a <= l && r <= b) return node[k];
    T vl = getval(a, b, 2 * k + 1, l, (l + r) / 2);
    T vr = getval(a, b, 2 * k + 2, (l + r) / 2, r);
    return f(vl, vr);
  }

  void print(int n) {
    for (int i = 0; i < n; i++) {
      cout << getval(i, i + 1) << " ";
    }
    cout << endl;
  }
};

signed main() {
  int n;
  cin >> n;
  vector<int> p(n - 1);
  cin >> p;

  vector<vector<int>> g(n);
  for (int i = 0; i < n - 1; i++) {
    g[p[i] - 1].push_back(i + 1);
    g[i + 1].push_back(p[i] - 1);
  }

  int q;
  cin >> q;
  vector<vector<pair<int, int>>> query(n);
  for (int i = 0; i < q; i++) {
    int u, d;
    cin >> u >> d;
    u--;
    query[d].push_back({i, u});
  }

  int num = 0;
  vector<int> in(n), out(n);
  vector<vector<int>> depth(n);
  auto dfs = [&](auto &&dfs, int u, int par, int d) -> void {
    in[u] = num++;
    depth[d].push_back(u);
    for (int v : g[u]) {
      if (v == par) continue;
      dfs(dfs, v, u, d + 1);
    }
    out[u] = num++;
  };
  dfs(dfs, 0, -1, 0);

  SegmentTree<int> seg(vector<int>(num, 0), 0,
                       [&](int x, int y) { return (x + y); });

  vector<int> ans(q);
  for (int i = 0; i < n; i++) {
    for (int node : depth[i]) {
      seg.update(in[node], 1);
    }
    for (auto p : query[i]) {
      ans[p.first] = seg.getval(in[p.second], out[p.second] + 1);
    }
    for (int node : depth[i]) {
      seg.update(in[node], 0);
    }
  }

  for (int i = 0; i < q; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}
/*
これを入れて実行
g++ code.cpp
./a.out
 */
 
#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <math.h>
#include <tuple>
#include <iomanip>
#include <bitset>
#include <functional>
#include <cassert>
#include <random>

#define all(x) (x).begin(),(x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
 
using namespace std;
typedef long long ll;
typedef long double ld;
 
int dy4[4] = {-1, 0, +1, 0};
int dx4[4] = {0, +1, 0, -1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
 
const long long INF = 1LL << 61;
const ll MOD = 1e9 + 7;
 
bool greaterSecond(const pair<int, int>& f, const pair<int, int>& s){
    return f.second > s.second;
}
 
ll gcd(ll a, ll b){
	if (b == 0)return a;
	return gcd(b, a % b);
}
 
ll lcm(ll a, ll b){
    return a / gcd(a, b) * b;
}

ll conbinationMemo[61][31];

void cmemoInit(){
    rep(i, 61){
        rep(j, 31){
            conbinationMemo[i][j] = -1;
        }
    }
}
 
ll nCr(ll n, ll r){
    if(conbinationMemo[n][r] != -1) return conbinationMemo[n][r];
    if(r == 0 || r == n){
        return 1;
    } else if(r == 1){
        return n;
    }
    return conbinationMemo[n][r] = (nCr(n - 1, r) + nCr(n - 1, r - 1));
}
 
ll nPr(ll n, ll r){
    r = n - r;
    ll ret = 1;
    for (ll i = n; i >= r + 1; i--) ret *= i;
    return ret;
}
//-----------------------ここから-----------
ll n;
ll q;
vector<vector<ll>> g;
vector<vector<ll>> l;
ll in[200200];
ll out[200200];
void dfs(ll now, ll pa, ll d, ll &time){

    in[now] = time;
    l[d].push_back(time);
    time++;

    rep(i, g[now].size()){
        ll next = g[now][i];
        if(next == pa) continue;
        ll nextd = d + 1;
        dfs(next, now, nextd, time);
    }

    out[now] = time;
    time++;

}
int main(void){
    cin >> n;
    g.resize(n);
    l.resize(n);
    rep(i, n - 1){
        ll p;
        cin >> p;
        p--;
        g[p].push_back(i + 1);
    }
    ll time = 0;
    dfs(0, -1, 0, time);
    cin >> q;
    rep(i, q){
        ll u, d;
        cin >> u >> d;
        u--;

        auto itr1 = lower_bound(all(l[d]), in[u]);
        auto itr2 = lower_bound(all(l[d]), out[u]);
        cout << itr2 - itr1 << endl;
    }
}