// E - Count Descendants
#include <bits/stdc++.h>
using namespace std;

#define MAX 200000
vector<int> adj[MAX], T[MAX];
int L[MAX], R[MAX];
int depth, timer;

void dfs(int p){
	L[p] = timer++;
	T[depth].push_back(L[p]);
	depth++;
	for(int&c:adj[p]) dfs(c);
	depth--;
	R[p] = timer++;
}

int f(int d, int t){
	return lower_bound(T[d].begin(), T[d].end(), t) - T[d].begin();
}

int main(){
	int n; cin>>n;

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

	dfs(0);

	int q; cin>>q;
	while(q--){
		int u, d; cin>>u>>d;
		cout<< f(d, R[u-1]) - f(d, L[u-1]) <<endl;
	}
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
///Welcome to Nasif's Code
#define bug printf("bug\n");
#define bug2(var) cout<<#var<<" "<<var<<endl;
#define co(q) cout<<q<<endl;
#define all(q) (q).begin(),(q).end()
#define pi acos(-1)
#define inf 1000000000000000LL
#define FastRead    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define MODADD(ADD_X,ADD_Y)  (ADD_X+ADD_Y)%MOD
#define MODSUB(SUB_X,SUB_Y)  ((SUB_X-SUB_Y)+MOD)%MOD
#define MODMUL(MUL_X,MUL_Y)  (MUL_X*MUL_Y)%MOD
#define LCM(LCM_X,LCM_Y) (LCM_X/__gcd(LCM_X,LCM_Y))*LCM_Y
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD = (int)1e9+7;
const int MAX = 1e6;
int dx[]= {1,0,-1,0,1,-1,1,-1};
int dy[]= {0,1,0,-1,1,-1,-1,1};
ll st[MAX],en[MAX],lv[MAX],sz[MAX],idx=1;
vector<int>adj[MAX],v[MAX];
void dfs(int src,int par)
{
    st[src]=idx++;
    v[lv[src]].push_back(st[src]);
    int f=0;
    for(auto i:adj[src])
    {
        if(i==par)
            continue;
        lv[i]=lv[src]+1;
        dfs(i,src);
    }
    en[src]=idx++;
}

int main()
{
    FastRead
    //freopen("output.txt", "w", stdout);
    int n;
    cin>>n;
    for(int i=2; i<=n; i++)
    {
        int u=i,v;
        cin>>v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs(1,0);
    int q;
    cin>>q;
    while (q--)
    {
        int u,d;
        cin>>u>>d;
        int ans=lower_bound(all(v[d]),en[u])-lower_bound(all(v[d]),st[u]);
        cout<<ans<<endl;

    }
    return 0;
}
