// #define ONLINE_JUDGE
//                 _                      
//                (_)                     
// _ __ ___   __ _ _  ___  _ __ _ __ ___  
//| '_ ` _ \ / _` | |/ _ \| '__| '__/ _ \ 
//| | | | | | (_| | | (_) | |  | | | (_) |
//|_| |_| |_|\__,_| |\___/|_|  |_|  \___/ 
//               _/ |                     
//              |__/                      
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef tree<
    pll,//
    null_type,
    less<pll>,//
    rb_tree_tag,
    tree_order_statistics_node_update>
ordered_set;
 
#define majorro cout.precision(20); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define pb push_back
#define forn(i, n) for(ll (i) = 0; (i) < (n); ++(i))
#define fornm(i, m, n) for(ll (i) = (m); (i) < (n); ++(i))
#define rfornm(i, m, n) for(ll (i) = (m); (i) >= (n); --(i))
#define readvec(vector, n) {ll temp_vec_val;forn(inc, n){cin >> temp_vec_val;vector.push_back(temp_vec_val);}}
#define printvec(vector, delimeter) {ll length_of_vector=vector.size();                                    forn(elementvec, length_of_vector){cout << vector[elementvec] << delimeter;}}
#define all(vector) (vector).begin(), (vector).end()
#define rall(vector) (vector).rbegin(), (vector).rend()
#define endl "\n"
#define sz(something) (ll)something.size()

struct pair_hash
{
    template <class T1, class T2>
    size_t operator() (const pair<T1, T2>& pair) const
    {
        return hash<T1>()(pair.first) ^ hash<T2>()(pair.second);
    }
};
 
const ld EPS = 1e-8;
const ll MOD = 1e9+7;
// const ll MOD = 998244353;
const ll INF = 1e18;
const ld pi = 2.0*acos(0.0);
ll n, m, k, p, q, t, sum=0, cnt=0;
ll mx = -INF;
ll mn = INF;
bool flag = 0;
vll v;
string s = "", s1, s2;
char c;

void solve()
{
    cin >> n >> k;
    vector<pll> v;
    forn(i,n)
    {
        cin >> p >> q;
        v.pb({p,q});
    }
    sort(all(v));
    p = 0;
    q = 0;
    while(k)
    {
        p += k;
        k = 0;
        while(q<n&&v[q].first<=p) k += v[q++].second;
    }
    cout << p;
}

int main()
{
#if defined ACMP
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#elif !defined ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    majorro
    solve();
    return 0;
}
#include <bits/stdc++.h>
 
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
 
using namespace std;
 
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll, ll> pll;
typedef double ld;
typedef unsigned long long ull;
typedef long double lld;


#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif

void _print(ll t) {cerr << t;}
void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(lld t) {cerr << t;}
void _print(double t) {cerr << t;}
void _print(ull t) {cerr << t;}

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
 
template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
//-------------------------------------------------------------------------------

const ll N = 2e5+7;
vll g[N];
ll st[N],en[N];
vll d[N];
ll timer=0;

void dfs(ll v,ll depth=0){
	
	st[v] = timer++;
	d[depth].pb(st[v]);


	for(auto x:g[v]){
		dfs(x, depth+1);
	}

	en[v] = timer;


}
void solve(){


	int n;cin>>n;

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

	dfs(1);

	int q;
	cin>>q;
	while(q--){
		int u,D;cin>>u>>D;

		ll r = en[u], l= st[u];

		ll ans = lower_bound(d[D].begin(),d[D].end(),r) - lower_bound(d[D].begin(),d[D].end(),l);
		cout<<ans<<'\n';
	}




	
}


int main(){
	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
	#ifndef ONLINE_JUDGE
	freopen("E:\\input-output\\input.txt","r",stdin);
	freopen("E:\\input-output\\output.txt","w",stdout);
	freopen("E:\\input-output\\Error.txt", "w", stderr);
	#endif
	int t=1;
	
	while(t--){
		solve();
	}


}
 