#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <bitset>
#include <complex>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <cassert>
#include <functional>

using ll = long long;
using namespace std;

template<typename A, typename B>
bool chmin(A &a, const B b) {
  if (a <= b) return false;
  a = b;
  return true;
}

template<typename A, typename B>
bool chmax(A &a, const B b) {
  if (a >= b) return false;
  a = b;
  return true;
}

#ifndef LOCAL
#define debug(...) ;
#else
#define debug(...) cerr << __LINE__ << " : " << #__VA_ARGS__ << " = " << _tostr(__VA_ARGS__) << endl;

template<typename T>
ostream &operator<<(ostream &out, const vector<T> &v);

template<typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &p) {
  out << "{" << p.first << ", " << p.second << "}";
  return out;
}

template<typename T>
ostream &operator<<(ostream &out, const vector<T> &v) {
  out << '{';
  for (const T &item : v) out << item << ", ";
  out << "\b\b}";
  return out;
}

void _tostr_rec(ostringstream &oss) {
  oss << "\b\b \b";
}

template<typename Head, typename... Tail>
void _tostr_rec(ostringstream &oss, Head &&head, Tail &&...tail) {
  oss << head << ", ";
  _tostr_rec(oss, forward<Tail>(tail)...);
}

template<typename... T>
string _tostr(T &&...args) {
  ostringstream oss;
  int size = sizeof...(args);
  if (size > 1) oss << "{";
  _tostr_rec(oss, forward<T>(args)...);
  if (size > 1) oss << "}";
  return oss.str();
}
#endif

constexpr int mod = 1'000'000'007; //1e9+7(prime number)
constexpr int INF = 1'000'000'000; //1e9
constexpr ll LLINF = 2'000'000'000'000'000'000LL; //2e18
constexpr int SIZE = 200010;

/* Binary Indexed Tree(0-index) */

template<typename Type = int>
struct BIT {
  int N;
  vector<Type> data;

  BIT(int n): N(n), data(n + 1, 0) {}

  //add x to kth value
  void add(int k, Type x) {
    debug(k, x);
    for (k++; k <= N; k += k & -k)
      data[k] += x;
  }

  //sum of [0,k)
  Type query(int k) const {
    Type rec = 0;
    for (k = min(k, N); k; k -= k & -k)
      rec += data[k];
    return rec;
  }

  //sum of [a, b)
  Type query(int a, int b) const {
    return a < b ? query(b) - query(a) : 0;
  }
};

vector<int> G[SIZE];
vector<pair<int, int>> query[SIZE];

int in[SIZE], out[SIZE];
int id = 0;
vector<int> dNode[SIZE];

void dfs(int now, int d = 0) {
  in[now] = id++;
  dNode[d].push_back(now);

  for (int to : G[now]) {
    dfs(to, d + 1);
  }

  out[now] = id++;
}

int main() {
  int N, Q;

  scanf("%d", &N);

  for (int i = 0; i < N - 1; i++) {
    int p;
    scanf("%d", &p);
    G[p - 1].push_back(i + 1);
  }

  dfs(0);

  scanf("%d", &Q);

  for (int i = 0; i < Q; i++) {
    int U, D;
    scanf("%d%d", &U, &D);
    query[D].push_back({i, U - 1});
  }

  debug(id);

  BIT<int> bit(N * 2);
  vector<int> ans(Q);

  for (int D = 0; D < N; D++) {
    for (int n : dNode[D]) {
      bit.add(in[n], 1);
    }

    for (auto p : query[D]) {
      debug(p, in[p.second], out[p.second]);
      ans[p.first] = bit.query(in[p.second], out[p.second]);
      debug(ans[p.first]);
    }

    for (int n : dNode[D]) {
      bit.add(in[n], -1);
    }
  }

  for (int v : ans) {
    printf("%d\n", v);
  }


  return 0;
}
#include<bits/stdc++.h>
using namespace std;
using lli = long long;
#define rep(i,n) for(int i=0;i<n;i++)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<class T>
ostream &operator<<(ostream &os, const vector<T> &x){
    os << "{";
    for(size_t i = 0; i < x.size(); i++){
        if(i < x.size()-1) os << x[i] << ", ";
        else os << x[i];
    }
    os << "}";
    return os;
}

int main(void){
    int n;
    cin >> n;
    vector<vector<int>> v(n);
    rep(i, n-1){
        int p;
        cin >> p;
        p--;
        v[p].push_back(i+1);
    }
    vector<int> d(n);
    vector<vector<int>> in(n);
    vector<pair<int, int>> a(n);
    int idx = 0;
    function<void(int x)> dfs = [&](int x){
        a[x].first = idx++;
        in[d[x]].push_back(a[x].first);
        for(auto to : v[x]){
            d[to] = d[x]+1;
            dfs(to);
        }
        a[x].second = idx++;
    };
    dfs(0);
    for(auto &in : in) sort(in.begin(), in.end());
    int Q;
    cin >> Q;
    rep(q, Q){
        int U, D;
        cin >> U >> D;
        U--;
        auto [inx, outx] = a[U];
        int ans = lower_bound(in[D].begin(), in[D].end(), outx) - lower_bound(in[D].begin(), in[D].end(), inx);
        cout << ans << endl;
    }
    return 0;
}
