#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>
#include <cassert>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

using namespace std;

#define cauto const auto&
#define ALL(v) begin(v),end(v)
#else
#define ALL(v) (v).begin(),(v).end()
#endif

namespace{
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
	v.assign(a, vector<T>(b, t));
}
template <class T> inline bool chmin(T &x, const T &y){ if(y < x){ x = y; return true; } return false; }
template <class T> inline bool chmax(T &x, const T &y){ if(x < y){ x = y; return true; } return false; }
template <class F, class T>
void convert(const F &f, T &t){
	stringstream ss;
	ss << f;
	ss >> t;
}
template <class Con>
string concat(const Con &c, const string &spr){
	stringstream ss;
	typename Con::const_iterator it = c.begin(), en = c.end();
	bool fst = true;
	for(; it != en; ++it){
		if(!fst){ ss << spr; }
		fst = false;
		ss << *it;
	}
	return ss.str();
}

template <class Con, class Fun>
vector<typename Con::value_type> cfilter(const Con &c, Fun f) {
	vector<typename Con::value_type> ret;
	typename Con::const_iterator it = c.begin(), en = c.end();
	for(; it != en; ++it){
		if(f(*it)){
			ret.emplace_back(*it);
		}
	}
	return ret;
}
#if __cplusplus >= 201103L
template <class Con, class Fun>
auto cmap(const Con &c, Fun f) -> vector<decltype(f(*c.begin()))> {
	vector<decltype(f(*c.begin()))> ret;
	ret.reserve(c.size());
	for(const auto &x: c){
		ret.emplace_back(f(x));
	}
	return ret;
}
#endif

#if __cplusplus >= 201402L
#define lambda(e) ([&](const auto &_){ return (e); })
#define lambda2(e) ([&](const auto &_a, const auto &_b){ return (e); })
#endif


#define REP(i,n) for(int i=0;i<int(n);++i)
#define RALL(v) (v).rbegin(),(v).rend()
#define tget(t,i) get<i>(t)

#define MOD 1000000007LL
#define EPS 1e-8

VV(int) G;
VV(pii) qs;
vint ans;

deque<int> dfs(int u, int d){
	deque<int> ret = {1};
	for(int v : G[u]){
		auto r = dfs(v, d + 1);
		r.push_front(0);
		if(ret.size() < r.size()){ ret.swap(r); }
		for(size_t i = 0; i < r.size(); ++i){
			ret[i] += r[i];
		}
	}
	for(cauto p : qs[u]){
		int k = p.second - d;
		if(k < 0 || k >= (int)ret.size()){
			ans[p.first] = 0;
		}
		else{
			ans[p.first] = ret[k];
		}
	}
	return ret;
}


void mainmain(){
	int n;
	cin >> n;
	G.resize(n);
	for(int i = 1; i < n; ++i){
		int p;
		cin >> p;
		G[p - 1].push_back(i);
	}
	int q;
	cin >> q;
	ans.assign(q, -1);
	qs.resize(n);
	REP(i, q){
		int u, d;
		cin >> u >> d;
		qs[u - 1].emplace_back(i, d);
	}
	dfs(0, 0);
	for(int x : ans){ cout << x << '\n'; }
}



}
int main() {
	ios::sync_with_stdio(false); cin.tie(0);
	cout << fixed << setprecision(10);
	cerr << fixed << setprecision(4);
	mainmain();
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for(int i = (a); i < int(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

const int MAX = 200000;

int N, Q;
vector<int> G[MAX + 1], qs[MAX + 1];
int depth[MAX + 1], in[MAX + 1], out[MAX + 1]; 

void dfs(int u, int p, int d, int& k) {
    in[u] = k++;
    depth[u] = d;
    qs[d].push_back(in[u]);
    for(int v : G[u]) {
        if(v == p) continue;
        dfs(v, u, d+1, k);
    }
    out[u] = k++;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> N;
    REP(i, N-1) {
        int p;
        cin >> p; p--;
        G[p].push_back(i+1);
        G[i+1].push_back(p);
    }
    {
        int k = 0;
        dfs(0, -1, 0, k);
    }
    cin >> Q;
    while(Q--) {
        int u, d;
        cin >> u >> d;
        u--;
        int res = upper_bound(ALL(qs[d]), out[u]) - lower_bound(ALL(qs[d]), in[u]);
        cout << res << "\n";
    }
}
