/*
    // editorial used
    author : aryan57
    created : 22-May-2021 21:32:36 IST
*/
#include <bits/stdc++.h>
using namespace std;

template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#ifndef ONLINE_JUDGE
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

#define int long long
#define X first
#define Y second
#define pb push_back
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define F(i, a, b) for (int i = a; i <= b; i++)
#define RF(i, a, b) for (int i = a; i >= b; i--)

const int mxn = 2e5;
const long long INF = 2e18;
const int32_t M = 1000000007;
// const int32_t M = 998244353;
const long double pie = acos(-1);

vector <int> adj[mxn+1];
vector <int> depth[2][mxn+1];
vector <int> tin(mxn+1);
vector <int> tout(mxn+1);
int T=0;


bool comp_tin(int u,int v)
{
    return tin[u]<tin[v];
}
bool comp_tout(int u,int v)
{
    return tout[u]<tout[v];
}

void dfs(int u,int par=-1,int dep=0)
{
    tin[u]=++T;
    depth[0][dep].push_back(u);
    depth[1][dep].push_back(u);
    for(int to : adj[u]){
        if(to==par)continue;
        dfs(to,u,dep+1);
    }

    tout[u]=++T;
}

void solve_LOG()
{
    int n;
    cin>>n;

    F(i,2,n)
    {
        int p;
        cin>>p;
        adj[p].push_back(i);
        adj[i].push_back(p);
    }

    dfs(1);

    F(i,0,n)
    {
        sort(all(depth[0][i]),comp_tin);
        sort(all(depth[1][i]),comp_tout);
    }

    int q;
    cin>>q;
    while (q--)
    {
        int u,d;
        cin>>u>>d;

        vector <int> &vec1=depth[0][d];
        vector <int> &vec2=depth[1][d];
        // dbg(vec1,vec2);

        int pos1=lower_bound(all(vec1),u,comp_tin)-vec1.begin();
        int pos2=upper_bound(all(vec2),u,comp_tout)-vec2.begin();
        // dbg(pos1,pos2);

        int ans=pos2-pos1;
        if(ans<0)ans=0;

        cout<<ans;
        cout<<"\n";

    }
    
}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
#endif
#ifdef ARYAN_SIEVE
    sieve();
#endif
#ifdef ARYAN_SEG_SIEVE
    segmented_sieve();
#endif
#ifdef ARYAN_FACT
    fact_init();
#endif
    // cout<<fixed<<setprecision(10);
    int _t=1;
    // cin>>_t;
    for (int i=1;i<=_t;i++)
    {
        // cout<<"Case #"<<i<<": ";
        solve_LOG();
    }
    return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <map>

#define amax(a, b) a = std::max(a, b)
#define amin(a, b) a = std::min(a, b)

using ll = long long;

int dfs(int i, std::vector<int> &depth, std::vector<std::vector<int>> &tree, std::vector<int> &in, std::vector<int> &out, int cnt=0, int d=0) {
    in[i] = cnt;
    depth[i] = d;
    cnt++; d++;
    for (auto &t : tree[i]) {
        cnt = dfs(t, depth, tree, in, out, cnt, d);
    }
    out[i] = cnt;
    return cnt;
}

int main() {
    int n;
    std::cin >> n;
    std::vector<std::vector<int>> tree(n);
    for (int i = 1, pi; i < n; i++) {
        std::cin >> pi;
        pi--;
        tree[pi].push_back(i);
    }

    std::vector<int> depth(n), in(n), out(n);

    dfs(0, depth, tree, in, out);
    
    std::vector<std::vector<int>> depth2in(n, std::vector<int>());

    for (int i = 0; i < n; i++) {
        depth2in[depth[i]].push_back(in[i]);
    }

    for (auto &di : depth2in) {
        std::sort(di.begin(), di.end());
    }

    int q;
    std::cin >> q;
    for (int i = 0, ui, di; i < q; i++) {
        std::cin >> ui >> di;
        ui--;

        auto left = std::lower_bound(depth2in[di].begin(), depth2in[di].end(), in[ui]);
        auto right = std::lower_bound(depth2in[di].begin(), depth2in[di].end(), out[ui]);
        std::cout << (right - left) << '\n';
    }
    
    return 0;
}