#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<int>v[100004];
vector<int>v2[100004];
bool seen[100004];
int par[100004],L[100004],root[100004],bap[100004][17];
int r,cnt=0,start=0;
stack<int>s;
bool seen2[100004];
int ST[100005],EN[100005];
void dfs(int x,int level)
{
    if(seen[x])return;seen[x]=1;
    v2[level].push_back(start);
    ST[x]=start;start++;
    ///root[x]=r;
    L[x]=level;
    for(int i=0;i<v[x].size();i++)dfs(v[x][i],level+1);
    EN[x]=start;
    start++;
}
void counting(int n)
{
    memset(bap,-1,sizeof(bap));
    for(int i=1;i<=n;i++)bap[i][0]=par[i];

    for(int i=1;(1<<i)<=n;i++)
        for(int j=1;j<=n;j++){
                bap[j][i]=bap[bap[j][i-1]][i-1];
        }
}
int go(int x,int diff)
{
    int log;
    if(diff==0)log=0;
    else
     log=log2(diff);
    for(int i=log;i>=0;i--){
        if(L[x]-(1<<i)>=0){x=bap[x][i];
        if(x==-1 || x==100002 || x==0)break;
        }
    }
    return x;
}
int main()
{
    int n,x,q,p;scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&x);
        par[i+1]=x;
        v[x].push_back(i+1);
    }
    n++;
    for(int i=0;i<n;i++)if(seen[i]==0)
    dfs(i,0);
    ///for(int i=0;i<n;i++)cout<<"level of "<<i<<" = "<<L[i]<<endl;
    //for(int i=0;i<v2[1].size();i++)cout<<"L2 = "<<v2[1][i]<<endl;return 0;
    ///for(int i=0;i<n;i++)cout<<"size of  "<<i<<" = "<<SZ[i]<<endl;
    counting(n);
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d %d",&x,&p);
        int ans=go(x,p);
        if(ans==-1 || ans==100002 || ans==0)printf("0 ");
        else
            {
                 ans=upper_bound(v2[L[x]].begin(),v2[L[x]].end(),EN[ans])-lower_bound(v2[L[x]].begin(),v2[L[x]].end(),ST[ans]);
                 printf("%d ",ans-1);
            }

    }
    return 0;
    /*
    7
7 7 7 7 3  2 0
7
5 2

*/

}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<int>v[100004];
vector<int>v2[100004];
bool seen[100004];
int par[100004],L[100004],root[100004],bap[100004][17];
int r,cnt=0,start=0;
stack<int>s;
bool seen2[100004];
int ST[100005],EN[100005];
void dfs(int x,int level)
{
    if(seen[x])return;seen[x]=1;
    v2[level].push_back(start);
    ST[x]=start;start++;
    ///root[x]=r;
    L[x]=level;
    for(int i=0;i<v[x].size();i++)dfs(v[x][i],level+1);
    EN[x]=start;
    start++;
}
void counting(int n)
{
    for(int i=1;i<=n;i++)bap[i][0]=par[i];

    for(int i=1;(1<<i)<=n;i++)
        for(int j=1;j<=n;j++){
                bap[j][i]=bap[bap[j][i-1]][i-1];
        }
}
int go(int x,int diff)
{
    int log;
    if(diff==0)log=0;
    else
     log=log2(diff);
    for(int i=log;i>=0;i--){
        if(L[x]-(1<<i)>=0){x=bap[x][i];
        if(x==0)break;
        }
    }
    return x;
}
int main()
{
    int n,x,q,p;scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&x);
        par[i]=x;
        if(x!=0)
        v[x].push_back(i);
    }
    for(int i=1;i<=n;i++)if(seen[i]==0)dfs(i,0);
   /// for(int i=1;i<=n;i++)cout<<"level of "<<i<<" = "<<L[i]<<endl;
    //for(int i=0;i<v2[1].size();i++)cout<<"L2 = "<<v2[1][i]<<endl;return 0;
    ///for(int i=0;i<n;i++)cout<<"size of  "<<i<<" = "<<SZ[i]<<endl;
    ///cout<<"ber korlam = "<<go(2,2)<<endl;
    counting(n);
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d %d",&x,&p);
        if(L[x]<p)printf("0 ");
        else{
        int ans=go(x,p);
        ///cout<<"ans = "<<ans<<endl;
        if(ans==0)printf("0 ");
        else
            {
                 ans=upper_bound(v2[L[x]].begin(),v2[L[x]].end(),EN[ans])-lower_bound(v2[L[x]].begin(),v2[L[x]].end(),ST[ans]);
                 printf("%d ",ans-1);
            }

        }

    }
    return 0;
    /*
    7
7 7 7 7 3  2 0
7
5 2

*/

}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<int>v[100004];
vector<int>v2[100004];
bool seen[100004];
int par[100004],L[100004],root[100004],bap[100004][17];
int r,cnt=0,start=0;
stack<int>s;
bool seen2[100004];
int ST[100005],EN[100005];
void dfs(int x,int level)
{
    if(seen[x])return;seen[x]=1;
    v2[level].push_back(start);
    ST[x]=start;start++;
    ///root[x]=r;
    L[x]=level;
    for(int i=0;i<v[x].size();i++)dfs(v[x][i],level+1);
    EN[x]=start;
    start++;
}
void counting(int n)
{
    memset(bap,-1,sizeof(bap));
    for(int i=1;i<=n;i++)bap[i][0]=par[i];

    for(int i=1;(1<<i)<n;i++)
        for(int j=1;j<=n;j++){
                if(bap[j][i-1]==-1)continue;
                bap[j][i]=bap[bap[j][i-1]][i-1];
        }
}
int go(int x,int diff)
{
    int log;
    if(diff==0)log=0;
    else
     log=log2(diff);
    for(int i=log;i>=0;i--){
        if(L[x]-(1<<i)>=0){x=bap[x][i];
        if(x==-1 || x==100002 || x==0)break;
        }
    }
    return x;
}
int main()
{
    int n,x,q,p;scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&x);
        par[i+1]=x;
        v[x].push_back(i+1);
    }
    n++;
    for(int i=0;i<n;i++)if(seen[i]==0)
    dfs(i,0);
    ///for(int i=0;i<n;i++)cout<<"level of "<<i<<" = "<<L[i]<<endl;
    //for(int i=0;i<v2[1].size();i++)cout<<"L2 = "<<v2[1][i]<<endl;return 0;
    ///for(int i=0;i<n;i++)cout<<"size of  "<<i<<" = "<<SZ[i]<<endl;
    counting(n);
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d %d",&x,&p);
        int ans=go(x,p);
        if(ans==-1 || ans==100002 || ans==0)printf("0 ");
        else
            {
                 ans=upper_bound(v2[L[x]].begin(),v2[L[x]].end(),EN[ans])-lower_bound(v2[L[x]].begin(),v2[L[x]].end(),ST[ans]);
                 printf("%d ",ans-1);
            }

    }
    return 0;
    /*
    7
7 7 7 7 3  2 0
7
5 2

*/

}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
vector<int>v[100004];
bool seen[100004];
int par[100004],child[100004],L[100004],root[100004],bap[100004][17];
int r;
stack<int>s;
bool seen2[100004];

void Topsort(int x)
{
    if(seen2[x])return;seen2[x]=true;
    for(int i=0;i<v[x].size();i++)Topsort(v[x][i]);
    s.push(x);
   /// cout<<"x = "<<x<<endl;
}
void dfs(int x,int level)
{
    if(seen[x])return;seen[x]=1;
    root[x]=r;
    L[x]=level;
    if(par[x]!=100002)
    child[par[x]]++;
    for(int i=0;i<v[x].size();i++)dfs(v[x][i],level+1);
}
void counting(int n)
{
    memset(bap,-1,sizeof(bap));
    for(int i=0;i<n;i++)bap[i][0]=par[i];

    for(int i=1;(1<<i)<n;i++)
        for(int j=0;j<n;j++)bap[j][i]=bap[bap[j][i-1]][i-1];
}
int go(int x,int diff)
{
    int log=log2(diff);
    for(int i=log;i>=0;i--){
        if(L[x]-(1<<i)>=0){x=bap[x][i];
        if(x==-1)break;
        }
    }
    return x;
}
int main()
{
    int n,x,q,p;scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&x);x--;
        if(x>=0){v[x].push_back(i);
        par[i]=x;
        }
        else par[i]=100002;
    }
    for(int i=0;i<n;i++){
        if(seen2[i]==0)Topsort(i);
    }
    while(!s.empty())
    {
        int t=s.top();
        if(seen[t]==0){
        ///cout<<"go = "<<t<<endl;
        par[t]=100002,r=t;
        dfs(t,0);
        }
        s.pop();
    }
    ///for(int i=0;i<n;i++)cout<<"child of "<<i<<" = "<<child[i]<<endl;
    counting(n);
    scanf("%d",&q);
    for(int i=0;i<q;i++){
        scanf("%d %d",&x,&p);
        x--;
        int diff=L[x]-L[root[x]];
        if(p>diff)printf("0 ");
        else{
            int ans=go(x,diff+1);
            if(ans==-1 || ans==100002)printf("0 ");
            else{
                ans=child[par[x]]-1;
                printf("%d ",ans);
            }
        }
    }
    return 0;

}


#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 1, LOG = 18;

vector<int> g[ N ];
vector<pair<int,int> > query[ N ];
int st[ N ], c[ N ], en[ N ], d[ N ], sub[ N ];
int t, res[ N ], nodeAt[ N ], p[ N ][ LOG ];

void dfs(int u) {
    st[ u ] = ++t;
    nodeAt[ t ] = u;
    sub[ u ] = 1;
    for(int i = 1; i < LOG; i += 1)
        p[ u ][ i ] = p[ p[ u ][ i - 1 ] ][ i - 1 ];

    for(int v : g[ u ]) {
        dfs(v);
        sub[ u ] += sub[ v ];
    }
    en[ u ] = t;
}

