#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<algorithm>
#include<numeric>
#include<cmath>
#include<iomanip>
#include<regex>
using namespace std;
#define int long long
const int mod=1e9+7;
signed main(){
	int n,m,x,y;
	cin>>n>>m;
	map<int,vector<int>>mp;
	while(cin>>x>>y)
		mp[x].push_back(y);
	set<int>st={n};
	for(auto&e:mp){
		vector<int>&ys=e.second,in,out;
		for(int y:ys){
			if(st.count(y))
				out.push_back(y);
			if(st.count(y-1)||st.count(y+1))
				in.push_back(y);
		}
		for(int y:out)
			st.erase(y);
		for(int y:in)
			st.insert(y);
	}
	cout<<st.size()<<endl;
}
// #pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
// #pragma GCC optimize(3 , "Ofast" , "inline")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization("unroll-loops")
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <map>
#include <list>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <stack>
#include <set>
#include <bitset>
#include <deque>
using namespace std ;
#define ios ios::sync_with_stdio(false) , cin.tie(0)
#define x first
#define y second
#define pb push_back
#define ls rt << 1
#define rs rt << 1 | 1
typedef long long ll ;
const double esp = 1e-6 , pi = acos(-1) ;
typedef pair<int , int> PII ;
const int N = 1e6 + 10 , INF = 0x3f3f3f3f , mod = 1e9 + 7;
int n , m ;
vector<int> v[N] ;
vector<int>  dep[N] ;
int in[N] , out[N] , idx = 0 ;
void dfs(int u , int d) {
	in[u] = ++ idx ;
	dep[d].pb(in[u]) ;
	for(auto x : v[u]) 
		dfs(x , d + 1) ;
	out[u] = ++ idx ;
}
int work()
{
  cin >> n ;
  for(int i = 2; i <= n ;i ++ ) {
  	int x ;
  	cin >> x ;
  	v[x].pb(i) ;
  }
  cin >> m ;
  dfs(1 , 0) ;
  while(m -- ) {
  	int x , y ;
  	cin >> x >> y ;
  	cout << (int)(upper_bound(dep[y].begin() , dep[y].end() , out[x]) - lower_bound(dep[y].begin() , dep[y].end() , in[x]))<< "\n";
  } 
  return 0 ;
}
int main()
{
  //   freopen("C://Users//spnooyseed//Desktop//in.txt" , "r" , stdin) ;
  //   freopen("C://Users//spnooyseed//Desktop//out.txt" , "w" , stdout) ;

  work() ;
  return 0 ;
}
/*

*/
	