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

typedef long long ll;

typedef pair <int, int> pii;
typedef pair <ll,ll> pll;

typedef vector <int> vint;
typedef vector <ll> vll;
typedef vector <pii> vpii;
typedef vector <pll> vpll;
typedef vector <string> vs;
typedef priority_queue <int> pqi;

#define FOR(i, a, b) for(int i = a; i < (b); ++i)
#define F0R(i, a) for(int i = 0; i < (a); ++i)
#define FORd(i, a, b) for(int i = (b) - 1; i >= a; --i)
#define F0Rd(i, a) for(int i = (a) - 1; i >= 0; --i)
#define trav(x, a) for(auto& x : a)

#define pb push_back
#define mkp make_pair
#define vbb vector <bool>
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()

const int mod = 1000000007;
const long long inf = 1e18;
const char nl = '\n';

vector <vint> tree, util;
int tim = 0;
vint xin, xout, depth;

void dfs(int i){
    xin[i] = tim++;
    util[depth[i]].pb(xin[i]);
    for(int x : tree[i]){
        depth[x] = depth[i] + 1;
        dfs(x);
    }
    xout[i] = tim++;
}

void solve_it(){
    int n; cin>>n;
    vector <vint> temp(n + 1);
    FOR(i, 2, n + 1){
        int p; cin>>p;
        temp[p].pb(i);
    }
    util = vector <vint> (n + 1);
    tree = temp;
    xin = xout = depth = vint (n + 1);
    dfs(1);
    int q; cin>>q;
    while(q--){
        int u, d; cin>>u>>d;
        vint v = util[d];
        cout<<lb(all(v), xout[u]) - lb(all(v), xin[u])<<nl;
    }
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    solve_it();
    return 0;
}

// Problem: E - Count Descendants
// Contest: AtCoder - AISing Programming Contest 2021（AtCoder Beginner Contest 202）
// URL: https://atcoder.jp/contests/abc202/tasks/abc202_e
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// Author: abhidot

#include <bits/stdc++.h>
#define int long long
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define mod 1000000007 //998244353
#define lld long double
#define pii pair<int, int>
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=x; i<y; i++)
#define fill(a,b) memset(a, b, sizeof(a))
#define vi vector<int>
#define setbits(x) __builtin_popcountll(x)
#define w(x)  int x; cin>>x; while(x--)
using namespace std;
const long long N=200005, INF=2000000000000000000;

/*------------- Modular exponentiation -------------*/
int power(int a, int b, int p)
    {
        if(a==0)
        return 0;
        int res=1;
        a%=p;
        while(b>0)
        {
            if(b&1)
            res=(res*a)%p;
            b>>=1;
            a=(a*a)%p;
        }
        return res;
    }
	

/*------------- Sieve --------------*/	
vector <int> prime;
bool is_composite[N];

void sieve (int n) {
	fill (is_composite, false);
	for (int i = 2; i < n; ++i) {
		if (!is_composite[i]) prime.push_back (i);
		for (int j = 0; j < prime.size () && i * prime[j] < n; ++j) {
			is_composite[i * prime[j]] = true;
			if (i % prime[j] == 0) break;
		}
	}
}


/*------------ Utitlity function ---------------*/
void print(bool n){
    if(n){
        cout<<"YES";
    }else{
        cout<<"NO";
    }
}

vector<int> g[N], d[N];
int tin[N], tout[N], tim=0;


void dfs(int u, int depth=0){
	tin[u] = ++tim;
	d[depth].pb(tin[u]);
	for(auto v: g[u]){
		dfs(v, depth+1);
	}
	tout[u] = ++tim;
}


int32_t main()
{
    IOS;
    int n;
    cin>>n;
    for(int i=2;i<=n;i++){
    	int p;
    	cin>>p;
    	g[p].pb(i);
    }
    
    dfs(1);
    
    w(T){
    	int u, k;
    	cin>>u>>k;
    	int l = tin[u], r = tout[u];
    	int ans = lower_bound(all(d[k]), r) - lower_bound(all(d[k]), l);
    	cout<<ans<<"\n";
    }
}

// Written by abhidot