#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> pii;
#define ffor(i,n) for(ll i=0;i<n;i++)
#define rfor(x,y) for(ll i=x;i<y;i++)
#define ff first
#define ss second
#define pb push_back
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define all(v) v.begin(),v.end()
#define vi std::vector<ll>
#define M 1000000007
bool cmp(pii a,pii b)
{
	return 2*a.ff + a.ss > 2*b.ff + b.ss;
}
void solve()
{	
	ll n;
	cin>>n;
	vector<pii> v(n);
	ll a=0,k=0;
	ffor(i,n){
	cin>>v[i].ff>>v[i].ss;
	a+=v[i].ff;}
	k = 0;
	sort(all(v),cmp);
	ll i=0;
	while(a >= k)
	{
		k += (v[i].ff + v[i].ss);
		a -= v[i].ff;
		i++;
	}
	cout<<i<<endl;
	return;


}
int main()
{   
    #ifndef ONLINE_JUDGE
    freopen("input.in","r",stdin);
    freopen("output.in","w",stdout);
    #endif
    fastio
    ll t=1;
    // cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}
// code by lynmisakura. wish to be accepted!
#include<bits/stdc++.h>

using ll = long long;
using namespace std;
#define debug(arr) std::cout << #arr << " = " << arr << '\n'
#define debug2(a,b) std::cout<<"["<<#a<<","<<#b<<"] = "<<"["<<a<<","<<b<<"]\n"
#define debug3(a,b,c) std::cout<<"["<<#a<<","<<#b<<","<<#c<<"] = "<<"["<<a<<","<<b<<","<<c<<"]\n"
template<class T> std::ostream &operator << (std::ostream& out, const std::vector<T>& arr) {
  std::cout << "{"; for (int i = 0; i < arr.size(); i++)std::cout << (!i ? "" : ", ") << arr[i]; std::cout << "}";
  return out;
}
template<class T> std::ostream &operator << (std::ostream& out, const std::vector<std::vector<T> >& arr) {
  std::cout << "{\n"; for (auto& vec : arr)std::cout << "  " << vec << ",\n"; std::cout << "}";
  return out;
}
template<class S,class T> std::ostream &operator << (std::ostream& out, const std::pair<S,T>& p){
  std::cout << "{" << p.first << "," << p.second << "}" << '\n';
  return out;
}
template<class T> std::istream &operator >> (std::istream& in, std::vector<T>& arr) {
  for (auto& i : arr) std::cin >> i; return in;
}
template<class S,class T> std::istream &operator >> (std::istream& in,std::pair<S,T>& p){
  std::cin >> p.first >> p.second; return in;
}
template<class T> void debug_a(T *a , int N){
  std::cout << "debug array : {";
  for(int i = 0;i < N;i++){
    std::cout << *(a + i) << (i < N-1 ? ", " : "}\n");
  }
}
#define REP(i,N) for(int i = 0;i < N;i++)

int n,q;
vector<int> graph[200020];
int U[200020],D[200020];

void __input__(){
  cin >> n;
  REP(i,n-1){
    int p;cin >> p;
    p--;
    graph[i+1].push_back(p);
    graph[p].push_back(i+1);
  }
  cin >> q;
  REP(i,q) cin >> U[i] >> D[i] , U[i]--;
}

int dist[200020];
pair<int,int> pos[200020];
vector<int> dists[200020];
int num = 0;
vector<int> tour;
void dfs(int x,int p,int d){
  tour.push_back(x);
  dists[d].push_back(num);
  pos[x].first = num++;
  dist[x] = d;
  for(int to : graph[x]){
    if(to != p) dfs(to,x,d+1);
  }
  tour.push_back(-x);
  pos[x].second = num++;
  return;
}

int main(){

  __input__();
  
  dfs(0,-1,0);
//  debug(tour);
  REP(i,200020) dists[i].push_back(100000000);
  REP(i,q){
    cout << lower_bound(dists[D[i]].begin(),dists[D[i]].end(),pos[U[i]].second) 
              -  lower_bound(dists[D[i]].begin(),dists[D[i]].end(),pos[U[i]].first) << '\n';
  }

}
