#include<bits/stdc++.h>
#define ll long long int
#define pragi(a,b) for(ll i = a;i<b;i++)
#define pragj(a,b) for(ll j = a;j<b;j++)
#define pragk(a,b) for(ll k = a;k>=b;k--)
#define all(v) (v.begin(),v.end())
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define MP make_pair
#define MT make_tuple
#define F first
#define S second
#define KAKA ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
const int mod = 1e9 + 7;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<bool> vb;
typedef long double ld;
typedef pair<int, int> pii;
//typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef pair<ll, ll> pll;
vector<ll> parent,level;
vector<vector<ll> > Adj;
vector<ll> flat_tree;
vector<ll> pre,post;
ll timer = 0;
vector<vector<ll> > segtree(5e6);

void build(ll node,ll tl,ll tr)
{
	if(tl == tr)
	{
		segtree[node] = vector<ll> (1,flat_tree[tl]);
		return;
	}
	else
	{
		ll mid = (tl + tr)/2;
		build(2 * node,tl,mid);
		build(2 * node + 1,mid + 1,tr);
		merge(segtree[node*2].begin(), segtree[node*2].end(), segtree[node*2+1].begin(), segtree[node*2+1].end(),
              back_inserter(segtree[node]));
	}
}
ll query(ll node,ll tl,ll tr,ll ql,ll qr,ll k)
{
  if(ql > qr)
  {
  	return 0ll;
  }
  else if((ql == tl) and (qr == tr))
  {
    auto it2 = upper_bound(segtree[node].begin(),segtree[node].end(),k);
    auto it1 = lower_bound(segtree[node].begin(),segtree[node].end(),k);
    ll ans = it2 - it1;
    return ans;
  }
  else
  {
  	ll mid = (tl + tr)/2;
  	ll l = query(node * 2,tl,mid,ql,min(qr,mid),k);
  	ll r = query(node * 2 + 1,mid + 1,tr,max(ql,mid + 1),qr,k);
  	return (l + r);
  }
}
void dfs(ll node,ll par,ll dist)
{
	parent[node] = par;
	level[node] = dist;
	flat_tree.eb(level[node]);
	pre[node] = timer;
	timer++;
	for(auto child : Adj[node])
	{
		if(child == par) continue;
		else
		{
			dfs(child,node,dist + 1);
		}
	}
    post[node] = timer++;
    flat_tree.eb(level[node]);

}

void Compute(ll n)
{
	KAKA
	parent.resize(n + 1);
	level.resize(n + 1);
	Adj.resize(n + 1);
	pre.resize(n + 1);post.resize(n + 1);
	pragi(2,n + 1)
	{
		cin>>parent[i];
		Adj[i].eb(parent[i]);
		Adj[parent[i]].eb(i);
	}
	dfs(1,-1,0);
    /*for(auto ele : flat_tree)
    {
    	cout<<ele<<" ";
    }
    cout<<"\n";*/
    ll m = flat_tree.size();
    build(1,0,m - 1);
    ll q;
    cin>>q;
    while(q--)
    {
    	ll u,d;
    	cin>>u>>d;
    	ll ans = query(1,0,m - 1,pre[u],post[u],d);
    	cout<<ans/2<<"\n";
    }
}
int main()
{
	KAKA
	ll n;
	cin>>n;
	Compute(n);
}
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <algorithm>
#define rep(i,n) for(int i=0;i<(n);++i)
#define all(a) (a).begin(),(a).end()
using namespace std;
using Graph = vector<vector<int>>;
typedef long long ll;
//using Graph = vector<vector<pair<ll,ll>>>;
const int mod =1e+9+7;

const int dy[4]={0,1,0,-1};
const int dx[4]={1,0,-1,0};

const ll INF=1e18;

int main(){
    ll n; cin>>n;
    vector<vector<ll>>x(n,vector<ll>(4));
    vector<ll>a(n);
    ll suma=0;
    rep(i,n){
        cin>>x[i][1]>>x[i][2];
        x[i][3]=i;
        x[i][0]=2*x[i][1]+x[i][2];
        a[i]=x[i][1];
        suma+=a[i];
    }
    sort(all(x));
    reverse(all(x));
    ll Takahashi=0;
    ll cnt=0;
    rep(i,n){
        Takahashi+=x[i][1]+x[i][2];

        suma-=x[i][1];
        cnt++;
        if(Takahashi>suma){
            cout<<cnt<<endl;
            return 0;
        }
    }

}







