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

//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")


#define pb push_back
#define fi first
#define se second
#define ll long long
#define tp top()
#define fr front()
#define vi vector<int>
#define sz size()
#define rep(i,a,b) for(int i = a; i < b; ++i)
#define mem(a,  b) memset(a,  (b),  sizeof(a))
#define clr(a) memset(a,  0,  sizeof(a))
#define sqr(x) ( (x) * (x) )
#define all(v) v.begin(),  v.end()

typedef pair<int, int> pii;
typedef pair<int,pii> pip;
typedef pair<pii,int> ppi;
typedef pair<pii,pii> ppp;



void base(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}

#define N 200020
vector<int> adj[N];
vector<pii> qq[N];
int h[N], ans[N];

void dfs(int cur, int hh){	
	for(auto x : qq[cur]) ans[x.se] -= h[x.fi];	
	h[hh]++;
	for(int to : adj[cur]) dfs(to,hh+1);			
	for(auto x : qq[cur]) ans[x.se] += h[x.fi];	
}

void solve(){
	int n; cin>>n;
	rep(i,2,n+1){
		int u; cin>>u;
		adj[u].pb(i);
	}
	int q; cin>>q;
	rep(i,0,q){
		int u,d; cin>>u>>d;
		qq[u].pb({d,i});
	}
	dfs(1,0);
	rep(i,0,q){
		cout<<ans[i]<<"\n";
	}
}

int main()
{
	base();
//  freopen("input.txt", "r", stdin);
//  freopen("output.txt", "w", stdout);
	int t=1;
//	cin>>t;
	while(t--){
		solve();
	}
	return 0;
}





#include <bits/stdc++.h>
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
using namespace std;
using ll = long long;
struct Edge
{
    ll to;
    ll cost;
};
using Graph = vector<vector<ll>>;
using P = pair<ll, ll>;
#define mp make_pair
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ALL(v) (v).begin(), v.end()
const ll MOD = 1000000007;
const ll nmax = 8;
const ll INF = LLONG_MAX;
const int MAX = 510000;
bool graph[nmax][nmax];
long long fac[MAX], finv[MAX], inv[MAX];

void COMinit()
{
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++)
    {
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

ll COM(int n, int k)
{
    if (n < k)
        return 0;
    if (n < 0 || k < 0)
        return 0;

    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

// vector<vector<ll>> dist = vector<vector<ll>>(nmax, vector<ll>(nmax, INF));
// void warshall_floyd(ll n)
// {
//     for (size_t i = 0; i < n; i++)
//     {
//         for (size_t j = 0; j < n; j++)
//         {
//             for (size_t k = 0; k < n; k++)
//             {
//                 dist[j][k] = min(dist[j][k], dist[j][i] + dist[i][k]);
//             }
//         }
//     }
// }

ll gcd(ll a, ll b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}

ll lcm(ll a, ll b)
{
    ll g = gcd(a, b);
    return a / g * b;
}
struct Matrix
{
    int sz;
    ll x[33][33];
};

Matrix multiply(Matrix A, Matrix B)
{
    Matrix C;
    C.sz = A.sz;
    for (int i = 0; i < C.sz; i++)
    {
        for (int j = 0; j < C.sz; j++)
        {
            C.x[i][j] = 0;
        }
    }

    for (int i = 0; i < A.sz; i++)
    {
        for (int j = 0; j < A.sz; j++)
        {
            for (int k = 0; k < A.sz; k++)
            {
                C.x[i][k] += A.x[i][j] * B.x[j][k];
                C.x[i][k] %= MOD;
            }
        }
    }

    return C;
}

Matrix powers(Matrix A, ll T)
{
    Matrix E[64], F;
    E[0] = A;
    for (int i = 1; i < 62; i++)
    {
        E[i] = multiply(E[i - 1], E[i - 1]);
    }

    F.sz = E[0].sz;

    for (int i = 0; i < F.sz; i++)
    {
        for (int j = 0; j < F.sz; j++)
        {
            if (i == j)
                F.x[i][j] = 1;
            else
                F.x[i][j] = 0;
        }
    }

    for (int i = 62; i >= 0; i--)
    {
        if ((T & (1LL << i)) != 0LL)
        {
            F = multiply(F, E[i]);
        }
    }
    return F;
}

ll ceil(ll a, ll b)
{
    return (a + b - 1) / b;
}

vector<bool> seen;

vector<vector<ll>> children, lst;
vector<ll> in, out, depth;
ll timer;

void dfs(int v)
{
    in[v] = timer++;
    lst[depth[v]].push_back(in[v]);
    for (auto &&i : children[v])
    {
        depth[i] = depth[v] + 1;
        dfs(i);
    }
    out[v] = timer++;
}
int main()
{

    long long N;
    std::scanf("%lld", &N);
    children = lst = vector<vector<ll>>(N);
    in = out = depth = vector<ll>(N);
    for (int i = 1; i < N; i++)
    {
        ll p;
        cin >> p;
        --p;
        children[p].push_back(i);
    }
    dfs(0);

    ll Q;
    cin >> Q;
    while (Q--)
    {
        ll u, d;
        cin >> u >> d;
        --u;

        const auto &v = lst[d];
        cout << lower_bound(ALL(v), out[u]) - lower_bound(ALL(v), in[u]) << endl;
    }

    return 0;
}