void dsu(int u, int keep) {
    int bigson = -1;
    for(int v : g[ u ]) {
        if( bigson == -1 || sub[ bigson ] < sub[ v ])
            bigson = v;
    }
    for(int v : g[ u ]) {
        if( v == bigson ) continue;
        dsu(v, 0);
    }
    if( ~bigson ) dsu(bigson, 1);

    for(int v : g[ u ]) {
        if(v == bigson ) continue;
        for(int i = st[ v ]; i <= en[ v ]; i += 1) {
            int x = nodeAt[ i ];
            c[ d[ x ] ] += 1;
        }
    }
    c[ d[ u ] ] += 1;
    for(auto q : query[ u ]) {
        res[ q.second ] = c[ q.first ] - 1;
    }

    if( keep == 0 ) {
        for(int i = st[ u ]; i <= en[ u ]; i += 1) {
            int x = nodeAt[ i ];
            c[ d[ x ] ] -= 1;
        }
    }
}
int main() {
    int n;
    scanf("%d", &n);
    for(int u = 1; u <= n; u += 1) {
        int v;
        scanf("%d", &v);
        g[ v ].push_back( u );
        p[ u ][ 0 ] = v;
        d[ u ] = d[ v ] + 1;
    }

    dfs(0);

    int m;
    scanf("%d", &m);

    for(int i = 1; i <= m; i += 1) {
        int v, deep;
        scanf("%d %d", &v, &deep);

        int dx = deep, h = d[ v ];
        for (int k = 0; k < LOG; k += 1) {
            if( (h - deep - d[ v ]) >> 1 & 1 )
                v = p[ v ][ k ];
        }
        if( v == 0 ) res[ i ] = 0;
        else query[ v ].push_back( {h, i} );
    }

    dsu(0, 0);
    for(int i = 1; i <= m; i += 1) {
        printf("%d ", res[ i ]);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 1, LOG = 18;

vector<int> g[ N ];
vector<pair<int,int> > query[ N ];
int st[ N ], c[ N ], en[ N ], d[ N ], sub[ N ];
int t, res[ N ], nodeAt[ N ], p[ N ][ LOG ];

void dfs(int u, int par) {
    st[ u ] = ++t;
    if( u == 0 ) d[ u ] = 0;
    else d[ u ] = d[ par ] + 1;
    sub[ u ] = 1;
    nodeAt[ t ] = u;

    p[ u ][ 0 ] = par;
    for(int i = 1; i < LOG; i += 1)
        p[ u ][ i ] = p[ p[ u ][ i - 1 ] ][ i - 1 ];

    for(int v : g[ u ]) {
        if( v == par ) continue;
        dfs(v, u);
        sub[ u ] += sub[ v ];
    }
    en[ u ] = t;
}

void dsu(int u, int par, int keep) {
    int bigson = -1;
    for(int v : g[ u ]) {
        if( v == par ) continue;
        if( bigson == -1 || sub[ bigson ] < sub[ v ])
            bigson = v;
    }
    for(int v : g[ u ]) {
        if( v == par || v == bigson ) continue;
        dsu(v, u, 0);
    }
    if( ~bigson ) dsu(bigson, u, 1);

    for(int v : g[ u ]) {
        if( v == par || v == bigson ) continue;
        for(int i = st[ v ]; i <= en[ v ]; i += 1) {
            int x = nodeAt[ i ];
            c[ d[ x ] ] += 1;
        }
    }
    c[ d[ u ] ] += 1;
    for(auto q : query[ u ]) {
        res[ q.second ] = c[ q.first ] - 1;
    }
    if( keep == 0 ) {
        for(int i = st[ u ]; i <= en[ u ]; i += 1) {
            int x = nodeAt[ i ];
            c[ d[ x ] ] -= 1;
        }
    }
}
int main() {
    int n;
    scanf("%d", &n);
    for(int u = 1; u <= n; u += 1) {
        int v;
        scanf("%d", &v);
        g[ u ].push_back( v );
        g[ v ].push_back( u );
    }

    dfs(0, 0);

    int m;
    scanf("%d", &m);

    for(int i = 1; i <= m; i += 1) {
        int v, deep;
        scanf("%d %d", &v, &deep);

        int dx = deep, h = d[ v ];
        for (int k = 0; k < LOG; k += 1) {
            if( (h - deep - d[ v ]) >> 1 & 1 )
                v = p[ v ][ k ];
        }
        if( v == 0 ) res[ i ] = 0;
        else query[ v ].push_back( {h, i} );
    }

    dsu(0, 0, 0);
    for(int i = 1; i <= m; i += 1) {
        printf("%d ", res[ i ]);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 1, LOG = 18;

vector<int> g[ N ];
vector<pair<int,int> > query[ N ];
int st[ N ], c[ N ], en[ N ], d[ N ], sub[ N ];
int t, res[ N ], nodeAt[ N ], p[ N ][ LOG ];

void dfs(int u) {
    st[ u ] = ++t;
    nodeAt[ t ] = u;
    sub[ u ] = 1;
    for(int i = 1; i < LOG; i += 1)
        p[ u ][ i ] = p[ p[ u ][ i - 1 ] ][ i - 1 ];

    for(int v : g[ u ]) {
        dfs(v);
        sub[ u ] += sub[ v ];
    }
    en[ u ] = t;
}

void dsu(int u, int keep) {
    int bigson = -1;
    for(int v : g[ u ]) {
        if( bigson == -1 || sub[ bigson ] < sub[ v ])
            bigson = v;
    }
    for(int v : g[ u ]) {
        if( v == bigson ) continue;
        dsu(v, 0);
    }
    if( ~bigson ) dsu(bigson, 1);

    for(int v : g[ u ]) {
        if(v == bigson ) continue;
        for(int i = st[ v ]; i <= en[ v ]; i += 1) {
            int x = nodeAt[ i ];
            c[ d[ x ] ] += 1;
        }
    }
    c[ d[ u ] ] += 1;
    for(auto q : query[ u ]) {
        res[ q.second ] = c[ q.first + d[ u ] ] - 1;
    }
    if( keep == 0 ) {
        for(int i = st[ u ]; i <= en[ u ]; i += 1) {
            int x = nodeAt[ i ];
            c[ d[ x ] ] -= 1;
        }
    }
}
int main() {
    int n;
    scanf("%d", &n);
    for(int u = 1; u <= n; u += 1) {
        int v;
        scanf("%d", &v);
        g[ v ].push_back( u );
        p[ u ][ 0 ] = v;
        d[ u ] = d[ v ] + 1;
    }

    dfs(0);

    int m;
    scanf("%d", &m);

    for(int i = 1; i <= m; i += 1) {
        int v, deep;
        scanf("%d %d", &v, &deep);

        int d = deep;
        for (int k = LOG - 1; d && ~k; k -= 1) {
            if( d < (k << 1)) continue;
            d -= (k << 1);
            v = p[ v ][ k ];
        }

        if( v == 0 ) res[ i ] = 0;
        else query[ v ].push_back( {deep, i} );
    }

    dsu(0, 0);
    for(int i = 1; i <= m; i += 1) {
        printf("%d ", res[ i ]);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 1, LOG = 18;

vector<int> g[ N ];
vector<pair<int,int> > query[ N ];
int st[ N ], c[ N ], en[ N ], d[ N ], sub[ N ];
int t, res[ N ], nodeAt[ N ], p[ N ][ LOG ];

void dfs(int u, int par) {
    st[ u ] = ++t;
    if( u == 0 ) d[ u ] = 0;
    else d[ u ] = d[ par ] + 1;
    sub[ u ] = 1;
    nodeAt[ t ] = u;

    p[ u ][ 0 ] = par;
    for(int i = 1; i < LOG; i += 1)
        p[ u ][ i ] = p[ p[ u ][ i - 1 ] ][ i - 1 ];

    for(int v : g[ u ]) {
        if( v == par ) continue;
        dfs(v, u);
        sub[ u ] += sub[ v ];
    }
    en[ u ] = t;
}

void dsu(int u, int par, int keep) {
    int bigson = -1;
    for(int v : g[ u ]) {
        if( v == par ) continue;
        if( bigson == -1 || sub[ bigson ] < sub[ v ])
            bigson = v;
    }
    for(int v : g[ u ]) {
        if( v == par || v == bigson ) continue;
        dsu(v, u, 0);
    }
    if( ~bigson ) dsu(bigson, u, 1);

    for(int v : g[ u ]) {
        if( v == par || v == bigson ) continue;
        for(int i = st[ v ]; i <= en[ v ]; i += 1) {
            int x = nodeAt[ i ];
            c[ d[ x ] ] += 1;
        }
    }
    c[ d[ u ] ] += 1;
    for(auto q : query[ u ]) {
        res[ q.second ] = c[ q.first + d[ u ] ] - 1;
    }
    if( keep == 0 ) {
        for(int i = st[ u ]; i <= en[ u ]; i += 1) {
            int x = nodeAt[ i ];
            c[ d[ x ] ] -= 1;
        }
    }
}
int main() {
    int n;
    scanf("%d", &n);
    for(int u = 1; u <= n; u += 1) {
        int v;
        scanf("%d", &v);
        g[ u ].push_back( v );
        g[ v ].push_back( u );
    }

    dfs(0, 0);

    int m;
    scanf("%d", &m);

    for(int i = 1; i <= m; i += 1) {
        int v, deep;
        scanf("%d %d", &v, &deep);

        int dx = deep, h = d[ v ];
        for (int k = 0; k < LOG; k += 1) {
            if( (h - deep - d[ v ]) >> 1 & 1 )
                v = p[ v ][ k ];
        }
        if( v == 0 ) res[ i ] = 0;
        else query[ v ].push_back( {deep, i} );
    }

    dsu(0, 0, 0);
    for(int i = 1; i <= m; i += 1) {
        printf("%d ", res[ i ]);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 1, LOG = 18;

vector<int> g[ N ];
vector<pair<int,int> > query[ N ];
int c[ N ], d[ N ], sub[ N ], isBig[ N ];
int t, res[ N ], p[ N ][ LOG ];

void dfs(int u, int par) {
    sub[ u ] = 1;
    if( ~par ) d[ u ] = d[ par ] + 1;
    p[ u ][ 0 ] = par;
    for(int i = 1; i < LOG; i += 1)
        p[ u ][ i ] = p[ p[ u ][ i - 1 ] ][ i - 1 ];

    for(int v : g[ u ]) {
        if( v == par ) continue;
        dfs(v, u);
        sub[ u ] += sub[ v ];
    }
}

void add(int u, int par, int x) {
    c[ d[ u ] ] += x;
    for(int v : g[ u ]) {
        if( v == par || isBig[ v ] ) continue;
        add(v, u, x);
    }
}
void dsu(int u, int par, int keep) {
    int bigson = -1;
    for(int v : g[ u ]) {
        if( v == par ) continue;
        if( bigson == -1 || sub[ bigson ] < sub[ v ])
            bigson = v;
    }
    for(int v : g[ u ]) {
        if( v == par || v == bigson ) continue;
        dsu(v, u, 0);
    }
    if( ~bigson ) dsu(bigson, u, 1), isBig[ bigson ] = 1;

    add(u, par, 1);
    for(auto q : query[ u ]) {
        res[ q.second ] = c[ q.first + d[ u ] ] - 1;
    }
    if( ~bigson ) isBig[ bigson ] = 0;
    if( keep == 0 ) {
        add(u, par, -1);
    }
}
int main() {
    int n;
    scanf("%d", &n);
    d[ 0 ] = 0;
    for(int u = 1; u <= n; u += 1) {
        int v;
        scanf("%d", &v);
        g[ u ].push_back( v );
        g[ v ].push_back( u );
    }

    dfs(0, 0);

    int m;
    scanf("%d", &m);

    for(int i = 1; i <= m; i += 1) {
        int v, deep;
        scanf("%d %d", &v, &deep);

        int d = deep;
        for (int k = LOG - 1; d >= 0 && ~k; k -= 1) {
            if( d < (k << 1)) continue;
            d -= (k << 1);
            v = p[ v ][ k ];
        }
        if( v == 0 ) res[ i ] = 0;
        else query[ v ].push_back( {deep, i} );
    }

    dsu(0, 0, 0);
    for(int i = 1; i <= m; i += 1) {
        printf("%d ", res[ i ]);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 1, LOG = 18;

vector<int> g[ N ];
vector<pair<int,int> > query[ N ];
int c[ N ], d[ N ], sub[ N ], isBig[ N ];
int t, res[ N ], p[ N ][ LOG ];

void dfs(int u) {
    sub[ u ] = 1;
    for(int v : g[ u ]) {
        dfs(v);
        sub[ u ] += sub[ v ];
    }
}

void add(int u, int x) {
    c[ d[ u ] ] += x;
    for(int v : g[ u ]) {
        if( isBig[ v ] ) continue;
        add(v, x);
    }
}
void dsu(int u, int keep) {
    int bigson = -1;
    for(int v : g[ u ]) {
        if( bigson == -1 || sub[ bigson ] < sub[ v ])
            bigson = v;
    }
    for(int v : g[ u ]) {
        if( v == bigson ) continue;
        dsu(v, 0);
    }
    if( ~bigson ) dsu(bigson, 1), isBig[ bigson ] = 1;

    add(u, 1);
    for(auto q : query[ u ]) {
        res[ q.second ] = c[ q.first ] - 1;
    }
    if( ~bigson ) isBig[ bigson ] = 0;
    if( keep == 0 ) {
        add(u, -1);
    }
}
int main() {
    int n;
    scanf("%d", &n);

    for(int u = 1; u <= n; u += 1) {
        int v;
        scanf("%d", &v);
        g[ v ].push_back( u );
        d[ u ] = d[ v ] + 1;
        p[ u ][ 0 ] = v;
    }

    for(int i = 1; i <= n; i += 1) {
        for(int j = 1; j < LOG; j += 1) {
            p[ i ][ j ] = p[ p[ i ][ j - 1 ] ][ j - 1 ];
        }
    }

    dfs(0);

    int m;
    scanf("%d", &m);

    for(int i = 1; i <= m; i += 1) {
        int v, deep;
        scanf("%d %d", &v, &deep);

        int dx = deep, h = d[ v ];
        for (int k = 0; k < LOG; k += 1) {
            if( (h - deep - d[ v ]) >> 1 & 1 )
                v = p[ v ][ k ];
        }
        if( v == 0 ) res[ i ] = 0;
        else query[ v ].push_back( {h, i} );
    }

    dsu(0, 0);
    for(int i = 1; i <= m; i += 1) {
        printf("%d ", res[ i ]);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 1, LOG = 18;

vector<int> g[ N ];
vector<pair<int,int> > query[ N ];
int st[ N ], c[ N ], en[ N ], d[ N ], sub[ N ];
int t, res[ N ], nodeAt[ N ], p[ N ][ LOG ];

void dfs(int u, int par) {
    st[ u ] = ++t;
    if( u == 0 ) d[ u ] = 0;
    else d[ u ] = d[ par ] + 1;
    sub[ u ] = 1;
    nodeAt[ t ] = u;

    p[ u ][ 0 ] = par;
    for(int i = 1; i < LOG; i += 1)
        p[ u ][ i ] = p[ p[ u ][ i - 1 ] ][ i - 1 ];

    for(int v : g[ u ]) {
        if( v == par ) continue;
        dfs(v, u);
        sub[ u ] += sub[ v ];
    }
    en[ u ] = t;
}

void dsu(int u, int par, int keep) {
    int bigson = -1;
    for(int v : g[ u ]) {
        if( v == par ) continue;
        if( bigson == -1 || sub[ bigson ] < sub[ v ])
            bigson = v;
    }
    for(int v : g[ u ]) {
        if( v == par || v == bigson ) continue;
        dsu(v, u, 0);
    }
    if( ~bigson ) dsu(bigson, u, 1);

    for(int v : g[ u ]) {
        if( v == par || v == bigson ) continue;
        for(int i = st[ v ]; i <= en[ v ]; i += 1) {
            int x = nodeAt[ i ];
            c[ d[ x ] ] += 1;
        }
    }
    c[ d[ u ] ] += 1;
    for(auto q : query[ u ]) {
        res[ q.second ] = c[ q.first + d[ u ] ] - 1;
    }
    if( keep == 0 ) {
        for(int i = st[ u ]; i <= en[ u ]; i += 1) {
            int x = nodeAt[ i ];
            c[ d[ x ] ] -= 1;
        }
    }
}

int getKthParent(int u, int k) {
    for(int i = LOG - 1; ~i; i -= 1) {
        if( k & (1 << i) )
            u = p[ u ][ i ];
    }
    return u;
}
int main() {
    int n;
    scanf("%d", &n);
    for(int u = 1; u <= n; u += 1) {
        int v;
        scanf("%d", &v);
        g[ u ].push_back( v );
        g[ v ].push_back( u );
    }

    dfs(0, 0);

    int m;
    scanf("%d", &m);

    for(int i = 1; i <= m; i += 1) {
        int v, k;
        scanf("%d %d", &v, &k);
        v = getKthParent(v, k);
        if( v == 0 ) res[ i ] = 0;
        else query[ v ].push_back( {k, i} );
    }

    dsu(0, 0, 0);
    for(int i = 1; i <= m; i += 1) {
        printf("%d ", res[ i ]);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 1, LOG = 18;

vector<int> g[ N ];
vector<pair<int,int> > query[ N ];
int st[ N ], c[ N ], en[ N ], d[ N ], sub[ N ];
int t, res[ N ], nodeAt[ N ], p[ N ][ LOG ];

void dfs(int u, int par) {
    st[ u ] = ++t;
    if( u == 0 ) d[ u ] = 0;
    else d[ u ] = d[ par ] + 1;
    sub[ u ] = 1;
    nodeAt[ t ] = u;

    p[ u ][ 0 ] = par;
    for(int i = 1; i < LOG; i += 1)
        p[ u ][ i ] = p[ p[ u ][ i - 1 ] ][ i - 1 ];

    for(int v : g[ u ]) {
        if( v == par ) continue;
        dfs(v, u);
        sub[ u ] += sub[ v ];
    }
    en[ u ] = t;
}

void dsu(int u, int par, int keep) {
    int bigson = -1;
    for(int v : g[ u ]) {
        if( v == par ) continue;
        if( bigson == -1 || sub[ bigson ] < sub[ v ])
            bigson = v;
    }
    for(int v : g[ u ]) {
        if( v == par || v == bigson ) continue;
        dsu(v, u, 0);
    }
    if( ~bigson ) dsu(bigson, u, 1);

    for(int v : g[ u ]) {
        if( v == par || v == bigson ) continue;
        for(int i = st[ v ]; i <= en[ v ]; i += 1) {
            int x = nodeAt[ i ];
            c[ d[ x ] ] += 1;
        }
    }
    c[ d[ u ] ] += 1;
    for(auto q : query[ u ]) {
        res[ q.second ] = c[ q.first ] - 1;
    }
    if( keep == 0 ) {
        for(int i = st[ u ]; i <= en[ u ]; i += 1) {
            int x = nodeAt[ i ];
            c[ d[ x ] ] -= 1;
        }
    }
}

int getParent(int u, int k) {
    for(int i = LOG - 1; ~i; i -= 1) {
        if( k & (1 << i) )
            u = p[ u ][ i ];
    }
    return u;
}
int main() {
    int n;
    scanf("%d", &n);
    for(int u = 1; u <= n; u += 1) {
        int v;
        scanf("%d", &v);
        g[ u ].push_back( v );
        g[ v ].push_back( u );
    }

    dfs(0, 0);

    int m;
    scanf("%d", &m);

    for(int i = 1; i <= m; i += 1) {
        int v, deep;
        scanf("%d %d", &v, &deep);
        v = getParent(v, deep);
        if( v == 0 ) res[ i ] = 0;
        else query[ v ].push_back( {deep + d[ v ], i} );
    }

    dsu(0, 0, 0);
    for(int i = 1; i <= m; i += 1) {
        printf("%d ", res[ i ]);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 1, LOG = 18;

vector<int> g[ N ];
vector<pair<int,int> > query[ N ];
int st[ N ], c[ N ], en[ N ], d[ N ], sub[ N ];
int t, res[ N ], nodeAt[ N ], p[ N ][ LOG ];

void dfs(int u, int par) {
    st[ u ] = ++t;
    if( u == 0 ) d[ u ] = 0;
    else d[ u ] = d[ par ] + 1;
    sub[ u ] = 1;
    nodeAt[ t ] = u;

    p[ u ][ 0 ] = par;
    for(int i = 1; i < LOG; i += 1)
        p[ u ][ i ] = p[ p[ u ][ i - 1 ] ][ i - 1 ];

    for(int v : g[ u ]) {
        if( v == par ) continue;
        dfs(v, u);
        sub[ u ] += sub[ v ];
    }
    en[ u ] = t;
}

void dsu(int u, int par, int keep) {
    int bigson = -1;
    for(int v : g[ u ]) {
        if( v == par ) continue;
        if( bigson == -1 || sub[ bigson ] < sub[ v ])
            bigson = v;
    }
    for(int v : g[ u ]) {
        if( v == par || v == bigson ) continue;
        dsu(v, u, 0);
    }
    if( ~bigson ) dsu(bigson, u, 1);

    for(int v : g[ u ]) {
        if( v == par || v == bigson ) continue;
        for(int i = st[ v ]; i <= en[ v ]; i += 1) {
            int x = nodeAt[ i ];
            c[ d[ x ] ] += 1;
        }
    }
    c[ d[ u ] ] += 1;
    for(auto q : query[ u ]) {
        res[ q.second ] = c[ q.first + d[ u ] ] - 1;
    }
    if( keep == 0 ) {
        for(int i = st[ u ]; i <= en[ u ]; i += 1) {
            int x = nodeAt[ i ];
            c[ d[ x ] ] -= 1;
        }
    }
}
int main() {
    int n;
    scanf("%d", &n);
    for(int u = 1; u <= n; u += 1) {
        int v;
        scanf("%d", &v);
        g[ u ].push_back( v );
        g[ v ].push_back( u );
    }

    dfs(0, 0);

    int m;
    scanf("%d", &m);

    for(int i = 1; i <= m; i += 1) {
        int v, deep;
        scanf("%d %d", &v, &deep);

        int d = deep;
        for (int k = LOG - 1; d && ~k; k -= 1) {
            if( d < (k << 1)) continue;
            d -= (k << 1);
            v = p[ v ][ k ];
        }
        if( v == 0 ) res[ i ] = 0;
        else query[ v ].push_back( {deep, i} );
    }

    dsu(0, 0, 0);
    for(int i = 1; i <= m; i += 1) {
        printf("%d ", res[ i ]);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 1, LOG = 18;

vector<int> g[ N ];
vector<pair<int,int> > query[ N ];
int st[ N ], c[ N ], en[ N ], d[ N ], sub[ N ];
int t, res[ N ], nodeAt[ N ], p[ N ][ LOG ];

void dfs(int u) {
    st[ u ] = ++t;
    nodeAt[ t ] = u;
    sub[ u ] = 1;
    for(int i = 1; i < LOG; i += 1)
        p[ u ][ i ] = p[ p[ u ][ i - 1 ] ][ i - 1 ];

    for(int v : g[ u ]) {
        dfs(v);
        sub[ u ] += sub[ v ];
    }
    en[ u ] = t;
}

void dsu(int u, int keep) {
    int bigson = -1;
    for(int v : g[ u ]) {
        if( bigson == -1 || sub[ bigson ] < sub[ v ])
            bigson = v;
    }
    for(int v : g[ u ]) {
        if( v == bigson ) continue;
        dsu(v, 0);
    }
    if( ~bigson ) dsu(bigson, 1);

    for(int v : g[ u ]) {
        if(v == bigson ) continue;
        for(int i = st[ v ]; i <= en[ v ]; i += 1) {
            int x = nodeAt[ i ];
            c[ d[ x ] ] += 1;
        }
    }
    c[ d[ u ] ] += 1;
    for(auto q : query[ u ]) {
        res[ q.second ] = c[ q.first ] - 1;
    }

    if( keep == 0 ) {
        for(int i = st[ u ]; i <= en[ u ]; i += 1) {
            int x = nodeAt[ i ];
            c[ d[ x ] ] -= 1;
        }
    }
}
int main() {
    int n;
    scanf("%d", &n);
    for(int u = 1; u <= n; u += 1) {
        int v;
        scanf("%d", &v);
        g[ v ].push_back( u );
        p[ u ][ 0 ] = v;
        d[ u ] = d[ v ] + 1;
    }

    dfs(0);

    int m;
    scanf("%d", &m);

    for(int i = 1; i <= m; i += 1) {
        int v, deep;
        scanf("%d %d", &v, &deep);

        int dx = deep, h = d[ v ];
        for (int k = LOG - 1; dx && ~k; k -= 1) {
            if( dx < (k << 1)) continue;
            dx -= (k << 1);
            v = p[ v ][ k ];
        }

        if( v == 0 ) res[ i ] = 0;
        else query[ v ].push_back( {h, i} );
    }

    dsu(0, 0);
    for(int i = 1; i <= m; i += 1) {
        printf("%d ", res[ i ]);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 1, LOG = 18;

vector<int> g[ N ];
vector<pair<int,int> > query[ N ];
int c[ N ], d[ N ], sub[ N ], isBig[ N ];
int t, res[ N ], p[ N ][ LOG ];

void dfs(int u) {
    sub[ u ] = 1;
    for(int v : g[ u ]) {
        dfs(v);
        sub[ u ] += sub[ v ];
    }
}

void add(int u, int x) {
    c[ d[ u ] ] += x;
    for(int v : g[ u ]) {
        if( isBig[ v ] ) continue;
        add(v, x);
    }
}
void dsu(int u, int keep) {
    int bigson = -1;
    for(int v : g[ u ]) {
        if( bigson == -1 || sub[ bigson ] < sub[ v ])
            bigson = v;
    }
    for(int v : g[ u ]) {
        if( v == bigson ) continue;
        dsu(v, 0);
    }
    if( ~bigson ) dsu(bigson, 1), isBig[ bigson ] = 1;

    add(u, 1);
    for(auto q : query[ u ]) {
        res[ q.second ] = c[ q.first ] - 1;
    }
    if( ~bigson ) isBig[ bigson ] = 0;
    if( keep == 0 ) {
        add(u, -1);
    }
}
int main() {
    int n;
    scanf("%d", &n);

    for(int u = 1; u <= n; u += 1) {
        int v;
        scanf("%d", &v);
        g[ v ].push_back( u );
        d[ u ] = d[ v ] + 1;
        p[ u ][ 0 ] = v;
    }

    for(int i = 1; i <= n; i += 1) {
        for(int j = 1; j < LOG; j += 1) {
            p[ i ][ j ] = p[ p[ i ][ j - 1 ] ][ j - 1 ];
        }
    }

    dfs(0);

    int m;
    scanf("%d", &m);

    for(int i = 1; i <= m; i += 1) {
        int v, deep;
        scanf("%d %d", &v, &deep);

        int dx = deep, h = d[ v ];
        for (int k = LOG - 1; dx >= 0 && ~k; k -= 1) {
            if( dx < (k << 1)) continue;
            dx -= (k << 1);
            v = p[ v ][ k ];
        }
        if( v == 0 ) res[ i ] = 0;
        else query[ v ].push_back( {h, i} );
    }

    dsu(0, 0);
    for(int i = 1; i <= m; i += 1) {
        printf("%d ", res[ i ]);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 1, LOG = 18;

vector<int> g[ N ];
vector<pair<int,int> > query[ N ];
int c[ N ], d[ N ], sub[ N ], isBig[ N ];
int t, res[ N ], p[ N ][ LOG ];

void dfs(int u, int par) {
    sub[ u ] = 1;
    p[ u ][ 0 ] = par;
    for(int i = 1; i < LOG; i += 1)
        p[ u ][ i ] = p[ p[ u ][ i - 1 ] ][ i - 1 ];

    for(int v : g[ u ]) {
        if( v == par ) continue;
        dfs(v, u);
        sub[ u ] += sub[ v ];
    }
}

void add(int u, int x) {
    c[ d[ u ] ] += x;
    for(int v : g[ u ]) {
        if( isBig[ v ] ) continue;
        add(v, x);
    }
}
void dsu(int u, int keep) {
    int bigson = -1;
    for(int v : g[ u ]) {
        if( bigson == -1 || sub[ bigson ] < sub[ v ])
            bigson = v;
    }
    for(int v : g[ u ]) {
        if( v == bigson ) continue;
        dsu(v, 0);
    }
    if( ~bigson ) dsu(bigson, 1), isBig[ bigson ] = 1;

    add(u, 1);
    for(auto q : query[ u ]) {
        res[ q.second ] = c[ q.first ] - 1;
    }
    if( ~bigson ) isBig[ bigson ] = 0;
    if( keep == 0 ) {
        add(u, -1);
    }
}
int main() {
    int n;
    scanf("%d", &n);
    d[ 0 ] = 0;
    for(int u = 1; u <= n; u += 1) {
        int v;
        scanf("%d", &v);
        g[ v ].push_back( u );
        d[ u ] = d[ v ] + 1;
    }

    dfs(0, 0);

    int m;
    scanf("%d", &m);

    for(int i = 1; i <= m; i += 1) {
        int v, deep;
        scanf("%d %d", &v, &deep);

        int dx = deep, h = d[ v ];
        for (int k = LOG - 1; dx >= 0 && ~k; k -= 1) {
            if( dx < (k << 1)) continue;
            dx -= (k << 1);
            v = p[ v ][ k ];
        }
        if( v == 0 ) res[ i ] = 0;
        else query[ v ].push_back( {h, i} );
    }

    dsu(0, 0);
    for(int i = 1; i <= m; i += 1) {
        printf("%d ", res[ i ]);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 1, LOG = 18;

vector<int> g[ N ];
vector<pair<int,int> > query[ N ];
int st[ N ], c[ N ], en[ N ], d[ N ], sub[ N ];
int t, res[ N ], nodeAt[ N ], p[ N ][ LOG ];

void dfs(int u, int par) {
    st[ u ] = ++t;
    if( u == 0 ) d[ u ] = 0;
    else d[ u ] = d[ par ] + 1;
    sub[ u ] = 1;
    nodeAt[ t ] = u;

    p[ u ][ 0 ] = par;
    for(int i = 1; i < LOG; i += 1)
        p[ u ][ i ] = p[ p[ u ][ i - 1 ] ][ i - 1 ];

    for(int v : g[ u ]) {
        if( v == par ) continue;
        dfs(v, u);
        sub[ u ] += sub[ v ];
    }
    en[ u ] = t;
}

void dsu(int u, int par, int keep) {
    int bigson = -1;
    for(int v : g[ u ]) {
        if( v == par ) continue;
        if( bigson == -1 || sub[ bigson ] < sub[ v ])
            bigson = v;
    }
    for(int v : g[ u ]) {
        if( v == par || v == bigson ) continue;
        dsu(v, u, 0);
    }
    if( ~bigson ) dsu(bigson, u, 1);

    for(int v : g[ u ]) {
        if( v == par || v == bigson ) continue;
        for(int i = st[ v ]; i <= en[ v ]; i += 1) {
            int x = nodeAt[ i ];
            c[ d[ x ] ] += 1;
        }
    }
    c[ d[ u ] ] += 1;
    for(auto q : query[ u ]) {
        res[ q.second ] = c[ q.first ] - 1;
    }
    if( keep == 0 ) {
        for(int i = st[ u ]; i <= en[ u ]; i += 1) {
            int x = nodeAt[ i ];
            c[ d[ x ] ] -= 1;
        }
    }
}
int main() {
    int n;
    scanf("%d", &n);
    for(int u = 1; u <= n; u += 1) {
        int v;
        scanf("%d", &v);
        g[ u ].push_back( v );
        g[ v ].push_back( u );
    }

    dfs(0, 0);

    int m;
    scanf("%d", &m);

    for(int i = 1; i <= m; i += 1) {
        int v, deep;
        scanf("%d %d", &v, &deep);

        int dx = deep;
        for (int k = LOG - 1; dx && ~k; k -= 1) {
            if( dx < (k << 1)) continue;
            dx -= (k << 1);
            v = p[ v ][ k ];
        }
        if( v == 0 ) res[ i ] = 0;
        else query[ v ].push_back( {deep + d[ v ], i} );
    }

    dsu(0, 0, 0);
    for(int i = 1; i <= m; i += 1) {
        printf("%d ", res[ i ]);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 1, LOG = 18;

vector<int> g[ N ];
vector<pair<int,int> > query[ N ];
int st[ N ], c[ N ], en[ N ], d[ N ], sub[ N ];
int t, res[ N ], nodeAt[ N ], p[ N ][ LOG ];

void dfs(int u, int par) {
    st[ u ] = ++t;
    if( u == 0 ) d[ u ] = 0;
    else d[ u ] = d[ par ] + 1;
    sub[ u ] = 1;
    nodeAt[ t ] = u;

    p[ u ][ 0 ] = par;
    for(int i = 1; i < LOG; i += 1)
        p[ u ][ i ] = p[ p[ u ][ i - 1 ] ][ i - 1 ];

    for(int v : g[ u ]) {
        if( v == par ) continue;
        dfs(v, u);
        sub[ u ] += sub[ v ];
    }
    en[ u ] = t;
}

void dsu(int u, int par, int keep) {
    int bigson = -1;
    for(int v : g[ u ]) {
        if( v == par ) continue;
        if( bigson == -1 || sub[ bigson ] < sub[ v ])
            bigson = v;
    }
    for(int v : g[ u ]) {
        if( v == par || v == bigson ) continue;
        dsu(v, u, 0);
    }
    if( ~bigson ) dsu(bigson, u, 1);

    for(int v : g[ u ]) {
        if( v == par || v == bigson ) continue;
        for(int i = st[ v ]; i <= en[ v ]; i += 1) {
            int x = nodeAt[ i ];
            c[ d[ x ] ] += 1;
        }
    }
    c[ d[ u ] ] += 1;
    for(auto q : query[ u ]) {
        res[ q.second ] = c[ q.first + d[ u ] ] - 1;
    }
    if( keep == 0 ) {
        for(int i = st[ u ]; i <= en[ u ]; i += 1) {
            int x = nodeAt[ i ];
            c[ d[ x ] ] -= 1;
        }
    }
}
int main() {
    int n;
    scanf("%d", &n);
    for(int u = 1; u <= n; u += 1) {
        int v;
        scanf("%d", &v);
        g[ u ].push_back( v );
        g[ v ].push_back( u );
    }

    dfs(0, 0);

    int m;
    scanf("%d", &m);

    for(int i = 1; i <= m; i += 1) {
        int v, deep;
        scanf("%d %d", &v, &deep);

        int dx = deep, h = d[ v ];
        for (int k = LOG - 1; dx >= 0 && ~k; k -= 1) {
            if( dx < (k << 1)) continue;
            dx -= (k << 1);
            v = p[ v ][ k ];
        }
        if( v == 0 ) res[ i ] = 0;
        else query[ v ].push_back( {h, i} );
    }

    dsu(0, 0, 0);
    for(int i = 1; i <= m; i += 1) {
        printf("%d ", res[ i ]);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 1, LOG = 18;

vector<int> g[ N ];
vector<pair<int,int> > query[ N ];
int st[ N ], c[ N ], en[ N ], d[ N ], sub[ N ];
int t, res[ N ], nodeAt[ N ], p[ N ][ LOG ];

void dfs(int u, int par) {
    st[ u ] = ++t;
    if( u == 0 ) d[ u ] = 0;
    else d[ u ] = d[ par ] + 1;
    sub[ u ] = 1;
    nodeAt[ t ] = u;

    p[ u ][ 0 ] = par;
    for(int i = 1; i < LOG; i += 1)
        p[ u ][ i ] = p[ p[ u ][ i - 1 ] ][ i - 1 ];

    for(int v : g[ u ]) {
        if( v == par ) continue;
        dfs(v, u);
        sub[ u ] += sub[ v ];
    }
    en[ u ] = t;
}

void dsu(int u, int par, int keep) {
    int bigson = -1;
    for(int v : g[ u ]) {
        if( v == par ) continue;
        if( bigson == -1 || sub[ bigson ] < sub[ v ])
            bigson = v;
    }
    for(int v : g[ u ]) {
        if( v == par || v == bigson ) continue;
        dsu(v, u, 0);
    }
    if( ~bigson ) dsu(bigson, u, 1);

    for(int v : g[ u ]) {
        if( v == par || v == bigson ) continue;
        for(int i = st[ v ]; i <= en[ v ]; i += 1) {
            int x = nodeAt[ i ];
            c[ d[ x ] ] += 1;
        }
    }
    c[ d[ u ] ] += 1;
    for(auto q : query[ u ]) {
        res[ q.second ] = c[ q.first ] - 1;
    }
    if( keep == 0 ) {
        for(int i = st[ u ]; i <= en[ u ]; i += 1) {
            int x = nodeAt[ i ];
            c[ d[ x ] ] -= 1;
        }
    }
}
int main() {
    int n;
    scanf("%d", &n);
    for(int u = 1; u <= n; u += 1) {
        int v;
        scanf("%d", &v);
        g[ u ].push_back( v );
        g[ v ].push_back( u );
    }

    dfs(0, 0);

    int m;
    scanf("%d", &m);

    for(int i = 1; i <= m; i += 1) {
        int v, deep;
        scanf("%d %d", &v, &deep);

        int dx = deep, h = d[ v ];
        for (int k = LOG - 1; dx >= 0 && ~k; k -= 1) {
            if( dx < (k << 1)) continue;
            dx -= (k << 1);
            v = p[ v ][ k ];
        }
        if( v == 0 ) res[ i ] = 0;
        else query[ v ].push_back( {h, i} );
    }

    dsu(0, 0, 0);
    for(int i = 1; i <= m; i += 1) {
        printf("%d ", res[ i ]);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 1, LOG = 18;

vector<int> g[ N ];
vector<pair<int,int> > query[ N ];
int c[ N ], d[ N ], sub[ N ];
int t, res[ N ], p[ N ][ LOG ];

void dfs(int u, int par) {
    sub[ u ] = 1;
    p[ u ][ 0 ] = par;
    for(int i = 1; i < LOG; i += 1)
        p[ u ][ i ] = p[ p[ u ][ i - 1 ] ][ i - 1 ];

    for(int v : g[ u ]) {
        if( v == par ) continue;
        dfs(v, u);
        sub[ u ] += sub[ v ];
    }
}

void add(int u, int par, int x) {
    c[ d[ u ] ] += x;
    for(int v : g[ u ]) {
        if( v == par ) continue;
        add(v, u, x);
    }
}
void dsu(int u, int par, int keep) {
    int bigson = -1;
    for(int v : g[ u ]) {
        if( v == par ) continue;
        if( bigson == -1 || sub[ bigson ] < sub[ v ])
            bigson = v;
    }
    for(int v : g[ u ]) {
        if( v == par || v == bigson ) continue;
        dsu(v, u, 0);
    }
    if( ~bigson ) dsu(bigson, u, 1);

    for(int v : g[ u ]) {
        if( v == par || v == bigson ) continue;
        add(v, u, 1);
    }
    c[ d[ u ] ] += 1;
    for(auto q : query[ u ]) {
        res[ q.second ] = c[ q.first + d[ u ] ] - 1;
    }
    if( keep == 0 ) {
        add(u, par, -1);
    }
}
int main() {
    int n;
    scanf("%d", &n);
    d[ 0 ] = 0;
    for(int u = 1; u <= n; u += 1) {
        int v;
        scanf("%d", &v);
        d[ u ] = d[ v ] + 1;
        g[ u ].push_back( v );
        g[ v ].push_back( u );
    }

    dfs(0, 0);

    int m;
    scanf("%d", &m);

    for(int i = 1; i <= m; i += 1) {
        int v, deep;
        scanf("%d %d", &v, &deep);

        int d = deep;
        for (int k = LOG - 1; d >= 0 && ~k; k -= 1) {
            if( d < (k << 1)) continue;
            d -= (k << 1);
            v = p[ v ][ k ];
        }
        if( v == 0 ) res[ i ] = 0;
        else query[ v ].push_back( {deep, i} );
    }

    dsu(0, 0, 0);
    for(int i = 1; i <= m; i += 1) {
        printf("%d ", res[ i ]);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 1, LOG = 18;

vector<int> g[ N ];
vector<pair<int,int> > query[ N ];
int st[ N ], c[ N ], en[ N ], d[ N ], sub[ N ];
int t, res[ N ], nodeAt[ N ], p[ N ][ LOG ];

void dfs(int u, int par) {
    st[ u ] = ++t;
    if( u == 0 ) d[ u ] = 0;
    else d[ u ] = d[ par ] + 1;
    sub[ u ] = 1;
    nodeAt[ t ] = u;

    p[ u ][ 0 ] = par;
    for(int i = 1; i < LOG; i += 1)
        p[ u ][ i ] = p[ p[ u ][ i - 1 ] ][ i - 1 ];

    for(int v : g[ u ]) {
        if( v == par ) continue;
        dfs(v, u);
        sub[ u ] += sub[ v ];
    }
    en[ u ] = t;
}

void dsu(int u, int par, int keep) {
    int bigson = -1;
    for(int v : g[ u ]) {
        if( v == par ) continue;
        if( bigson == -1 || sub[ bigson ] < sub[ v ])
            bigson = v;
    }
    for(int v : g[ u ]) {
        if( v == par || v == bigson ) continue;
        dsu(v, u, 0);
    }
    if( ~bigson ) dsu(bigson, u, 1);

    for(int v : g[ u ]) {
        if( v == par || v == bigson ) continue;
        for(int i = st[ v ]; i <= en[ v ]; i += 1) {
            int x = nodeAt[ i ];
            c[ d[ x ] ] += 1;
        }
    }
    c[ d[ u ] ] += 1;
    for(auto q : query[ u ]) {
        res[ q.second ] = c[ q.first ] - 1;
    }
    if( keep == 0 ) {
        for(int i = st[ u ]; i <= en[ u ]; i += 1) {
            int x = nodeAt[ i ];
            c[ d[ x ] ] -= 1;
        }
    }
}
int main() {
    int n;
    scanf("%d", &n);
    for(int u = 1; u <= n; u += 1) {
        int v;
        scanf("%d", &v);
        g[ u ].push_back( v );
        g[ v ].push_back( u );
    }

    dfs(0, 0);

    int m;
    scanf("%d", &m);

    for(int i = 1; i <= m; i += 1) {
        int v, deep;
        scanf("%d %d", &v, &deep);

        int dx = deep, h = d[ v ];
        for (int k = 0; k < LOG; k += 1) {
            if( (h - deep - d[ v ]) >> 1 & 1 )
                v = p[ v ][ k ];
        }
        if( v == 0 ) res[ i ] = 0;
        else query[ v ].push_back( { d[ v ] + deep, i} );
    }

    dsu(0, 0, 0);
    for(int i = 1; i <= m; i += 1) {
        printf("%d ", res[ i ]);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 1, LOG = 18;

vector<int> g[ N ];
vector<pair<int,int> > query[ N ];
int st[ N ], c[ N ], en[ N ], d[ N ], sub[ N ];
int t, res[ N ], nodeAt[ N ], p[ N ][ LOG ];

void dfs(int u, int par) {
    st[ u ] = ++t;
    if( u == 0 ) d[ u ] = 0;
    else d[ u ] = d[ par ] + 1;
    sub[ u ] = 1;
    nodeAt[ t ] = u;

    p[ u ][ 0 ] = par;
    for(int i = 1; i < LOG; i += 1)
        p[ u ][ i ] = p[ p[ u ][ i - 1 ] ][ i - 1 ];

    for(int v : g[ u ]) {
        if( v == par ) continue;
        dfs(v, u);
        sub[ u ] += sub[ v ];
    }
    en[ u ] = t;
}

void dsu(int u, int par, int keep) {
    int bigson = -1;
    for(int v : g[ u ]) {
        if( v == par ) continue;
        if( bigson == -1 || sub[ bigson ] < sub[ v ])
            bigson = v;
    }
    for(int v : g[ u ]) {
        if( v == par || v == bigson ) continue;
        dsu(v, u, 0);
    }
    if( ~bigson ) dsu(bigson, u, 1);

    for(int v : g[ u ]) {
        if( v == par || v == bigson ) continue;
        for(int i = st[ v ]; i <= en[ v ]; i += 1) {
            int x = nodeAt[ i ];
            c[ d[ x ] ] += 1;
        }
    }
    c[ d[ u ] ] += 1;
    for(auto q : query[ u ]) {
        res[ q.second ] = c[ q.first + d[ u ] ] - 1;
    }
    if( keep == 0 ) {
        for(int i = st[ u ]; i <= en[ u ]; i += 1) {
            int x = nodeAt[ i ];
            c[ d[ x ] ] -= 1;
        }
    }
}

int getParent(int u, int k) {
    for(int i = LOG - 1; ~i; i -= 1) {
        if( k & (1 << i) )
            u = p[ u ][ i ];
    }
    return u;
}
int main() {
    int n;
    scanf("%d", &n);
    for(int u = 1; u <= n; u += 1) {
        int v;
        scanf("%d", &v);
        g[ u ].push_back( v );
        g[ v ].push_back( u );
    }

    dfs(0, 0);

    int m;
    scanf("%d", &m);

    for(int i = 1; i <= m; i += 1) {
        int v, deep;
        scanf("%d %d", &v, &deep);
        v = getParent(v, deep);
        if( v == 0 ) res[ i ] = 0;
        else query[ v ].push_back( {deep, i} );
    }

    dsu(0, 0, 0);
    for(int i = 1; i <= m; i += 1) {
        printf("%d ", res[ i ]);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 5, LOG = 18;

vector<int> g[ N ];
vector<pair<int,int> > query[ N ];
int st[ N ], c[ N ], en[ N ], d[ N ], sub[ N ];
int t, res[ N ], nodeAt[ N ], p[ N ][ LOG ];

void dfs(int u) {
    st[ u ] = ++t;
    nodeAt[ t ] = u;
    sub[ u ] = 1;
    for(int i = 1; i < LOG; i += 1)
        p[ u ][ i ] = p[ p[ u ][ i - 1 ] ][ i - 1 ];

    for(int v : g[ u ]) {
        dfs(v);
        sub[ u ] += sub[ v ];
    }
    en[ u ] = t;
}

void dsu(int u, int keep) {
    int bigson = -1;
    for(int v : g[ u ]) {
        if( bigson == -1 || sub[ bigson ] < sub[ v ])
            bigson = v;
    }
    for(int v : g[ u ]) {
        if( v == bigson ) continue;
        dsu(v, 0);
    }
    if( ~bigson ) dsu(bigson, 1);

    for(int v : g[ u ]) {
        if(v == bigson ) continue;
        for(int i = st[ v ]; i <= en[ v ]; i += 1) {
            int x = nodeAt[ i ];
            c[ d[ x ] ] += 1;
        }
    }
    c[ d[ u ] ] += 1;
    for(auto q : query[ u ]) {
        res[ q.second ] = c[ q.first ] - 1;
    }

    if( keep == 0 ) {
        for(int i = st[ u ]; i <= en[ u ]; i += 1) {
            int x = nodeAt[ i ];
            c[ d[ x ] ] -= 1;
        }
    }
}
int main() {
    int n;
    scanf("%d", &n);
    for(int u = 1; u <= n; u += 1) {
        int v;
        scanf("%d", &v);
        g[ v ].push_back( u );
        p[ u ][ 0 ] = v;
        d[ u ] = d[ v ] + 1;
    }
    n += 1;
    dfs(0);

    int m;
    scanf("%d", &m);

    for(int i = 1; i <= m; i += 1) {
        int v, deep;
        scanf("%d %d", &v, &deep);

        int dx = deep, h = d[ v ];
        for (int k = 0; k < LOG; k += 1) {
            if( (h - deep - d[ v ]) >> 1 & 1 )
                v = p[ v ][ k ];
        }
        if( v == 0 ) res[ i ] = 0;
        else query[ v ].push_back( {h, i} );
    }

    dsu(0, 0);
    for(int i = 1; i <= m; i += 1) {
        printf("%d ", res[ i ]);
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define INF 0x3f3f3f3f
#define MAX  100005

vector<int> adj[MAX];
int par[MAX];
int level[MAX];
int table[MAX][22];
int firstOccur[MAX];
int lastOccur[MAX];
vector<int> adjOfLevel[MAX];
int n;
int timer;
void dfs(int u, int p)
{
    par[u] = p;
    firstOccur[u] = ++timer;
    int len = adj[u].size();
    for(int i = 0; i < len; i++) {
        int v = adj[u][i];
        if(v != p) {
            level[v] = level[u]+1;
            dfs(v,u);
        }
    }
    lastOccur[u] = ++timer;
}

void init()
{
    for(int i = 1; i <= n; i++) {
        table[i][0] = par[i];
    }
    for(int j = 1; (1<<j) < n; j++) {
        for(int i = 1; i <= n; i++) {
            if(table[i][j-1] != -1)
                table[i][j] = table[table[i][j-1]][j-1];
        }
    }
}

int lca(int p, int lvl)
{
    int temp;
    int log = 1;
    while(1) {
        int temp = log+1;
        if((1<<temp) > level[p])
            break;
        log++;
    }
    for(int i = log; i >= 0; i--) {
        if(level[p]-(1<<i) >= lvl)
            p = table[p][i];
    }
    return p;
}

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int q, u, v, p;
    scanf("%d",&n);
    for(int i = 1; i <= n; i++) {
        scanf("%d",&v);
        if(v) {
            adj[i].push_back(v);
            adj[v].push_back(i);
        }
    }
    for(int i = 1; i <= n; i++) {
        if(level[i] == 0) {
            //cout << i << endl;
            dfs(i,-1);
        }
    }
    for(int i = 1; i <= n; i++) {
        adjOfLevel[level[i]].push_back(firstOccur[i]);
    }
    for(int i = 0; i < n; i++) {
        sort(adjOfLevel[i].begin(),adjOfLevel[i].end());
    }
    init();
    scanf("%d",&q);

    while(q--) {
        scanf("%d %d",&v,&p);
        //cout << endl << v << " --> " << p << endl;
        //cout << level[v] << "   " << p << endl;
        if(level[v]-p < 0) {
            printf("0 ");
            continue;
        }
        int ancestor = lca(v,level[v]-p);
        int x = lower_bound(adjOfLevel[level[v]].begin(),adjOfLevel[level[v]].end(),firstOccur[ancestor]) - adjOfLevel[level[v]].begin();
        int y = upper_bound(adjOfLevel[level[v]].begin(),adjOfLevel[level[v]].end(),lastOccur[ancestor]) - adjOfLevel[level[v]].begin();
        printf("%d ",y-x-1);
    }
}


#include <bits/stdc++.h>

using namespace std;

#define INF 0x3f3f3f3f
#define MAX  100005

vector<int> adj[MAX];
int par[MAX];
int level[MAX];
int table[MAX][22];
vector<int> levelOfDecendents[MAX];
int n;

void dfs(int u, int p)
{
    par[u] = p;
    int len = adj[u].size();
    for(int i = 0; i < len; i++) {
        int v = adj[u][i];
        if(v != p) {
            level[v] = level[u]+1;
            levelOfDecendents[u].push_back(level[v]);
            dfs(v,u);
            levelOfDecendents[u].insert(levelOfDecendents[u].end(),levelOfDecendents[v].begin(),levelOfDecendents[v].end());
        }
    }
}

void init()
{
    for(int i = 1; i <= n; i++) {
        table[i][0] = par[i];
    }
    for(int j = 1; (1<<j) < n; j++) {
        for(int i = 1; i <= n; i++) {
            if(table[i][j-1] != -1)
                table[i][j] = table[table[i][j-1]][j-1];
        }
    }
}

int lca(int p, int lvl)
{
    int temp;
    int log = 1;
    while(1) {
        int temp = log+1;
        if((1<<temp) > level[p])
            break;
        log++;
    }
    for(int i = log; i >= 0; i--) {
        if(level[p]-(1<<i) >= lvl)
            p = table[p][i];
    }
    return p;
}

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int q, u, v, p;
    scanf("%d",&n);
    for(int i = 1; i <= n; i++) {
        scanf("%d",&v);
        if(v) {
            adj[i].push_back(v);
            adj[v].push_back(i);
        }
    }
    for(int i = 1; i <= n; i++) {
        if(level[i] == 0) {
            //cout << i << endl;
            dfs(i,-1);
        }
    }
    init();
    scanf("%d",&q);

    for(int i = 1; i <= n; i++)
        sort(levelOfDecendents[i].begin(),levelOfDecendents[i].end());
//    for(int i = 0; i < n; i++) {
//        cout << "nodes adjcent with " << i << " in level = ";
//        for(int j = 0; j < levelOfDecendents[i].size(); j++)
//            cout << levelOfDecendents[i][j] << " ";
//        cout << endl;
//    }

    while(q--) {
        scanf("%d %d",&v,&p);
        //cout << endl << v << " --> " << p << endl;
        //cout << level[v] << "   " << p << endl;
        if(level[v]-p < 0) {
            printf("0 ");
            continue;
        }
        int ancestor = lca(v,level[v]-p);
        int x = lower_bound(levelOfDecendents[ancestor].begin(),levelOfDecendents[ancestor].end(),level[v]) - levelOfDecendents[ancestor].begin();
        int y = upper_bound(levelOfDecendents[ancestor].begin(),levelOfDecendents[ancestor].end(),level[v]) - levelOfDecendents[ancestor].begin();
        printf("%d ",y-x-1);
    }
}


#include <bits/stdc++.h>

using namespace std;

#define INF 0x3f3f3f3f
#define MAX  100005

vector<int> adj[MAX];
int par[MAX];
int level[MAX];
int table[MAX][22];
int firstOccur[MAX];
int lastOccur[MAX];
vector<int> adjOfLevel[MAX];
int n;
int timer;
void dfs(int u, int p)
{
    par[u] = p;
    firstOccur[u] = ++timer;
    int len = adj[u].size();
    for(int i = 0; i < len; i++) {
        int v = adj[u][i];
        if(v != p) {
            level[v] = level[u]+1;
            dfs(v,u);
        }
    }
    lastOccur[u] = ++timer;
}

void init()
{
    for(int i = 1; i <= n; i++) {
        table[i][0] = par[i];
    }
    for(int j = 1; (1<<j) < n; j++) {
        for(int i = 1; i <= n; i++) {
            if(table[i][j-1] != -1)
                table[i][j] = table[table[i][j-1]][j-1];
        }
    }
}

int lca(int p, int lvl)
{
    int temp;
    int log = 1;
    while(1) {
        int temp = log+1;
        if((1<<temp) > level[p])
            break;
        log++;
    }
    for(int i = log; i >= 0; i--) {
        if(level[p]-(1<<i) >= lvl)
            p = table[p][i];
    }
    return p;
}

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int q, u, v, p;
    scanf("%d",&n);
    for(int i = 1; i <= n; i++) {
        scanf("%d",&v);
        if(v) {
            adj[i].push_back(v);
            adj[v].push_back(i);
        }
    }
    for(int i = 1; i <= n; i++) {
        if(level[i] == 0) {
            //cout << i << endl;
            dfs(i,-1);
        }
    }
    for(int i = 1; i <= n; i++) {
        adjOfLevel[level[i]].push_back(firstOccur[i]);
    }
    init();
    scanf("%d",&q);

    while(q--) {
        scanf("%d %d",&v,&p);
        //cout << endl << v << " --> " << p << endl;
        //cout << level[v] << "   " << p << endl;
        if(level[v]-p < 0) {
            printf("0 ");
            continue;
        }
        int ancestor = lca(v,level[v]-p);
        int x = lower_bound(adjOfLevel[level[v]].begin(),adjOfLevel[level[v]].end(),firstOccur[ancestor]) - adjOfLevel[level[v]].begin();
        int y = upper_bound(adjOfLevel[level[v]].begin(),adjOfLevel[level[v]].end(),lastOccur[ancestor]) - adjOfLevel[level[v]].begin();
        printf("%d ",y-x-1);
    }
}


#include <bits/stdc++.h>

using namespace std;

#define INF 0x3f3f3f3f
#define MAX  100005

vector<int> adj[MAX];
int par[MAX];
int level[MAX];
int table[MAX][22];
vector<int> levelOfDecendents[MAX];
int n;

void dfs(int u, int p)
{
    par[u] = p;
    int len = adj[u].size();
    for(int i = 0; i < len; i++) {
        int v = adj[u][i];
        if(v != p) {
            level[v] = level[u]+1;
            levelOfDecendents[u].push_back(level[v]);
            dfs(v,u);
        }
    }
}

void init()
{
    for(int i = 1; i <= n; i++) {
        table[i][0] = par[i];
    }
    for(int j = 1; (1<<j) < n; j++) {
        for(int i = 1; i <= n; i++) {
            if(table[i][j-1] != -1)
                table[i][j] = table[table[i][j-1]][j-1];
        }
    }
}

int lca(int p, int lvl)
{
    int temp;
    int log = 1;
    while(1) {
        int temp = log+1;
        if((1<<temp) > level[p])
            break;
        log++;
    }
    for(int i = log; i >= 0; i--) {
        if(level[p]-(1<<i) >= lvl)
            p = table[p][i];
    }
    return p;
}

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int q, u, v, p;
    scanf("%d",&n);
    for(int i = 1; i <= n; i++) {
        scanf("%d",&v);
        if(v) {
            adj[i].push_back(v);
            adj[v].push_back(i);
        }
    }
    for(int i = 1; i <= n; i++) {
        if(level[i] == 0) {
            //cout << i << endl;
            dfs(i,-1);
        }
    }
    init();
    scanf("%d",&q);
//    for(int i = 0; i < n; i++) {
//        cout << "nodes adjcent with " << i << " in level = ";
//        for(int j = 0; j < levelOfDecendents[i].size(); j++)
//            cout << levelOfDecendents[i][j] << " ";
//        cout << endl;
//    }
    for(int i = 1; i <= n; i++)
        sort(levelOfDecendents[i].begin(),levelOfDecendents[i].end());
    while(q--) {
        scanf("%d %d",&v,&p);
        //cout << endl << v << " --> " << p << endl;
        //cout << level[v] << "   " << p << endl;
        if(level[v]-p < 0) {
            printf("0 ");
            continue;
        }
        int ancestor = lca(v,level[v]-p);
        int x = lower_bound(levelOfDecendents[ancestor].begin(),levelOfDecendents[ancestor].end(),level[v]) - levelOfDecendents[ancestor].begin();
        int y = upper_bound(levelOfDecendents[ancestor].begin(),levelOfDecendents[ancestor].end(),level[v]) - levelOfDecendents[ancestor].begin();
        printf("%d ",y-x-1);
    }
}


#include <bits/stdc++.h>

using namespace std;

#define INF 0x3f3f3f3f
#define MAX  100005

vector<int> adj[MAX];
int par[MAX];
int level[MAX];
int table[MAX][22];
vector<int> levelOfDecendents[MAX];
int n;

void dfs(int u, int p)
{
    par[u] = p;
    int len = adj[u].size();
    for(int i = 0; i < len; i++) {
        int v = adj[u][i];
        if(v != p) {
            level[v] = level[u]+1;
            levelOfDecendents[u].push_back(level[v]);
            dfs(v,u);
        }
    }
}

void init()
{
    for(int i = 1; i <= n; i++) {
        table[i][0] = par[i];
    }
    for(int j = 1; (1<<j) < n; j++) {
        for(int i = 1; i <= n; i++) {
            if(table[i][j-1] != -1)
                table[i][j] = table[table[i][j-1]][j-1];
        }
    }
}

int lca(int p, int lvl)
{
    int temp;
    int log = 1;
    while(1) {
        int temp = log+1;
        if((1<<temp) > level[p])
            break;
        log++;
    }
    for(int i = log; i >= 0; i--) {
        if(level[p]-(1<<i) >= lvl)
            p = table[p][i];
    }
    if(lvl != level[p])
        return 0;
    return p;
}

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int q, u, v, p;
    scanf("%d",&n);
    for(int i = 1; i <= n; i++) {
        scanf("%d",&v);
        if(v) {
            adj[i].push_back(v);
            adj[v].push_back(i);
        }
    }
    for(int i = 1; i <= n; i++) {
        if(level[i] == 0) {
            //cout << i << endl;
            dfs(i,-1);
        }
    }
    init();
    scanf("%d",&q);
//    for(int i = 0; i < n; i++) {
//        cout << "nodes adjcent with " << i << " in level = ";
//        for(int j = 0; j < levelOfDecendents[i].size(); j++)
//            cout << levelOfDecendents[i][j] << " ";
//        cout << endl;
//    }
    while(q--) {
        scanf("%d %d",&v,&p);
        //cout << endl << v << " --> " << p << endl;
        //cout << level[v] << "   " << p << endl;
        int ancestor = lca(v,level[v]-p);
        if(ancestor == 0) {
            printf("0 ");
            continue;
        }
        int x = lower_bound(levelOfDecendents[ancestor].begin(),levelOfDecendents[ancestor].end(),level[v]) - levelOfDecendents[ancestor].begin();
        int y = upper_bound(levelOfDecendents[ancestor].begin(),levelOfDecendents[ancestor].end(),level[v]) - levelOfDecendents[ancestor].begin();
        printf("%d ",y-x-1);
    }
}


#include <bits/stdc++.h>

using namespace std;

#define INF 0x3f3f3f3f
#define MAX  100005

vector<int> adj[MAX];
int par[MAX];
int level[MAX];
int table[MAX][22];
vector<int> levelOfDecendents[MAX];
int n;

void dfs(int u, int p)
{
    par[u] = p;
    int len = adj[u].size();
    for(int i = 0; i < len; i++) {
        int v = adj[u][i];
        if(v != p) {
            level[v] = level[u]+1;
            levelOfDecendents[u].push_back(level[v]);
            dfs(v,u);
        }
    }
}

void init()
{
    for(int i = 1; i <= n; i++) {
        table[i][0] = par[i];
    }
    for(int j = 1; (1<<j) < n; j++) {
        for(int i = 1; i <= n; i++) {
            if(table[i][j-1] != -1)
                table[i][j] = table[table[i][j-1]][j-1];
        }
    }
}

int lca(int p, int lvl)
{
    int temp;
    int log = 1;
    while(1) {
        int temp = log+1;
        if((1<<temp) > level[p])
            break;
        log++;
    }
    for(int i = log; i >= 0; i--) {
        if(level[p]-(1<<i) >= lvl)
            p = table[p][i];
    }
    if(lvl != level[p])
        return 0;
    return p;
}

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int q, u, v, p;
    scanf("%d",&n);
    for(int i = 1; i <= n; i++) {
        scanf("%d",&v);
        if(v) {
            adj[i].push_back(v);
            adj[v].push_back(i);
        }
    }
    for(int i = 1; i <= n; i++) {
        if(level[i] == 0) {
            //cout << i << endl;
            dfs(i,-1);
        }
    }
    init();
    scanf("%d",&q);
//    for(int i = 0; i < n; i++) {
//        cout << "nodes adjcent with " << i << " in level = ";
//        for(int j = 0; j < levelOfDecendents[i].size(); j++)
//            cout << levelOfDecendents[i][j] << " ";
//        cout << endl;
//    }
    for(int i = 1; i <= n; i++)
        sort(levelOfDecendents[i].begin(),levelOfDecendents[i].end());
    while(q--) {
        scanf("%d %d",&v,&p);
        //cout << endl << v << " --> " << p << endl;
        //cout << level[v] << "   " << p << endl;
        int ancestor = lca(v,level[v]-p);
        if(ancestor == 0) {
            printf("0 ");
            continue;
        }
        int x = lower_bound(levelOfDecendents[ancestor].begin(),levelOfDecendents[ancestor].end(),level[v]) - levelOfDecendents[ancestor].begin();
        int y = upper_bound(levelOfDecendents[ancestor].begin(),levelOfDecendents[ancestor].end(),level[v]) - levelOfDecendents[ancestor].begin();
        printf("%d ",y-x-1);
    }
}


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

int n, m;
vector<vector<int> > g;
vector<int> arr, ans;
vector<vector<pair<int, int> > > qu;

int cnt;
vector<pair<int, int> > u;
vector<vector<int> > child;
vector<int> y, cnt_y;

void dfs0(int v, int cur) {
	cnt_y[v] = cnt++;
	u[v].first = cnt_y[v];
	child[cur].push_back(v);
	for(int i = 0; i < g[v].size(); ++i) {
		dfs0(g[v][i], cur + 1);
	}
	u[v].second = cur - 1;
}

void dfs1(int v, int cur) {
	for(int i = 0; i < qu[v].size(); ++i) {
		int cur_p = qu[v][i].first,
			cur_num = qu[v][i].second;
		if(cur_p > cur) {
			ans[cur_num] = 0;
		} else {
			int ll = u[y[cur - cur_p]].first,
				rr = u[y[cur - cur_p]].second;
			int l = 0,
				r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = ((l + r) >> 1) + ((l + r) & 1),
					pp = cnt_y[child[cur][mid]];
				if(pp <= rr) {
					l = mid;
				} else {
					r = mid - 1;
				}
			}
			rr = r;
			l = 0,
				r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = (l + r) >> 1,
					pp = cnt_y[child[cur][mid]];
				if(pp >= l) {
					r = mid;
				} else {
					l = mid + 1;
				}
			}
			ll = l;
			ans[cur_num] = rr - ll + 1;
		}
	}
	y.push_back(v);
	for(int i = 0; i < g[v].size(); ++i) {
		dfs1(g[v][i], cur + 1);
	}
	y.pop_back();
}

void solve(int num) {
	cnt = 0;
	child.clear();
	child.resize(n);
	dfs0(num, 0);
	dfs1(num, 0);
}

int main (int argc, char *argv[])
{
	scanf("%d", &n);
	g.resize(n);
	qu.resize(n);
	cnt_y.resize(n);
	u.resize(n);
	for(int i = 0; i < n; ++i) {
		int p;
		scanf("%d", &p);
		if(p) {
			--p;
			g[p].push_back(i);
		} else {
			arr.push_back(i);
		}
	}
	scanf("%d", &m);
	ans.resize(m, -1);
	for(int i = 0; i < m; ++i) {
		int v, p;
		scanf("%d%d", &v, &p);
		--v;
		qu[v].push_back(make_pair(p, i));
	}
	for(int i = 0; i < arr.size(); ++i) {
		solve(arr[i]);
	}
	for(int i = 0; i < m; ++i) {
		printf("%d ", ans[i]);
	}

	return 0;
}


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

int n, m;
vector<vector<int> > g;
vector<int> arr, ans;
vector<vector<pair<int, int> > > qu;

int cnt = 0, mx;
vector<pair<int, int> > u;
vector<vector<int> > child;
vector<int> y, cnt_y;

void dfs0(int v, int cur) {
	cnt_y[v] = cnt++;
	u[v].first = cnt_y[v];
	child[cur].push_back(v);
	for(int i = 0; i < g[v].size(); ++i) {
		dfs0(g[v][i], cur + 1);
	}
	u[v].second = cnt - 1;
}

void dfs1(int v, int cur) {
	y.push_back(v);
	for(int i = 0; i < qu[v].size(); ++i) {
		int cur_p = qu[v][i].first,
			cur_num = qu[v][i].second;
		if(cur_p > cur) {
			ans[cur_num] = 0;
		} else {
			int ll = u[y[cur - cur_p]].first,
				rr = u[y[cur - cur_p]].second;
			int l = 0,
				r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = ((l + r) >> 1) + ((l + r) & 1),
					pp = cnt_y[child[cur][mid]];
				if(pp <= rr) {
					l = mid;
				} else {
					r = mid - 1;
				}
			}
			rr = r;
			l = 0,
				r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = (l + r) >> 1,
					pp = cnt_y[child[cur][mid]];
				if(pp >= ll) {
					r = mid;
				} else {
					l = mid + 1;
				}
			}
			ll = l;
			ans[cur_num] = rr - ll;
			ll = u[y[cur - cur_p + 1]].first,
			rr = u[y[cur - cur_p + 1]].second;
			l = 0,
				r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = ((l + r) >> 1) + ((l + r) & 1),
					pp = cnt_y[child[cur][mid]];
				if(pp <= rr) {
					l = mid;
				} else {
					r = mid - 1;
				}
			}
			rr = r;
			l = 0,
				r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = (l + r) >> 1,
					pp = cnt_y[child[cur][mid]];
				if(pp >= ll) {
					r = mid;
				} else {
					l = mid + 1;
				}
			}
			ll = l;
			ans[cur_num] -= rr - ll;
		}
	}
	for(int i = 0; i < g[v].size(); ++i) {
		dfs1(g[v][i], cur + 1);
	}
	y.pop_back();
}

bool cmp(const int &a, const int &b) {
	return cnt_y[a] < cnt_y[b];
}

int main (int argc, char *argv[])
{
	scanf("%d", &n);
	child.resize(n);
	g.resize(n);
	qu.resize(n);
	cnt_y.resize(n);
	u.resize(n);
	for(int i = 0; i < n; ++i) {
		int p;
		scanf("%d", &p);
		if(p) {
			--p;
			g[p].push_back(i);
		} else {
			arr.push_back(i);
		}
	}
	scanf("%d", &m);
	ans.resize(m, -1);
	for(int i = 0; i < m; ++i) {
		int v, p;
		scanf("%d%d", &v, &p);
		--v;
		qu[v].push_back(make_pair(p, i));
	}
	for(int i = 0; i < arr.size(); ++i) {
		dfs0(arr[i], 0);
	}
	for(int i = 0; i < arr.size(); ++i) {
		dfs1(arr[i], 0);
	}
	for(int i = 0; i < m; ++i) {
		printf("%d ", ans[i]);
	}

	return 0;
}


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

int n, m;
vector<vector<int> > g;
vector<int> arr, ans;
vector<vector<pair<int, int> > > qu;

int cnt;
vector<pair<int, int> > u;
vector<vector<int> > child;
vector<int> y, cnt_y;

void dfs0(int v, int cur) {
	cnt_y[v] = cur++;
	u[v].first = cnt_y[v];
	child[cur].push_back(v);
	for(int i = 0; i < g[v].size(); ++i) {
		dfs0(g[v][i], cur + 1);
	}
	u[v].second = cur - 1;
}

void dfs1(int v, int cur) {
	for(int i = 0; i < qu[v].size(); ++i) {
		int cur_p = qu[v][i].first,
			cur_num = qu[v][i].second;
		if(cur_p > cur) {
			ans[cur_num] = 0;
		} else {
			int ll = u[cur - cur_p].first,
				rr = u[cur - cur_p].second;
			int l = 0,
				r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = ((l + r) >> 1) + ((l + r) & 1),
					pp = cnt_y[child[cur][mid]];
				if(pp <= rr) {
					l = mid;
				} else {
					r = mid - 1;
				}
			}
			rr = r;
			l = 0,
				r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = (l + r) >> 1,
					pp = cnt_y[child[cur][mid]];
				if(pp >= l) {
					r = mid;
				} else {
					l = mid + 1;
				}
			}
			ll = l;
			ans[cur_num] = rr - ll + 1;
		}
	}
	y.push_back(v);
	for(int i = 0; i < g[v].size(); ++i) {
		dfs1(g[v][i], cur + 1);
	}
	y.pop_back();
}

void solve(int num) {
	cnt = 0;
	child.clear();
	child.resize(n);
	dfs0(num, 0);
	dfs1(num, 0);
}

int main (int argc, char *argv[])
{
	scanf("%d", &n);
	g.resize(n);
	qu.resize(n);
	cnt_y.resize(n);
	u.resize(n);
	for(int i = 0; i < n; ++i) {
		int p;
		scanf("%d", &p);
		if(p) {
			--p;
			g[p].push_back(i);
		} else {
			arr.push_back(i);
		}
	}
	scanf("%d", &m);
	ans.resize(m, -1);
	for(int i = 0; i < m; ++i) {
		int v, p;
		scanf("%d%d", &v, &p);
		--v;
		qu[v].push_back(make_pair(p, i));
	}
	for(int i = 0; i < arr.size(); ++i) {
		solve(arr[i]);
	}
	for(int i = 0; i < m; ++i) {
		printf("%d ", ans[i]);
	}

	return 0;
}


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

int n, m;
vector<vector<int> > g;
vector<int> arr, ans;
vector<vector<pair<int, int> > > qu;

int cnt = 0, mx;
vector<pair<int, int> > u;
vector<vector<int> > child;
vector<int> y, cnt_y;

void dfs0(int v, int cur) {
	mx = max(mx, cur);
	cnt_y[v] = cnt++;
	u[v].first = cnt_y[v];
	child[cur].push_back(v);
	for(int i = 0; i < g[v].size(); ++i) {
		dfs0(g[v][i], cur + 1);
	}
	u[v].second = cnt - 1;
}

void dfs1(int v, int cur) {
	y.push_back(v);
	for(int i = 0; i < qu[v].size(); ++i) {
		int cur_p = qu[v][i].first,
			cur_num = qu[v][i].second;
		if(cur_p > cur) {
			ans[cur_num] = 0;
		} else {
			int ll = u[y[cur - cur_p]].first,
				rr = u[y[cur - cur_p]].second;
			int l = 0,
				r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = ((l + r) >> 1) + ((l + r) & 1),
					pp = cnt_y[child[cur][mid]];
				if(pp <= rr) {
					l = mid;
				} else {
					r = mid - 1;
				}
			}
			rr = r;
			l = 0,
				r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = (l + r) >> 1,
					pp = cnt_y[child[cur][mid]];
				if(pp >= ll) {
					r = mid;
				} else {
					l = mid + 1;
				}
			}
			ll = l;
			ans[cur_num] = rr - ll;
			ll = u[y[cur - cur_p + 1]].first,
			rr = u[y[cur - cur_p + 1]].second;
			l = 0,
			r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = ((l + r) >> 1) + ((l + r) & 1),
					pp = cnt_y[child[cur][mid]];
				if(pp <= rr) {
					l = mid;
				} else {
					r = mid - 1;
				}
			}
			rr = r;
			l = 0,
				r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = (l + r) >> 1,
					pp = cnt_y[child[cur][mid]];
				if(pp >= ll) {
					r = mid;
				} else {
					l = mid + 1;
				}
			}
			ll = l;
			ans[cur_num] -= rr - ll;
		}
	}
	for(int i = 0; i < g[v].size(); ++i) {
		dfs1(g[v][i], cur + 1);
	}
	y.pop_back();
}

bool cmp(const int &a, const int &b) {
	return cnt_y[a] < cnt_y[b];
}

void solve(int num) {
	mx = 0;
	dfs0(num, 0);
	dfs1(num, 0);
}

int main (int argc, char *argv[])
{
	scanf("%d", &n);
	child.resize(n);
	g.resize(n);
	qu.resize(n);
	cnt_y.resize(n);
	u.resize(n);
	for(int i = 0; i < n; ++i) {
		int p;
		scanf("%d", &p);
		if(p) {
			--p;
			g[p].push_back(i);
		} else {
			arr.push_back(i);
		}
	}
	scanf("%d", &m);
	ans.resize(m, -1);
	for(int i = 0; i < m; ++i) {
		int v, p;
		scanf("%d%d", &v, &p);
		--v;
		qu[v].push_back(make_pair(p, i));
	}
	for(int i = 0; i < arr.size(); ++i) {
		solve(arr[i]);
	}
	for(int i = 0; i < m; ++i) {
		printf("%d ", ans[i]);
	}

	return 0;
}


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

int n, m;
vector<vector<int> > g;
vector<int> arr, ans;
vector<vector<pair<int, int> > > qu;

int cnt = 0, mx;
vector<pair<int, int> > u;
vector<vector<int> > child;
vector<int> y, cnt_y;

void dfs0(int v, int cur) {
	cnt_y[v] = cnt++;
	u[v].first = cnt_y[v];
	child[cur].push_back(v);
	for(int i = 0; i < g[v].size(); ++i) {
		dfs0(g[v][i], cur + 1);
	}
	u[v].second = cnt - 1;
}

void dfs1(int v, int cur) {
	y.push_back(v);
	for(int i = 0; i < qu[v].size(); ++i) {
		int cur_p = qu[v][i].first,
			cur_num = qu[v][i].second;
		if(cur_p > cur) {
			ans[cur_num] = 0;
		} else {
			int ll = u[y[cur - cur_p]].first,
				rr = u[y[cur - cur_p]].second;
			int l = 0,
				r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = ((l + r) >> 1) + ((l + r) & 1),
					pp = cnt_y[child[cur][mid]];
				if(pp <= rr) {
					l = mid;
				} else {
					r = mid - 1;
				}
			}
			rr = r;
			l = 0,
				r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = (l + r) >> 1,
					pp = cnt_y[child[cur][mid]];
				if(pp >= ll) {
					r = mid;
				} else {
					l = mid + 1;
				}
			}
			ll = l;
			ans[cur_num] = rr - ll;

			/*ll = u[y[cur - cur_p + 1]].first,
			rr = u[y[cur - cur_p + 1]].second;
			l = 0,
				r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = ((l + r) >> 1) + ((l + r) & 1),
					pp = cnt_y[child[cur][mid]];
				if(pp <= rr) {
					l = mid;
				} else {
					r = mid - 1;
				}
			}
			rr = r;
			l = 0,
				r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = (l + r) >> 1,
					pp = cnt_y[child[cur][mid]];
				if(pp >= ll) {
					r = mid;
				} else {
					l = mid + 1;
				}
			}
			ll = l;
			ans[cur_num] -= rr - ll;*/
		}
	}
	for(int i = 0; i < g[v].size(); ++i) {
		dfs1(g[v][i], cur + 1);
	}
	y.pop_back();
}

bool cmp(const int &a, const int &b) {
	return cnt_y[a] < cnt_y[b];
}

int main (int argc, char *argv[])
{
	scanf("%d", &n);
	child.resize(n);
	g.resize(n);
	qu.resize(n);
	cnt_y.resize(n);
	u.resize(n);
	for(int i = 0; i < n; ++i) {
		int p;
		scanf("%d", &p);
		if(p) {
			--p;
			g[p].push_back(i);
		} else {
			arr.push_back(i);
		}
	}
	scanf("%d", &m);
	ans.resize(m, -1);
	for(int i = 0; i < m; ++i) {
		int v, p;
		scanf("%d%d", &v, &p);
		--v;
		qu[v].push_back(make_pair(p, i));
	}
	for(int i = 0; i < arr.size(); ++i) {
		dfs0(arr[i], 0);
	}
	for(int i = 0; i < arr.size(); ++i) {
		dfs1(arr[i], 0);
	}
	for(int i = 0; i < m; ++i) {
		printf("%d ", ans[i]);
	}

	return 0;
}


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

int n, m;
vector<vector<int> > g;
vector<int> arr, ans;
vector<vector<pair<int, int> > > qu;

int cnt, mx;
vector<pair<int, int> > u;
vector<vector<int> > child;
vector<int> y, cnt_y;

void dfs0(int v, int cur) {
	mx = max(mx, cur);
	cnt_y[v] = cnt++;
	u[v].first = cnt_y[v];
	child[cur].push_back(v);
	for(int i = 0; i < g[v].size(); ++i) {
		dfs0(g[v][i], cur + 1);
	}
	u[v].second = cnt - 1;
}

void dfs1(int v, int cur) {
	y.push_back(v);
	for(int i = 0; i < qu[v].size(); ++i) {
		int cur_p = qu[v][i].first,
			cur_num = qu[v][i].second;
		if(cur_p > cur) {
			ans[cur_num] = 0;
		} else {
			int ll = u[y[cur - cur_p]].first,
				rr = u[y[cur - cur_p]].second;
			int l = 0,
				r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = ((l + r) >> 1) + ((l + r) & 1),
					pp = cnt_y[child[cur][mid]];
				if(pp <= rr) {
					l = mid;
				} else {
					r = mid - 1;
				}
			}
			rr = r;
			l = 0,
				r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = (l + r) >> 1,
					pp = cnt_y[child[cur][mid]];
				if(pp >= ll) {
					r = mid;
				} else {
					l = mid + 1;
				}
			}
			ll = l;
			ans[cur_num] = rr - ll;
			ll = u[y[cur - cur_p + 1]].first,
			rr = u[y[cur - cur_p + 1]].second;
			l = 0,
			r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = ((l + r) >> 1) + ((l + r) & 1),
					pp = cnt_y[child[cur][mid]];
				if(pp <= rr) {
					l = mid;
				} else {
					r = mid - 1;
				}
			}
			rr = r;
			l = 0,
				r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = (l + r) >> 1,
					pp = cnt_y[child[cur][mid]];
				if(pp >= ll) {
					r = mid;
				} else {
					l = mid + 1;
				}
			}
			ll = l;
			ans[cur_num] -= rr - ll;
		}
	}
	for(int i = 0; i < g[v].size(); ++i) {
		dfs1(g[v][i], cur + 1);
	}
	y.pop_back();
}

bool cmp(const int &a, const int &b) {
	return cnt_y[a] < cnt_y[b];
}

void solve(int num) {
	mx = 0;
	dfs0(num, 0);
	for(int i = 0; i <= mx; ++i) {
		sort(child[i].begin(), child[i].end(), cmp);
	}
	dfs1(num, 0);
}

int main (int argc, char *argv[])
{
	scanf("%d", &n);
	child.resize(n);
	g.resize(n);
	qu.resize(n);
	cnt_y.resize(n);
	u.resize(n);
	for(int i = 0; i < n; ++i) {
		int p;
		scanf("%d", &p);
		if(p) {
			--p;
			g[p].push_back(i);
		} else {
			arr.push_back(i);
		}
	}
	scanf("%d", &m);
	ans.resize(m, -1);
	for(int i = 0; i < m; ++i) {
		int v, p;
		scanf("%d%d", &v, &p);
		--v;
		qu[v].push_back(make_pair(p, i));
	}
	for(int i = 0; i < arr.size(); ++i) {
		solve(arr[i]);
	}
	for(int i = 0; i < m; ++i) {
		printf("%d ", ans[i]);
	}

	return 0;
}


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

int n, m;
vector<vector<int> > g;
vector<int> arr, ans;
vector<vector<pair<int, int> > > qu;

int cnt, mx;
vector<pair<int, int> > u;
vector<vector<int> > child;
vector<int> y, cnt_y;

void dfs0(int v, int cur) {
	mx = max(mx, cur);
	cnt_y[v] = cnt++;
	u[v].first = cnt_y[v];
	child[cur].push_back(v);
	for(int i = 0; i < g[v].size(); ++i) {
		dfs0(g[v][i], cur + 1);
	}
	u[v].second = cnt - 1;
}

void dfs1(int v, int cur) {
	y.push_back(v);
	for(int i = 0; i < qu[v].size(); ++i) {
		int cur_p = qu[v][i].first,
			cur_num = qu[v][i].second;
		if(cur_p > cur) {
			ans[cur_num] = 0;
		} else {
			int ll = u[y[cur - cur_p]].first,
				rr = u[y[cur - cur_p]].second;
			int l = 0,
				r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = ((l + r) >> 1) + ((l + r) & 1),
					pp = cnt_y[child[cur][mid]];
				if(pp <= rr) {
					l = mid;
				} else {
					r = mid - 1;
				}
			}
			rr = r;
			l = 0,
				r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = (l + r) >> 1,
					pp = cnt_y[child[cur][mid]];
				if(pp >= l) {
					r = mid;
				} else {
					l = mid + 1;
				}
			}
			ll = l;
			ans[cur_num] = rr - ll;
			ll = u[y[cur - cur_p + 1]].first,
			rr = u[y[cur - cur_p + 1]].second;
			l = 0,
			r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = ((l + r) >> 1) + ((l + r) & 1),
					pp = cnt_y[child[cur][mid]];
				if(pp <= rr) {
					l = mid;
				} else {
					r = mid - 1;
				}
			}
			rr = r;
			l = 0,
				r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = (l + r) >> 1,
					pp = cnt_y[child[cur][mid]];
				if(pp >= ll) {
					r = mid;
				} else {
					l = mid + 1;
				}
			}
			ll = l;
			ans[cur_num] -= rr - ll;
		}
	}
	for(int i = 0; i < g[v].size(); ++i) {
		dfs1(g[v][i], cur + 1);
	}
	y.pop_back();
}

bool cmp(const int &a, const int &b) {
	return cnt_y[a] < cnt_y[b];
}

void solve(int num) {
	cnt = 0;
	mx = 0;
	child.clear();
	child.resize(n);
	dfs0(num, 0);
	for(int i = 0; i <= mx; ++i) {
		sort(child[i].begin(), child[i].end(), cmp);
	}
	dfs1(num, 0);
}

int main (int argc, char *argv[])
{
	scanf("%d", &n);
	g.resize(n);
	qu.resize(n);
	cnt_y.resize(n);
	u.resize(n);
	for(int i = 0; i < n; ++i) {
		int p;
		scanf("%d", &p);
		if(p) {
			--p;
			g[p].push_back(i);
		} else {
			arr.push_back(i);
		}
	}
	scanf("%d", &m);
	ans.resize(m, -1);
	for(int i = 0; i < m; ++i) {
		int v, p;
		scanf("%d%d", &v, &p);
		--v;
		qu[v].push_back(make_pair(p, i));
	}
	for(int i = 0; i < arr.size(); ++i) {
		solve(arr[i]);
	}
	for(int i = 0; i < m; ++i) {
		printf("%d ", ans[i]);
	}

	return 0;
}


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

int n, m;
vector<vector<int> > g;
vector<int> arr, ans;
vector<vector<pair<int, int> > > qu;

int cnt, mx;
vector<pair<int, int> > u;
vector<vector<int> > child;
vector<int> y, cnt_y;

void dfs0(int v, int cur) {
	mx = max(mx, cur);
	cnt_y[v] = cnt++;
	u[v].first = cnt_y[v];
	child[cur].push_back(v);
	for(int i = 0; i < g[v].size(); ++i) {
		dfs0(g[v][i], cur + 1);
	}
	u[v].second = cnt - 1;
}

void dfs1(int v, int cur) {
	y.push_back(v);
	for(int i = 0; i < qu[v].size(); ++i) {
		int cur_p = qu[v][i].first,
			cur_num = qu[v][i].second;
		if(cur_p > cur) {
			ans[cur_num] = 0;
		} else {
			int ll = u[y[cur - cur_p]].first,
				rr = u[y[cur - cur_p]].second;
			int l = 0,
				r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = ((l + r) >> 1) + ((l + r) & 1),
					pp = cnt_y[child[cur][mid]];
				if(pp <= rr) {
					l = mid;
				} else {
					r = mid - 1;
				}
			}
			rr = r;
			l = 0,
				r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = (l + r) >> 1,
					pp = cnt_y[child[cur][mid]];
				if(pp >= ll) {
					r = mid;
				} else {
					l = mid + 1;
				}
			}
			ll = l;
			ans[cur_num] = rr - ll;
			ll = u[y[cur - cur_p + 1]].first,
			rr = u[y[cur - cur_p + 1]].second;
			l = 0,
			r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = ((l + r) >> 1) + ((l + r) & 1),
					pp = cnt_y[child[cur][mid]];
				if(pp <= rr) {
					l = mid;
				} else {
					r = mid - 1;
				}
			}
			rr = r;
			l = 0,
				r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = (l + r) >> 1,
					pp = cnt_y[child[cur][mid]];
				if(pp >= ll) {
					r = mid;
				} else {
					l = mid + 1;
				}
			}
			ll = l;
			ans[cur_num] -= rr - ll;
		}
	}
	for(int i = 0; i < g[v].size(); ++i) {
		dfs1(g[v][i], cur + 1);
	}
	y.pop_back();
}

bool cmp(const int &a, const int &b) {
	return cnt_y[a] < cnt_y[b];
}

void solve(int num) {
	cnt = 0;
	mx = 0;
	child.clear();
	child.resize(n);
	dfs0(num, 0);
	for(int i = 0; i <= mx; ++i) {
		sort(child[i].begin(), child[i].end(), cmp);
	}
	dfs1(num, 0);
}

int main (int argc, char *argv[])
{
	scanf("%d", &n);
	g.resize(n);
	qu.resize(n);
	cnt_y.resize(n);
	u.resize(n);
	for(int i = 0; i < n; ++i) {
		int p;
		scanf("%d", &p);
		if(p) {
			--p;
			g[p].push_back(i);
		} else {
			arr.push_back(i);
		}
	}
	scanf("%d", &m);
	ans.resize(m, -1);
	for(int i = 0; i < m; ++i) {
		int v, p;
		scanf("%d%d", &v, &p);
		--v;
		qu[v].push_back(make_pair(p, i));
	}
	for(int i = 0; i < arr.size(); ++i) {
		solve(arr[i]);
	}
	for(int i = 0; i < m; ++i) {
		printf("%d ", ans[i]);
	}

	return 0;
}


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

int n, m;
vector<vector<int> > g;
vector<int> arr, ans;
vector<vector<pair<int, int> > > qu;

int cnt;
vector<pair<int, int> > u;
vector<vector<int> > child;
vector<int> y, cnt_y;

void dfs0(int v, int cur) {
	cnt_y[v] = cnt++;
	u[v].first = cnt_y[v];
	child[cur].push_back(v);
	for(int i = 0; i < g[v].size(); ++i) {
		dfs0(g[v][i], cur + 1);
	}
	u[v].second = cnt - 1;
}

void dfs1(int v, int cur) {
	for(int i = 0; i < qu[v].size(); ++i) {
		int cur_p = qu[v][i].first,
			cur_num = qu[v][i].second;
		if(cur_p > cur) {
			ans[cur_num] = 0;
		} else {
			int ll = u[y[cur - cur_p]].first,
				rr = u[y[cur - cur_p]].second;
			int l = 0,
				r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = ((l + r) >> 1) + ((l + r) & 1),
					pp = cnt_y[child[cur][mid]];
				if(pp <= rr) {
					l = mid;
				} else {
					r = mid - 1;
				}
			}
			rr = r;
			l = 0,
				r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = (l + r) >> 1,
					pp = cnt_y[child[cur][mid]];
				if(pp >= l) {
					r = mid;
				} else {
					l = mid + 1;
				}
			}
			ll = l;
			ans[cur_num] = rr - ll;
		}
	}
	y.push_back(v);
	for(int i = 0; i < g[v].size(); ++i) {
		dfs1(g[v][i], cur + 1);
	}
	y.pop_back();
}

void solve(int num) {
	cnt = 0;
	child.clear();
	child.resize(n);
	dfs0(num, 0);
	dfs1(num, 0);
}

int main (int argc, char *argv[])
{
	scanf("%d", &n);
	g.resize(n);
	qu.resize(n);
	cnt_y.resize(n);
	u.resize(n);
	for(int i = 0; i < n; ++i) {
		int p;
		scanf("%d", &p);
		if(p) {
			--p;
			g[p].push_back(i);
		} else {
			arr.push_back(i);
		}
	}
	scanf("%d", &m);
	ans.resize(m, -1);
	for(int i = 0; i < m; ++i) {
		int v, p;
		scanf("%d%d", &v, &p);
		--v;
		qu[v].push_back(make_pair(p, i));
	}
	for(int i = 0; i < arr.size(); ++i) {
		solve(arr[i]);
	}
	for(int i = 0; i < m; ++i) {
		printf("%d ", ans[i]);
	}

	return 0;
}


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

int n, m;
vector<vector<int> > g;
vector<int> arr, ans;
vector<vector<pair<int, int> > > qu;

int cnt, mx;
vector<pair<int, int> > u;
vector<vector<int> > child;
vector<int> y, cnt_y;

void dfs0(int v, int cur) {
	mx = max(mx, cur);
	cnt_y[v] = cnt++;
	u[v].first = cnt_y[v];
	child[cur].push_back(v);
	for(int i = 0; i < g[v].size(); ++i) {
		dfs0(g[v][i], cur + 1);
	}
	u[v].second = cnt - 1;
}

void dfs1(int v, int cur) {
	for(int i = 0; i < qu[v].size(); ++i) {
		int cur_p = qu[v][i].first,
			cur_num = qu[v][i].second;
		if(cur_p > cur) {
			ans[cur_num] = 0;
		} else {
			int ll = u[y[cur - cur_p]].first,
				rr = u[y[cur - cur_p]].second;
			int l = 0,
				r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = ((l + r) >> 1) + ((l + r) & 1),
					pp = cnt_y[child[cur][mid]];
				if(pp <= rr) {
					l = mid;
				} else {
					r = mid - 1;
				}
			}
			rr = r;
			l = 0,
				r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = (l + r) >> 1,
					pp = cnt_y[child[cur][mid]];
				if(pp >= l) {
					r = mid;
				} else {
					l = mid + 1;
				}
			}
			ll = l;
			ans[cur_num] = rr - ll;
		}
	}
	y.push_back(v);
	for(int i = 0; i < g[v].size(); ++i) {
		dfs1(g[v][i], cur + 1);
	}
	y.pop_back();
}

bool cmp(const int &a, const int &b) {
	return cnt_y[a] < cnt_y[b];
}

void solve(int num) {
	cnt = 0;
	mx = 0;
	child.clear();
	child.resize(n);
	dfs0(num, 0);
	for(int i = 0; i <= mx; ++i) {
		sort(child[i].begin(), child[i].end(), cmp);
	}
	dfs1(num, 0);
}

int main (int argc, char *argv[])
{
	scanf("%d", &n);
	g.resize(n);
	qu.resize(n);
	cnt_y.resize(n);
	u.resize(n);
	for(int i = 0; i < n; ++i) {
		int p;
		scanf("%d", &p);
		if(p) {
			--p;
			g[p].push_back(i);
		} else {
			arr.push_back(i);
		}
	}
	scanf("%d", &m);
	ans.resize(m, -1);
	for(int i = 0; i < m; ++i) {
		int v, p;
		scanf("%d%d", &v, &p);
		--v;
		qu[v].push_back(make_pair(p, i));
	}
	for(int i = 0; i < arr.size(); ++i) {
		solve(arr[i]);
	}
	for(int i = 0; i < m; ++i) {
		printf("%d ", ans[i]);
	}

	return 0;
}


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

int n, m;
vector<vector<int> > g;
vector<int> arr, ans;
vector<vector<pair<int, int> > > qu;

int cnt, mx;
vector<pair<int, int> > u;
vector<vector<int> > child;
vector<int> y, cnt_y;

void dfs0(int v, int cur) {
	mx = max(mx, cur);
	cnt_y[v] = cnt++;
	u[v].first = cnt_y[v];
	child[cur].push_back(v);
	for(int i = 0; i < g[v].size(); ++i) {
		dfs0(g[v][i], cur + 1);
	}
	u[v].second = cnt - 1;
}

void dfs1(int v, int cur) {
	y.push_back(v);
	for(int i = 0; i < qu[v].size(); ++i) {
		int cur_p = qu[v][i].first,
			cur_num = qu[v][i].second;
		if(cur_p > cur) {
			ans[cur_num] = 0;
		} else {
			int ll = u[y[cur - cur_p]].first,
				rr = u[y[cur - cur_p]].second;
			int l = 0,
				r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = ((l + r) >> 1) + ((l + r) & 1),
					pp = cnt_y[child[cur][mid]];
				if(pp <= rr) {
					l = mid;
				} else {
					r = mid - 1;
				}
			}
			rr = r;
			l = 0,
				r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = (l + r) >> 1,
					pp = cnt_y[child[cur][mid]];
				if(pp >= ll) {
					r = mid;
				} else {
					l = mid + 1;
				}
			}
			ll = l;
			ans[cur_num] = rr - ll;
			ll = u[y[cur - cur_p + 1]].first,
			rr = u[y[cur - cur_p + 1]].second;
			l = 0,
			r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = ((l + r) >> 1) + ((l + r) & 1),
					pp = cnt_y[child[cur][mid]];
				if(pp <= rr) {
					l = mid;
				} else {
					r = mid - 1;
				}
			}
			rr = r;
			l = 0,
				r = int(child[cur].size()) - 1;
			while(l != r) {
				int mid = (l + r) >> 1,
					pp = cnt_y[child[cur][mid]];
				if(pp >= ll) {
					r = mid;
				} else {
					l = mid + 1;
				}
			}
			ll = l;
			ans[cur_num] -= rr - ll;
		}
	}
	for(int i = 0; i < g[v].size(); ++i) {
		dfs1(g[v][i], cur + 1);
	}
	y.pop_back();
}

