/// HuDzG - NHDanDz "Elderwood" (DangCuto Dep Trai vcl)
#include <bits/stdc++.h>
#define reset(a) memset(a,0,sizeof(a))
#define ll long long
#define ld long double
#define endl '\n'
#define AutoAC int main()
#define OO 1000000000000000000
#define F first
#define S second
#define pii pair <ll, ll>
#define pb push_back
#define nmax 200005
#define HUNGIT "C"
#define MOD 998244353

using namespace std;

const ll M = 1000000;

ll n, Begin[nmax], End[nmax], res[nmax], q, ST[4 * nmax], cnt = 0;
struct pp
{
    ll u, d, id;
} Q[nmax];
vector <ll> g[nmax], node[2];

bool cmp(pp a, pp b)
{
    return a.d < b.d;
}

ll dfs(ll u)
{
    ll Max = 0;
    Begin[u] = ++Begin[0];
    for(auto v : g[u])
        Max = max(Max, dfs(v) + 1);
    End[u] = Begin[0];
    return Max;
}

void Update(ll id, ll l, ll r, ll i, ll v)
{
    if(r < i || i < l) return;
    if(l == r)
    {
        ST[id] = v;
        return;
    }
    ll mid = (l + r) / 2;
    Update(id * 2, l, mid, i, v);
    Update(id * 2 + 1, mid + 1, r, i, v);
    ST[id] = ST[id * 2] + ST[id * 2 + 1];
}

ll Get(ll id, ll l, ll r, ll x, ll y)
{
    if(r < x || y < l) return 0;
    if(x <= l && r <= y) return ST[id];
    ll mid = (l + r) / 2;
    return Get(id * 2, l, mid, x, y) + Get(id * 2 + 1, mid + 1, r, x, y);
}

void bfs()
{
    ll now = cnt % 2, Next = (cnt + 1) % 2;
    cnt++;
    while(!node[now].empty())
    {
        ll u = node[now].back();
        Update(1, 1, n, Begin[u], 0);
        for(auto v : g[u])
        {
            node[Next].pb(v);
            Update(1, 1, n, Begin[v], 1);
        }
        node[now].pop_back();
    }
}

void solve()
{
    cin >> n;
    for (int i = 2; i <= n; i++)
    {
        ll p;
        cin >> p;
        g[p].pb(i);
    }
    cin >> q;
    for(int i = 1; i <= q; i++)
    {
        cin >> Q[i].u >> Q[i].d;
        Q[i].id = i;
    }
    sort(Q + 1, Q + 1 + q, cmp);
    ll Max = dfs(1);
    node[0].pb(1);
    Update(1, 1, n, Begin[1], 1);
    for(int i = 1; i <= q; i++)
    {
        ll u = Q[i].u, d = Q[i].d, id = Q[i].id;
        if(d > Max) break;
        while(cnt < d) bfs();
        res[id] = Get(1, 1, n, Begin[u], End[u]);
    }
    for(int i = 1; i <= q; i++)
        cout << res[i] << endl;
}

AutoAC
{
    // clock_t START, FINISH;
    // START = clock();
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    // freopen (HUNGIT".inp", "r", stdin);
    // freopen (HUNGIT".out", "w", stdout);
    ll T = 1;

    // cin >> T;

    for (int i = 1; i <= T; i++)
    {
        solve();
    }

    // FINISH = clock();
    // cout << fixed << setprecision(4);
    // cout << endl << "TIME: " << (ld)((ld)(FINISH - START) / CLOCKS_PER_SEC);
    return 0;
}

#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
typedef long long ll;
typedef long double ld;
#define rep(i,n) for(ll i=0;i<(n);i++)
#define repr(i,n) for(ll i=(n-1);i>=0;i--)
#define all(x) x.begin(),x.end()
#define br cout << "\n";
using namespace std;
const long long INF = 1e18;
const long long MOD = 1e9+7;
using Graph = vector<vector<ll>>;
template<class T> inline bool chmin(T &a, T b) { if(a > b){ a = b; return true;} return false;}
template<class T> inline bool chmax(T &a, T b) { if(a < b){ a = b; return true;} return false;}
ll ceilll(ll a, ll b) {return (a + b-1) / b;} // if(a%b != 0) (a/b) + 1
ll get_digit(ll a) {ll digit = 0; while(a != 0){a /= 10; digit++;} return digit;} // a != 0
template<typename T> void vecdbg(vector<T>& v){ rep(i, v.size()){cerr << v[i] << " ";} br;}
template<typename T> void vecvecdbg(vector<vector<T>>& v){ rep(i, v.size()){rep(j, v[i].size()){cerr << v[i][j] << " ";} br;}}
ll POW(ll a, ll n){ ll res = 1; while(n > 0){ if(n & 1){ res = res * a; } a *= a; n >>= 1; } return res; }
using P = pair<ll, ll>;

// 0 false, 1 true 
// string number to int : -48 or - '0'
// a to A : -32
// ceil(a)  1.2->2.0
// c++17	g++ -std=c++17 a.cpp
// global vector -> 0 initialization
// DONT FORGET TO INTIALIZE
// The type of GRID is CHAR. DONT USE STRING
// If the result in local and judge is different, USE CODETEST!!
// (a * b)over flow?   if(a > INF / b){ /* overflow */}

ll n;
ll cnt = 0;
vector<pair<ll, ll>> inout(n);
vector<ll> depth(n);
vector<ll> seen(n);
void dfs(vector<vector<ll>> &g, ll v, ll d){
    //in
    inout[v].first = cnt;
    depth[v] = d;
    seen[v] = 1;
    cnt++;
    for(ll next : g[v]){
        if(seen[next] == 0){
            dfs(g, next, d+1);
            cnt++;
        }
    }
    //out
    inout[v].second = cnt;
}

int main() {
    std::cout << std::fixed << std::setprecision(15);
    cin >> n;
    vector<vector<ll>> g(n);
    depth.assign(n, 0);
    inout.assign(n, {-1, -1});
    seen.assign(n, 0);
    rep(i, n-1){
        ll p; cin >> p; p--;
        g[i+1].push_back(p); g[p].push_back(i+1);
    }

    dfs(g, 0, 0);
    vector<vector<ll>> hukasa(n);
    rep(i, n){
        ll x = depth[i];
        hukasa[x].push_back(inout[i].first);
    }
    rep(i, n){
        sort(all(hukasa[i]));
    }


    ll q; cin >> q;
    rep(i, q){
        ll u, d; cin >> u >> d; u--;
        ll r = lower_bound(all(hukasa[d]), inout[u].second) - hukasa[d].begin();
        ll l = lower_bound(all(hukasa[d]), inout[u].first) - hukasa[d].begin();
        cout << r - l << endl;
    }
}