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

#define Mod(x) (x>=P)&&(x-=P)
#define rep(i,a,b) for(int i=a,i##end=b;i<=i##end;++i)
#define drep(i,a,b) for(int i=a,i##end=b;i>=i##end;--i)
#define erep(i,a) for(int i=hd[a];i;i=nxt[i])

typedef long long ll;
void Max(int &x,int y){x<y&&(x=y);}
void Min(int &x,int y){x>y&&(x=y);}

bool vio;
char IO;
int rd(int res=0){
	bool f=0;
	while(IO=getchar(),IO<48||IO>57)
		f|=IO=='-';
	do res=(res<<1)+(res<<3)+(IO^48);
	while(IO=getchar(),isdigit(IO));
	return f?-res:res;
}
const int M=2e5+10;
struct node{int x,id;};
vector<node>g[M];
int cnt[M],ans[M],dep[M],n,q;
int nxt[M],to[M],hd[M],ecnt;
void Add(int a,int b){
	nxt[++ecnt]=hd[a],to[hd[a]=ecnt]=b;
}
void dfs(int x,int f){
	rep(i,0,(int)g[x].size()-1)
		ans[g[x][i].id]-=cnt[g[x][i].x];
	dep[x]=dep[f]+1;
	cnt[dep[x]]++;
	erep(i,x)dfs(to[i],x);
	rep(i,0,(int)g[x].size()-1)
		ans[g[x][i].id]+=cnt[g[x][i].x];
}
bool let;
int main(){
	cerr<<(&vio-&let)/1024.0/1024<<endl;	
	n=rd();
	rep(i,2,n)Add(rd(),i);
	q=rd();
	rep(i,1,q){
		int x=rd(),y=rd();
		g[x].push_back((node){y+1,i});
	}
	dfs(1,0);
	rep(i,1,q)printf("%d\n",ans[i]);
}	

#ifdef DEBUG_BUILD
# define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#ifdef DEBUG_BUILD
# include "debugger.hpp"
#else
# define debug(...)
# define debugdo(...)
#endif
using namespace std;
using ll = long long;
using ld = long double;
using pll = std::pair<ll, ll>;
template <class T> using vec = std::vector<T>;
template <class T> using vec2 = std::vector<vec<T>>;
template <class T> using vec3 = std::vector<vec2<T>>;
#define rep(i, N) for (ll i = 0; i < (N); i++)
#define rrep(i, N) for (ll i = (N) - 1; i >= 0; i--)
#define range(i, A, B) for (ll i = (A); i < (B); i++)
constexpr int MOD = 1000000007;
constexpr ll INFL = std::numeric_limits<ll>::max();
template <class T> bool chmax(T &a, const T b) { if (a < b) { a = b; return true; } return false; }
template <class T> bool chmin(T &a, const T b) { if (a > b) { a = b; return true; } return false; }

ll N;
vec<ll> P;
vec2<ll> C;

vec<ll> In, Out, Depth;
ll timer = 0;

vec2<ll> List;

void dfs(const ll now) {
    In[now] = timer;
    timer++;
    List[Depth[now]].push_back(In[now]);
    for (ll to : C[now]) {
        Depth[to] = Depth[now] + 1;
        dfs(to);
    }
    Out[now] = timer;
    timer++;
}

void Main() {
    cin >> N;
    P.resize(N);
    C.resize(N);
    range(i, 1, N) {
        cin >> P[i];
        P[i]--;
        C[P[i]].push_back(i);
    }

    In.resize(N);
    Out.resize(N);
    Depth.resize(N);
    List.resize(N);
    dfs(0);

    debug(List);
    debug(In);
    debug(Out);
    debug(Depth);

    ll Q;
    cin >> Q;
    rep(q, Q) {
        ll u, d;
        cin >> u >> d;
        u--;

        const auto& v = List[d];
        debug(v);
        debug(In[u], Out[u], Depth[u]);

        cout << (
            lower_bound(v.begin(), v.end(), Out[u]) -
            lower_bound(v.begin(), v.end(), In[u])
        ) << "\n";
    }
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    std::cout << std::fixed << std::setprecision(10);
    Main();
    std::cout << std::flush;
    std::cerr << "--end--" << std::flush;
    return 0;
}
