/**
 * Author: kumasento
 * Date:   2021-05-22T13:24:20.000-05:00
 */

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

/// Types:
using LL = long long;
using PII = pair<int, int>;
using PIL = pair<int, LL>;
using PLI = pair<LL, int>;
using PLL = pair<LL, LL>;

/// Constants:
constexpr int INT_INF = 1000000000;
constexpr LL LL_INF = 10000000000000000LL;

/// Globals:
vector<vector<int>> children, lst;
vector<int> tin, tout, depth;
int timer;

/// Solution:

void dfs(int u) {
  tin[u] = timer ++;
  lst[depth[u]].push_back(tin[u]);
  for (int v : children[u]) {
    depth[v] = depth[u] + 1;
    dfs(v);
  }
  tout[u] = timer ++;
}


int main() {
  ios_base::sync_with_stdio(false); cin.tie(NULL);
#ifdef LOCAL_DEBUG
  ifstream in("E.in"); cin.rdbuf(in.rdbuf());
#endif

  int N; cin >> N;
  children.resize(N);
  lst.resize(N);
  tin.resize(N);
  tout.resize(N);
  depth.resize(N);

  for (int i = 1; i < N; ++ i) {
    int p; cin >> p;
    children[p - 1].push_back(i);
  }

  timer = 0;
  dfs(0);

  /// lst associates the depth with the in time of all nodes with that depth.

  int Q; cin >> Q;
  while (Q --) {
    int u, d; cin >> u >> d; u --;
    auto &v = lst[d];

    // Nodes enter time should be lower than tout[u], and >= tin[u].
    cout << (lower_bound(v.begin(), v.end(), tout[u]) -
             lower_bound(v.begin(), v.end(), tin[u])) << endl;
  }
}

#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<set>
#include<map>
#include<utility>
#include<queue>
#include<vector>
#include<stack>
#include<sstream>
#include<algorithm>
/************************************************/
#define rep(i,n) for(int i=0;i<n;i++)
#define m_p make_pair
#define pb push_back
#define fr first
#define se second
#define ford(i,n) for(int i=n-1;i>=0;i--)
#define forn(i,a,n) for(int i=a;i<n;i++)
#define foreach(i,c) for(__typeof(c.begin())i=(c.begin());i!=(c).end();i++)
#define pii pair<int,int>
#define vi vector<int>
#define ll long long
#define vll vector<ll>
#define sz(s) (int)(s.size())
#define all(s) s.begin(),s.end()
#define zero(x) memset(x,0,sizeof(x))
#define vii vector<pair<int,int> >
#define mpis map<int,string>
#define mpii map<int,int>
#define mpsi map<string,int>
#define re return
#define mod 1000000007
/************************************************/
using namespace std;

long long get(){
	char c=getchar();
	long long x=0LL;
	while(c<'0'||c>'9')
	c=getchar();
	while(c>='0'&&c<='9'){
		x*=10LL;
		x+=(c-'0');
		c=getchar();
	}
	return x;
}
string i_t_s(int x){
	string t="";
	while(x){
		t+=x%10+'0';
		x/=10;
	}
	reverse(all(t));
	re t;
}
int s_t_i(string t){
	int x=0;
	rep(i,sz(t)){
		x=x*10+(t[i]-'0');
	}
	re x;
}
ll q_p(ll x,ll y){
	ll res=1;
	x%=mod;
	while(y){
		if(y%2){
			res=res*x;
			res%=mod;
		}
		y/=2;
		x=x*x;
		x%=mod;
	}
	re res;
}
bool ok(int x,int y,int n,int m){
	 re(x>=0&&x<n&&y>=0&&y<m);
}
bool isprime(int x){
	if(x<2)
	re false;
	for(int i=2;i*i<=x;i++)
	if(x%i==0)
	re false;
	re true;
}
int dx[4]={0,-1,0,1},dy[4]={1,0,-1,0};
int ks[200001],js[200001];
int n,q;
vi v[200001];
int deep[200001];
int d[200001];
vector<pair<pii,int> > w;
int cnt=-1,B,num[200001];
void dfs(int x,int y){
	cnt++;
	deep[cnt]=y;
	ks[cnt]=cnt;
	num[x]=cnt;
	rep(i,sz(v[x]))
	dfs(v[x][i],y+1);
	js[num[x]]=cnt;
}
bool cmp(pair<pii,int> a,pair<pii,int> b){
	if(a.fr.fr/B!=b.fr.fr/B)
	re a.fr.fr/B<b.fr.fr/B;
	if(a.fr.se!=b.fr.se)
	re a.fr.se<b.fr.se;
	re a.fr.fr<b.fr.fr;
}
int ans[200001],now[200001];
int main(){
	ios::sync_with_stdio(0);
	cin>>n;
	B=sqrt(n);
	rep(i,n-1){
		int x;
		cin>>x;
		x--;
		v[x].pb(i+1);
	}
	dfs(0,0);
	cin>>q;
	rep(i,q){
		int x;
		cin>>x>>d[i];
		x--;
		w.pb(m_p(m_p(ks[num[x]],js[num[x]]),i));
	}
	sort(all(w),cmp);
	int nx=0,ny=-1;
	rep(i,q){
		while(ny<w[i].fr.se){
			ny++;
			now[deep[ny]]++;
		}
		while(ny>w[i].fr.se){
			now[deep[ny]]--;
			ny--;
		}
		while(nx<w[i].fr.fr){
			now[deep[nx]]--;
			nx++;
		}
		while(nx>w[i].fr.fr){
			nx--;
			now[deep[nx]]++;
		}
		ans[w[i].se]=now[d[w[i].se]];
		
	}
	rep(i,q)
	cout<<ans[i]<<"\n";
	re 0;
}
/*
检查循环是rep(i,n)还是rep(i,m)!!
long long所要的时间比int长!!
没有long long必要不写long long!!
写公式前先想一想!!
二分前看一看边界对不对，有没有特殊值!!
提交前要测特殊数据（边界上的）!!
*/