#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <tuple>
#include <cstdio>
#include <bitset>
#include <sstream>
#include <iterator>
#include <numeric>
#include <map>
#include <cstring>
#include <set>
#include <functional>
#include <iomanip>
#include <cassert>

using namespace std;

#define DEBUG_ //!!$BDs=P;~$K%3%a%s%H%"%&%H(B!!
#ifdef DEBUG_
#define dump(x)  cerr << #x << " = " << (x) << endl;
#else
#define dump(x)  ;
#endif

#define equals(a,b) (fabs((a)-(b)) < EPS)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back

//#define int long long

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

    template <typename T>
std::string printVector(const std::vector<T> &data)
{
    std::stringstream ss;
    std::ostream_iterator<T> out_it(ss, ", ");
    ss << "[";
    std::copy(data.begin(), data.end() - 1, out_it);
    ss << data.back() << "]";
    return ss.str();
}

template <typename T>
void print_array(const T &ary, int size){
    REP(i,size){
        cout << ary[i] << " ";
    }
    cout << endl;
}

const int mod = 1e9+7;
//const int mod = 998244353;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;
const double EPS = (1e-10);
const long double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899;
int dx[] = {0,0,-1,1};
int dy[] = {-1,1,0,0};

int N;
VVI g;
VI d;
VVI Qv; //頂点iのqidxが格納
vector<PII> qlist; //クエスチョンが連番
VI ans;

void dfs1(int v, int p, int dep){
    d[v] = dep;
    for(int u : g[v]){
        if(u == p) continue;

        dfs1(u,v,dep+1);
    }
}

map<int,int> dfs2(int v, int p){
    map<int,int> res;
    res[d[v]]++;
    for(int u : g[v]){
        if(u == p) continue;

        auto qmap = dfs2(u,v);

        if(res.size() < qmap.size()){
            swap(res, qmap);
        }

        for(auto p : qmap){
            res[p.first] += p.second;
        }


    }
    int sz = SZ(Qv[v]);
    REP(i,sz){ //この時点でクエリに答える
        int qidx = Qv[v][i];
        int x = qlist[qidx].second;
        if(res.find(x) == res.end()){
            ans[qidx] = 0;
        }else{
            ans[qidx] = res[x];
        }
    }


    return res;
   
}

signed main(int argc, char const *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(12);

    cin >> N;
    g.resize(N);
    d.resize(N);
    Qv.resize(N);
    FOR(i,1,N){
        int p;
        cin >> p;
        p--;
        g[i].eb(p);
        g[p].eb(i);

    }

    int Q; cin >> Q;
    qlist.resize(Q);
    ans.resize(Q);
    REP(i,Q){
        int u,d;
        cin >> u >> d;
        u--;
        qlist[i] = make_pair(u,d);
        Qv[u].eb(i);
    }

    dfs1(0,-1,0);
    dfs2(0,-1);

    REP(i,Q){
        cout << ans[i] << "\n";
    }

}
// E - Count Descendants
#include <bits/stdc++.h>
using namespace std;
#define vec vector
using vi = vec<int>;

vec<vi> adj, T;
vi L, R;

int dfs(int d, int p, int t){
	T[d].push_back(t);
	L[p] = t;
	for(int&c:adj[p]) t = dfs(d+1, c, t+1);
	R[p] = t+1;
	return t+1;
}

int f(int d, int t){
	return lower_bound(T[d].begin(), T[d].end(), t) - T[d].begin();
}

int main(){
	int n; cin>>n;

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

	T.resize(n), L.resize(n), R.resize(n);
	dfs(0, 0, 0);

	int q; cin>>q;
	while(q--){
		int u, d; cin>>u>>d;
		cout<< f(d, R[u-1]) - f(d, L[u-1]) <<endl;
	}
}
