#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T> using vt = vector<T>;
template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define pb push_back
#define SZ(x) ((int)((x).size()))
#define EACH(x, a) for (auto& x: a)
#define FOR(i,s,n) for (ll i = (s); (i) < (n); ++i)
#define FORD(i,s,l) for (ll i = (s); (i) >= l; --i)
#define F first
#define S second
#define TC int __tc; cin >> __tc; FOR(case_num,1,__tc+1)
#define TEST(x,i) ((x)&(1ll<<(i)))
#define SET(x,i) ((x)|(1ll<<(i)))
#define FLIP(x,i) ((x)^(1ll<<(i)))
#define CLEAR(x,i) ((x)&~(1ll<<(i)))
const double pi = 4 * atan(1);
using ll = long long;
using pll = pair<ll, ll>;


mt19937 mt_rng(chrono::steady_clock::now().time_since_epoch().count());
ll randint(ll a, ll b) {
	return uniform_int_distribution<ll>(a, b)(mt_rng);
}

template<class T> bool umin(T& a, const T& b) {
	return b<a?a=b, 1:0;
}
template<class T> bool umax(T& a, const T& b) { 
	return a<b?a=b, 1:0;
}

template<class ForwardIterator> void print_vec(ForwardIterator first,
		ForwardIterator last, string sep = " ", string end = "\n") {
	bool ft = true;
	while (first != last) {
		if (!ft) {cout << sep;} else {ft = false;}
		cout << (*first);
		++first;
	}
	cout << end;
}

template<typename T1, typename T2>
std::ostream& operator<<(std::ostream &o, const pair<T1, T2> &p) {
	return o << p.F << " " << p.S;
}

template<typename T>
vector<int> argsort(const vector<T> &v, function<bool(T,T)> cmp) {
    vector<int> indices(v.size());
    iota(indices.begin(), indices.end(), 0);
    sort(indices.begin(), indices.end(),[&v,&cmp](int left, int right) -> bool {return cmp(v[left],v[right]);});
    return indices;
}

template<typename T>
vector<int> argsort(const vector<T> &v) {
    return argsort<T>(v, less<T>());
}

inline ll floorDiv(ll x, ll y) {
	ll d = x / y;
	ll r = x % y;
	return r ? (d - ((x < 0) ^ (y < 0))) : d;
}

ll ceilDiv(ll x, ll y) {
	return -floorDiv(-x, y);
}

ll bin_search(ll lo, ll hi, function<bool(ll)> predicate) {
	int sign = lo <= hi ? 1 : -1;
	lo *= sign;
	hi *= sign;
	hi++;
	while (lo < hi) {
		ll mid = lo + floorDiv(hi-lo, 2);
		if (!predicate(sign * mid)) {
			lo = mid + 1;
		} else {
			hi = mid;
		}
	}
	return sign * lo;
}
const ll MOD = 1000000007;
const int MAXN = 200005;

vector<vector<int>> adj;
vector<vector<pair<int,int>>> queries;
vector<int> counts;

void dfs(int u, vector<int> &ans, int p = 1, int d = 0) {
	vector<int> before;
	for (int i = 0; i < (int)queries[u].size(); i++) {
		before.pb(counts[queries[u][i].F]);
	}
	counts[d]++;	
	for (auto &v : adj[u]) {
		if (v != p) {
			dfs(v, ans, u, d+1);
		}
	}
	vector<int> after;
	for (int i = 0; i < (int)queries[u].size(); i++) {
		after.pb(counts[queries[u][i].F]);
	}
	for (int i = 0; i < (int)queries[u].size(); i++) {
		ans[queries[u][i].S]=after[i]-before[i];
	}
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	
	int n;
	cin >> n;
	adj.assign(n+1, {});
	queries.assign(n+1, {});
	counts.assign(n+1,0);
	
	for (int i = 2; i <= n; i++) {
		int p;
		cin >> p;
		adj[i].pb(p);
		adj[p].pb(i);
	}
	
	int q;
	cin >> q;
	vector<int> ans(q);
	for (int i = 0; i < q; i++) {
		int u, d;
		cin >> u >> d;
		queries[u].pb({d,i});
	}
	
	dfs(1, ans);
	
	print_vec(ans.begin(),ans.end(),"\n");

	
	
}

/**
 *    author:  FromDihPout
 *    created: 2021-06-05
**/

#include <bits/stdc++.h>
using namespace std;

const int MAX_DEPTH = 2e5 + 5;
int t;
vector<vector<int>> adj;
vector<vector<int>> depth;
vector<int> tin, tout;

void dfs(int u, int d) {
    depth[d].push_back(t);
    tin[u] = t;
    t++;
    for (int v : adj[u]) {
        dfs(v, d + 1);
    }
    tout[u] = t;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int n;
    cin >> n;
    adj.resize(n);
    for (int i = 1; i < n; i++) {
        int p;
        cin >> p;
        p--;
        adj[p].push_back(i);
    }
    
    t = 0;
    depth.resize(MAX_DEPTH);
    tin.resize(n);
    tout.resize(n);
    dfs(0, 0);
    
    int q;
    cin >> q;
    while (q--) {
        int u, d;
        cin >> u >> d;
        u--;
        
        vector<int> nodes = depth[d];
        int r = lower_bound(nodes.begin(), nodes.end(), tout[u]) - nodes.begin();
        int l = lower_bound(nodes.begin(), nodes.end(), tin[u]) - nodes.begin();
        cout << r - l << '\n';
    }
    return 0;
}