#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define rep2(i,x,n) for(int i=x; i<(n); i++)
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define rng(v) v.begin(),v.end()
#define rngr(v) v.rbegin(),v.rend()
const long long INF = 1LL << 60;
using namespace std;
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;
using P3 = tuple<int, int, int>;
#define vi vector<int>
#define vvi vector<vi>
#define vll vector<ll>
#define vs vector<string>
using vp = vector<P>;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
using CP = complex<double>;
double dot( const CP &z, const CP &w ){ return (z.real()*w.real() + z.imag()*w.imag()); }
double cross( const CP &z, const CP &w ){ return (z.real()*w.imag() - z.imag()*w.real()); }
int n, q;
vvi g, ql, qr, pre;
vi cnt, res;
void dfs(int i, int d){
    int m = ql[i].size();
    pre[i].resize(m);
    rep(mi, m){
        pre[i][mi] = cnt[ql[i][mi]];
    }
    cnt[d]++;
    for(auto j : g[i]){
        dfs(j, d+1);
    }
    rep(mi, m){
        int k = ql[i][mi];
        res[qr[i][mi]] = cnt[k]-pre[i][mi];
    }
}
int main()
{
    cin >> n;
    g.resize(n);
    ql.resize(n);
    qr.resize(n);
    cnt.resize(n);
    pre.resize(n);
    rep(i, n-1){
        int p;
        cin >> p;
        --p;
        g[p].eb(i+1);
    }
    cin >> q;
    res.resize(q);
    rep(qi, q){
        int u, d;
        cin >> u >> d;
        --u;
        ql[u].eb(d);
        qr[u].eb(qi);
    }
    dfs(0, 0);
    for(auto i : res) cout << i << endl;
    return 0;
}
#include <bits/stdc++.h>
#define db(a) cout << a << endl
#define db2(a,b) cout << a << " " << b << endl
#define dbd(a) cout << #a << ": " << a << endl
#define dbd2(a,b) cout << #a << ": " << a << ", " << #b << ": " << b << endl 
#define dbp(a) cout << a.first << " " << a.second << endl
#define adb(a) for(auto _i:a) cout << _i << " "; cout << endl
#define adbp(a) for(auto _i:a) cout << _i.first << " " << _i.second << endl;
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define mod 1000000007
#define N 400010
#define M 100010
using namespace std;

vector<vector<int>> graph(N);
int depth[N], in[N], out[N], timer = 0;
vector<int> tour;

void dfs(int v, int p, int d)
{
    tour.push_back(v);
    in[v] = timer++;
    depth[v] = d;

    for(int x:graph[v])
        if(x != p)
            dfs(x, v, d + 1);

    tour.push_back(v);
    out[v] = timer++;
}

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
    #endif

    fastIO;
    int n;
    cin >> n;

    int p[n];
    for(int i=1; i<n; i++)
    {
        cin >> p[i], p[i]--;
        graph[i].push_back(p[i]);
        graph[p[i]].push_back(i);
    } 

    dfs(0, -1, 0);

    vector<vector<int>> mp(N); 

    for(int i=0; i<tour.size(); i++)
        mp[depth[tour[i]]].push_back(i);

    int q;
    cin >> q;

    while(q--)
    {
        int u, d;
        cin >> u >> d, u--;

        auto l = lower_bound(mp[d].begin(), mp[d].end(), in[u]);
        auto r = upper_bound(mp[d].begin(), mp[d].end(), out[u]);

        if(r == mp[u].end()) r--;

        int ans = (r - l + 1) / 2;
        cout << ans << endl;
    }
}