#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
using ll = long long;
const int INF = 1001001001;
const ll LINF = 1002003004005006007ll;

void solve(){
  int n;cin >> n;
  ll k; cin >> k;
  vector<pair<ll,ll> > a(n);
  ll aa,bb;
  rep(i,n){
    cin >> aa >> bb;
    a[i]=make_pair(aa,bb);
  }
  sort(all(a));
  ll inde = 0;
  rep(i,n){
    if((k-(a[i].first-inde))<0){
      cout << k + inde << endl;
      return;
    }
    else {
      k -= a[i].first-inde;
      k += a[i].second;
      inde = a[i].first;
    }
  }
  cout << inde + k << endl;
  return;
}

int main(){
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(16);

  solve();
    return 0;
}
// E - Count Descendants
#include <bits/stdc++.h>
using namespace std;
#define vec vector
using vi = vec<int>;
using vvi = vec<vi>;
using ll = int64_t;
using PR = pair<ll,ll>;
#define all(x) (x).begin(),(x).end()
#define rp(i,s,e) for(int i=(s);i<(e);++i)
#define irp(i,s,e) for(int i=(s);i>(e);--i)
#define sz(a) int(a.size())
#define rep(i,e) for(int i=0;i<(e);++i)

vec<vi> adj, T;
vi I, O;

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

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

	adj.resize(n);
	rep(i, n-1){
		int p; cin>>p;
		adj[p-1].push_back(i+1);
	}

	T.resize(n);
	I.resize(n);
	O.resize(n);
	dfs(0, 0, 0);

	int q; cin>>q;
	while(q--){
		int u, d; cin>>u>>d;
		int r = upper_bound(all(T[d]), O[u-1]) - T[d].begin();
		int l = lower_bound(all(T[d]), I[u-1]) - T[d].begin();
		cout<< r - l <<endl;
	}
}
