#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <iostream>
#include <cassert>
#include <cmath>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <cstdlib>

using namespace std;

#define INF 1e+9
#define mp make_pair
#define pb push_back
#define fi first
#define fs first
#define se second
#define i64 long long
#define li long long
#define lint long long
#define pii pair<int, int>
#define vi vector<int>

#define forn(i, n) for (int i = 0; i < (int)n; i++)
#define fore(i, b, e) for (int i = (int)b; i <= (int)e; i++)

const int MAXN = 2e5+5;

int d[MAXN];
int t_in[MAXN];
int t_out[MAXN];
int timer = 0;
vi edges[MAXN];

void dfs(int v, int depth) {
	d[v] = depth;
	t_in[v] = timer++;
	for (int u : edges[v]) {
		dfs(u, depth + 1);
	}
	t_out[v] = timer++;
}

int main() {
	int n;
	scanf("%d", &n);
	fore(i, 2, n) {
		int x;
		scanf("%d", &x);
		edges[x].pb(i);
	}
	dfs(1, 0);
	vector<vi> lst(n + 1);
	fore(i, 1, n) {
		lst[d[i]].pb(t_in[i]);
	}
	forn(i, n) {
		sort(lst[i].begin(), lst[i].end());
	}
	int queries;
	scanf("%d", &queries);
	forn(q, queries) {
		int u, d;
		scanf("%d%d", &u, &d);
		if (lst[d].empty()) {
			printf("0\n");
			continue;
		}
		auto beg = lower_bound(lst[d].begin(), lst[d].end(), t_in[u]);
		auto end = upper_bound(lst[d].begin(), lst[d].end(), t_out[u]);
		printf("%d\n", end - beg);
	}
}


//================code===================//

//#define TLE

#ifdef TLE
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#endif

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <ctime>


#define ci(t) cin>>t
#define co(t) cout<<t
#define LL long long

#define ld double
#define fa(i,a,b) for(LL i=(a);i<(LL)(b);++i)
#define fd(i,a,b) for(LL i=(a);i>(LL)(b);--i)
#define setp tuple<LL,LL,LL>
#define setl pair<int,int>
#define micro 0.000001

using namespace std;

LL gcd(LL a, LL b) { return a % b ? gcd(b, a % b) : b; }


#ifdef OHSOLUTION
#define ce(t) cerr<<t
#define AT cerr << "\n=================ANS=================\n"
#define AE cerr << "\n=====================================\n"
#define DB(a) cerr << __LINE__ << ": " << #a << " = " << (a) << endl;
#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64
#else
#define AT
#define AE
#define ce(t)
#endif

pair <int, int> vu[9] = { {0,1},{1,0},{0,1} ,{-1,0},{1,1},{1,-1}, {-1,1} , {-1,-1},{-1,-1} }; //RDLU EWSN

template<typename T, typename U> void ckmax(T& a, U b) { a = a < b ? b : a; }
template<typename T, typename U> void ckmin(T& a, U b) { a = a > b ? b : a; }
struct gcmp { bool operator()(LL a, LL b) { return a < b; } bool operator()(setl& a, setl& b) { return a.second < b.second; } };
struct lcmp { bool operator()(LL a, LL b) { return a > b; } bool operator()(setl& a, setl& b) { return a.second > b.second; } };

const int max_v = 2e5 + 7;
const int max_k = 5e2 + 7;
const int bsz = (1ll << 10) + 7;
const int INF = 1e9 + 7;
const LL LNF = (LL)5e18 + 7ll;
LL mod = 1e9 + 7;
template<typename T, typename U> void MOD(T& a, U b) { a += b; if (a >= mod) a -= mod; }

vector<int> adj[max_v];
vector<int> edge[max_v];
int t=-1;
setl in[max_v];

void dfs(int u,int d=0)
{
	in[u].first = ++t;
	edge[d].push_back(in[u].first);
	for (auto& v : adj[u]) dfs(v,d+1);
	in[u].second = t;
}

int main()
{
#ifdef OHSOLUTION
	freopen("input.txt", "r", stdin);
#endif
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int v; ci(v);

	fa(i, 1, v)
	{
		int x; ci(x); --x;
		adj[x].push_back(i);
	}

	dfs(0);

	fa(i, 0, v) sort(edge[i].begin(), edge[i].end());

	int q; ci(q);
	while (q--)
	{
		int x, y; ci(x >> y); --x;

		int ret = upper_bound(edge[y].begin(), edge[y].end(), in[x].second) - lower_bound(edge[y].begin(), edge[y].end(), in[x].first);
		co(ret << "\n");
	}

	return 0;
}


