#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);
	}
}

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
#define fi first
#define se second
#define N 214514
#define all(x) (x).begin(), (x).end()
const ll mod = 1000000007;
// const ll mod = 998244353;
const ll inf = 8000000000000000000;
void dfs(ll from, vector<vector<ll>>& v, vector<P>& pos, ll* cnt, ll hei,
         vector<vector<ll>>& num, vector<ll>& h) {
    pos[from].fi = *cnt;
    (*cnt)++;
    num[hei].push_back(pos[from].fi);
    h[from] = hei;
    for (ll x : v[from]) {
        hei++;
        dfs(x, v, pos, &(*cnt), hei, num, h);
        hei--;
    }
    pos[from].se = *cnt;
    (*cnt)++;
}
void solve() {
    ll n;
    cin >> n;
    vector<vector<ll>> v(n + 1);
    for (int i = 2; i <= n; i++) {
        ll x;
        cin >> x;
        v[x].push_back(i);
    }
    vector<vector<ll>> num(n + 1);
    ll cnt = 0, hei = 0;
    vector<P> pos(n + 1);
    vector<ll> h(n + 1);
    dfs(1, v, pos, &cnt, hei, num, h);
    ll q;
    cin >> q;
    while (q--) {
        ll u, d;
        cin >> u >> d;
        auto itl = lower_bound(all(num[d]), pos[u].fi),
             itr = lower_bound(all(num[d]), pos[u].se);
        cout << distance(num[d].begin(), itr) - distance(num[d].begin(), itl)
             << endl;
    }
}

int main() {
    solve();
    return 0;
}