/*
Hey  why peeping here -_'_-?
I believe on myself and I will achieve
this->author = Fuad Ashraful Mehmet, CSE ,University of Asia Pacific
Todo:
https://codeforces.com/problemset?tags=1500-2300
*/
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define all(o) (o).begin(), (o).end()
#define rall(o) (o).rbegin(), (o).rend()
#define sz(o) int(o.size())
#define FAST_IO  ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr);
#define dbg(o) cerr << "at " << __LINE__ << " response = " << o << endl;
#define dbgA(a,L,R) cerr<<#a<<"=[";for(int i=L;i<=R;i++) cerr<<a[i]<<(i==R?"]\n":" ");
#define dbgV(O) cerr << #O << "=["; for (auto o : O) cerr << o << ", "; cerr << "E]\n";
#define pb push_back
using namespace std;
using pii = pair<int, int>;
typedef long long ll;
int  n,m,k,tc=1,len,q;
const int N=2e5+2,LOG=20;
int d[N],in[N],out[N],t=1;
vector<int>g[N],tree[N<<2];
void dfs(int from,int p,int l){
	d[t]=l;
	in[from]=t++;
	for(int to:g[from])if(to!=p){
		dfs(to,from,l+1);
	}
	out[from]=t-1;
}

void build(int l,int r,int nd=1){
	if(l==r){
		tree[nd].pb(d[l]);
		return;
	}
	int mid=(l+r)>>1;
	build(l,mid,nd<<1);
	build(mid+1,r,nd<<1|1);
	merge(all(tree[nd<<1]),all(tree[nd<<1|1]),back_inserter(tree[nd]));
	return;
}

int query(int l,int r,int x,int y,int d,int nd=1){
	if(l>y or r<x)return 0;
	if(l>=x && r<=y){ // overlapped segment
		//cout<<l<<" == "<<r<<endl;
		//dbgV(tree[nd])
		int idx1=lower_bound(all(tree[nd]),d)-tree[nd].begin();
		int idx2=upper_bound(all(tree[nd]),d)-tree[nd].begin();
		return idx2-idx1;
	}
	int mid=(l+r)>>1;
	return query(l,mid,x,y,d,nd<<1)+query(mid+1,r,x,y,d,nd<<1|1);
}
void solve(){
	cin>>n;
	int x;
	for(int i=2;i<=n;++i){
		cin>>x;
		g[i].pb(x);
		g[x].pb(i);
	}
	dfs(1,0,0);
	build(1,t-1,1);
	//dbgA(d,1,t-1);
	cin>>q;
	int u,d;
	while(q--){
		cin>>u>>d;
		//cout<<in[u]<<" -- "<<out[u]<<'\n';
		cout<<query(1,t-1,in[u],out[u],d)<<'\n';
	}
	return;
}
int main(){
	FAST_IO
	//cout<<setprecision(20)<<fixed;
	//cin>>tc;
	while(tc--){
		solve();
	}
	return 0;
}
#include<bits/stdc++.h>
using namespace std ;
const int maxn = 1e6 + 10 ;
typedef long long ll ;
int timstep , n ;
vector<int> g[maxn] ;
int in[maxn] , out[maxn] ;
vector<int> listt[maxn] ;

void dfs(int u , int dep){
	in[u] = timstep++ ;
	listt[dep].push_back(in[u]) ;
	for(auto v : g[u])
		dfs(v , dep + 1) ;
	out[u] = timstep++ ;
		
}

int main(){
	int n ;
	cin >> n ;
	for(int v = 1 ; v < n ; v++){
		int u ;
		cin >> u ;
		u-- ;
		g[u].push_back(v) ;
	}
	dfs(0 , 0) ;
	int q ;
	cin >> q ;
	for(int i = 0 ; i < q ; i++){
		int u , d ;
		cin >> u >> d ;
		u-- ;
		cout << lower_bound(listt[d].begin() , listt[d].end() , out[u]) - lower_bound(listt[d].begin() , listt[d].end() , in[u]) << endl ;
	}
	return 0 ;
	
}