#include <iostream>
#include <map>
#include <vector>
#include <set>
#include <cmath>
#include <algorithm>
#include <utility>
#include <limits.h>
#include <cstring>
using namespace std;
const int N = 200005;
int n, s[N], e[N], tim = 1;
vector<int> adj[N];
vector<int> lvl[N];
int cnt(int idx, int l, int r)
{
    return upper_bound(lvl[idx].begin(), lvl[idx].end(), r) - lower_bound(lvl[idx].begin(), lvl[idx].end(), l);
}
void dfs(int u = 1, int l = 0)
{
    s[u] = tim;
    lvl[l].push_back(s[u]);
    for (int v: adj[u])
    {
        tim ++;
        dfs(v, l + 1);
    }
    e[u] = tim;
}
int main(int argc, char const *argv[])
{
	scanf("%d", &n);
    for (int i = 2; i <= n; ++i)
    {
        int p;
        scanf("%d", &p);
        adj[p].push_back(i);
    }
    dfs();
    int q;
    scanf("%d", &q);
    while (q --)
    {
        int u, v;
        scanf("%d", &u);
        scanf("%d", &v);
        printf("%d\n", cnt(v, s[u], e[u]));
    }
    return 0;
}
#include<bits/stdc++.h>
using namespace std;
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')';}
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) {os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
template<typename T> void dbg_a(const T *a,int l,int r){cerr<<" {";for(int i = l;i<r;i++) cerr<<a[i]<<", ";cerr<<a[r]<<"}"<<endl;}
typedef long long ll;
//#define int ll
inline int read(){
    int f(1),r(0);char c(getchar());
    while(c<'0'||c>'9'){if(c == '-') f = -1; c = getchar();}
    while(c>='0'&&c<='9'){ r = (r<<1) + (r<<3) + c - '0';c = getchar();}
    return f*r;
}
void read(int &x){ x = read();}
template<class Head, class... Tail> void read(Head &head, Tail &... tail) { read(head); read(tail...); }
typedef pair<int,int> PII;
#define fi first
#define se second
#define mst(a,b) memset(a,b,sizeof(a))
#define For(i,a,b) for(int i = a;i<=b;i++)
#define For_(i,a,b) for(int i = a;i>=b;i--)
#define rep(i,a) for(int i = 0;i<a;i++)
#define All(x) x.begin(),x.end()

//    For(i,1,n) For(j,1,m) printf("f[%lld][%lld] = %lld\n",i,j,f[i][j]);
//const ll INF = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 6,M = 2020+ 6;
const int mod = 1e9 + 7;



int n;
vector<vector<int>> G,lis;
int timer;
int in[N],out[N],dep[N];
void dfs(int x,int pr){
    in[x] = ++timer;
    lis[dep[x]].emplace_back(in[x]);
    for(auto y:G[x]) if(y != pr){
        dep[y] = dep[x] + 1;
        dfs(y,x);
    }
    out[x] = ++timer;
}

void solve(){
    n = read();
    G.resize(n+1);
    lis.resize(n+1);
    For(i,2,n){
        int x = read();
        G[i].emplace_back(x);
        G[x].emplace_back(i);
    }
    dfs(1,0);
    int q = read();
    dbg_a(in,1,7);
    dbg_a(out,1,7);
    while(q--){
        int x,d;
        read(x,d);
        auto depth = lis[d];
        int res = lower_bound(All(depth),out[x]) - lower_bound(All(depth),in[x]);
        cout<<res<<"\n";
    }
    
}
signed main(){
    int T = 1;
    //T = read();
    while(T--) solve();
    return 0;
}