#include <bits/stdc++.h>
using namespace std;
#define ios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
#define ll long long
#define ding cerr<<endl<<"Time: "<<(clock()*1000./CLOCKS_PER_SEC)<<" ms"<<endl;
const int maxn=2e5+10;
int siz[maxn],dfn[maxn],rdfn[maxn],tot,totd,n;
vector<int> yuan[maxn],cun[maxn];
void dfs(int now,int fa,int dep){
    dfn[now]=++tot;rdfn[tot]=now;
    siz[now]=1;
    totd=max(totd,dep);
    cun[dep].push_back(dfn[now]);
    for(int i=0;i<yuan[now].size();i++){
        int v=yuan[now][i];
        if(v==fa) continue;
        dfs(v,now,dep+1);
        siz[now]+=siz[v];
    }
}
ll req(int now1,int now2,int dep){
    return upper_bound(cun[dep].begin(),cun[dep].end(),now2)-lower_bound(cun[dep].begin(),cun[dep].end(),now1);
}
signed main(){
    ios;cin>>n;
    for(int i=2;i<=n;i++){
        int a1;cin>>a1;
        yuan[i].push_back(a1);yuan[a1].push_back(i);
    }
    dfs(1,0,1);
    int q;cin>>q;
    while(q--){
        int a1,a2;cin>>a1>>a2;a2++;
        cout<<req(dfn[a1],dfn[a1]+siz[a1]-1,a2)<<endl;
    }
    ding;return 0;
}
//بسم الله الرحمن الرحيم

// ----->  Author :    N A Y E M
// ----->           [ CSE-19 , CU ]

#include<bits/stdc++.h>
using namespace std ;
typedef long long ll;
typedef unsigned long long ull ;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<double, double> pdd;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;
typedef double dl;
//using u128 = __uint128_t;
#define el << '\n'
#define PI 3.141592653589793238
#define _test_ int _ ; cin >> _ ; while( _-- )
#define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define file() freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout)
ll gcd ( ll  a, ll b ) { return __gcd ( a, b ) ; }
ll lcm ( ll a, ll b ) { return a * ( b / gcd ( a, b ) ) ; }
ll big_mod(ll base , ll power , ll mod){
    ll ret=1;base%=mod;while(power){if(power&1)ret=ret*base%mod;base=base*base%mod;power>>=1;}return ret;}
ll mod_inverse(ll a , ll mod ){ return big_mod(a,mod-2,mod);}
ull POW(ll a , ll b){if(b==0) return 1;ull ret=1;while(b--)ret*=a;return ret;}
int dx[]={0,0,+1,-1,+1,-1,-1,+1} ;
int dy[]={+1,-1,0,0,+1,-1,+1,-1} ;
// ------------------>
const ll infLL = 1e15;
const int inf = 1e8+123;
const ll mod = 1e9+7;
const ll mx = 3e5+123;
// ------------------>
pdd p[mx];


void _case_()
{
    ll n , ans = 0 ;
    cin >> n ;
    for(int i=0;i<n;i++)
    {
        cin >> p[i].first >> p[i].second ;
    }
    for(int i=0;i<n-1;i++)
        for(int j=i+1;j<n;j++)
        {
            double m = (abs(p[i].second-p[j].second)/abs(p[i].first-p[j].first));
            if(m>=-1 and m<=1) ans ++ ;
        }
    cout << ans el;
}

int main( )
{
    optimize() ;
    //_test_
       _case_() ;
    return 0 ;
}