#include <bits/stdc++.h>
using namespace std;
#define int long long
#define quickread                     \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);                    \
    cout.tie(NULL);
#define pb push_back
#define all(x) x.begin(), x.end()
#define hii cout << "Hii there Sparky! \n"
#define endl '\n'
#define mod 1000000007
//#define mod 998244353
#define maxn 100010 //check the limits, dummy

#define N 200001
#define inputG push_back
vector<int> G[N];
bool visited[N] = {};
int parent[N] = {};
int depth[N] = {};
int intime[N], outtime[N];
vector<int> tim;
int timer = 0;
// int weight[N] = {};
// int op[N] = {};
// int components = 0;
void inp(int a, int b) {
    a--, b--;
    G[a].inputG(b);
    G[b].inputG(a);
}
void dfs(int cur) {
    timer++;
    intime[cur] = timer;
    tim.pb(cur);
    visited[cur] = true;
    depth[cur] = depth[parent[cur]] + 1;
    for (auto u : G[cur]) {
        if (visited[u])
            continue;
        parent[u] = cur;
        dfs(u);
    }
    timer++;
    outtime[cur] = timer;
    tim.pb(cur);
}
void DFS(int n) {
    for (int i = 0; i < n; i++) {
        if (!visited[i]) {
            dfs(i);
        }
    }
}
void clearGraph(int n) {
    for (int i = 0; i < n; i++)
        G[i].clear();
    memset(visited, false, sizeof(visited));
    memset(parent, 0, sizeof(parent));
    memset(depth, 0, sizeof(depth));
    // memset(weight, 0, sizeof(weight));
    // memset(op, 0, sizeof(op));
    // components = 0;
}

unordered_map<int, vector<int>> m;

void solve() {
    int n;
    cin >> n;
    for (int i = 2; i <= n; i++) {
        int x;
        cin >> x;
        inp(i, x);
    }
    DFS(n);
    for (int i = 0; i < tim.size(); i++) {
        tim[i] = depth[tim[i]];
        m[tim[i]].pb(i);
    }
    int q;
    cin >> q;
    while (q--) {
        int u, d;
        cin >> u >> d;
        u--;
        int l = intime[u];
        int r = outtime[u];
        int val = d + 1;
        l--, r--;
        int a = lower_bound(all(m[val]), l) - m[val].begin();
        int b = upper_bound(all(m[val]), r) - m[val].begin();
        cout << (b - a) / 2 << endl;
    }
}

int32_t main() {
    quickread;
    int test = 1;
    //cin >> test;
    for (int i = 0; i < test; i++) {
        solve();
    }
    return 0;
}
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define mp make_pair
#define pb push_back
#define lp(i,s,f) for(ll i = s; i < ll(f); i++)
#define inF freopen("input.in", "r", stdin);
#define outF freopen("output.in", "w", stdout);
#define endl '\n'
#define MOD 1000000007
#define mm(arr) memset(arr, 0, sizeof(arr))
#define F first
#define S second

const int N = 2e5 + 5;
int Time = 0;
int intime[N];
int outtime[N];
int n;
vector<int> adj[N];
vector<pair<int, int> > depth[N];
int dd[N];

void dfs(int s, int d){
    dd[s] = d;
    pair<int, int> p;
    p = {Time++, 0};
    intime[s] = Time - 1;
    for(int i = 0; i < adj[s].size(); i++){
        dfs(adj[s][i], d + 1);
    }
    Time++;
    p.second = Time;
    outtime[s] = Time;
    depth[d].pb(p);
}
int main(){
    FAST
    cin >> n;
    for(int i = 1; i < n; i++){
        int p; cin >> p;
        p--;
        adj[p].pb(i);
    }
    dfs(0, 0);
    vector<int> depth1[N];
    vector<int> depth2[N];
    for(int i = 0; i < n; i++){
        sort(depth[i].begin(), depth[i].end());
        for(int j = 0; j < depth[i].size(); j++){
            depth1[i].pb(depth[i][j].first);
            depth2[i].pb(depth[i][j].second);
        }
    }
    int q; cin >> q;
    while(q--){
        int U, d;
        cin >> U >> d;
        U--;
        if(depth1[d].size() == 0 || dd[U] > d){
            cout << 0 << endl;
            continue;
        }
        if(dd[U] == d){
            cout << 1 << endl;
            continue;
        }
        //intime[U], outtime[U];
        int a = intime[U], b = outtime[U];
        int num = lower_bound(depth1[d].begin(), depth1[d].end(), a) - depth1[d].begin();
        if(num == depth1[d].size()){
            cout << 0 << endl;
            continue;
        }
        if(depth2[d][num] > b){
            cout << 0 << endl;
            continue;
        }
        int lo = num, hi = depth1[d].size() - 1;
        while(lo < hi){
            int mid = (lo + hi + 1)/2;
            if(depth2[d][mid] > b){
                hi = mid - 1;
            }
            else{
                lo = mid;
            }
        }
        cout << lo - num + 1 << endl;
    }
    return 0;
}
