#include <bits/stdc++.h>
// #include <atcoder/all>
using namespace std;
// using namespace atcoder;
typedef int64_t lint;
#define rep(i, n) for(int i=0; i<n; i++)
#define repx(i, l, n) for(int i=l; i<n; i++)
#define all(v) v.begin(), v.end()
#define show(x) cout << #x << ": " << x << endl;
#define list(x) cout << #x << ": " << x << "  ";
#define pb push_back
using vi = vector<int>;
using vvi = vector<vector<int>>;
template<class T> inline void vin(vector<T>& v) { rep(i, v.size()) cin >> v.at(i); }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> inline void drop(T x) { cout << x << endl; exit(0); }
template<class T> void vout(vector<T> v) { rep(i, v.size()) { cout << v.at(i) << ' '; } cout << endl; }
constexpr int INF = INT_MAX/2;

vector<vector<int>> G;
void make_graph(int N, int M) {
	G.resize(N);
	int x, y;
	rep(i, M) {
		cin >> x;
		G[x-1].push_back(i+1);
	}
}

vector<int> dist;
void bfs(int root) {
	int x, y, z, N = G.size();
	dist.resize(N, -1);
	queue<int> Q;
	Q.push(root), dist[root] = 0;
	while (!Q.empty()) {
		x = Q.front(); Q.pop();
		rep(i, G[x].size()) {
			y = G[x][i];
			if (dist[y] == -1) { Q.push(y); dist[y] = dist[x]+1; }
		}
	}
}

std::vector<set<int>> s(200200);
std::vector<map<int, int>> m(200200);
vi t(200200);
stack<int> q;
void rec() {
	int a=0, b=0, c=0, x, y, z;
	x = q.top();
	for (auto d : s[x]) {
		m[x][d] -= t[d];
	}
	t[dist[x]]++;
	for (auto n : G[x]) {
		q.push(n);
		rec();
	}
	for (auto d : s[x]) {
		m[x][d] += t[d];
	}
	q.pop();
	return;
}

int main() {
	int N, Q;
	cin >> N;
	make_graph(N, N-1);
	bfs(0);

	int a=0, b=0, c=0, x, y, z;
	cin >> Q;
	vi U(Q), D(Q);
	rep(i, Q) {
		cin >> U[i] >> D[i];
		U[i]--;
		s[U[i]].insert(D[i]);
	}
	q.push(0);
	rec();
	rep(i, Q) {
		std::cout << m[U[i]][D[i]] << '\n';
	}
}

#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <iomanip>
#include <limits>
#include <math.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(a) a.begin(),a.end()
#define P pair<int,int>
#define Pll pair<long,long>
#define mp make_pair
#define INF 1000000001
#define INFl 1000000000000000001
#define ll long long
using namespace std;


struct edge{long to,cost,k;};

template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

void search(vector<int> &L,vector<int> &R,vector<vector<int> > &son,vector<int> &dis,vector<int> &Num,vector<vector<int> > &con,int &n,int e){
    L[e]=n;
    rep(i,son[e].size()){
        dis[son[e][i]]=dis[e]+1;
        search(L,R,son,dis,Num,con,n,son[e][i]);
    }
    Num[e]=n;
    con[dis[e]].push_back(Num[e]);
    R[e]=n;
    n++;
}

int main() {
    int N;cin>>N;
    vector<vector<int> > son(N+1,vector<int>(0));
    vector<int> dis(N+1);
    vector<int> Num(N+1);
    vector<int> L(N+1);
    vector<int> R(N+1);
    vector<vector<int> > con(N+1,vector<int>(0));//距離がiの集合
    rep(i,N-1){
        int p;cin>>p;
        son[p].push_back(i+2);
    }
    dis[1]=0;
    int n=1;
    search(L,R,son,dis,Num,con,n,1);
    rep(i,N+1){
        if(con[i].size()==0)continue;
        sort(all(con[i]));
    }
    int Q;cin>>Q;
    rep(i,Q){
        int u,d;cin>>u>>d;
        int l=L[u];
        int r=R[u];
        int c=upper_bound(all(con[d]),r)-lower_bound(all(con[d]),l);
        cout<<c<<endl;
    }
}