#include<iostream>
#include<cstdio>
#include<queue>
#define ll long long
using namespace std;
struct node{
	int num,id;
};
int n,x,Q,u,d;
int edgenum,NIL=-1,head[200005],nex[400005],vet[400005];
int h[200005],fa[200005],Size[200005],son[200005];
int Num[200005],t[200005];
bool vis[200005];
queue<node> q[200005];
void addedge(int u,int v){
	edgenum++;
	nex[edgenum]=head[u];
	vet[edgenum]=v;
	head[u]=edgenum;
}
void DFS(int u,int H){
	int Max=0;
	h[u]=H;
	for(int i=head[u];i!=NIL;i=nex[i]){
		int v=vet[i];
		if(v==fa[u]) continue;
		fa[v]=u;
		Size[v]=1;
		DFS(v,H+1);
		Size[u]+=Size[v];
		if(Size[v]>Max){
			Max=Size[v];
			son[u]=v;
		}
	}
}
void clac(int u,int num){
    if(num>0)
    {
        Num[h[u]]++;
    }
    else
    {
    	Num[h[u]]--;
    }
	for(int i=head[u];i!=NIL;i=nex[i]){
		int v=vet[i];
		if(v==fa[u]||vis[v]) continue;
		clac(v,num);
	}
}
void DSU(int u,int keep){
	for(int i=head[u];i!=NIL;i=nex[i]){
		int v=vet[i];
		if(v==fa[u]||v==son[u]) continue;
		DSU(v,0);
	}
	if(son[u]){
		DSU(son[u],1);vis[son[u]]=true;
	}
	clac(u,1);vis[son[u]]=false;
	while(!q[u].empty()){
		node X=q[u].front();
		q[u].pop();
		if(h[u]<=X.num)
			t[X.id]=Num[X.num];
		else t[X.id]=0;
	}
	if(keep==0) clac(u,-1);
}
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++) head[i]=NIL;
	for(int i=2;i<=n;i++){
		scanf("%d",&x);
		addedge(x,i);
		addedge(i,x);
	}
	scanf("%d",&Q);
	for(int i=1;i<=Q;i++){
		scanf("%d%d",&u,&d);
		node X;
		X.num=d+1;
		X.id=i;
		q[u].push(X);
	}
	DFS(1,1);
	DSU(1,0);
	for(int i=1;i<=Q;i++){
		printf("%d\n",t[i]);
	}
	return 0;
}
#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << "\n"
#define COUTF(x) cout << setprecision(15) << (x) << "\n"
#define ENDL cout << "\n"
#define DF(x) x.erase(x.begin())
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define RSORT(x) sort(x.rbegin(), x.rend())
#define REVERSE(x) reverse(ALL(x))
#define MAX(x) *max_element(ALL(x))
#define MAXI(x) max_element(ALL(x)) - x.begin()
#define SUM(x) accumulate(ALL(x), 0ll)
#define COUNT(x, y) count(ALL(x), y);
#define ANS cout << ans << "\n"
#define YES cout << "YES\n";
#define NO cout << "NO\n";
#define Yes cout << "Yes\n";
#define No cout << "No\n";
#define init() \
  cin.tie(0);  \
  ios::sync_with_stdio(false)
#define debug(x) cerr << "[debug] " << #x << ": " << x << endl;
#define debugV(v)                       \
  cerr << "[debugV] " << #v << ":";     \
  rep(z, v.size()) cerr << " " << v[z]; \
  cerr << endl;
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;
using vp = vector<P>;
using vs = vector<string>;

template <typename T>
inline istream& operator>>(istream& i, vector<T>& v) {
  rep(j, v.size()) i >> v[j];
  return i;
}

template <typename T1, typename T2>
inline istream& operator>>(istream& i, pair<T1, T2>& v) {
  return i >> v.first >> v.second;
}

constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) {
  return to_string(x).size();
}

ll gcd(ll x, ll y) {
  return y ? gcd(y, x % y) : x;
}

ll lcm(ll a, ll b) {
  return a / gcd(a, b) * b;
}

template <class T>
void chmax(T& a, const T& b) {
  if (a < b) a = b;
}

template <class T>
void chmin(T& a, const T& b) {
  if (b < a) a = b;
}

vvll to;
vp range;
vll dep;
vll points;
ll dfs(ll i, ll s, ll d) {
  ll count = 1;
  points.pb(i);
  for (ll t : to[i]) count += dfs(t, s + count, d + 1);
  dep[i] = d;
  range[i] = {s, s + count - 1};
  return count;
}

signed main() {
  init();
  ll N;
  cin >> N;
  vll A(N - 1);
  cin >> A;
  rep(i, N - 1) A[i]--;
  to.resize(N);
  range.resize(N);
  dep.resize(N);
  rep(i, N - 1) to[A[i]].pb(i + 1);

  dfs(0, 0, 0);
  vector<unordered_map<ll, ll>> x(N);
  unordered_map<ll, ll> m;

  ll M = 1130;
  rep(i, N) {
    ll p = points[i];
    m[dep[p]]++;
    if (i % M == 0) x[i] = m;
  }

  ll Q;
  cin >> Q;
  rep(i, Q) {
    ll u, d;
    cin >> u >> d;
    u--;
    ll l = range[u].first;
    ll r = range[u].second;
    ll ans = x[r - r % M][d];
    rep(k, r % M) if (dep[points[r - k]] == d) ans++;
    if (l != 0) {
      l--;
      rep(k, l % M) if (dep[points[l - k]] == d) ans--;
      ans -= x[l - l % M][d];
    }
    COUT(ans);
  }
  return 0;
}