#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <unordered_set>
#include <queue>
#include <deque>
#include <string>
#include <sstream>
#include <iomanip>
#include <map>
#include <unordered_map>
#include <stack>
#include <cstdio>
#include <climits>
#include <tuple>
#include <ctime>
#include <cstring>
#include <numeric>
#include <functional>
#include <chrono>
#include <cassert>
#include <bitset>
// printf("%.10f\n", ans);
using ll = long long;
using namespace std;
const ll mod = 1e9 + 7;
const int N = 1e5 + 1;
int n, timer;
vector<vector<int>> tree, h;
vector<int> tin, tout;

void dfs(int u, int p, int he)
{
	tin[u] = timer++;
	h[he].push_back(u);
	for (int& v : tree[u]) if (v != p) dfs(v, u, he + 1);
	tout[u] = timer++;
}

int bs1(int& u, vector<int>& vec)
{
	int l = 0, r = vec.size() - 1;
	int id = -1;
	while (l <= r)
	{
		int m = (l + r) >> 1;
		int v = vec[m];
		if (tin[u] <= tin[v] && tout[u] >= tout[v]) r = m - 1, id = m;
		else if (tin[u] <= tin[v]) r = m - 1;
		else l = m + 1;
	}
	return id;
}

int bs2(int& u, vector<int>& vec)
{
	int l = 0, r = vec.size() - 1;
	int id = -2;
	while (l <= r)
	{
		int m = (l + r) >> 1;
		int v = vec[m];
		if (tin[u] <= tin[v] && tout[u] >= tout[v]) l = m + 1, id = m;
		else if (tin[u] <= tin[v]) r = m - 1;
		else l = m + 1;
	}
	return id;
}

int main()
{
	scanf("%d", &n);
	tree = h = vector<vector<int>>(n);
	tin = tout = vector<int>(n);
	for (int i = 1; i < n; i++)
	{
		int p;
		scanf("%d", &p);
		tree[--p].push_back(i);
		tree[i].push_back(p);
	}
	dfs(0, -1, 0);
	int q;
	scanf("%d", &q);
	while (q--)
	{
		int u, d;
		scanf("%d%d", &u, &d);
		u--;
		int l = bs1(u, h[d]);
		int r = bs2(u, h[d]);
		printf("%d\n", r - l + 1);
	}
	return 0;
}
#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
const int mod = 1000000007;
//const int mod = 998244353;

const int MAX_V = 200005;
int k = 0;

vector<vector<int>> to(MAX_V);
vector<bool> used(MAX_V);
vector<int> dep(MAX_V);
vector<int> in(MAX_V);
vector<int> out(MAX_V);
vector<vector<int>> ls(MAX_V);


void dfs(int v, int d){
    in[v] = k++;
    dep[v] = d;
    used[v] = true;
    for(int u : to[v]){
        if(used[u]) continue;
        dfs(u, d+1);
    }
    out[v] = k;
}

int main()
{
    int n;
    cin >> n;
    rep(i, 0, n-1){
        int p;
        cin >> p;
        p--;
        to[p].push_back(i+1);
        to[i+1].push_back(p);
    }
    dfs(0, 0);
    rep(i, 0, n) ls[dep[i]].push_back(in[i]);
    rep(i, 0, n) sort(ls[i].begin(), ls[i].end());
    auto f = [&](int d, int r){
        return int(lower_bound(ls[d].begin(), ls[d].end(), r) - ls[d].begin());
    };
    int q;
    cin >> q;
    rep(i, 0, q){
        int u, d;
        cin >> u >> d;
        u--;
        printf("%d\n", f(d, out[u]) - f(d, in[u]));
    }
    return 0;
}