bool cmp(const int &a, const int &b) {
	return cnt_y[a] < cnt_y[b];
}

void solve(int num) {
	mx = 0;
	child.clear();
	child.resize(n);
	dfs0(num, 0);
	for(int i = 0; i <= mx; ++i) {
		sort(child[i].begin(), child[i].end(), cmp);
	}
	dfs1(num, 0);
}

int main (int argc, char *argv[])
{
	scanf("%d", &n);
	g.resize(n);
	qu.resize(n);
	cnt_y.resize(n);
	u.resize(n);
	for(int i = 0; i < n; ++i) {
		int p;
		scanf("%d", &p);
		if(p) {
			--p;
			g[p].push_back(i);
		} else {
			arr.push_back(i);
		}
	}
	scanf("%d", &m);
	ans.resize(m, -1);
	for(int i = 0; i < m; ++i) {
		int v, p;
		scanf("%d%d", &v, &p);
		--v;
		qu[v].push_back(make_pair(p, i));
	}
	for(int i = 0; i < arr.size(); ++i) {
		solve(arr[i]);
	}
	for(int i = 0; i < m; ++i) {
		printf("%d ", ans[i]);
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

vector < vector <int> > g;
vector <int> roots;
vector < vector < pair <int, int> > > raw;

struct border {
    int l, r;
};
int h[100005];
vector <border> br;
vector <int> topsort;
int cnt = 0;

void dfs1(int v, int _h) {
    h[v] = _h;
    topsort.push_back(v);
    br[v].l = cnt++;
    for (auto u : g[v]) dfs1(u, _h + 1);
    topsort.push_back(v);
    br[v].r = cnt++;
}

struct query {
    int v, p;
    int qn;
};
vector <query> qs;

void dfs2(int v, vector <int> st) {
    for (auto pr : raw[v]) {
        int k = pr.first;
        int qn = pr.second;
        query q;
        q.v = v;
        q.qn = qn;
        if (k <= st.size()) {
            q.p = st[st.size() - k];
        }
        else {
            q.v = q.p = -1;
        }
        qs.push_back(q);
    }
    st.push_back(v);
    for (auto u : g[v]) {
        dfs2(u, st);
    }
}

int hcnt[201234] = {0};

int K;
bool qcmp(query a, query b) {
    if (br[a.p].l % K != br[b.p].l % K)
        return br[a.p].l < br[b.p].l;
    return br[a.p].r < br[b.p].r;
}

int wl, wr;
void movel(int l) {
    if (wl == l) return;
    while (wl > l) {
        hcnt[h[topsort[--wl]]]++;
    }
    while (wl < l) {
        hcnt[h[topsort[wl++]]]--;
    }
}

void mover(int r) {
    if (wr == r) return;
    while (wr < r) {
        hcnt[h[topsort[++wr]]]++;
    }
    while (wr > r) {
        hcnt[h[topsort[wr--]]]--;
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(NULL);

    // read graph
    int n;
    cin >> n;
    g.resize(n);
    for (int i = 0; i < n; i++) {
        int p;
        cin >> p;
        if (p > 0) {
            g[p - 1].push_back(i);
        }
        else {
            roots.push_back(i);
        }
    }

    // topsort
    br.resize(n);
    for (auto r : roots)
        dfs1(r, 0);

    // modify queries
    raw.resize(n);
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int v, k;
        cin >> v >> k;
        raw[v - 1].push_back(make_pair(k, i));
    }
    for (auto r : roots)
        dfs2(r, vector <int> (0));

    // MO
    // sort queries
    K = (int) sqrt(n + n);
    sort(qs.begin(), qs.end(), qcmp);

    vector <int> ans (q);

    wl = br[qs[0].p].l, wr = wl;
    cnt[h[topsort[wl]]++;
    for (auto qr : qs) {
        if (qr.v == -1) {
            ans[qr.qn] = 0;
            continue;
        }
        movel(br[qr.p].l);
        mover(br[qr.p].r);
        ans[qr.qn] = hcnt[h[qr.v]] / 2 - 1;
    }

    for (auto el : ans) cout << el << " ";
    cout << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

vector < vector <int> > g;
vector <int> roots;
vector < vector < pair <int, int> > > raw;

struct border {
    int l, r;
};
int h[100005];
vector <border> br;
vector <int> topsort;
int cnt = 0;

void dfs1(int v, int _h) {
    h[v] = _h;
    topsort.push_back(v);
    br[v].l = cnt++;
    for (auto u : g[v]) dfs1(u, _h + 1);
    topsort.push_back(v);
    br[v].r = cnt++;
}

struct query {
    int v, p;
    int qn;
};
vector <query> qs;

void dfs2(int v, vector <int> st) {
    for (auto pr : raw[v]) {
        int k = pr.first;
        int qn = pr.second;
        query q;
        q.v = v;
        q.qn = qn;
        if (k <= st.size()) {
            q.p = st[st.size() - k];
        }
        else {
            q.v = q.p = -1;
        }
        qs.push_back(q);
    }
    st.push_back(v);
    for (auto u : g[v]) {
        dfs2(u, st);
    }
}

int hcnt[201234] = {0};

int K;
bool qcmp(query a, query b) {
    if (br[a.p].l % K != br[b.p].l % K)
        return br[a.p].l < br[b.p].l;
    return br[a.p].r < br[b.p].r;
}

int wl, wr;
void movel(int l) {
    if (wl == l) return;
    while (wl > l) {
        hcnt[h[topsort[--wl]]]++;
    }
    while (wl < l) {
        hcnt[h[topsort[wl++]]]--;
    }
}

void mover(int r) {
    if (wr == r) return;
    while (wr < r) {
        hcnt[h[topsort[++wr]]]++;
    }
    while (wr > r) {
        hcnt[h[topsort[wr--]]]--;
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(NULL);

    // read graph
    int n;
    cin >> n;
    g.resize(n);
    for (int i = 0; i < n; i++) {
        int p;
        cin >> p;
        if (p > 0) {
            g[p - 1].push_back(i);
        }
        else {
            roots.push_back(i);
        }
    }

    // topsort
    br.resize(n);
    for (auto r : roots)
        dfs1(r, 0);

    // modify queries
    raw.resize(n);
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int v, k;
        cin >> v >> k;
        raw[v - 1].push_back(make_pair(k, i));
    }
    for (auto r : roots)
        dfs2(r, vector <int> (0));

    // MO
    // sort queries
    K = (int) sqrt(n + n);
    sort(qs.begin(), qs.end(), qcmp);

    vector <int> ans (q);

    wl = br[qs[0].p].l, wr = wl;
    if (qs[0].v != -1) hcnt[h[topsort[wl]]]++;
    for (auto qr : qs) {
        if (qr.v == -1) {
            ans[qr.qn] = 0;
            continue;
        }
        movel(br[qr.p].l);
        mover(br[qr.p].r);
        ans[qr.qn] = hcnt[h[qr.v]] / 2 - 1;
    }

    for (auto el : ans) cout << el << " ";
    cout << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

vector < vector <int> > g;
vector <int> roots;
vector < vector < pair <int, int> > > raw;

struct border {
    int l, r;
};
int h[100305];
vector <border> br;
vector <int> topsort;
int cnt = 0;

void dfs1(int v, int _h) {
    h[v] = _h;
    topsort[cnt] = v;
    br[v].l = cnt++;
    for (auto u : g[v]) dfs1(u, _h + 1);
    topsort[cnt] = v;
    br[v].r = cnt++;
}

struct query {
    int v, p;
    int qn;
};
vector <query> qs;
int qt = 0;

vector <int> st;
void dfs2(int v) {
    for (auto pr : raw[v]) {
        int k = pr.first;
        int qn = pr.second;
        query q;
        q.v = v;
        q.qn = qn;
        if (k <= st.size()) {
            q.p = st[st.size() - k];
        }
        else {
            q.v = q.p = -1;
        }
        qs[qt++] = q;
    }
    st.push_back(v);
    for (auto u : g[v]) {
        dfs2(u);
    }
    st.pop_back();
}

int hcnt[201234] = {0};

int K;
bool qcmp(query a, query b) {
    if (br[a.p].l % K != br[b.p].l % K)
        return br[a.p].l < br[b.p].l;
    return br[a.p].r < br[b.p].r;
}

int wl, wr;
void movel(int l) {
    if (wl == l) return;
    while (wl > l) {
        hcnt[h[topsort[--wl]]]++;
    }
    while (wl < l) {
        hcnt[h[topsort[wl++]]]--;
    }
}

void mover(int r) {
    if (wr == r) return;
    while (wr < r) {
        hcnt[h[topsort[++wr]]]++;
    }
    while (wr > r) {
        hcnt[h[topsort[wr--]]]--;
    }
}

int n;
void read() {
    cin >> n;
    g.resize(n);
    for (int i = 0; i < n; i++) {
        int p;
        cin >> p;
        if (p > 0) {
            g[p - 1].push_back(i);
        }
        else {
            roots.push_back(i);
        }
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(NULL);

    // read graph
    read();

    // topsort
    br.resize(n);
    topsort.resize(n + n + 1337);
    for (auto r : roots)
        dfs1(r, 0);

    // modify queries
    raw.resize(n);
    int q;
    cin >> q;
    qs.resize(q);
    for (int i = 0; i < q; i++) {
        int v, k;
        cin >> v >> k;
        raw[v - 1].push_back(make_pair(k, i));
    }
    for (auto r : roots)
        dfs2(r);

    // MO
    // sort queries
    K = (int) sqrt(n + n);
    sort(qs.begin(), qs.end(), qcmp);

    vector <int> ans (q);

    wl = 0, wr = -1;
    for (auto qr : qs) {
        if (qr.v == -1) {
            ans[qr.qn] = 0;
            continue;
        }
        mover(br[qr.p].r);
        movel(br[qr.p].l);
        ans[qr.qn] = hcnt[h[qr.v]] / 2 - 1;
    }

    for (auto el : ans) cout << el << " ";
    cout << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

vector < vector <int> > g;
vector <int> roots;
vector < vector < pair <int, int> > > raw;

struct border {
    int l, r;
};
int h[100305];
vector <border> br;
vector <int> topsort;
int cnt = 0;

void dfs1(int v, int _h) {
    h[v] = _h;
    topsort[cnt] = v;
    br[v].l = cnt++;
    for (auto u : g[v]) dfs1(u, _h + 1);
    topsort[cnt] = v;
    br[v].r = cnt++;
}

struct query {
    int v, p;
    int qn;
};
vector <query> qs;
int qt = 0;

void dfs2(int v, vector <int> st) {
    for (auto pr : raw[v]) {
        int k = pr.first;
        int qn = pr.second;
        query q;
        q.v = v;
        q.qn = qn;
        if (k <= st.size()) {
            q.p = st[st.size() - k];
        }
        else {
            q.v = q.p = -1;
        }
        qs[qt++] = q;
    }
    st.push_back(v);
    for (auto u : g[v]) {
        dfs2(u, st);
    }
}

int hcnt[201234] = {0};

int K;
bool qcmp(query a, query b) {
    if (br[a.p].l % K != br[b.p].l % K)
        return br[a.p].l < br[b.p].l;
    return br[a.p].r < br[b.p].r;
}

int wl, wr;
void movel(int l) {
    if (wl == l) return;
    while (wl > l) {
        hcnt[h[topsort[--wl]]]++;
    }
    while (wl < l) {
        hcnt[h[topsort[wl++]]]--;
    }
}

void mover(int r) {
    if (wr == r) return;
    while (wr < r) {
        hcnt[h[topsort[++wr]]]++;
    }
    while (wr > r) {
        hcnt[h[topsort[wr--]]]--;
    }
}

int n;
void read() {
    cin >> n;
    g.resize(n);
    for (int i = 0; i < n; i++) {
        int p;
        cin >> p;
        if (p > 0) {
            g[p - 1].push_back(i);
        }
        else {
            roots.push_back(i);
        }
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(NULL);

    // read graph
    read();

    // topsort
    br.resize(n);
    topsort.resize(n + n + 1337);
    for (auto r : roots)
        dfs1(r, 0);

    // modify queries
    raw.resize(n);
    int q;
    cin >> q;
    qs.resize(q);
    for (int i = 0; i < q; i++) {
        int v, k;
        cin >> v >> k;
        raw[v - 1].push_back(make_pair(k, i));
    }
    for (auto r : roots)
        dfs2(r, vector <int> (0));

    // MO
    // sort queries
    K = (int) sqrt(n + n);
    sort(qs.begin(), qs.end(), qcmp);

    vector <int> ans (q);

    wl = 0, wr = -1;
    for (auto qr : qs) {
        if (qr.v == -1) {
            ans[qr.qn] = 0;
            continue;
        }
        mover(br[qr.p].r);
        movel(br[qr.p].l);
        ans[qr.qn] = hcnt[h[qr.v]] / 2 - 1;
    }

    for (auto el : ans) cout << el << " ";
    cout << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

vector < vector <int> > g;
vector <int> roots;
vector < vector < pair <int, int> > > raw;

struct border {
    int l, r;
};
int h[100005];
vector <border> br;
vector <int> topsort;
int cnt = 0;

void dfs1(int v, int _h) {
    h[v] = _h;
    topsort.push_back(v);
    br[v].l = cnt++;
    for (auto u : g[v]) dfs1(u, _h + 1);
    topsort.push_back(v);
    br[v].r = cnt++;
}

struct query {
    int v, p;
    int qn;
};
vector <query> qs;

void dfs2(int v, vector <int> st) {
    for (auto pr : raw[v]) {
        int k = pr.first;
        int qn = pr.second;
        query q;
        q.v = v;
        q.qn = qn;
        if (k <= st.size()) {
            q.p = st[st.size() - k];
        }
        else {
            q.v = q.p = -1;
        }
        qs.push_back(q);
    }
    st.push_back(v);
    for (auto u : g[v]) {
        dfs2(u, st);
    }
}

int hcnt[201234] = {0};

int K;
bool qcmp(query a, query b) {
    if (br[a.p].l % K != br[b.p].l % K)
        return br[a.p].l < br[b.p].l;
    return br[a.p].r < br[b.p].r;
}

int wl, wr;
void movel(int l) {
    if (wl == l) return;
    while (wl > l) {
        hcnt[h[topsort[--wl]]]++;
    }
    while (wl < l) {
        hcnt[h[topsort[wl++]]]--;
    }
}

void mover(int r) {
    if (wr == r) return;
    while (wr < r) {
        hcnt[h[topsort[++wr]]]++;
    }
    while (wr > r) {
        hcnt[h[topsort[wr--]]]--;
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(NULL);

    // read graph
    int n;
    cin >> n;
    g.resize(n);
    for (int i = 0; i < n; i++) {
        int p;
        cin >> p;
        if (p > 0) {
            g[p - 1].push_back(i);
        }
        else {
            roots.push_back(i);
        }
    }

    // topsort
    br.resize(n);
    for (auto r : roots)
        dfs1(r, 0);

    // modify queries
    raw.resize(n);
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int v, k;
        cin >> v >> k;
        raw[v - 1].push_back(make_pair(k, i));
    }
    for (auto r : roots)
        dfs2(r, vector <int> (0));

    // MO
    // sort queries
    K = (int) sqrt(n + n);
    sort(qs.begin(), qs.end(), qcmp);

    vector <int> ans (q);

    wl = 0, wr = -1;
    for (auto qr : qs) {
        if (qr.v == -1) {
            ans[qr.qn] = 0;
            continue;
        }
        mover(br[qr.p].r);
        movel(br[qr.p].l);
        ans[qr.qn] = hcnt[h[qr.v]] / 2 - 1;
    }

    for (auto el : ans) cout << el << " ";
    cout << endl;

    return 0;
}