
//================code===================//

//#define TLE

#ifdef TLE
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#endif

#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <ctime>


#define ci(t) cin>>t
#define co(t) cout<<t
#define LL long long

#define ld double
#define fa(i,a,b) for(LL i=(a);i<(LL)(b);++i)
#define fd(i,a,b) for(LL i=(a);i>(LL)(b);--i)
#define setp tuple<LL,LL,LL>
#define setl pair<int,int>
#define micro 0.000001

using namespace std;

LL gcd(LL a, LL b) { return a % b ? gcd(b, a % b) : b; }


#ifdef OHSOLUTION
#define ce(t) cerr<<t
#define AT cerr << "\n=================ANS=================\n"
#define AE cerr << "\n=====================================\n"
#define DB(a) cerr << __LINE__ << ": " << #a << " = " << (a) << endl;
#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64
#else
#define AT
#define AE
#define ce(t)
#endif

pair <int, int> vu[9] = { {0,1},{1,0},{0,1} ,{-1,0},{1,1},{1,-1}, {-1,1} , {-1,-1},{-1,-1} }; //RDLU EWSN

template<typename T, typename U> void ckmax(T& a, U b) { a = a < b ? b : a; }
template<typename T, typename U> void ckmin(T& a, U b) { a = a > b ? b : a; }
struct gcmp { bool operator()(LL a, LL b) { return a < b; } bool operator()(setl& a, setl& b) { return a.second < b.second; } };
struct lcmp { bool operator()(LL a, LL b) { return a > b; } bool operator()(setl& a, setl& b) { return a.second > b.second; } };

const int max_v = 2e5 + 7;
const int max_k = 5e2 + 7;
const int bsz = (1ll << 10) + 7;
const int INF = 1e9 + 7;
const LL LNF = (LL)5e18 + 7ll;
LL mod = 1e9 + 7;
template<typename T, typename U> void MOD(T& a, U b) { a += b; if (a >= mod) a -= mod; }

vector<int> adj[max_v];
vector<int> edge[max_v];
int t=-1;
setl in[max_v];

void dfs(int u,int d=0)
{
	in[u].first = ++t;
	edge[d].push_back(in[u].first);
	for (auto& v : adj[u]) dfs(v,d+1);
	in[u].second = t;
}

int main()
{
#ifdef OHSOLUTION
	freopen("input.txt", "r", stdin);
#endif
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int v; ci(v);

	fa(i, 1, v)
	{
		int x; ci(x); --x;
		adj[x].push_back(i);
	}

	dfs(0);

	fa(i, 0, v) sort(edge[i].begin(), edge[i].end());

	int q; ci(q);
	while (q--)
	{
		int x, y; ci(x >> y); --x;

		int ret = upper_bound(edge[y].begin(), edge[y].end(), in[x].second) - lower_bound(edge[y].begin(), edge[y].end(), in[x].first);
		co(ret << "\n");
	}

	return 0;
}



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

#define rep(i,_l,_r) for(signed i=(_l),_end=(_r);i<=_end;++i)
#define fep(i,_l,_r) for(signed i=(_l),_end=(_r);i>=_end;--i)
#define erep(i,u) for(signed i=head[u],v=to[i];i;i=nxt[i],v=to[i])
#define efep(i,u) for(signed i=Head[u],v=to[i];i;i=nxt[i],v=to[i])
#define print(x,y) write(x),putchar(y)
//#define debug(...) do {cerr<<__LINE__<<" : ("#__VA_ARGS__<<") = "; Out(__VA_ARGS__); cerr<<flush;} while(0)
//template <typename T> void Out(T x) {cerr<<x<<"\n";}
//template <typename T,typename ...I> void Out(T x,I ...NEXT) {cerr<<x<<", "; Out(NEXT...);}

template <class T> inline T read(const T sample) {
    T x=0; int f=1; char s;
    while((s=getchar())>'9'||s<'0') if(s=='-') f=-1;
    while(s>='0'&&s<='9') x=(x<<1)+(x<<3)+(s^48),s=getchar();
    return x*f;
}
template <class T> inline void write(const T x) {
    if(x<0) return (void) (putchar('-'),write(-x));
    if(x>9) write(x/10);
    putchar(x%10^48);
}
template <class T> inline T Max(const T x,const T y) {if(x>y) return x; return y;}
template <class T> inline T Min(const T x,const T y) {if(x<y) return x; return y;}
template <class T> inline T fab(const T x) {return x>0?x:-x;}
template <class T> inline T gcd(const T x,const T y) {return y?gcd(y,x%y):x;}
template <class T> inline T lcm(const T x,const T y) {return x/gcd(x,y)*y;}
template <class T> inline T Swap(T &x,T &y) {x^=y^=x^=y;}

const int maxn=2e5+5;

int n,siz[maxn],dep[maxn],idx,id[maxn];
vector <int> g[maxn],t[maxn];

void dfs(int u) {
	siz[u]=1; id[u]=++idx;
//	printf("co %d %d\n",u,id[u]);
	t[dep[u]].push_back(id[u]);
	for(int i=0;i<g[u].size();++i) {
		int v=g[u][i]; dep[v]=dep[u]+1;
		dfs(v);
		siz[u]+=siz[v];
	}
} 

int cal(int op,int L,int R) {
	int l=0,r=t[op].size()-1,mid,ansl=-1,ansr=-1;
//	rep(i,l,r) print(t[op][i],'\n');
//	printf("HHH %d %d\n",L,R);
	while(l<=r) {
		mid=l+r>>1;
		if(t[op][mid]>=L) ansl=mid,r=mid-1;
		else l=mid+1;
	}
	l=0,r=t[op].size()-1;
	while(l<=r) {
		mid=l+r>>1;
		if(t[op][mid]<=R) ansr=mid,l=mid+1;
		else r=mid-1;
	}
	if(ansl==-1 || ansr==-1) return 0;
	return ansr-ansl+1;
}

int main() {
	n=read(9);
	rep(i,2,n) g[read(9)].push_back(i);
	dfs(1);
	rep(q,1,read(9)) {
		int x=read(9),y=read(9);
		print(cal(y,id[x],id[x]+siz[x]-1),'\n');
	}
	return 0;
}