#include <bits/stdc++.h>
// clang-format off
using namespace std; using ll=long long; using ull=unsigned long long; using pll=pair<ll,ll>; const ll INF=4e18;
void print0(){}; template<typename H,typename... T> void print0(H h,T... t){cout<<h;print0(t...);}
void print(){print0("\n");}; template<typename H,typename... T>void print(H h,T... t){print0(h);if(sizeof...(T)>0)print0(" ");print(t...);}
void perr0(){}; template<typename H,typename... T> void perr0(H h,T... t){cerr<<fixed<<setprecision(10)<<h;perr0(t...);}
void perr(){perr0("\n");}; template<typename H,typename... T>void perr(H h,T... t){perr0(h);if(sizeof...(T)>0)perr0(" ");perr(t...);}
void ioinit() { cout << fixed << setprecision(15); ios_base::sync_with_stdio(0); cin.tie(0); }
// clang-format on

ll n;
ll msb(ll v) {
    // assert v > 0
    return 64 - __builtin_clzll(v);
}
// ダブリング: 各ノードから 2^i 回、根に向かって移動をして到達する場所 (-1は移動不可とする)
vector<ll> parent(200005);
vector<vector<ll>> depth(200005);
vector<ll> depth0(200005);
vector<ll> depthid(200005);
vector<vector<ll>> edgmap(200005);
void doubling_mv_to_root(vector<vector<ll>> &dbl) {
    ll logn = dbl.size();
    for (ll i = 0; i < logn; i++) {
        for (ll j = 0; j < n; j++) {
            if (i == 0) {
                dbl[i][j] = parent[j];
            } else if (dbl[i - 1][j] >= 0) {
                dbl[i][j] = dbl[i - 1][dbl[i - 1][j]];
            }
        }
    }
}
ll ancestor_at_depth(ll u, ll diff, vector<vector<ll>> &dbl) {
    ll logn = dbl.size();
    while (diff > 0) {
        // 2^i 回の移動をしてもdiffが0以上残る最大のi
        ll i = msb(diff) - 1;
        ll pow2i = (1ULL << i);
        diff -= pow2i;
        u = dbl[i][u];
    }
    return u;
}
void dfs(ll u, ll dep) {
    depth[dep].push_back(u);
    depthid[u] = -1 + depth[dep].size();
    depth0[u] = dep;
    for (auto v : edgmap[u]) {
        dfs(v, dep + 1);
    }
}
int main() {
    ioinit();
    cin >> n;
    for (ll i = 1; i < n; i++) {
        ll p;
        cin >> p;
        parent[i] = p - 1;
        edgmap[parent[i]].push_back(i);
    }
    ll q;
    cin >> q;
    vector<pll> queries(q);
    for (ll i = 0; i < q; i++) {
        ll u, d;
        cin >> u >> d;
        u--;
        queries[i] = {u, d};
    }

    dfs(0, 0);

    ll logn = 24;
    vector<vector<ll>> dbl(logn, vector<ll>(n, -1));
    doubling_mv_to_root(dbl);

    for (auto query : queries) {
        ll u = query.first;
        ll d = query.second;
        ll diff = d - depth0[u];
        if (diff == 0) {
            print(1);
            continue;
        }
        if (diff < 0) {
            print(0);
            continue;
        }

        ll k = depthid[u];
        ll mx = -10 * n - 10;
        {
            // diff回上に上がってdepthidがk以下になる最大
            ll right = depth[d].size() - 1;
            ll left = 0;
            while (left <= right) {
                ll mid = (left + right) / 2;
                ll did = depthid[ancestor_at_depth(depth[d][mid], diff, dbl)];
                if (did <= k) {
                    mx = max(mid, mx);
                    left = mid + 1;
                } else {
                    right = mid - 1;
                }
            }
        }
        ll mn = 10 * n + 10;
        {
            // diff回上に上がってdepthidがk以上になる最小
            ll right = depth[d].size() - 1;
            ll left = 0;
            while (left <= right) {
                ll mid = (left + right) / 2;
                ll did = depthid[ancestor_at_depth(depth[d][mid], diff, dbl)];
                if (did >= k) {
                    mn = min(mid, mn);
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            }
        }
        ll ans = mx - mn + 1;
        if (ans < 0) ans = 0;
        print(ans);
    }
}

#include <bits/stdc++.h>
using namespace std;
#define deb(k) cerr << #k << ": " << k << "\n";
#define size(a) (int)a.size()
#define fastcin cin.tie(0)->sync_with_stdio(0);
#define st first
#define nd second
#define pb push_back
#define mk make_pair
#define int long long
typedef long double ldbl;
typedef double dbl;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef map<int, int> mii;
typedef vector<int> vint;
#define MAXN 300100
#define MAXLG 20
const int inf = 0x3f3f3f3f;
const ll mod = 1000000007;
const ll linf = 0x3f3f3f3f3f3f3f3f;
const int N = 300100;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

vint g[N];
int ans[N];
vector<pii>qr[N];
int f[N];
vint aux[N];

void dfs(int x, int p = -1, int d = 0){
	for(int v : g[x]){
		for(auto e : qr[v]){
			int i = e.nd, j = e.st;
			aux[j].pb(f[j]);
			f[j] = 0;
		}
		dfs(v, x, d+1);
		for(auto e : qr[v]){
			int i = e.nd, j = e.st;
			f[j] += aux[j].back();
			aux[j].pop_back();
		}
	}
	f[d]++;
	for(auto e : qr[x]){
		int i = e.nd, j = e.st;
		ans[i] = f[j];
	}
}

void solve(){
	int n;
	cin>>n;
	for(int i=2;i<=n;i++){
		int x;
		cin>>x;
		g[x].pb(i);
	}
	int q;
	cin>>q;
	for(int i=0;i<q;i++){
		int a,b;
		cin>>a>>b;
		qr[a].pb({b, i});
	}
	dfs(1);
	for(int i=0;i<q;i++) cout<<ans[i]<<"\n";






	// Have you read the problem again?
	// Maybe you understood the wrong problem
}

int32_t main(){
	fastcin;
	int t_ = 1;
	//cin>>t_;
	while(t_--)
		solve();
	return 0;
}
