#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define int                     long long int
#define ld                      long double
#define pb                      push_back
#define MOD                     1000000007
#define inf                     3e18
#define vi                      vector<int>
#define vld                     vector<ld>
#define pii                     pair<int,int>
#define mii                     map<int,int>
#define fi                      first
#define se                      second
#define fastIO                  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define db(...)                 __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cerr << " "<< name << " : " << arg1 <<'\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}

typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> pbds; 
		//order_of_key (k) : Number of items strictly smaller than k .
		//find_by_order(k) : K-th element in a set (counting from zero) (returns an iterator)           


void inp_out()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	freopen("debug.txt", "w", stderr);
	#endif
}

vector < vi > adj, vec; 
vector < vector < pii >> a;  
vi ans, sz, cnt, depth, mp; 

void cal(int node)
{
	for(auto i : a[node])
	{
		ans[i.se] = cnt[i.fi]; 
	}
}

void dfs(int root, int keep)
{

	int bC =  - 1, mx = 0; 
	
	for(auto i : adj[root])
	{
		if(sz[i] > mx)
		{
			mx = sz[i]; 
			bC = i; 
		} 
	}

	for(auto i : adj[root])
	{
		if(i != bC)
		{
			dfs(i, 0); 
		}
	}

	if(bC !=  - 1)
	{
		dfs(bC, 1);
		mp[root] = mp[bC]; 
	}

	cnt[depth[root]]++ ; 
	vec[mp[root]].pb(depth[root]); 

	for(auto i : adj[root])
	{
		if(i != bC)
		{
			for(auto j : vec[mp[i]])
			{
				cnt[j]++ ;
				vec[mp[root]].pb(j);  
			}
		}
	}

	cal(root); 

	if(!keep)
	{
		for(auto i : vec[mp[root]])
		{
			cnt[i]-- ; 
		}
	}
}

void pre_cal(int root)
{
	sz[root]++ ; 
	for(auto i : adj[root])
	{
		depth[i] = depth[root] + 1; 
		pre_cal(i); 
		sz[root] += sz[i]; 
	}
}

int32_t main()
{
	fastIO
	inp_out();

	int n; 
	cin >> n; 

	adj = vec = vector < vi > (n + 1); 
	a = vector < vector < pii >> (n + 1); 
	sz = cnt = depth = mp = vi(n + 1); 

	iota(mp.begin(), mp.end(), 0); 

	for(int i = 2; i <= n; ++ i)
	{
		int x; 
		cin >> x;
		adj[x].pb(i);  
	}	

	int q; 
	cin >> q; 

	ans = vi(q); 

	for(int i = 0; i < q; ++ i)
	{
		int u, d; 
		cin >> u >> d; 
		a[u].pb({d, i}); 
	}	

	pre_cal(1); 
	dfs(1, 1); 

	for(auto i : ans)
		cout << i << '\n'; 
	 
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ff              first
#define ss              second
#define int             long long
#define w(x)            int x; cin>>x; while(x--)
#define mii             map<int,int>
#define endl            ("\n")
#define vi              vector<int>
#define pii             pair<int,int>
#define f(i, a, n)      for(int i=a; i<n; i++)
#define r(i, n, a)      for(int i=n-1;i>=0;i--)
#define fd(i, a, n, d)  for(int i=a; i<n;i+=d)
#define rd(i, n, a, d)  for(int i=n-1;i>=0;i-=d)
#define in(a,n)         f(i,0,n){cin>>a[i];}
#define pb              push_back
#define yy              cout<<"YES\n"
#define nn              cout<<"NO\n"
#define kk              cout<<"-1\n"
#define inf             (long long)1e18
#define mod             1000000007LL
#define cc(r)           cout<<r<<" "
#define ce(r)           cout<<r<<endl
#define out(a,n)        f(i,0,n){cc(a[i]);}
#define cn              continue
#define br              break
#define ins             insert
#define pi              3.14159265358979323846
#define aint(a)          a.begin(),a.end()

//################################################################################

void ovector_n()
{
    ce("Fastest Man Alive!");
}

//################################################################################

void flash() {

    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}
//################################################################################
int32_t main()
{
    flash();//1 June++;
    int T = clock();
    int x, y;
    cin >> x >> y;
    int q = (3LL - (x + y));
    if (x == y)
        ce(x);
    else
        ce(q);
    cerr << "TIME: " << (long double)(clock() - T) / CLOCKS_PER_SEC << " sec\n";
    return 0;
}
