#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <array>
#include <stack>
#include <queue>
#include <random>
#include <numeric>
#include <functional>
#include <chrono>
#include <utility>
#include <iomanip>
#include <assert.h>

using namespace std;

void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)

#define rng_init mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define rng_seed(x) mt19937 rng(x)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()
// #define int long long

const int MXN = 2e5 + 5, INF = 1e9 + 5;
vector<int> g[MXN], level[MXN];
int timer;
int tin[MXN], tout[MXN];

void dfs(int u, int p, int depth) {
    tin[u] = timer++;
    level[depth].push_back(tin[u]);

    for (const auto &v : g[u]) {
        if (v == p) continue;
        dfs(v, u, depth + 1);
    }

    tout[u] = timer - 1;
}

void solve() {
    int N;
    cin >> N;

    for (int i = 1; i < N; i++) {
        int p;
        cin >> p;
        p--;

        g[i].push_back(p);
        g[p].push_back(i);
    }

    dfs(0, -1, 0);

    int Q;
    cin >> Q;

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

        auto l = lower_bound(all(level[d]), tin[u]);
        auto r = upper_bound(all(level[d]), tout[u]);

        cout << r - l << "\n";
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int TC = 1;
    // cin >> TC;
    while (TC--) solve();
}
#include<bits/stdc++.h>
using namespace std;
using ll = long long int;
using ld = long double;
#define pow(n,m) powl(n,m)
#define sqrt(n) sqrtl(n)
const ll MAX = 5000000000000000000;
const ll MOD = 0;
void randinit(){srand((unsigned)time(NULL));}
template <typename T>
struct BIT {
    int n;          // 配列の要素数(数列の要素数+1)
    vector<T> bit;  // データの格納先
    BIT(int n_) : n(n_ + 1), bit(n, 0) {}
    void add(int i, T x) {
        for (int idx = i; idx < n; idx += (idx & -idx)) {
            bit[idx] += x;
        }
    }
    T sum(int i) {
        T s(0);
        for (int idx = i; idx > 0; idx -= (idx & -idx)) {
            s += bit[idx];
        }
        return s;
    }
    T query(int l, int r) { return sum(r - 1) - sum(l - 1); }
};
int main(){
    ll N;
    cin >> N;
    vector<vector<ll>> G(N);
    for(ll i = 1;i < N;i++){
        ll a;
        cin >> a;
        G[a - 1].emplace_back(i);
    }
    stack<tuple<ll,ll>> que;
    que.push(make_tuple(0,0));
    ll c = 3;
    vector<ll> Dis(N,-1),S(N),T(N);
    while(!que.empty()){
        ll a = get<0>(que.top());
        ll b = get<1>(que.top());
        que.pop();
        if(Dis[a] != -1) T[a] = c;
        else{
            que.push(make_tuple(a,-1));
            Dis[a] = b;
            S[a] = c;
            for(ll i = 0;i < G[a].size();i++) que.push(make_tuple(G[a][i],b + 1));
        }
        c++;
    }
    ll q;
    cin >> q;
    vector<tuple<ll,ll,ll>> Q(q);
    for(ll i = 0;i < q;i++){
        ll u,d;
        cin >> u >> d;
        Q[i] = make_tuple(d,u - 1,i);
    }
    BIT<ll> tree(N * 2 + 10);
    sort(Q.begin(),Q.end());
    vector<vector<ll>> AAA(N);
    vector<ll> ans(q,0);
    for(ll i = 0;i < N;i++) AAA[Dis[i]].emplace_back(i);
    ll kari = 0;
    for(ll i = 0;i < N;i++){
        for(ll j = 0;j < AAA[i].size();j++){
            tree.add(S[AAA[i][j]],1);
        }
        while(kari < q && get<0>(Q[kari]) == i){
            ans[get<2>(Q[kari])] = tree.query(S[get<1>(Q[kari])],T[get<1>(Q[kari])]);
            //cout<<get<2>(Q[kari])+1<<endl;
            kari++;
        }
        for(ll j = 0;j < AAA[i].size();j++){
            tree.add(S[AAA[i][j]],-1);
        }
    }
    for(ll i = 0;i < q;i++) cout << ans[i] << endl;
}
