// Problem: C - Made Up
// Contest: AtCoder - AISing Programming Contest 2021(AtCoder Beginner Contest 202)
// URL: https://atcoder.jp/contests/abc202/tasks/abc202_c
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PLL;
typedef pair<int, int>PII;
typedef pair<double, double>PDD;
#define I_int ll
inline ll read()
{
ll x = 0, f = 1;
char ch = getchar();
while(ch < ‘0‘ || ch > ‘9‘)
{
if(ch == ‘-‘)f = -1;
ch = getchar();
}
while(ch >= ‘0‘ && ch <= ‘9‘)
{
x = x * 10 + ch - ‘0‘;
ch = getchar();
}
return x * f;
}
inline void out_(ll x){
if (x < 0) x = ~x + 1, putchar(‘-‘);
if (x > 9) out_(x / 10);
putchar(x % 10 + ‘0‘);
}
inline void write(ll x){
if (x < 0) x = ~x + 1, putchar(‘-‘);
if (x > 9) write(x / 10);
putchar(x % 10 + ‘0‘);
puts("");
}
#define read read()
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define perr(i,a,b) for(int i=(a);i>(b);i--)
ll ksm(ll a, ll b, ll p)
{
ll res = 1;
while(b)
{
if(b & 1)res = res * a % p;
a = a * a % p;
b >>= 1;
}
return res;
}
const int inf = 0x3f3f3f3f;
#define PI acos(-1)
const int maxn=2e5+100;
int n,q;
vector<int>g[maxn];
vector<int>dis[maxn];
int in[maxn],out[maxn],tot;
void dfs(int u,int fa,int tmpd){
in[u]=++tot;
dis[tmpd].push_back(in[u]);
for(int i=0;i<g[u].size();i++){
int j=g[u][i];
if(j==fa) continue;
dfs(j,u,tmpd+1);
}
out[u]=tot;
}
int main(){
n=read;
rep(i,2,n){
int x=read;
g[x].push_back(i);
g[i].push_back(x);
}
dfs(1,-1,0);
/*rep(i,1,n){
cout<<i<<"*******"<<in[i]<<"******"<<out[i]<<"\n";
}
for(int i=0;i<=4;i++){
for(int j=0;j<dis[i].size();j++){
cout<<dis[i][j]<<"=====";
}
puts("");
}
*/
q=read;
while(q--){
int u=read,d=read;
int res=upper_bound(dis[d].begin(),dis[d].end(),out[u])-dis[d].begin();
int ans=lower_bound(dis[d].begin(),dis[d].end(),in[u])-dis[d].begin();
//cout<<res-ans<<endl;
printf("%d\n",res-ans);
}
return 0;
}
AISing Programming Contest 2021(AtCoder Beginner Contest 202)E - Count Descendants