#include <bits/stdc++.h>
using namespace std;
 
#define pb push_back
#define FOR(i,f,n) for(int i=f;i<n;i++)
#define FORI(i,f,n) for(int i=f;i>=n;i--)
#define sz(a) ((int)(a).size())
#define ff first
#define ss second
#define all(a) (a).begin(),(a).end()
#define alli(a) (a).rbegin(),(a).rend()
#define approx(a) fixed<<setprecision(a)
#define trav(a,x) for(auto& a : x)
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<int,bool> pib;
typedef pair<ll,bool> plb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vld;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
template <class T> void ckmin(T &a, const T &b) { a = min(a, b); }
template <class T> void ckmax(T &a, const T &b) { a = max(a, b); }
 
template <class T> void read(vector<T> &v);
template <class F, class S> void read(pair<F, S> &p);
template <class T> void read(T &x) {cin>>x;}
template <class R, class... T> void read(R& r, T&... t){read(r); read(t...);};
template <class T> void read(vector<T> &v) {trav(x, v) read(x);}
template <class F, class S> void read(pair<F, S> &p) {read(p.ff, p.ss);}
 
template <class F, class S> void pr(const pair<F, S> &x);
template <class T> void pr(const T &x) {cout<<x;}
template <class R, class... T> void pr(const R& r, const T&... t) {pr(r); pr(t...);}
template <class F, class S> void pr(const pair<F, S> &x) {pr("{", x.ff, ", ", x.ss, "}");}
void ps() {pr("\n");}
template <class T> void ps(const T &x) {pr(x); ps();}
template <class T> void ps(vector<T> &v) {trav(x, v) pr(x, ' '); ps();}
template <class F, class S> void ps(const pair<F, S> &x) {pr(x.ff, ' ', x.ss); ps();}
template <class R, class... T> void ps(const R& r, const T &...t) {pr(r, ' '); ps(t...);}

const int MXN = 2e5+1;
vi adj[MXN], level[MXN], in(MXN), out(MXN);
int timer = 0;

void dfs(int v, int d){
    in[v] = ++timer;
    level[d].pb(in[v]);
    for(int u : adj[v]){    
        dfs(u, d+1);
    }
    out[v] = ++timer;
}

void solve(){
    int n; read(n);
    FOR(i,1,n){
        int x; read(x);
        adj[x].pb(i+1);
    }
    dfs(1, 0);
    int q; read(q);
    FOR(i,0,q){
        int u, d; read(u, d);
        int ans = lower_bound(all(level[d]), out[u]) - lower_bound(all(level[d]), in[u]);
        ps(ans);
    }
}
 
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int t=1;
    //cin>>t;
    while(t--){
        solve();
    }
}
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define pb push_back
#define mii map <int, int>
#define mll map <ll, ll>
#define pii pair <int, int>
#define pll pair <ll, ll>
#define vi vector <int>
#define vd vector <double>
#define vll vector <ll>
#define fi first
#define se second
#define si set <int>
#define sll set <ll>
#define spii set <pii>
#define vs vector <string>
#define vpii vector <pair <int, int> >
#define vpll vector <pair <long long, long long> >
#define vvi vector <vector <int> >
#define vvpii vector <vector <pii > >
#define vb vector <bool>
#define vvb vector <vb>
#define mp make_pair
#define vvll vector <vll>
#define vsi vector <si>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define MANX MAXN
#define itn int
#define dbg(x); {cout << #x << "=" << x << ", ";}
#define in(x); { for (auto &to : x) cin >> to;}
#define out(x); { for (auto &to : x) cout << to << " "; cout << '\n'; }

template<class T>
bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }

template<class T>
bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

const ll INFLL = 1e18;
const int MAXN = 2e5 + 100;
const ll INF = 1e9;
const int mod1 = 1e9 + 7;
const int mod2 = 1e9 + 21;
vvi g;
vvi d;
int ans[MAXN];
vvpii qu;
vector<map<int, int>> mm;

void dfs(int s, int ok) {
    mm[s][ok++]++;
    for (auto to:g[s]) {
        dfs(to, ok);
        if (mm[to].size() > mm[s].size())
            swap(mm[to], mm[s]);
        for (auto too:mm[to])
            mm[s][too.fi] += too.se;
    }
    for (auto to:qu[s]) {
        ans[to.se] = (mm[s].find(to.fi) == mm[s].end() ? 0 : mm[s][to.fi]);
    }
}

void solve() {
    int n;
    cin >> n;
    mm = vector<map<int, int>>(n);
    d = vvi(n);
    g = vvi(n);
    qu = vvpii(n);
    for (int i = 1; i < n; i++) {
        int x;
        cin >> x;
        x--;
        g[x].pb(i);
    }
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int x, y;
        cin >> x >> y;
        x--;
        qu[x].pb({y, i});
    }
    dfs(0, 0);
    for (int i = 0; i < q; i++)
        cout << ans[i] << '\n';
}

int main() {
#ifdef Mip182
    freopen("a.in", "r", stdin);
#else
    ios_base::sync_with_stdio(0);
    cin.tie(0);
#endif
    int _t;
    _t = 1;
//    cin>>_t;
    while (_t--)
        solve();
#ifdef Mip182
    cout << '\n' << "Time : " << (double) (clock()) / CLOCKS_PER_SEC << '\n';
#endif
}
