/*
--------------              |   /
      |                     |  /
      |                     | /
      |             *       |/          |    |         ------            *
      |                     |           |    |        /      \
      |             |       |\          |    |       |       |\          |
   \  |             |       | \         |    |       |       | \         |
    \ |             |       |  \        |    |        \     /   \        |
     V              |       |   \        \__/|         -----     \       |
*/
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << "\e[1;31m" << #x << " = " << (x) << "\e[0m\n"
#define print(x) emilia_mata_tenshi(#x, begin(x), end(x))
template<typename T> void emilia_mata_tenshi(const char *s, T l, T r) {
	cerr << "\e[1;33m" << s << " = [";
	while(l != r) {
		cerr << *l;
		cerr << (++l == r ? ']' : ',');
	}
	cerr << "\e[0m\n";
}

#define EmiliaMyWife ios::sync_with_stdio(0); cin.tie(NULL);
using ll = int64_t;
using ull = uint64_t;
using ld = long double;
using uint = uint32_t;
const double EPS  = 1e-8;
const int INF     = 0x3F3F3F3F;
const ll LINF     = 4611686018427387903;
const int MOD     = 1e9+7;
/*--------------------------------------------------------------------------------------*/

signed main() {
	int n;
	cin >> n;
	vector<vector<int>> edge(n + 1);
	vector<int> in(n + 1), out(n + 1), dep(n + 1);
	vector<vector<int>> d(n);
	for(int i = 2; i <= n; i++) {
		int x;
		cin >> x;
		edge[x].push_back(i);
	}
	int t = 0;
	function<void(int)> dfs = [&](int u) {
		d[dep[u]].push_back(t);
		in[u] = t++;
		for(int v : edge[u])
			dep[v] = dep[u] + 1, dfs(v);
		d[dep[u]].push_back(t);
		out[u] = t++;
	};
	dfs(1);
	int q;
	cin >> q;
	while(q--) {
		int u, de;
		cin >> u >> de;
		cout << (upper_bound(d[de].begin(), d[de].end(), out[u]) - lower_bound(d[de].begin(), d[de].end(), in[u])) / 2 << '\n';
	}
}

#include <cstdio>
#include <cmath>
#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
#include <map>
#include <cassert>
#include <string>
#include <cstring>
#include <queue>

using namespace std;

#define rep(i,a,b) for(int i = a; i < b; i++)
#define S(x) scanf("%d",&x)
#define S2(x,y) scanf("%d%d",&x,&y)
#define P(x) printf("%d\n",x)
#define all(v) v.begin(),v.end()
#define FF first
#define SS second
#define pb push_back
#define mp make_pair

typedef long long int LL;
typedef pair<int, int > pii;
typedef vector<int > vi;

const int N = 200005;

vi g[N];
vi nodes[N];
int D[N];
int _tm;
int tin[N], tout[N];

void dfs(int c, int d) {
  D[c] = d;
  tin[c] = ++_tm;
  nodes[d].pb(tin[c]);
  rep(i,0,g[c].size()) {
    dfs(g[c][i], d + 1);
  }
  tout[c] = ++_tm;
}

int bs(int d, int x) {
  int lo = 0, hi = (int)nodes[d].size() - 1;
  int ans = 0;
  while(lo <= hi) {
    int mi = (lo + hi) >> 1;
    if(nodes[d][mi] <= x) {
      ans = mi + 1;
      lo = mi + 1;
    } else {
      hi = mi - 1;
    }
  }
  return ans;
}

int main() {
  int n;
  S(n);
  rep(i,2,n+1) {
    int p;
    S(p);
    g[p].pb(i);
  }
  dfs(1, 0);
  int q;
  S(q);
  while(q--) {
    int u,d;
    S2(u,d);
    if(D[u] > d) {
      P(0);
      continue;
    }
    P(bs(d, tout[u]) - bs(d, tin[u] - 1));
  }
  return 0;
}
