#include <bits/stdc++.h>
using namespace std;
#define int long long
#define F(i,a,b) for(int i=(int)(a);i<=(int)(b);i++)
#define R(i,b,a) for(int i=(int)(b);i>=(int)(a);i--)
#define endl "\n"
#define ios ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define pii pair<int,int>
#define pb push_back
#define all(v) v.begin(),v.end()
#define I first
#define S second

int dp[13][202];

int32_t main(){
   ios;
   int L;
   cin>>L;

   dp[0][0]=1;
   F(i,1,12){
   	F(j,0,L){
   		F(l,1,L-11)
			if(j>=l) dp[i][j]+=dp[i-1][j-l];
   	}
   }
   cout<<dp[12][L]<<endl;
}

//#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <queue>
#include <algorithm>
#include <sstream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cctype>
#include <cstdlib>      // srand,rand
#include <random>
#include <functional>
#include <list>
#include <stack>




using namespace std;
#define ll long long
#define ll128 long long
#define lp(i,n) for(ll i=0;i<n;i++)

#define modd 1000000007
#define mod2 998244353
const double EPS = 1e-20;
const double PI = acos(-1);

#define INF 8223372036854775807ll
#define ALL(a)  (a).begin(),(a).end()


typedef pair<long long, long long> pl;
typedef pair<double, double> pd;
typedef pair<ll, string> pls;



typedef string::const_iterator State;
class ParseError {};




vector<ll> qv[200001];
ll N;
ll P[200001];
ll Q;
ll U[200001];
ll D[200001];
ll ans[200001];
ll count_d[200001];
vector<ll> child[200001];

void Search(ll node, ll cd[200001], ll depth) {
	
  	lp(i, qv[node].size()) {
		ll qnum = qv[node][i];
		ans[qnum] -= cd[D[qnum]];
	}
  cd[depth]++;
  

  lp(i, child[node].size()) {
    Search(child[node][i], cd, depth + 1);
  }

	lp(i, qv[node].size()) {
		ll qnum = qv[node][i];
		ans[qnum] += cd[D[qnum]];
	}

}

int main() {
	cin >> N;
	for (ll i = 2; i <= N; i++) {
		cin >> P[i];
		child[P[i]].push_back(i);
	}
	cin >> Q;
	lp(i, Q)cin >> U[i] >> D[i];
	lp(i, Q) {
		qv[U[i]].push_back(i);
	}
	Search(1, count_d, 0);
	lp(i, Q) {
		cout << ans[i] << endl;
	}

	//cout << fixed << setprecision(12) << bb << endl;
	return 0;
}