#include<bits/stdc++.h>
using namespace std;
#define ll int
vector<ll>seg[4*200009];
vector<ll>ad[200009];
ll s[200009];
ll f[200009];
ll T;
ll val[200009];
ll lev[200009];
void dfs(ll a,ll p)
{
    lev[a]=lev[p]+1;
    T++;
    s[a]=T;
    val[T]=a;
    ll b,i,l=ad[a].size();
    for(i=0;i<l;i++)
    {
        b=ad[a][i];
        if(b!=p)
        {
            dfs(b,a);
        }
    }
    f[a]=T;
}
void merge_(ll pos,ll l,ll r)
{
    ll i,j,n,m;
    n=seg[l].size();
    m=seg[r].size();
    i=j=0;
    while(1)
    {
        if(i==n || j==m)
        {
            break;
        }
        if(seg[l][i]<=seg[r][j])
        {
            seg[pos].push_back(seg[l][i]);
            i++;
        }
        else
        {
            seg[pos].push_back(seg[r][j]);
            j++;
        }
    }
    while(i<n)
    {
        seg[pos].push_back(seg[l][i]);
        i++;
    }
    while(j<m)
    {
        seg[pos].push_back(seg[r][j]);
        j++;
    }
}
void create(ll lo,ll hi,ll pos)
{
    if(lo==hi)
    {
        seg[pos].push_back(lev[val[lo]]);
        return;
    }
    ll m=(lo+hi)/2;
    create(lo,m,2*pos);
    create(m+1,hi,2*pos+1);
    merge_(pos,2*pos,2*pos+1);
    return;

}
ll query(ll qlo,ll qhi,ll lo,ll hi,ll pos,ll val)
{
    if(qlo>hi || qhi<lo)
    {
        return 0;
    }
    if(qlo<=lo && qhi>=hi)
    {
        ll a=lower_bound(seg[pos].begin(),seg[pos].end(),val)-seg[pos].begin();
        ll b=upper_bound(seg[pos].begin(),seg[pos].end(),val)-seg[pos].begin();
        return (b-a);
    }

    ll m=(lo+hi)/2;
    return query(qlo,qhi,lo,m,2*pos,val)+query(qlo,qhi,m+1,hi,2*pos+1,val);

}
int main()
{
    ll a,b,c,d,e,i,j,k,l,n,m,x,y,t,p;
    scanf("%d",&n);
    for(i=2;i<=n;i++)
    {
        scanf("%d",&a);
        ad[a].push_back(i);
    }
    T=0;
    lev[0]=-1;
    dfs(1,0);
    create(1,n,1);
    scanf("%d",&m);
    for(i=0;i<m;i++)
    {
        scanf("%d%d",&a,&c);
        x=query(s[a],f[a],1,n,1,c);
        printf("%d\n",x);
    }
}

#include "bits/stdc++.h"
using namespace std;
#define FastIO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long
#define mod 1000000007

const int N = 2e5 + 5;
vector<int> tree[N], depth(N);
vector<int> time_in(N), time_out(N);
vector<int> orders[N];
int timer = 0, n;

void dfs(int s) {
	time_in[s] = timer++;
	orders[depth[s]].push_back(time_in[s]);
	for (int v : tree[s]) {
		depth[v] = 1 + depth[s];
		dfs(v);
	}
	time_out[s] = timer++;
}

void test_case() {
	cin >> n;
	int j;
	for (int i = 2; i <= n; i++) {
		cin >> j;
		tree[j].push_back(i);
	}
	dfs(1);
	int q;
	cin >> q;
	while (q--) {
		int u, d;
		cin >> u >> d;
		auto &arr = orders[d];
		auto y = lower_bound(arr.begin(), arr.end(), time_out[u]);
		auto x = lower_bound(arr.begin(), arr.end(), time_in[u]);
		cout << y - x << "\n";
	}
}

signed main() {
	FastIO;
	test_case();
	return 0;
}
/*
         ▄              ▄
        ▌▒█           ▄▀▒▌
        ▌▒▒█        ▄▀▒▒▒▐
       ▐▄█▒▒▀▀▀▀▄▄▄▀▒▒▒▒▒▐
     ▄▄▀▒▒▒▒▒▒▒▒▒▒▒█▒▒▄█▒▐
   ▄▀▒▒▒░░░▒▒▒░░░▒▒▒▀██▀▒▌
  ▐▒▒▒▄▄▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▀▄▒▌
  ▌░░▌█▀▒▒▒▒▒▄▀█▄▒▒▒▒▒▒▒█▒▐
 ▐░░░▒▒▒▒▒▒▒▒▌██▀▒▒░░░▒▒▒▀▄▌
 ▌░▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░░░░░▒▒▒▒▌
▌▒▒▒▄██▄▒▒▒▒▒▒▒▒░░░░░░░░▒▒▒▐
▐▒▒▐▄█▄█▌▒▒▒▒▒▒▒▒▒▒░▒░▒░▒▒▒▒▌
▐▒▒▐▀▐▀▒▒▒▒▒▒▒▒▒▒▒▒▒░▒░▒░▒▒▐
 ▌▒▒▀▄▄▄▄▄▄▒▒▒▒▒▒▒▒░▒░▒░▒▒▒▌
 ▐▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░▒░▒▒▄▒▒▐
  ▀▄▒▒▒▒▒▒▒▒▒▒▒▒▒░▒░▒▄▒▒▒▒▌
    ▀▄▒▒▒▒▒▒▒▒▒▒▄▄▄▀▒▒▒▒▄▀
      ▀▄▄▄▄▄▄▀▀▀▒▒▒▒▒▄▄▀
         ▀▀▀▀▀▀▀▀▀▀▀▀
*/