#include<bits/stdc++.h>
#define it register int
#define ct const int
#define il inline
using namespace std;
typedef long long ll;
#define rll register ll
#define cll const ll
typedef double db;
#define rdb register db
#define cdb const db
typedef long double ldb;
typedef unsigned long long ull;
#define pb push_back
#define mkp make_pair
#define pl pair<ll,int> 
#define pi pair<int,int>
#define fir first
#define sec second
namespace io{
	il char nc(){
		static char buf[100000],*p1=buf,*p2=buf;
		return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++; 
	}
	template <class I> 
	il void fr(I &num){
		num=0;register char c=nc();it p=1;
		while(c<'0'||c>'9') c=='-'?p=-1,c=nc():c=nc();
		while(c>='0'&&c<='9') num=num*10+c-'0',c=nc();
		num*=p;
	}
	char obuf[1000001],*os=obuf, *ot=obuf+1000001;
	il void flush(){fwrite(obuf,1,os-obuf,stdout),os=obuf;}
	il void ptc(const char x){*os++=x,os==ot?flush():void();}
	template<class I>
	il void wr(I x){
		x<0?ptc('-'),x=-x:0;
		static char qu[65];
		char *tmp=qu;
		do *tmp++=(x%10)^'0';while(x/=10);
		while(tmp--!=qu) ptc(*tmp);
	}
	struct flusher{ ~flusher() { flush(); } }_;
	template <class I> 
	il I Max(I p,I q){return p>q?p:q;}
	template <class I> 
	il I Min(I p,I q){return p<q?p:q;}
	template <class I> 
	il I A(I x){return x<0?-x:x;}
	template <class I> 
	il void sp(I&p,I&q){I x=p;p=q,q=x;}
	template <class I>
	il void ckMax(I&p,I q){p=(p>q?p:q);}
	template <class I>
	il void ckMin(I&p,I q){p=(p<q?p:q);}
};
using io :: fr;
using io :: nc;
using io :: wr;
using io :: ptc;
using io :: Max;
using io :: Min; 
using io :: sp;
using io :: ckMax;
using io :: ckMin;
const int N=1000005;
int h[N],nxt[N],adj[N],u,v,t,a[N],n,dep[N],fa[N],path[N],cnp,ans[N],mxd,id,son[N];
bool flag[N];
il void add(){nxt[++t]=h[u],h[u]=t,adj[t]=v,nxt[++t]=h[v],h[v]=t,adj[t]=u;}
il void predfs(ct x){
	flag[x]=(x==id);
	for(it i=h[x],j;i;i=nxt[i])
		if((j=adj[i])^fa[x]){
			fa[j]=x,predfs(j);
			if(flag[j]) son[x]=j,flag[x]=1;
		}
}
il void dfs(ct x){
	path[++cnp]=x,dep[x]=dep[fa[x]]+1;
	for(it i=h[x],j;i;i=nxt[i])
		if(((j=adj[i])^fa[x])&&(j^son[x]))
			fa[j]=x,dfs(j);
	if(son[x]) fa[son[x]]=x,dfs(son[x]);
	path[++cnp]=x;
}
il void DFS(ct x,ct fa,ct dep){
	if(dep>mxd) mxd=dep,id=x;
	for(it i=h[x],j;i;i=nxt[i])
		if((j=adj[i])^fa)
			DFS(j,x,dep+1);
}
int main(){ 
	scanf("%d",&n);it i;
	for(i=1;i<n;++i) scanf("%d%d",&u,&v),add();
	DFS(1,0,0),mxd=0,i=id,id=0,DFS(i,0,0),std::swap(i,id),predfs(i),dfs(i);
	for(i=cnp;i;--i) ans[path[i]]=i; 
	for(i=1;i<=n;++i) printf("%d ",ans[i]); 
	return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>
std::vector<std::vector<int>> children, list;
std::vector<int> in, out, depth;
int timer;
void dfs(const int u) {
    in[u] = timer++;
    list[depth[u]].push_back(in[u]);
    for (const int v : children[u]) {
        depth[v] = depth[u] + 1;
        dfs(v);
    }
    out[u] = timer++;
}
int main() {
    int N;
    std::cin >> N;
    children = list = std::vector<std::vector<int>>(N);
    in = out = depth = std::vector<int>(N);
    for (int i = 1; i < N; ++i) {
        int p;
        std::cin >> p;
        children[p - 1].push_back(i);
    }
    dfs(0);
    int Q;
    std::cin >> Q;
    while (Q--) {
        int u, d;
        std::cin >> u >> d;
        u -= 1;
        const auto& v = list[d];
        std::cout << std::lower_bound(v.cbegin(), v.cend(), out[u]) - std::lower_bound(v.cbegin(), v.cend(), in[u]) << '\n';
    }
    return 0;
}