#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 - 1;
    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;
    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;
#define int long long int
#define pb push_back
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define pa pair<int,int>
#define ld long double
#define sz(x) x.size()
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define INF 1e18
#define PI 3.14159265
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
priority_queue<int, vector<int>, greater<int> > pq;//Min Heap
double startTime;
double getCurrentTime() {
  return ((double)clock() - startTime) / CLOCKS_PER_SEC;
}
bool isPrime(int n)
{
    if (n <= 1)  return false;
    if (n <= 3)  return true;
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
int power(int x, unsigned int y, int p)
{
    int res=1;
    x=x%p;
    while(y>0)
    {
        if(y&1) res=(res*x)%p;
        y=y>>1;
        x=(x*x)%p;
    }
    return res;
}
const int N=1e5+10;
const int maxn=log2(N);
int LCA[N+10][maxn+10],level[N],visit[N];
vector<int> adj[N];
void init()
{
    for(int j=1;j<=maxn;j++)
    {
        for(int i=1;i<N;i++)
        {
            if(LCA[i][j-1]!=-1)
            {
                int par=LCA[i][j-1];
                LCA[i][j]=LCA[par][j-1];
            }
        }
    }
}
int num[N];
vector<int> tmp[N];
int T=1;
void DFS(int node,int par=-1,int depth=0)
{
    num[node]=1;
    tmp[depth].pb(T++);
    level[node]=depth;
    visit[node]=1;
    for(auto ch:adj[node])
    {
        if(ch==par)
            continue;
        DFS(ch,node,depth+1);
        num[node]+=num[ch];
    }
}
int32_t main()
{
    IOS;
    int n;
    cin>>n;
    memset(LCA,-1,sizeof(LCA));
    for(int i=1;i<=n;i++)
    {
        int x;
        cin>>x;
        if(x!=0)
        {
            LCA[i][0]=x;
            adj[i].pb(x);
            adj[x].pb(i);
        }
    }
    init();
    for(int i=1;i<=n;i++)
    {
        if(!visit[i])
            DFS(i);
    }
    int m;
    cin>>m;
    while(m--)
    {
        int nd,p;
        cin>>nd>>p;
        int d=p;
        for(int i=maxn;i>=0;i--)
        {
            int pwr=1<<i;
            if(d-pwr>=0)
            {
                nd=LCA[nd][i];
                d-=pwr;
            }
        }
        if(nd==-1||d!=0)
        {
            cout<<0<<" ";
            continue;
        }
        int ans=lower_bound(all(tmp[p]),num[nd]-1)-tmp[p].begin();
        cout<<ans<<" ";
    }
    return 0;
}


/*              `-._:  .:'   `:::  .:\           |\__/|           /::  .:'   `:::  .:.-'
                    \      :          \          |:   |          /         :       /
                     \     ::    .     `-_______/ ::   \_______-'   .      ::   . /
                      |  :   :: ::'  :   :: ::'  :   :: ::'      :: ::'  :   :: :|
                      |     ;::         ;::         ;::         ;::         ;::  |
                      |  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:|
                      /     :           :           :           :           :    \
                     /______::_____     ::    .     ::    .     ::   _____._::____\
                                   `----._:: ::'  :   :: ::'  _.----'
                                          `--.       ;::  .--'
                                              `-. .:'  .-'
                                                 \    /
                                                  \  /
                                                   */

//ヾ(⌐■_■)ノ♪ ◢◤
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define pa pair<int,int>
#define ld long double
#define sz(x) x.size()
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define INF 1e18
#define PI 3.14159265
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
priority_queue<int, vector<int>, greater<int> > pq;//Min Heap
double startTime;
double getCurrentTime() {
  return ((double)clock() - startTime) / CLOCKS_PER_SEC;
}
bool isPrime(int n)
{
    if (n <= 1)  return false;
    if (n <= 3)  return true;
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
int power(int x, unsigned int y, int p)
{
    int res=1;
    x=x%p;
    while(y>0)
    {
        if(y&1) res=(res*x)%p;
        y=y>>1;
        x=(x*x)%p;
    }
    return res;
}
const int N=1e5+10;
const int maxn=log2(N);
int LCA[N][maxn+10],visit[N],tout[N],tin[N];
vector<int> adj[N];
vector<int> level[maxn+10];
int maxd=0;
int T=0;
void DFS(int node,int depth=0)
{
    visit[node]=1;
    level[depth].pb(node);
    tin[node]=T++;
    for(auto ch:adj[node])
    {
        if(!visit[ch])
            DFS(ch,depth+1);
    }
    maxd=max(maxd,depth);
    tout[node]=T++;
}
void init()
{
    for(int j=1;j<=maxn;j++)
    {
        for(int i=1;i<N;i++)
        {
            if(LCA[i][j-1]!=-1)
            {
                int par=LCA[i][j-1];
                LCA[i][j]=LCA[par][j-1];
            }
        }
    }
}
bool checkAncestor(int a,int b)//Check if a is ancestor of b
{
    if(tin[a]<=tin[b]&&tout[a]>=tout[b])
        return true;
    return false;
}
int32_t main()
{
    IOS;
    int n;
    cin>>n;
    memset(LCA,-1,sizeof(LCA));
    for(int i=1;i<=n;i++)
    {
        int x;
        cin>>x;
        if(x!=0)
        {
            LCA[i][0]=x;
            adj[i].pb(x);
            adj[x].pb(i);
        }
    }
    init();
    for(int i=1;i<=n;i++)
    {
        if(!visit[i])
            DFS(i);
    }
    int m;
    cin>>m;
    while(m--)
    {
        int nd,p;
        cin>>nd>>p;
        int d=p;
        for(int i=maxd;i>=0;i--)
        {
            int pwr=(1<<i);
            if(d-pwr>=0)
            {
                nd=LCA[nd][i];
                d-=pwr;
            }
        }
        int ans=0;
        if(nd==-1||d!=0)
        {
            cout<<ans<<" ";
            continue;
        }
        for(auto it:level[p])
        {
            if(checkAncestor(nd,it))
                ans++;
        }
        cout<<ans-1<<" ";
    }
    return 0;
}


/*              `-._:  .:'   `:::  .:\           |\__/|           /::  .:'   `:::  .:.-'
                    \      :          \          |:   |          /         :       /
                     \     ::    .     `-_______/ ::   \_______-'   .      ::   . /
                      |  :   :: ::'  :   :: ::'  :   :: ::'      :: ::'  :   :: :|
                      |     ;::         ;::         ;::         ;::         ;::  |
                      |  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:|
                      /     :           :           :           :           :    \
                     /______::_____     ::    .     ::    .     ::   _____._::____\
                                   `----._:: ::'  :   :: ::'  _.----'
                                          `--.       ;::  .--'
                                              `-. .:'  .-'
                                                 \    /
                                                  \  /
                                                   */

//ヾ(⌐■_■)ノ♪ ◢◤
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define pb push_back
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define pa pair<int,int>
#define ld long double
#define sz(x) x.size()
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define INF 1e18
#define PI 3.14159265
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
priority_queue<int, vector<int>, greater<int> > pq;//Min Heap
double startTime;
double getCurrentTime() {
  return ((double)clock() - startTime) / CLOCKS_PER_SEC;
}
bool isPrime(int n)
{
    if (n <= 1)  return false;
    if (n <= 3)  return true;
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
int power(int x, unsigned int y, int p)
{
    int res=1;
    x=x%p;
    while(y>0)
    {
        if(y&1) res=(res*x)%p;
        y=y>>1;
        x=(x*x)%p;
    }
    return res;
}
const int N=1e5+10;
const int maxn=log2(N);
int LCA[N+10][maxn+10],level[N],visit[N];
vector<int> adj[N];
void init()
{
    for(int j=1;j<=maxn;j++)
    {
        for(int i=1;i<N;i++)
        {
            if(LCA[i][j-1]!=-1)
            {
                int par=LCA[i][j-1];
                LCA[i][j]=LCA[par][j-1];
            }
        }
    }
}
int num[N];
vector<int> tmp[N];
int T=1;
void DFS(int node,int par=-1,int depth=0)
{
    num[node]=1;
    tmp[depth].pb(T++);
    level[node]=depth;
    visit[node]=1;
    for(auto ch:adj[node])
    {
        if(ch==par)
            continue;
        DFS(ch,node,depth+1);
        num[node]+=num[ch];
    }
}
int32_t main()
{
    IOS;
    int n;
    cin>>n;
    memset(LCA,-1,sizeof(LCA));
    for(int i=1;i<=n;i++)
    {
        int x;
        cin>>x;
        if(x!=0)
        {
            LCA[i][0]=x;
            adj[i].pb(x);
            adj[x].pb(i);
        }
    }
    init();
    for(int i=1;i<=n;i++)
    {
        if(!visit[i])
            DFS(i);
    }
    int m;
    cin>>m;
    while(m--)
    {
        int nd,p;
        cin>>nd>>p;
        int d=p;
        for(int i=maxn;i>=0;i--)
        {
            int pwr=1<<i;
            if(d-pwr>=0)
            {
                nd=LCA[nd][i];
                d-=pwr;
            }
        }
        if(nd==-1||d!=0)
        {
            cout<<0<<" ";
            continue;
        }
        int ans=lower_bound(all(tmp[p]),num[nd])-tmp[p].begin();
        cout<<ans<<" ";
    }
    return 0;
}


/*              `-._:  .:'   `:::  .:\           |\__/|           /::  .:'   `:::  .:.-'
                    \      :          \          |:   |          /         :       /
                     \     ::    .     `-_______/ ::   \_______-'   .      ::   . /
                      |  :   :: ::'  :   :: ::'  :   :: ::'      :: ::'  :   :: :|
                      |     ;::         ;::         ;::         ;::         ;::  |
                      |  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:|
                      /     :           :           :           :           :    \
                     /______::_____     ::    .     ::    .     ::   _____._::____\
                                   `----._:: ::'  :   :: ::'  _.----'
                                          `--.       ;::  .--'
                                              `-. .:'  .-'
                                                 \    /
                                                  \  /
                                                   */

//ヾ(⌐■_■)ノ♪ ◢◤
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define pa pair<int,int>
#define ld long double
#define sz(x) x.size()
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define INF 1e18
#define PI 3.14159265
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
priority_queue<int, vector<int>, greater<int> > pq;//Min Heap
double startTime;
double getCurrentTime() {
  return ((double)clock() - startTime) / CLOCKS_PER_SEC;
}
bool isPrime(int n)
{
    if (n <= 1)  return false;
    if (n <= 3)  return true;
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
int power(int x, unsigned int y, int p)
{
    int res=1;
    x=x%p;
    while(y>0)
    {
        if(y&1) res=(res*x)%p;
        y=y>>1;
        x=(x*x)%p;
    }
    return res;
}
const int N=1e5+10;
const int maxn=log2(N);
int LCA[N][maxn+10],visit[N],tout[N],tin[N];
vector<int> adj[N];
vector<int> level[N+10];
int T=0;
void DFS(int node,int depth=0)
{
    visit[node]=1;
    level[depth].pb(node);
    tin[node]=T++;
    for(auto ch:adj[node])
    {
        if(!visit[ch])
            DFS(ch,depth+1);
    }
    tout[node]=T++;
}
void init()
{
    for(int j=1;j<=maxn;j++)
    {
        for(int i=1;i<N;i++)
        {
            if(LCA[i][j-1]!=-1)
            {
                int par=LCA[i][j-1];
                LCA[i][j]=LCA[par][j-1];
            }
        }
    }
}
bool checkAncestor(int a,int b)//Check if a is ancestor of b
{
    if(tin[a]<=tin[b]&&tout[a]>=tout[b])
        return true;
    return false;
}
int32_t main()
{
    IOS;
    int n;
    cin>>n;
    memset(LCA,-1,sizeof(LCA));
    for(int i=1;i<=n;i++)
    {
        int x;
        cin>>x;
        if(x!=0)
        {
            LCA[i][0]=x;
            adj[i].pb(x);
            adj[x].pb(i);
        }
    }
    init();
    for(int i=1;i<=n;i++)
    {
        if(!visit[i])
            DFS(i);
    }
    int m;
    cin>>m;
    while(m--)
    {
        int nd,p;
        cin>>nd>>p;
        int d=p;
        for(int i=maxn;i>=0;i--)
        {
            int pwr=(1<<i);
            if(d-pwr>=0)
            {
                nd=LCA[nd][i];
                d-=pwr;
            }
        }
        int ans=0;
        if(nd==-1)
        {
            cout<<ans<<" ";
            continue;
        }
        for(auto it:level[p])
        {
            if(checkAncestor(nd,it))
                ans++;
        }
        cout<<ans-1<<" ";
    }
    return 0;
}


/*              `-._:  .:'   `:::  .:\           |\__/|           /::  .:'   `:::  .:.-'
                    \      :          \          |:   |          /         :       /
                     \     ::    .     `-_______/ ::   \_______-'   .      ::   . /
                      |  :   :: ::'  :   :: ::'  :   :: ::'      :: ::'  :   :: :|
                      |     ;::         ;::         ;::         ;::         ;::  |
                      |  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:|
                      /     :           :           :           :           :    \
                     /______::_____     ::    .     ::    .     ::   _____._::____\
                                   `----._:: ::'  :   :: ::'  _.----'
                                          `--.       ;::  .--'
                                              `-. .:'  .-'
                                                 \    /
                                                  \  /
                                                   */

//ヾ(⌐■_■)ノ♪ ◢◤
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define pa pair<int,int>
#define ld long double
#define sz(x) x.size()
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define INF 1e18
#define PI 3.14159265
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
priority_queue<int, vector<int>, greater<int> > pq;//Min Heap
double startTime;
double getCurrentTime() {
  return ((double)clock() - startTime) / CLOCKS_PER_SEC;
}
bool isPrime(int n)
{
    if (n <= 1)  return false;
    if (n <= 3)  return true;
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
int power(int x, unsigned int y, int p)
{
    int res=1;
    x=x%p;
    while(y>0)
    {
        if(y&1) res=(res*x)%p;
        y=y>>1;
        x=(x*x)%p;
    }
    return res;
}
const int N=1e5+10;
const int maxn=log2(N);
int LCA[N][maxn+10],visit[N],tout[N],tin[N];
vector<int> adj[N];
vector<int> level[N+10];
int maxd=0;
int T=0;
void DFS(int node,int depth=0)
{
    visit[node]=1;
    level[depth].pb(node);
    tin[node]=T++;
    for(auto ch:adj[node])
    {
        if(!visit[ch])
            DFS(ch,depth+1);
    }
    maxd=max(maxd,depth);
    tout[node]=T++;
}
void init()
{
    for(int j=1;j<=maxn;j++)
    {
        for(int i=1;i<N;i++)
        {
            if(LCA[i][j-1]!=-1)
            {
                int par=LCA[i][j-1];
                LCA[i][j]=LCA[par][j-1];
            }
        }
    }
}
bool checkAncestor(int a,int b)//Check if a is ancestor of b
{
    if(tin[a]<=tin[b]&&tout[a]>=tout[b])
        return true;
    return false;
}
int32_t main()
{
    IOS;
    int n;
    cin>>n;
    memset(LCA,-1,sizeof(LCA));
    for(int i=1;i<=n;i++)
    {
        int x;
        cin>>x;
        if(x!=0)
        {
            LCA[i][0]=x;
            adj[i].pb(x);
            adj[x].pb(i);
        }
    }
    init();
    // for(int i=1;i<=8;i++)
    // {
    //     for(int j=0;j<=7;j++)
    //         cout<<LCA[i][j]<<" ";
    //     cout<<endl;
    // }
    for(int i=1;i<=n;i++)
    {
        if(!visit[i])
            DFS(i);
    }
    int m;
    cin>>m;
    while(m--)
    {
        int nd,p;
        cin>>nd>>p;
        int d=p;
        for(int i=maxn;i>=0;i--)
        {
            int pwr=(1<<i);

            if(d-pwr>=0)
            {
                if(LCA[nd][i]==-1)
                    continue;
                nd=LCA[nd][i];
                d-=pwr;
            }
        }
        int ans=0;
        if(nd==-1||d>0)
        {
            cout<<ans<<" ";
            continue;
        }
        for(auto it:level[p])
        {
            if(checkAncestor(nd,it))
                ans++;
        }
        cout<<ans-1<<" ";
    }
    return 0;
}


/*              `-._:  .:'   `:::  .:\           |\__/|           /::  .:'   `:::  .:.-'
                    \      :          \          |:   |          /         :       /
                     \     ::    .     `-_______/ ::   \_______-'   .      ::   . /
                      |  :   :: ::'  :   :: ::'  :   :: ::'      :: ::'  :   :: :|
                      |     ;::         ;::         ;::         ;::         ;::  |
                      |  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:|
                      /     :           :           :           :           :    \
                     /______::_____     ::    .     ::    .     ::   _____._::____\
                                   `----._:: ::'  :   :: ::'  _.----'
                                          `--.       ;::  .--'
                                              `-. .:'  .-'
                                                 \    /
                                                  \  /
                                                   */

//ヾ(⌐■_■)ノ♪ ◢◤
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define pa pair<int,int>
#define ld long double
#define sz(x) x.size()
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define INF 1e18
#define PI 3.14159265
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
priority_queue<int, vector<int>, greater<int> > pq;//Min Heap
double startTime;
double getCurrentTime() {
  return ((double)clock() - startTime) / CLOCKS_PER_SEC;
}
bool isPrime(int n)
{
    if (n <= 1)  return false;
    if (n <= 3)  return true;
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
int power(int x, unsigned int y, int p)
{
    int res=1;
    x=x%p;
    while(y>0)
    {
        if(y&1) res=(res*x)%p;
        y=y>>1;
        x=(x*x)%p;
    }
    return res;
}
const int N=1e5+10;
const int maxn=log2(N);
int LCA[N][maxn+10],visit[N],tout[N],tin[N];
vector<int> adj[N];
vector<int> level[N+10];
int maxd=0;
int T=0;
void DFS(int node,int depth=0)
{
    visit[node]=1;
    level[depth].pb(node);
    tin[node]=T++;
    for(auto ch:adj[node])
    {
        if(!visit[ch])
            DFS(ch,depth+1);
    }
    maxd=max(maxd,depth);
    tout[node]=T++;
}
void init()
{
    for(int j=1;j<=maxn;j++)
    {
        for(int i=1;i<N;i++)
        {
            if(LCA[i][j-1]!=-1)
            {
                int par=LCA[i][j-1];
                LCA[i][j]=LCA[par][j-1];
            }
        }
    }
}
bool checkAncestor(int a,int b)//Check if a is ancestor of b
{
    if(tin[a]<=tin[b]&&tout[a]>=tout[b])
        return true;
    return false;
}
int32_t main()
{
    IOS;
    int n;
    cin>>n;
    memset(LCA,-1,sizeof(LCA));
    for(int i=1;i<=n;i++)
    {
        int x;
        cin>>x;
        if(x!=0)
        {
            LCA[i][0]=x;
            adj[i].pb(x);
            adj[x].pb(i);
        }
    }
    init();
    for(int i=1;i<=8;i++)
    {
        for(int j=0;j<=7;j++)
            cout<<LCA[i][j]<<" ";
        cout<<endl;
    }
    for(int i=1;i<=n;i++)
    {
        if(!visit[i])
            DFS(i);
    }
    int m;
    cin>>m;
    while(m--)
    {
        int nd,p;
        cin>>nd>>p;
        int d=p;
        for(int i=maxn;i>=0;i--)
        {
            int pwr=(1<<i);

            if(d-pwr>=0)
            {
                if(LCA[nd][i]==-1)
                    continue;
                nd=LCA[nd][i];
                d-=pwr;
            }
        }
        int ans=0;
        if(nd==-1||d>0)
        {
            cout<<ans<<" ";
            continue;
        }
        for(auto it:level[p])
        {
            if(checkAncestor(nd,it))
                ans++;
        }
        cout<<ans-1<<" ";
    }
    return 0;
}


/*              `-._:  .:'   `:::  .:\           |\__/|           /::  .:'   `:::  .:.-'
                    \      :          \          |:   |          /         :       /
                     \     ::    .     `-_______/ ::   \_______-'   .      ::   . /
                      |  :   :: ::'  :   :: ::'  :   :: ::'      :: ::'  :   :: :|
                      |     ;::         ;::         ;::         ;::         ;::  |
                      |  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:|
                      /     :           :           :           :           :    \
                     /______::_____     ::    .     ::    .     ::   _____._::____\
                                   `----._:: ::'  :   :: ::'  _.----'
                                          `--.       ;::  .--'
                                              `-. .:'  .-'
                                                 \    /
                                                  \  /
                                                   */

//ヾ(⌐■_■)ノ♪ ◢◤
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define pa pair<int,int>
#define ld long double
#define sz(x) x.size()
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define INF 1e18
#define PI 3.14159265
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
priority_queue<int, vector<int>, greater<int> > pq;//Min Heap
double startTime;
double getCurrentTime() {
  return ((double)clock() - startTime) / CLOCKS_PER_SEC;
}
bool isPrime(int n)
{
    if (n <= 1)  return false;
    if (n <= 3)  return true;
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
int power(int x, unsigned int y, int p)
{
    int res=1;
    x=x%p;
    while(y>0)
    {
        if(y&1) res=(res*x)%p;
        y=y>>1;
        x=(x*x)%p;
    }
    return res;
}
const int N=1e5+10;
const int maxn=log2(N);
int LCA[N][maxn+10],visit[N],tout[N],tin[N];
vector<int> adj[N];
vector<int> level[maxn+10];
int maxd=0;
int T=0;
void DFS(int node,int depth=0)
{
    visit[node]=1;
    level[depth].pb(node);
    tin[node]=T++;
    for(auto ch:adj[node])
    {
        if(!visit[ch])
            DFS(ch,depth+1);
    }
    maxd=max(maxd,depth);
    tout[node]=T++;
}
void init()
{
    for(int j=1;j<=maxn;j++)
    {
        for(int i=1;i<N;i++)
        {
            if(LCA[i][j-1]!=-1)
            {
                int par=LCA[i][j-1];
                LCA[i][j]=LCA[par][j-1];
            }
        }
    }
}
bool checkAncestor(int a,int b)//Check if a is ancestor of b
{
    if(tin[a]<=tin[b]&&tout[a]>=tout[b])
        return true;
    return false;
}
int32_t main()
{
    IOS;
    int n;
    cin>>n;
    memset(LCA,-1,sizeof(LCA));
    for(int i=1;i<=n;i++)
    {
        int x;
        cin>>x;
        if(x!=0)
        {
            LCA[i][0]=x;
            adj[i].pb(x);
            adj[x].pb(i);
        }
    }
    init();
    for(int i=1;i<=n;i++)
    {
        if(!visit[i])
            DFS(i);
    }
    int m;
    cin>>m;
    while(m--)
    {
        int nd,p;
        cin>>nd>>p;
        int d=p;
        for(int i=maxd;i>=0;i--)
        {
            long long int pwr=(1<<i);
            if(d-pwr>=0)
            {
                nd=LCA[nd][i];
                d-=pwr;
            }
        }
        int ans=0;
        if(nd==-1||d!=0)
        {
            cout<<ans<<" ";
            continue;
        }
        for(auto it:level[p])
        {
            if(checkAncestor(nd,it))
                ans++;
        }
        cout<<ans-1<<" ";
    }
    return 0;
}


/*              `-._:  .:'   `:::  .:\           |\__/|           /::  .:'   `:::  .:.-'
                    \      :          \          |:   |          /         :       /
                     \     ::    .     `-_______/ ::   \_______-'   .      ::   . /
                      |  :   :: ::'  :   :: ::'  :   :: ::'      :: ::'  :   :: :|
                      |     ;::         ;::         ;::         ;::         ;::  |
                      |  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:|
                      /     :           :           :           :           :    \
                     /______::_____     ::    .     ::    .     ::   _____._::____\
                                   `----._:: ::'  :   :: ::'  _.----'
                                          `--.       ;::  .--'
                                              `-. .:'  .-'
                                                 \    /
                                                  \  /
                                                   */

//ヾ(⌐■_■)ノ♪ ◢◤
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define pa pair<int,int>
#define ld long double
#define sz(x) x.size()
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define INF 1e18
#define PI 3.14159265
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
priority_queue<int, vector<int>, greater<int> > pq;//Min Heap
double startTime;
double getCurrentTime() {
  return ((double)clock() - startTime) / CLOCKS_PER_SEC;
}
bool isPrime(int n)
{
    if (n <= 1)  return false;
    if (n <= 3)  return true;
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
int power(int x, unsigned int y, int p)
{
    int res=1;
    x=x%p;
    while(y>0)
    {
        if(y&1) res=(res*x)%p;
        y=y>>1;
        x=(x*x)%p;
    }
    return res;
}
const int N=1e5+10;
const int maxn=log2(N);
int LCA[N][maxn+10],visit[N],tout[N],tin[N];
vector<int> adj[N];
vector<int> level[maxn+10];
int T=0;
void DFS(int node,int depth=0)
{
    visit[node]=1;
    level[depth].pb(node);
    tin[node]=T++;
    for(auto ch:adj[node])
    {
        if(!visit[ch])
            DFS(ch,depth+1);
    }
    tout[node]=T++;
}
void init()
{
    for(int j=1;j<=maxn;j++)
    {
        for(int i=1;i<N;i++)
        {
            if(LCA[i][j-1]!=-1)
            {
                int par=LCA[i][j-1];
                LCA[i][j]=LCA[par][j-1];
            }
        }
    }
}
bool checkAncestor(int a,int b)//Check if a is ancestor of b
{
    if(tin[a]<=tin[b]&&tout[a]>=tout[b])
        return true;
    return false;
}
int32_t main()
{
    IOS;
    int n;
    cin>>n;
    memset(LCA,-1,sizeof(LCA));
    for(int i=1;i<=n;i++)
    {
        int x;
        cin>>x;
        if(x!=0)
        {
            LCA[i][0]=x;
            adj[i].pb(x);
            adj[x].pb(i);
        }
    }
    init();
    for(int i=1;i<=n;i++)
    {
        if(!visit[i])
            DFS(i);
    }
    int m;
    cin>>m;
    while(m--)
    {
        int nd,p;
        cin>>nd>>p;
        int d=p;
        for(int i=maxn;i>=0;i--)
        {
            int pwr=(1<<i);
            if(d-pwr>=0)
            {
                nd=LCA[nd][i];
                d-=pwr;
            }
        }
        int ans=0;
        if(nd==-1)
        {
            cout<<ans<<" ";
            continue;
        }
        for(auto it:level[p])
        {
            if(checkAncestor(nd,it))
                ans++;
        }
        cout<<ans-1<<" ";
    }
    return 0;
}


/*              `-._:  .:'   `:::  .:\           |\__/|           /::  .:'   `:::  .:.-'
                    \      :          \          |:   |          /         :       /
                     \     ::    .     `-_______/ ::   \_______-'   .      ::   . /
                      |  :   :: ::'  :   :: ::'  :   :: ::'      :: ::'  :   :: :|
                      |     ;::         ;::         ;::         ;::         ;::  |
                      |  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:|
                      /     :           :           :           :           :    \
                     /______::_____     ::    .     ::    .     ::   _____._::____\
                                   `----._:: ::'  :   :: ::'  _.----'
                                          `--.       ;::  .--'
                                              `-. .:'  .-'
                                                 \    /
                                                  \  /
                                                   */

//ヾ(⌐■_■)ノ♪ ◢◤
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define pb push_back
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define pa pair<int,int>
#define ld long double
#define sz(x) x.size()
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define INF 1e18
#define PI 3.14159265
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
priority_queue<int, vector<int>, greater<int> > pq;//Min Heap
double startTime;
double getCurrentTime() {
  return ((double)clock() - startTime) / CLOCKS_PER_SEC;
}
bool isPrime(int n)
{
    if (n <= 1)  return false;
    if (n <= 3)  return true;
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
int power(int x, unsigned int y, int p)
{
    int res=1;
    x=x%p;
    while(y>0)
    {
        if(y&1) res=(res*x)%p;
        y=y>>1;
        x=(x*x)%p;
    }
    return res;
}
const int N=1e5+10;
const int maxn=log2(N);
int LCA[N+10][maxn+10],level[N],visit[N];
vector<int> adj[N];
void init()
{
    for(int j=1;j<=maxn;j++)
    {
        for(int i=1;i<N;i++)
        {
            if(LCA[i][j-1]!=-1)
            {
                int par=LCA[i][j-1];
                LCA[i][j]=LCA[par][j-1];
            }
        }
    }
}
int num[N],tin[N];
vector<int> tmp[N];
int T=1;
void DFS(int node,int par=-1,int depth=0)
{
    num[node]++;
    tin[node]=T++;
    tmp[depth].pb(tin[node]);
    level[node]=depth;
    visit[node]=1;
    LCA[node][0]=par;
    for(auto ch:adj[node])
    {
        if(ch==par)
            continue;
        DFS(ch,node,depth+1);
        num[node]+=num[ch];
    }
}
int32_t main()
{
    IOS;
    int n;
    cin>>n;
    memset(LCA,-1,sizeof(LCA));
    for(int i=1;i<=n;i++)
    {
        int x;
        cin>>x;
        if(x!=0)
        {
            adj[i].pb(x);
            adj[x].pb(i);
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(!visit[i])
            DFS(i);
    }
    init();
    int m;
    cin>>m;
    while(m--)
    {
        int nd,p;
        cin>>nd>>p;
        int v=nd;
        int d=p;
        for(int i=maxn;i>=0;i--)
        {
            int pwr=1<<i;
            if(d-pwr>=0)
            {
                nd=LCA[nd][i];
                d-=pwr;
            }
        }
        if(nd==-1||d!=0)
        {
            cout<<0<<" ";
            continue;
        }
        int start=tin[nd];
        int q2=num[nd];
        int end=start+q2-1;
        // cout<<start<<" "<<end<<endl;
        int p1=lower_bound(all(tmp[level[v]]),start)-tmp[level[v]].begin();
        int p2=upper_bound(all(tmp[level[v]]),end)-tmp[level[v]].begin();
        cout<<p2-p1-1<<" ";
    }
    return 0;
}


/*              `-._:  .:'   `:::  .:\           |\__/|           /::  .:'   `:::  .:.-'
                    \      :          \          |:   |          /         :       /
                     \     ::    .     `-_______/ ::   \_______-'   .      ::   . /
                      |  :   :: ::'  :   :: ::'  :   :: ::'      :: ::'  :   :: :|
                      |     ;::         ;::         ;::         ;::         ;::  |
                      |  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:|
                      /     :           :           :           :           :    \
                     /______::_____     ::    .     ::    .     ::   _____._::____\
                                   `----._:: ::'  :   :: ::'  _.----'
                                          `--.       ;::  .--'
                                              `-. .:'  .-'
                                                 \    /
                                                  \  /
                                                   */

//ヾ(⌐■_■)ノ♪ ◢◤
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define int long long int
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define pa pair<int,int>
#define ld long double
#define sz(x) x.size()
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define INF 1e18
#define PI 3.14159265
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
priority_queue<int, vector<int>, greater<int> > pq;//Min Heap
double startTime;
double getCurrentTime() {
  return ((double)clock() - startTime) / CLOCKS_PER_SEC;
}
bool isPrime(int n)
{
    if (n <= 1)  return false;
    if (n <= 3)  return true;
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
int power(int x, unsigned int y, int p)
{
    int res=1;
    x=x%p;
    while(y>0)
    {
        if(y&1) res=(res*x)%p;
        y=y>>1;
        x=(x*x)%p;
    }
    return res;
}
const int N=1e5+10;
const int maxn=log2(N);
int LCA[N][maxn+10],visit[N],tout[N],tin[N];
vector<int> adj[N];
vector<int> level[maxn+10];
int maxd=0;
int T=0;
void DFS(int node,int depth=0)
{
    visit[node]=1;
    level[depth].pb(node);
    tin[node]=T++;
    for(auto ch:adj[node])
    {
        if(!visit[ch])
            DFS(ch,depth+1);
    }
    maxd=max(maxd,depth);
    tout[node]=T++;
}
void init()
{
    for(int j=1;j<=maxn;j++)
    {
        for(int i=1;i<N;i++)
        {
            if(LCA[i][j-1]!=-1)
            {
                int par=LCA[i][j-1];
                LCA[i][j]=LCA[par][j-1];
            }
        }
    }
}
bool checkAncestor(int a,int b)//Check if a is ancestor of b
{
    if(tin[a]<=tin[b]&&tout[a]>=tout[b])
        return true;
    return false;
}
int32_t main()
{
    IOS;
    int n;
    cin>>n;
    memset(LCA,-1,sizeof(LCA));
    for(int i=1;i<=n;i++)
    {
        int x;
        cin>>x;
        if(x!=0)
        {
            LCA[i][0]=x;
            adj[i].pb(x);
            adj[x].pb(i);
        }
    }
    init();
    for(int i=1;i<=n;i++)
    {
        if(!visit[i])
            DFS(i);
    }
    int m;
    cin>>m;
    while(m--)
    {
        int nd,p;
        cin>>nd>>p;
        int d=p;
        while(d>0&&nd!=-1)
        {
            int jump=log2(d);
            nd=LCA[nd][jump];
            d-=(1<<jump);//d-=2^(i);
        }
        int ans=0;
        if(nd==-1||d>0)
        {
            cout<<ans<<" ";
            continue;
        }
        for(auto it:level[p])
        {
            if(checkAncestor(nd,it))
                ans++;
        }
        cout<<ans-1<<" ";
    }
    return 0;
}


/*              `-._:  .:'   `:::  .:\           |\__/|           /::  .:'   `:::  .:.-'
                    \      :          \          |:   |          /         :       /
                     \     ::    .     `-_______/ ::   \_______-'   .      ::   . /
                      |  :   :: ::'  :   :: ::'  :   :: ::'      :: ::'  :   :: :|
                      |     ;::         ;::         ;::         ;::         ;::  |
                      |  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:|
                      /     :           :           :           :           :    \
                     /______::_____     ::    .     ::    .     ::   _____._::____\
                                   `----._:: ::'  :   :: ::'  _.----'
                                          `--.       ;::  .--'
                                              `-. .:'  .-'
                                                 \    /
                                                  \  /
                                                   */

//ヾ(⌐■_■)ノ♪ ◢◤
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define pa pair<int,int>
#define ld long double
#define sz(x) x.size()
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define INF 1e18
#define PI 3.14159265
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
priority_queue<int, vector<int>, greater<int> > pq;//Min Heap
double startTime;
double getCurrentTime() {
  return ((double)clock() - startTime) / CLOCKS_PER_SEC;
}
bool isPrime(int n)
{
    if (n <= 1)  return false;
    if (n <= 3)  return true;
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
int power(int x, unsigned int y, int p)
{
    int res=1;
    x=x%p;
    while(y>0)
    {
        if(y&1) res=(res*x)%p;
        y=y>>1;
        x=(x*x)%p;
    }
    return res;
}
const int N=1e5+10;
const int maxn=log2(N);
int LCA[N][maxn+10],visit[N],tout[N],tin[N];
vector<int> adj[N];
vector<int> level[N+10];
int maxd=0;
int T=0;
void DFS(int node,int depth=0)
{
    visit[node]=1;
    level[depth].pb(node);
    tin[node]=T++;
    for(auto ch:adj[node])
    {
        if(!visit[ch])
            DFS(ch,depth+1);
    }
    maxd=max(maxd,depth);
    tout[node]=T++;
}
void init()
{
    for(int j=1;j<=maxn;j++)
    {
        for(int i=1;i<N;i++)
        {
            if(LCA[i][j-1]!=-1)
            {
                int par=LCA[i][j-1];
                LCA[i][j]=LCA[par][j-1];
            }
        }
    }
}
bool checkAncestor(int a,int b)//Check if a is ancestor of b
{
    if(tin[a]<=tin[b]&&tout[a]>=tout[b])
        return true;
    return false;
}
int32_t main()
{
    IOS;
    int n;
    cin>>n;
    memset(LCA,-1,sizeof(LCA));
    for(int i=1;i<=n;i++)
    {
        int x;
        cin>>x;
        if(x!=0)
        {
            LCA[i][0]=x;
            adj[i].pb(x);
            adj[x].pb(i);
        }
    }
    init();
    for(int i=1;i<=n;i++)
    {
        if(!visit[i])
            DFS(i);
    }
    int m;
    cin>>m;
    while(m--)
    {
        int nd,p;
        cin>>nd>>p;
        int d=p;
        for(int i=maxn;i>=0;i--)
        {
            int pwr=(1<<i);
            if(d-pwr>=0)
            {
                nd=LCA[nd][i];
                d-=pwr;
            }
        }
        int ans=0;
        if(nd==-1||d!=0)
        {
            cout<<ans<<" ";
            continue;
        }
        for(auto it:level[p])
        {
            if(checkAncestor(nd,it))
                ans++;
        }
        cout<<ans-1<<" ";
    }
    return 0;
}


/*              `-._:  .:'   `:::  .:\           |\__/|           /::  .:'   `:::  .:.-'
                    \      :          \          |:   |          /         :       /
                     \     ::    .     `-_______/ ::   \_______-'   .      ::   . /
                      |  :   :: ::'  :   :: ::'  :   :: ::'      :: ::'  :   :: :|
                      |     ;::         ;::         ;::         ;::         ;::  |
                      |  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:|
                      /     :           :           :           :           :    \
                     /______::_____     ::    .     ::    .     ::   _____._::____\
                                   `----._:: ::'  :   :: ::'  _.----'
                                          `--.       ;::  .--'
                                              `-. .:'  .-'
                                                 \    /
                                                  \  /
                                                   */

//ヾ(⌐■_■)ノ♪ ◢◤
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define pb push_back
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define pa pair<int,int>
#define ld long double
#define sz(x) x.size()
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define INF 1e18
#define PI 3.14159265
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
priority_queue<int, vector<int>, greater<int> > pq;//Min Heap
double startTime;
double getCurrentTime() {
  return ((double)clock() - startTime) / CLOCKS_PER_SEC;
}
bool isPrime(int n)
{
    if (n <= 1)  return false;
    if (n <= 3)  return true;
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
int power(int x, unsigned int y, int p)
{
    int res=1;
    x=x%p;
    while(y>0)
    {
        if(y&1) res=(res*x)%p;
        y=y>>1;
        x=(x*x)%p;
    }
    return res;
}
const int N=1e5+10;
const int maxn=log2(N);
int LCA[N+10][maxn+10],level[N],visit[N];
vector<int> adj[N];
void init()
{
    for(int j=1;j<=maxn;j++)
    {
        for(int i=1;i<N;i++)
        {
            if(LCA[i][j-1]!=-1)
            {
                int par=LCA[i][j-1];
                LCA[i][j]=LCA[par][j-1];
            }
        }
    }
}
int num[N],tin[N];
vector<int> tmp[N];
int T=1;
void DFS(int node,int par=-1,int depth=0)
{
    num[node]++;
    tin[node]=T++;
    tmp[depth].pb(tin[node]);
    level[node]=depth;
    visit[node]=1;
    LCA[node][0]=par;
    for(auto ch:adj[node])
    {
        if(ch==par)
            continue;
        DFS(ch,node,depth+1);
        num[node]+=num[ch];
    }
}
int kthanc(int node,int k)
{
    if(k==0)
    {
        return node;
    }
    if(level[node]<k)
        return -1;
    for(int i=20;i>=0;i--)
    {
        int pwr=1<<i;
        if(k>=pwr)
            return kthanc(LCA[node][i],k-pwr);
    }
}
int32_t main()
{
    IOS;
    int n;
    cin>>n;
    memset(LCA,-1,sizeof(LCA));
    for(int i=1;i<=n;i++)
    {
        int x;
        cin>>x;
        if(x!=0)
        {
            adj[i].pb(x);
            adj[x].pb(i);
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(!visit[i])
            DFS(i);
    }
    init();
    int m;
    cin>>m;
    while(m--)
    {
        int nd,p;
        cin>>nd>>p;
        int v=nd;
        nd=kthanc(nd,p);
        if(nd==-1)
        {
            cout<<0<<" ";
            continue;
        }
        int start=tin[nd];
        int q2=num[nd];
        int end=start+q2-1;
        // cout<<start<<" "<<end<<endl;
        int p1=lower_bound(all(tmp[level[v]]),start)-tmp[level[v]].begin();
        int p2=upper_bound(all(tmp[level[v]]),end)-tmp[level[v]].begin();
        cout<<p2-p1-1<<" ";
    }
    return 0;
}


/*              `-._:  .:'   `:::  .:\           |\__/|           /::  .:'   `:::  .:.-'
                    \      :          \          |:   |          /         :       /
                     \     ::    .     `-_______/ ::   \_______-'   .      ::   . /
                      |  :   :: ::'  :   :: ::'  :   :: ::'      :: ::'  :   :: :|
                      |     ;::         ;::         ;::         ;::         ;::  |
                      |  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:::  .:'   `:|
                      /     :           :           :           :           :    \
                     /______::_____     ::    .     ::    .     ::   _____._::____\
                                   `----._:: ::'  :   :: ::'  _.----'
                                          `--.       ;::  .--'
                                              `-. .:'  .-'
                                                 \    /
                                                  \  /
                                                   */

//ヾ(⌐■_■)ノ♪ ◢◤
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define int long long int
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define pa pair<int,int>
#define ld long double
#define sz(x) x.size()
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define INF 1e18
#define PI 3.14159265
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
priority_queue<int, vector<int>, greater<int> > pq;//Min Heap
double startTime;
double getCurrentTime() {
  return ((double)clock() - startTime) / CLOCKS_PER_SEC;
}
bool isPrime(int n)
{
    if (n <= 1)  return false;
    if (n <= 3)  return true;
    if (n%2 == 0 || n%3 == 0) return false;

    for (int i=5; i*i<=n; i=i+6)
        if (n%i == 0 || n%(i+2) == 0)
           return false;

    return true;
}
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
int power(int x, unsigned int y, int p)
{
    int res=1;
    x=x%p;
    while(y>0)
    {
        if(y&1) res=(res*x)%p;
        y=y>>1;
        x=(x*x)%p;
    }
    return res;
}
const int N=1e5+10;
const int maxn=log2(N);
int LCA[N][maxn+10],visit[N],tout[N],tin[N];
vector<int> adj[N];
vector<int> level[maxn+10];
int maxd=0;
int T=0;
void DFS(int node,int depth=0)
{
    visit[node]=1;
    level[depth].pb(node);
    tin[node]=T++;
    for(auto ch:adj[node])
    {
        if(!visit[ch])
            DFS(ch,depth+1);
    }
    maxd=max(maxd,depth);
    tout[node]=T++;
}
void init()
{
    for(int j=1;j<=maxn;j++)
    {
        for(int i=1;i<N;i++)
        {
            if(LCA[i][j-1]!=-1)
            {
                int par=LCA[i][j-1];
                LCA[i][j]=LCA[par][j-1];
            }
        }
    }
}
bool checkAncestor(int a,int b)//Check if a is ancestor of b
{
    if(tin[a]<=tin[b]&&tout[a]>=tout[b])
        return true;
    return false;
}
int32_t main()
{
    IOS;
    int n;
    cin>>n;
    memset(LCA,-1,sizeof(LCA));
    for(int i=1;i<=n;i++)
    {
        int x;
        cin>>x;
        if(x!=0)
        {
            LCA[i][0]=x;
            adj[i].pb(x);
            adj[x].pb(i);
        }
    }
    init();
    for(int i=1;i<=n;i++)
    {
        if(!visit[i])
            DFS(i);
    }
    int m;
    cin>>m;
    while(m--)
    {
        int nd,p;
        cin>>nd>>p;
        int d=p;
        for(int i=maxd;i>=0;i--)
        {
            int pwr=(1<<i);
            if(d-pwr>=0)
            {
                nd=LCA[nd][i];
                d-=pwr;
            }
        }
        int ans=0;
        if(nd==-1||d!=0)
        {
            cout<<ans<<" ";
            continue;
        }
        for(auto it:level[p])
        {
            if(checkAncestor(nd,it))
                ans++;
        }
        cout<<ans-1<<" ";
    }
    return 0;
}


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

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REV(i,b,a) for(int i=(a);i>=(b);i--)
#define pb push_back
#define SZ(v) (int)v.size()
#define sz(v) (int)v.size()
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define mem(s,v) memset(s,v,sizeof(s))
#define ppc(x) __builtin_popcount((x))
#define iter(it,s) for(__typeof(s.begin())it = s.begin();it!=s.end();it++)
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;

const int OO = (int) 2e9;
const double eps = 1e-9;

// 1 based
int daysInMonths[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int di[] = { -1, 0, 1, 0 };
int dj[] = { 0, 1, 0, -1 };
#define Nd 0
#define Ed 1
#define Sd 2
#define Wd 3

int n, x, y, m, cnt, dp[100002][20], depth[100002], sz[100002], mp[100002];
vector<int> adj[100002], nodesAt[100002], root;

int dfs(int u, int p){
    dp[u][0] = p;
    int sum = 0;
    for(int i = 0; i < adj[u].size(); i++){
        int v = adj[u][i];
        if(v == p) continue;
        mp[v]= ++cnt;
        depth[v] = depth[u] + 1;
        nodesAt[depth[v]].pb(mp[v]);
        sum += dfs(v, u);
    }
    return sz[u] = sum + 1;
}

int getAns(int u, int p){
    int u1 = u;
    int rem = p;
    for(int k = 19; k >=0; k--){
        if(rem >= (1<<k)){
            if(u == -1) continue;
            u = dp[u][k];
            rem -= (1<<k);
        }
    }
    if(u == -1)
        return 0;
    int ans = upper_bound(all(nodesAt[depth[u1]]), mp[u]+sz[u]-1) - lower_bound(all(nodesAt[depth[u1]]), mp[u]) - 1;
    if(ans < 0)
        ans = 0;
    return ans;
}

int main(){
	std::ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("in.txt", "rt", stdin);
	//	freopen("out.out", "wt", stdout);
#endif

    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> x;
        if(x == 0){
            root.pb(i);
            continue;
        }
        adj[x].pb(i);
        adj[i].pb(x);
    }

    memset(dp, -1, sizeof dp);

    for(int i = 0; i < root.size(); i++){
        mp[root[i]] = ++cnt;
        dfs(root[i], -1);
    }

    for(int k = 1; k <= 19; k++){
        for(int u = 1; u <= n; u++){
            if(dp[u][k-1] == -1) continue;
            dp[u][k] = dp[dp[u][k-1]][k-1];
        }
    }

    cin >> m;
    while(m--){
        cin >> x >> y;
        cout << getAns(x, y) << " ";
    }

	return 0;
}


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

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REV(i,b,a) for(int i=(a);i>=(b);i--)
#define mp make_pair
#define pb push_back
#define SZ(v) (int)v.size()
#define sz(v) (int)v.size()
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define mem(s,v) memset(s,v,sizeof(s))
#define ppc(x) __builtin_popcount((x))
#define iter(it,s) for(__typeof(s.begin())it = s.begin();it!=s.end();it++)
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;

const int OO = (int) 2e9;
const double eps = 1e-9;

// 1 based
int daysInMonths[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int di[] = { -1, 0, 1, 0 };
int dj[] = { 0, 1, 0, -1 };
#define Nd 0
#define Ed 1
#define Sd 2
#define Wd 3

int x, n, m, p, v, dp[100002][20], depth[100002];
vector<int> g[100002];
vector<int> nodesAt[100002];
vector<int> roots;
int nbr[100002];
int mpp[100002], r[100002];
int treeBegin[100002], treeEnd[100002], sizes[100002];;
int cnt, sum;

void dfs(int u, int parent, int root){
    dp[u][0] = parent;
    for(int i = 0; i < g[u].size(); i++){
        int v = g[u][i];
        if(v == parent) continue;
        depth[v] = depth[u] + 1;
        nbr[root]++;
        mpp[v] = ++cnt;
        nodesAt[depth[v]].pb(mpp[v]);
        r[v] = root;
        dfs(v, u, root);
    }
}

int dfs2(int u, int parent){
    int sum = 0;
    for(int i = 0; i < g[u].size(); i++){
        int v = g[u][i];
        if(v == parent) continue;
        sum += dfs2(v, u);
    }
    return sizes[u] = sum+1;
}

int ans(int v, int p){
    int rem = p;
    int u = v;
    for(int k = 19; k >= 0; k--){
        if(rem >= (1<<k)){
            u = dp[u][k];
            rem -= (1<<k);
        }
    }

    if(u == -1)
        return 0;
    //printf("size  at depth %d : %d\n", depth[v], nodesAt[depth[v]].size());
   /* printf("depth[v] : %d,  %d %d %d : \n", depth[v], u, mpp[u], sizes[u]);
    for(int i = 0; i < nodesAt[depth[v]].size(); i++){
        printf("%d ", nodesAt[depth[v]][i]);
    }
    printf("\n");*/
   // printf("%d %d\n", r[v], nbr[r[v]]);
   //printf("|%d %d %d|\n", u, mpp[u], sizes[u]);
    int ans =  upper_bound(all(nodesAt[depth[v]]), mpp[u] + sizes[u]) - lower_bound(all(nodesAt[depth[v]]), mpp[u]) - 1;
    if(ans <= 0)
        return 0;
    return ans;

}

int main(){
	std::ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("in.txt", "rt", stdin);
	//	freopen("out.out", "wt", stdout);
#endif

    scanf("%d", &n);
    for(int i = 1; i <= n; i++){
        scanf("%d", &x);
        if(x == 0){
            roots.push_back(i);
            continue;
        }
        g[i].pb(x);
        g[x].pb(i);
    }

    memset(dp, -1, sizeof dp);

    for(int i = 0; i < roots.size(); i++){
        mpp[roots[i]] = ++cnt;
        dfs(roots[i], -1, roots[i]);
        dfs2(roots[i], -1);
    }

    for(int k = 1; k <= 19; k++){
        for(int u = 1; u <= n; u++){
            if(dp[u][k-1] == -1) continue;
            dp[u][k] = dp[dp[u][k-1]][k-1];
        }
    }
    //for(int i = 1; i <= n; i++)
      //  printf("u:%d, mpp[u]:%d\n", i, mpp[i]);
    //printf("%d\n", sizes[6], sizes[7]);
    scanf("%d", &m);
    while(m--){
        scanf("%d%d", &v, &p);
        printf("%d ",ans(v, p));
    }

	return 0;
}


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

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REV(i,b,a) for(int i=(a);i>=(b);i--)
#define mp make_pair
#define pb push_back
#define SZ(v) (int)v.size()
#define sz(v) (int)v.size()
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define mem(s,v) memset(s,v,sizeof(s))
#define ppc(x) __builtin_popcount((x))
#define iter(it,s) for(__typeof(s.begin())it = s.begin();it!=s.end();it++)
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;

const int OO = (int) 2e9;
const double eps = 1e-9;

// 1 based
int daysInMonths[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int di[] = { -1, 0, 1, 0 };
int dj[] = { 0, 1, 0, -1 };
#define Nd 0
#define Ed 1
#define Sd 2
#define Wd 3

int x, n, m, p, v, dp[100002][20], depth[100002];
vector<int> g[100002];
vector<int> nodesAt[100002];
vector<int> roots;
int nbr[100002];
int mpp[100002], r[1000002];
int cnt;

void dfs(int u, int parent, int root){
    dp[u][0] = parent;
    for(int i = 0; i < g[u].size(); i++){
        int v = g[u][i];
        if(v == parent) continue;
        depth[v] = depth[u] + 1;
        nbr[root]++;
        mpp[v] = ++cnt;
        nodesAt[depth[v]].pb(mpp[v]);
        r[v] = root;
        dfs(v, u, root);
    }
}

int ans(int v, int p){
    int rem = p;
    int u = v;
    for(int k = 19; k >= 0; k--){
        if(rem - (1<<k) >= 0){
            u = dp[u][k];
            rem -= (1<<k);
        }
    }

    if(u == -1)
        return 0;
    //printf("size  at depth %d : %d\n", depth[v], nodesAt[depth[v]].size());
    /*for(int i = 0; i < nodesAt[depth[v]].size(); i++){
        printf("%d ", nodesAt[depth[v]][i]);
    }
    printf("\n");*/
   // printf("%d %d\n", r[v], nbr[r[v]]);
    int ans = upper_bound(all(nodesAt[depth[v]]), r[v]+nbr[r[v]]) - lower_bound(all(nodesAt[depth[v]]), r[v]) - 1;
    return ans;

}

int main(){
	std::ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("in.txt", "rt", stdin);
	//	freopen("out.out", "wt", stdout);
#endif

    scanf("%d", &n);
    for(int i = 1; i <= n; i++){
        scanf("%d", &x);
        if(x == 0){
            roots.push_back(i);
            continue;
        }
        g[i].pb(x);
        g[x].pb(i);
    }

    memset(dp, -1, sizeof dp);

    for(int i = 0; i < roots.size(); i++){
        cnt = roots[i];
        dfs(roots[i], -1, roots[i]);
    }

    for(int k = 1; k <= 19; k++){
        for(int u = 1; u <= n; u++){
            if(dp[u][k-1] == -1) continue;
            dp[u][k] = dp[dp[u][k-1]][k-1];
        }
    }

    scanf("%d", &m);
    while(m--){
        scanf("%d%d", &v, &p);
        printf("%d ",ans(v, p));
    }

	return 0;
}


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

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REV(i,b,a) for(int i=(a);i>=(b);i--)
#define pb push_back
#define SZ(v) (int)v.size()
#define sz(v) (int)v.size()
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define mem(s,v) memset(s,v,sizeof(s))
#define ppc(x) __builtin_popcount((x))
#define iter(it,s) for(__typeof(s.begin())it = s.begin();it!=s.end();it++)
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;

const int OO = (int) 2e9;
const double eps = 1e-9;

// 1 based
int daysInMonths[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int di[] = { -1, 0, 1, 0 };
int dj[] = { 0, 1, 0, -1 };
#define Nd 0
#define Ed 1
#define Sd 2
#define Wd 3

int n, x, y, m, cnt, dp[100002][17], depth[100002], sz[100002], mp[100002];
vector<int> adj[100002], nodesAt[100002], root;

int dfs(int u, int p){
    dp[u][0] = p;
    int sum = 0;
    for(int i = 0; i < adj[u].size(); i++){
        int v = adj[u][i];
        if(v == p) continue;
        mp[v]= ++cnt;
        depth[v] = depth[u] + 1;
        nodesAt[depth[v]].pb(mp[v]);
        sum += dfs(v, u);
    }
    return sz[u] = sum + 1;
}

int getAns(int u, int p){

    int u1 = u;
    int rem = p;
    for(int k = 17; k >=0; k--){
        if(rem >= (1<<k)){
            if(u1 == -1) continue;
            u = dp[u][k];
            rem -= (1<<k);
        }
    }
    if(u1 == -1)
        return 0;
    int ans = upper_bound(all(nodesAt[depth[u1]]), mp[u]+sz[u]-1) - lower_bound(all(nodesAt[depth[u1]]), mp[u]) - 1;
    if(ans < 0)
        ans = 0;
    return ans;
}

int main(){
	std::ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("in.txt", "rt", stdin);
	//	freopen("out.out", "wt", stdout);
#endif

    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> x;
        if(x == 0){
            root.pb(i);
            continue;
        }
        adj[x].pb(i);
        adj[i].pb(x);
    }

    memset(dp, -1, sizeof dp);

    for(int i = 0; i < root.size(); i++){
        mp[root[i]] = ++cnt;
        dfs(root[i], -1);
    }

    for(int k = 1; k <= 17; k++){
        for(int u = 1; u <= n; u++){
            if(dp[u][k-1] == -1) continue;
            dp[u][k] = dp[dp[u][k-1]][k-1];
        }
    }

    cin >> m;
    while(m--){
        cin >> x >> y;
        cout << getAns(x, y) << " ";
    }

	return 0;
}


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

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REV(i,b,a) for(int i=(a);i>=(b);i--)
#define pb push_back
#define SZ(v) (int)v.size()
#define sz(v) (int)v.size()
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define mem(s,v) memset(s,v,sizeof(s))
#define ppc(x) __builtin_popcount((x))
#define iter(it,s) for(__typeof(s.begin())it = s.begin();it!=s.end();it++)
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;

const int OO = (int) 2e9;
const double eps = 1e-9;

// 1 based
int daysInMonths[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int di[] = { -1, 0, 1, 0 };
int dj[] = { 0, 1, 0, -1 };
#define Nd 0
#define Ed 1
#define Sd 2
#define Wd 3

int n, x, y, m, cnt, dp[100002][17], depth[100002], sz[100002], mp[100002];
vector<int> adj[100002], nodesAt[100002], root;

int dfs(int u, int p){
    dp[u][0] = p;
    int sum = 0;
    for(int i = 0; i < adj[u].size(); i++){
        int v = adj[u][i];
        if(v == p) continue;
        mp[v]= ++cnt;
        depth[v] = depth[u] + 1;
        nodesAt[depth[v]].pb(mp[v]);
        sum += dfs(v, u);
    }
    return sz[u] = sum + 1;
}

int getAns(int u, int p){

    int u1 = u;

    for(int k = 17; k >=0; k--){
        if(depth[u] - (1<<k) > depth[u] - p)
            u = dp[u][k];
    }
    u = dp[u][0];
    if(u == -1)
        return 0;
    int ans = upper_bound(all(nodesAt[depth[u1]]), mp[u]+sz[u]-1) - lower_bound(all(nodesAt[depth[u1]]), mp[u]) - 1;
    if(ans < 0)
        ans = 0;
    return ans;
}

int main(){
	std::ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("in.txt", "rt", stdin);
	//	freopen("out.out", "wt", stdout);
#endif

    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> x;
        if(x == 0){
            root.pb(i);
            continue;
        }
        adj[x].pb(i);
        adj[i].pb(x);
    }

    memset(dp, -1, sizeof dp);

    for(int i = 0; i < root.size(); i++){
        mp[root[i]] = ++cnt;
        dfs(root[i], -1);
    }

    for(int k = 1; k <= 17; k++){
        for(int u = 1; u <= n; u++){
            if(dp[u][k-1] == -1) continue;
            dp[u][k] = dp[dp[u][k-1]][k-1];
        }
    }

    cin >> m;
    while(m--){
        cin >> x >> y;
        cout << getAns(x, y) << " ";
    }

	return 0;
}


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

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REV(i,b,a) for(int i=(a);i>=(b);i--)
#define mp make_pair
#define pb push_back
#define SZ(v) (int)v.size()
#define sz(v) (int)v.size()
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define mem(s,v) memset(s,v,sizeof(s))
#define ppc(x) __builtin_popcount((x))
#define iter(it,s) for(__typeof(s.begin())it = s.begin();it!=s.end();it++)
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;

const int OO = (int) 2e9;
const double eps = 1e-9;

// 1 based
int daysInMonths[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int di[] = { -1, 0, 1, 0 };
int dj[] = { 0, 1, 0, -1 };
#define Nd 0
#define Ed 1
#define Sd 2
#define Wd 3

int x, n, m, p, v, dp[100002][20], depth[100002];
vector<int> g[100002];
vector<int> nodesAt[100002];
vector<int> roots;
int nbr[100002];
int mpp[100002], r[100002];
int treeBegin[100002], treeEnd[100002], sizes[100002];;
int cnt, sum;

void dfs(int u, int parent, int root){
    dp[u][0] = parent;
    for(int i = 0; i < g[u].size(); i++){
        int v = g[u][i];
        if(v == parent) continue;
        depth[v] = depth[u] + 1;
        nbr[root]++;
        mpp[v] = ++cnt;
        nodesAt[depth[v]].pb(mpp[v]);
        r[v] = root;
        dfs(v, u, root);
    }
}

int dfs2(int u, int parent){
    int sum = 0;
    for(int i = 0; i < g[u].size(); i++){
        int v = g[u][i];
        if(v == parent) continue;
        sum += dfs2(v, u);
    }
    return sizes[u] = sum+1;
}

int ans(int v, int p){
    int rem = p;
    int u = v;
    for(int k = 19; k >= 0; k--){
        if(rem >= (1<<k)){
            u = dp[u][k];
            rem -= (1<<k);
        }
    }

    if(u == -1)
        return 0;
    //printf("size  at depth %d : %d\n", depth[v], nodesAt[depth[v]].size());
   /* printf("depth[v] : %d,  %d %d %d : \n", depth[v], u, mpp[u], sizes[u]);
    for(int i = 0; i < nodesAt[depth[v]].size(); i++){
        printf("%d ", nodesAt[depth[v]][i]);
    }
    printf("\n");*/
   // printf("%d %d\n", r[v], nbr[r[v]]);
    int ans =  upper_bound(all(nodesAt[depth[v]]), mpp[u] + sizes[u]) - lower_bound(all(nodesAt[depth[v]]), mpp[u]) - 1;
    if(ans <= 0)
        return 0;
    return ans;

}

int main(){
	std::ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("in.txt", "rt", stdin);
	//	freopen("out.out", "wt", stdout);
#endif

    scanf("%d", &n);
    for(int i = 1; i <= n; i++){
        scanf("%d", &x);
        if(x == 0){
            roots.push_back(i);
            continue;
        }
        g[i].pb(x);
        g[x].pb(i);
    }

    memset(dp, -1, sizeof dp);

    for(int i = 0; i < roots.size(); i++){
        mpp[i] = ++cnt;
        dfs(roots[i], -1, roots[i]);
        dfs2(roots[i], -1);
    }

    for(int k = 1; k <= 19; k++){
        for(int u = 1; u <= n; u++){
            if(dp[u][k-1] == -1) continue;
            dp[u][k] = dp[dp[u][k-1]][k-1];
        }
    }
    //printf("%d\n", sizes[6], sizes[7]);
    scanf("%d", &m);
    while(m--){
        scanf("%d%d", &v, &p);
        printf("%d ",ans(v, p));
    }

	return 0;
}


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

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REV(i,b,a) for(int i=(a);i>=(b);i--)
#define mp make_pair
#define pb push_back
#define SZ(v) (int)v.size()
#define sz(v) (int)v.size()
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define mem(s,v) memset(s,v,sizeof(s))
#define ppc(x) __builtin_popcount((x))
#define iter(it,s) for(__typeof(s.begin())it = s.begin();it!=s.end();it++)
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;

const int OO = (int) 2e9;
const double eps = 1e-9;

// 1 based
int daysInMonths[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int di[] = { -1, 0, 1, 0 };
int dj[] = { 0, 1, 0, -1 };
#define Nd 0
#define Ed 1
#define Sd 2
#define Wd 3

int x, n, m, p, v, dp[100002][20], depth[100002];
vector<int> g[100002];
vector<int> nodesAt[100002];
vector<int> roots;
int nbr[100002];
int mpp[100002], r[1000002];
int cnt;

void dfs(int u, int parent, int root){
    dp[u][0] = parent;
    for(int i = 0; i < g[u].size(); i++){
        int v = g[u][i];
        if(v == parent) continue;
        depth[v] = depth[u] + 1;
        nbr[root]++;
        mpp[v] = ++cnt;
        nodesAt[depth[v]].pb(mpp[v]);
        r[v] = root;
        dfs(v, u, root);
    }
}

int ans(int v, int p){
    int rem = p;
    int u = v;
    for(int k = 19; k >= 0; k--){
        if(rem >= (1<<k)){
            u = dp[u][k];
            rem -= (1<<k);
        }
    }

    if(u == -1)
        return 0;
    //printf("size  at depth %d : %d\n", depth[v], nodesAt[depth[v]].size());
    /*for(int i = 0; i < nodesAt[depth[v]].size(); i++){
        printf("%d ", nodesAt[depth[v]][i]);
    }
    printf("\n");*/
   // printf("%d %d\n", r[v], nbr[r[v]]);
    int ans = upper_bound(all(nodesAt[depth[v]]), r[v]+nbr[r[v]]) - lower_bound(all(nodesAt[depth[v]]), r[v]) - 1;
    return ans;

}

int main(){
	std::ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("in.txt", "rt", stdin);
	//	freopen("out.out", "wt", stdout);
#endif

    scanf("%d", &n);
    for(int i = 1; i <= n; i++){
        scanf("%d", &x);
        if(x == 0){
            roots.push_back(i);
            continue;
        }
        //g[i].pb(x);
        g[x].pb(i);
    }

    memset(dp, -1, sizeof dp);

    for(int i = 0; i < roots.size(); i++){
        cnt = roots[i];
        dfs(roots[i], -1, roots[i]);
    }

    for(int k = 1; k <= 19; k++){
        for(int u = 1; u <= n; u++){
            if(dp[u][k-1] == -1) continue;
            dp[u][k] = dp[dp[u][k-1]][k-1];
        }
    }

    scanf("%d", &m);
    while(m--){
        scanf("%d%d", &v, &p);
        printf("%d ",ans(v, p));
    }

	return 0;
}


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

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REV(i,b,a) for(int i=(a);i>=(b);i--)
#define pb push_back
#define SZ(v) (int)v.size()
#define sz(v) (int)v.size()
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define mem(s,v) memset(s,v,sizeof(s))
#define ppc(x) __builtin_popcount((x))
#define iter(it,s) for(__typeof(s.begin())it = s.begin();it!=s.end();it++)
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;

const int OO = (int) 2e9;
const double eps = 1e-9;

// 1 based
int daysInMonths[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int di[] = { -1, 0, 1, 0 };
int dj[] = { 0, 1, 0, -1 };
#define Nd 0
#define Ed 1
#define Sd 2
#define Wd 3

int n, x, y, m, sum, cnt, dp[100002][17], depth[100002], sz[100002], mp[100002];
vector<int> adj[100002], nodesAt[100002], root;

int dfs(int u, int p){
    dp[u][0] = p;
    for(int i = 0; i < adj[u].size(); i++){
        int v = adj[u][i];
        if(v == p) continue;
        mp[v]= ++cnt;
        depth[v] = depth[u] + 1;
        nodesAt[depth[v]].pb(mp[v]);
        sum += dfs(v, u);
    }
    return sz[u] = sum + 1;
}

int getAns(int u, int p){

    int u1 = u;

    for(int k = 17; k >=0; k--){
        if(depth[u] - (1<<k) > depth[u] - p)
            u = dp[u][k];
    }
    u = dp[u][0];
    int ans = lower_bound(all(nodesAt[depth[u1]]), mp[u]+sz[u]) - lower_bound(all(nodesAt[depth[u1]]), mp[u]) - 1;
    if(ans < 0)
        ans = 0;
    return ans;
}

int main(){
	std::ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("in.txt", "rt", stdin);
	//	freopen("out.out", "wt", stdout);
#endif

    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> x;
        if(x == 0){
            root.pb(i);
            continue;
        }
        adj[x].pb(i);
        adj[i].pb(x);
    }

    memset(dp, -1, sizeof dp);

    for(int i = 0; i < root.size(); i++){
        mp[root[i]] = ++cnt;
        dfs(root[i], -1);
    }

    for(int k = 1; k <= 17; k++){
        for(int u = 1; u <= n; u++){
            if(dp[u][k-1] == -1) continue;
            dp[u][k] = dp[dp[u][k-1]][k-1];
        }
    }

    cin >> m;
    while(m--){
        cin >> x >> y;
        cout << getAns(x, y) << " ";
    }

	return 0;
}


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

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REV(i,b,a) for(int i=(a);i>=(b);i--)
#define pb push_back
#define SZ(v) (int)v.size()
#define sz(v) (int)v.size()
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define mem(s,v) memset(s,v,sizeof(s))
#define ppc(x) __builtin_popcount((x))
#define iter(it,s) for(__typeof(s.begin())it = s.begin();it!=s.end();it++)
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;

const int OO = (int) 2e9;
const double eps = 1e-9;

// 1 based
int daysInMonths[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int di[] = { -1, 0, 1, 0 };
int dj[] = { 0, 1, 0, -1 };
#define Nd 0
#define Ed 1
#define Sd 2
#define Wd 3

int n, x, y, m, cnt, dp[100002][20], depth[100002], sz[100002], mp[100002];
vector<int> adj[100002], nodesAt[100002], root;

int dfs(int u, int p){
    dp[u][0] = p;
    int sum = 0;
    for(int i = 0; i < adj[u].size(); i++){
        int v = adj[u][i];
        if(v == p) continue;
        mp[v]= ++cnt;
        depth[v] = depth[u] + 1;
        nodesAt[depth[v]].pb(mp[v]);
        sum += dfs(v, u);
    }
    return sz[u] = sum + 1;
}

int getAns(int u, int p){
    int u1 = u;
    int rem = p;
    for(int k = 19; k >=0; k--){
        if(rem >= (1<<k)){
            if(u == -1) continue;
            u = dp[u][k];
            rem -= (1<<k);
        }
    }
    if(u == -1)
        return 0;
    int ans = upper_bound(all(nodesAt[depth[u1]]), mp[u]+sz[u]-1) - lower_bound(all(nodesAt[depth[u1]]), mp[u]) - 1;
    if(ans < 0)
        return 0;
    return ans;
}

int main(){
	std::ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("in.txt", "rt", stdin);
	//	freopen("out.out", "wt", stdout);
#endif

    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> x;
        if(x == 0){
            root.pb(i);
            continue;
        }
        adj[x].pb(i);
        adj[i].pb(x);
    }

    memset(dp, -1, sizeof dp);

    for(int i = 0; i < root.size(); i++){
        mp[root[i]] = ++cnt;
        dfs(root[i], -1);
    }

    for(int k = 1; k <= 19; k++){
        for(int u = 1; u <= n; u++){
            if(dp[u][k-1] == -1) continue;
            dp[u][k] = dp[dp[u][k-1]][k-1];
        }
    }

    cin >> m;
    while(m--){
        cin >> x >> y;
        cout << getAns(x, y) << " ";
    }

	return 0;
}


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

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REV(i,b,a) for(int i=(a);i>=(b);i--)
#define mp make_pair
#define pb push_back
#define SZ(v) (int)v.size()
#define sz(v) (int)v.size()
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define mem(s,v) memset(s,v,sizeof(s))
#define ppc(x) __builtin_popcount((x))
#define iter(it,s) for(__typeof(s.begin())it = s.begin();it!=s.end();it++)
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;

const int OO = (int) 2e9;
const double eps = 1e-9;

// 1 based
int daysInMonths[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int di[] = { -1, 0, 1, 0 };
int dj[] = { 0, 1, 0, -1 };
#define Nd 0
#define Ed 1
#define Sd 2
#define Wd 3

int x, n, m, p, v, dp[100002][20], depth[100002];
vector<int> g[100002];
vector<int> nodesAt[100002];
vector<int> roots;
int mpp[100002];
int sizes[100002];
int cnt, sum;

void dfs(int u, int parent){
    dp[u][0] = parent;
    for(int i = 0; i < g[u].size(); i++){
        int v = g[u][i];
        if(v == parent) continue;
        depth[v] = depth[u] + 1;
        mpp[v] = ++cnt;
        nodesAt[depth[v]].pb(mpp[v]);
        dfs(v, u);
    }
}

int dfs2(int u, int parent){
    int sum = 0;
    for(int i = 0; i < g[u].size(); i++){
        int v = g[u][i];
        if(v == parent) continue;
        sum += dfs2(v, u);
    }
    return sizes[u] = sum+1;
}

int ans(int v, int p){
    int rem = p;
    int u = v;
    for(int k = 19; k >= 0; k--){
        if(rem >= (1<<k)){
            u = dp[u][k];
            rem -= (1<<k);
        }
    }

    if(u == -1)
        return 0;
    //printf("size  at depth %d : %d\n", depth[v], nodesAt[depth[v]].size());
   /* printf("depth[v] : %d,  %d %d %d : \n", depth[v], u, mpp[u], sizes[u]);
    for(int i = 0; i < nodesAt[depth[v]].size(); i++){
        printf("%d ", nodesAt[depth[v]][i]);
    }
    printf("\n");*/
   // printf("%d %d\n", r[v], nbr[r[v]]);
   //printf("|%d %d %d|\n", u, mpp[u], sizes[u]);
    //sort(all(nodesAt[depth[v]]));
    int ans =  upper_bound(all(nodesAt[depth[v]]), mpp[u] + sizes[u]-1) - lower_bound(all(nodesAt[depth[v]]), mpp[u]) - 1;
    if(ans <= 0)
        return 0;
    return ans;
}

int main(){
	std::ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("in.txt", "rt", stdin);
	//	freopen("out.out", "wt", stdout);
#endif

    scanf("%d", &n);
    for(int i = 1; i <= n; i++){
        scanf("%d", &x);
        if(x == 0){
            roots.push_back(i);
            continue;
        }
        g[i].pb(x);
        g[x].pb(i);
    }

    memset(dp, -1, sizeof dp);

    for(int i = 0; i < roots.size(); i++){
        mpp[roots[i]] = ++cnt;
        dfs(roots[i], -1);
        dfs2(roots[i], -1);
    }

    for(int k = 1; k <= 19; k++){
        for(int u = 1; u <= n; u++){
            if(dp[u][k-1] == -1) continue;
            dp[u][k] = dp[dp[u][k-1]][k-1];
        }
    }
    for(int i = 1; i <= n; i++)
        printf("u:%d, mpp[u]:%d\n", i, mpp[i]);
    //printf("%d\n", sizes[6], sizes[7]);
    scanf("%d", &m);
    while(m--){
        scanf("%d%d", &v, &p);
        printf("%d ",ans(v, p));
    }

	return 0;
}


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

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REV(i,b,a) for(int i=(a);i>=(b);i--)
#define mp make_pair
#define pb push_back
#define SZ(v) (int)v.size()
#define sz(v) (int)v.size()
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define mem(s,v) memset(s,v,sizeof(s))
#define ppc(x) __builtin_popcount((x))
#define iter(it,s) for(__typeof(s.begin())it = s.begin();it!=s.end();it++)
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;

const int OO = (int) 2e9;
const double eps = 1e-9;

// 1 based
int daysInMonths[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int di[] = { -1, 0, 1, 0 };
int dj[] = { 0, 1, 0, -1 };
#define Nd 0
#define Ed 1
#define Sd 2
#define Wd 3

int x, n, m, p, v, dp[100002][20], depth[100002];
vector<int> g[100002];
vector<int> nodesAt[100002];
vector<int> roots;
int nbr[100002];
int mpp[100002], r[100002];
int treeBegin[100002], treeEnd[100002], sizes[100002];;
int cnt, sum;

void dfs(int u, int parent, int root){
    dp[u][0] = parent;
    for(int i = 0; i < g[u].size(); i++){
        int v = g[u][i];
        if(v == parent) continue;
        depth[v] = depth[u] + 1;
        nbr[root]++;
        mpp[v] = ++cnt;
        nodesAt[depth[v]].pb(mpp[v]);
        r[v] = root;
        dfs(v, u, root);
    }
}

int dfs2(int u, int parent){
    int sum = 0;
    for(int i = 0; i < g[u].size(); i++){
        int v = g[u][i];
        if(v == parent) continue;
        sum += dfs2(v, u);
    }
    return sizes[u] = sum+1;
}

int ans(int v, int p){
    int rem = p;
    int u = v;
    for(int k = 19; k >= 0; k--){
        if(rem >= (1<<k)){
            u = dp[u][k];
            rem -= (1<<k);
        }
    }

    if(u == -1)
        return 0;
    //printf("size  at depth %d : %d\n", depth[v], nodesAt[depth[v]].size());
   /* printf("%d  %d %d : \n", treeBegin[r[v]], nbr[r[v]], mpp[v]);
    for(int i = 0; i < nodesAt[depth[v]].size(); i++){
        printf("%d ", nodesAt[depth[v]][i]);
    }
    printf("\n");*/
   // printf("%d %d\n", r[v], nbr[r[v]]);
    int ans =  lower_bound(all(nodesAt[depth[v]]), mpp[u] + sizes[u]) - lower_bound(all(nodesAt[depth[v]]), mpp[u]) - 1;
    if(ans <= 0)
        return 0;
    return ans;

}

int main(){
	std::ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("in.txt", "rt", stdin);
	//	freopen("out.out", "wt", stdout);
#endif

    scanf("%d", &n);
    for(int i = 1; i <= n; i++){
        scanf("%d", &x);
        if(x == 0){
            roots.push_back(i);
            continue;
        }
        g[i].pb(x);
        g[x].pb(i);
    }

    memset(dp, -1, sizeof dp);

    for(int i = 0; i < roots.size(); i++){
        treeBegin[roots[i]] = cnt;
        dfs(roots[i], -1, roots[i]);
        dfs2(roots[i], -1);
        treeEnd[roots[i]] = cnt++;
    }

    for(int k = 1; k <= 19; k++){
        for(int u = 1; u <= n; u++){
            if(dp[u][k-1] == -1) continue;
            dp[u][k] = dp[dp[u][k-1]][k-1];
        }
    }
    //printf("%d\n", sizes[6], sizes[7]);
    scanf("%d", &m);
    while(m--){
        scanf("%d%d", &v, &p);
        printf("%d ",ans(v, p));
    }

	return 0;
}


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

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REV(i,b,a) for(int i=(a);i>=(b);i--)
#define mp make_pair
#define pb push_back
#define SZ(v) (int)v.size()
#define sz(v) (int)v.size()
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define mem(s,v) memset(s,v,sizeof(s))
#define ppc(x) __builtin_popcount((x))
#define iter(it,s) for(__typeof(s.begin())it = s.begin();it!=s.end();it++)
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;

const int OO = (int) 2e9;
const double eps = 1e-9;

// 1 based
int daysInMonths[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int di[] = { -1, 0, 1, 0 };
int dj[] = { 0, 1, 0, -1 };
#define Nd 0
#define Ed 1
#define Sd 2
#define Wd 3

int x, n, m, p, v, dp[100002][20], depth[100002];
vector<int> g[100002];
vector<int> nodesAt[100002];
vector<int> roots;
int nbr[100002];
int mpp[100002], r[100002];
int treeBegin[100002], treeEnd[100002];
int cnt;

void dfs(int u, int parent, int root){
    dp[u][0] = parent;
    for(int i = 0; i < g[u].size(); i++){
        int v = g[u][i];
        if(v == parent) continue;
        depth[v] = depth[u] + 1;
        nbr[root]++;
        mpp[v] = ++cnt;
        nodesAt[depth[v]].pb(mpp[v]);
        r[v] = root;
        dfs(v, u, root);
    }
}

int ans(int v, int p){
    int rem = p;
    int u = v;
    for(int k = 19; k >= 0; k--){
        if(rem >= (1<<k)){
            u = dp[u][k];
            rem -= (1<<k);
        }
    }

    if(u == -1)
        return 0;
    //printf("size  at depth %d : %d\n", depth[v], nodesAt[depth[v]].size());
    /*for(int i = 0; i < nodesAt[depth[v]].size(); i++){
        printf("%d ", nodesAt[depth[v]][i]);
    }
    printf("\n");*/
   // printf("%d %d\n", r[v], nbr[r[v]]);
    int ans = upper_bound(all(nodesAt[depth[v]]), treeBegin[r[v]]+nbr[r[v]]) - lower_bound(all(nodesAt[depth[v]]), treeBegin[r[v]]) - 1;
    return ans;

}

int main(){
	std::ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("in.txt", "rt", stdin);
	//	freopen("out.out", "wt", stdout);
#endif

    scanf("%d", &n);
    for(int i = 1; i <= n; i++){
        scanf("%d", &x);
        if(x == 0){
            roots.push_back(i);
            continue;
        }
        g[i].pb(x);
        g[x].pb(i);
    }

    memset(dp, -1, sizeof dp);

    for(int i = 0; i < roots.size(); i++){
        treeBegin[roots[i]] = cnt;
        dfs(roots[i], -1, roots[i]);
        treeEnd[roots[i]] = cnt++;
    }

    for(int k = 1; k <= 19; k++){
        for(int u = 1; u <= n; u++){
            if(dp[u][k-1] == -1) continue;
            dp[u][k] = dp[dp[u][k-1]][k-1];
        }
    }

    scanf("%d", &m);
    while(m--){
        scanf("%d%d", &v, &p);
        printf("%d ",ans(v, p));
    }

	return 0;
}


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

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REV(i,b,a) for(int i=(a);i>=(b);i--)
#define pb push_back
#define SZ(v) (int)v.size()
#define sz(v) (int)v.size()
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define mem(s,v) memset(s,v,sizeof(s))
#define ppc(x) __builtin_popcount((x))
#define iter(it,s) for(__typeof(s.begin())it = s.begin();it!=s.end();it++)
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;

const int OO = (int) 2e9;
const double eps = 1e-9;

// 1 based
int daysInMonths[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int di[] = { -1, 0, 1, 0 };
int dj[] = { 0, 1, 0, -1 };
#define Nd 0
#define Ed 1
#define Sd 2
#define Wd 3

int n, x, y, m, cnt, dp[100002][20], depth[100002], sz[100002], mp[100002];
vector<int> adj[100002], nodesAt[100002], root;

int dfs(int u, int p){
    dp[u][0] = p;
    int sum = 0;
    for(int i = 0; i < adj[u].size(); i++){
        int v = adj[u][i];
        if(v == p) continue;
        mp[v]= ++cnt;
        depth[v] = depth[u] + 1;
        nodesAt[depth[v]].pb(mp[v]);
        sum += dfs(v, u);
    }
    return sz[u] = sum + 1;
}

int getAns(int u, int p){

    int u1 = u;
    int rem = p;
    for(int k = 19; k >=0; k--){
        if(rem >= (1<<k)){
            if(u1 == -1) continue;
            u = dp[u][k];
            rem -= (1<<k);
        }
    }
    if(u1 == -1)
        return 0;
    int ans = upper_bound(all(nodesAt[depth[u1]]), mp[u]+sz[u]-1) - lower_bound(all(nodesAt[depth[u1]]), mp[u]) - 1;
    if(ans < 0)
        ans = 0;
    return ans;
}

int main(){
	std::ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("in.txt", "rt", stdin);
	//	freopen("out.out", "wt", stdout);
#endif

    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> x;
        if(x == 0){
            root.pb(i);
            continue;
        }
        adj[x].pb(i);
        adj[i].pb(x);
    }

    memset(dp, -1, sizeof dp);

    for(int i = 0; i < root.size(); i++){
        mp[root[i]] = ++cnt;
        dfs(root[i], -1);
    }

    for(int k = 1; k <= 19; k++){
        for(int u = 1; u <= n; u++){
            if(dp[u][k-1] == -1) continue;
            dp[u][k] = dp[dp[u][k-1]][k-1];
        }
    }

    cin >> m;
    while(m--){
        cin >> x >> y;
        cout << getAns(x, y) << " ";
    }

	return 0;
}


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

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REV(i,b,a) for(int i=(a);i>=(b);i--)
#define mp make_pair
#define pb push_back
#define SZ(v) (int)v.size()
#define sz(v) (int)v.size()
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define mem(s,v) memset(s,v,sizeof(s))
#define ppc(x) __builtin_popcount((x))
#define iter(it,s) for(__typeof(s.begin())it = s.begin();it!=s.end();it++)
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;

const int OO = (int) 2e9;
const double eps = 1e-9;

// 1 based
int daysInMonths[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int di[] = { -1, 0, 1, 0 };
int dj[] = { 0, 1, 0, -1 };
#define Nd 0
#define Ed 1
#define Sd 2
#define Wd 3

int x, n, m, p, v, dp[100002][20], depth[100002];
vector<int> g[100002];
vector<int> nodesAt[100002];
vector<int> roots;
int nbr[100002];
int mpp[100002], r[100002];
int treeBegin[100002], treeEnd[100002], sizes[100002];;
int cnt, sum;

void dfs(int u, int parent, int root){
    dp[u][0] = parent;
    for(int i = 0; i < g[u].size(); i++){
        int v = g[u][i];
        if(v == parent) continue;
        depth[v] = depth[u] + 1;
        nbr[root]++;
        mpp[v] = ++cnt;
        nodesAt[depth[v]].pb(mpp[v]);
        r[v] = root;
        dfs(v, u, root);
    }
}

int dfs2(int u, int parent){
    int sum = 0;
    for(int i = 0; i < g[u].size(); i++){
        int v = g[u][i];
        if(v == parent) continue;
        sum += dfs2(v, u);
    }
    return sizes[u] = sum+1;
}

int ans(int v, int p){
    int rem = p;
    int u = v;
    for(int k = 19; k >= 0; k--){
        if(rem >= (1<<k)){
            u = dp[u][k];
            rem -= (1<<k);
        }
    }

    if(u == -1)
        return 0;
    //printf("size  at depth %d : %d\n", depth[v], nodesAt[depth[v]].size());
   /* printf("%d  %d %d : \n", treeBegin[r[v]], nbr[r[v]], mpp[v]);
    for(int i = 0; i < nodesAt[depth[v]].size(); i++){
        printf("%d ", nodesAt[depth[v]][i]);
    }
    printf("\n");*/
   // printf("%d %d\n", r[v], nbr[r[v]]);
    return lower_bound(all(nodesAt[depth[v]]), mpp[u] + sizes[u]) - lower_bound(all(nodesAt[depth[v]]), mpp[u]) - 1;

}

int main(){
	std::ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("in.txt", "rt", stdin);
	//	freopen("out.out", "wt", stdout);
#endif

    scanf("%d", &n);
    for(int i = 1; i <= n; i++){
        scanf("%d", &x);
        if(x == 0){
            roots.push_back(i);
            continue;
        }
        g[i].pb(x);
        g[x].pb(i);
    }

    memset(dp, -1, sizeof dp);

    for(int i = 0; i < roots.size(); i++){
        treeBegin[roots[i]] = cnt;
        dfs(roots[i], -1, roots[i]);
        dfs2(roots[i], -1);
        treeEnd[roots[i]] = cnt++;
    }

    for(int k = 1; k <= 19; k++){
        for(int u = 1; u <= n; u++){
            if(dp[u][k-1] == -1) continue;
            dp[u][k] = dp[dp[u][k-1]][k-1];
        }
    }
    //printf("%d\n", sizes[6], sizes[7]);
    scanf("%d", &m);
    while(m--){
        scanf("%d%d", &v, &p);
        printf("%d ",ans(v, p));
    }

	return 0;
}


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

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REV(i,b,a) for(int i=(a);i>=(b);i--)
#define mp make_pair
#define pb push_back
#define SZ(v) (int)v.size()
#define sz(v) (int)v.size()
#define all(c) (c).begin(),(c).end()
#define rall(c) (c).rbegin(),(c).rend()
#define mem(s,v) memset(s,v,sizeof(s))
#define ppc(x) __builtin_popcount((x))
#define iter(it,s) for(__typeof(s.begin())it = s.begin();it!=s.end();it++)
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef vector<pi> vpi;

const int OO = (int) 2e9;
const double eps = 1e-9;

// 1 based
int daysInMonths[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int di[] = { -1, 0, 1, 0 };
int dj[] = { 0, 1, 0, -1 };
#define Nd 0
#define Ed 1
#define Sd 2
#define Wd 3

int x, n, m, p, v, dp[100002][20], depth[100002];
vector<int> g[100002];
vector<int> nodesAt[100002];
vector<int> roots;
int nbr[100002];
int mpp[100002], r[100002];
int treeBegin[100002], treeEnd[100002], sizes[100002];;
int cnt, sum;

void dfs(int u, int parent, int root){
    dp[u][0] = parent;
    for(int i = 0; i < g[u].size(); i++){
        int v = g[u][i];
        if(v == parent) continue;
        depth[v] = depth[u] + 1;
        nbr[root]++;
        mpp[v] = ++cnt;
        nodesAt[depth[v]].pb(mpp[v]);
        r[v] = root;
        dfs(v, u, root);
    }
}

int dfs2(int u, int parent){
    int sum = 0;
    for(int i = 0; i < g[u].size(); i++){
        int v = g[u][i];
        if(v == parent) continue;
        sum += dfs2(v, u);
    }
    return sizes[u] = sum+1;
}

int ans(int v, int p){
    int rem = p;
    int u = v;
    for(int k = 19; k >= 0; k--){
        if(rem >= (1<<k)){
            u = dp[u][k];
            rem -= (1<<k);
        }
    }

    if(u == -1)
        return 0;
    //printf("size  at depth %d : %d\n", depth[v], nodesAt[depth[v]].size());
   /* printf("depth[v] : %d,  %d %d %d : \n", depth[v], u, mpp[u], sizes[u]);
    for(int i = 0; i < nodesAt[depth[v]].size(); i++){
        printf("%d ", nodesAt[depth[v]][i]);
    }
    printf("\n");*/
   // printf("%d %d\n", r[v], nbr[r[v]]);
    return upper_bound(all(nodesAt[depth[v]]), mpp[u] + sizes[u]) - lower_bound(all(nodesAt[depth[v]]), mpp[u]) - 1;

}

int main(){
	std::ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("in.txt", "rt", stdin);
	//	freopen("out.out", "wt", stdout);
#endif

    scanf("%d", &n);
    for(int i = 1; i <= n; i++){
        scanf("%d", &x);
        if(x == 0){
            roots.push_back(i);
            continue;
        }
        g[i].pb(x);
        g[x].pb(i);
    }

    memset(dp, -1, sizeof dp);

    for(int i = 0; i < roots.size(); i++){
        mpp[i] = ++cnt;
        dfs(roots[i], -1, roots[i]);
        dfs2(roots[i], -1);
    }

    for(int k = 1; k <= 19; k++){
        for(int u = 1; u <= n; u++){
            if(dp[u][k-1] == -1) continue;
            dp[u][k] = dp[dp[u][k-1]][k-1];
        }
    }
    //printf("%d\n", sizes[6], sizes[7]);
    scanf("%d", &m);
    while(m--){
        scanf("%d%d", &v, &p);
        printf("%d ",ans(v, p));
    }

	return 0;
}