#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
// #include<ext/pb_ds/assoc_container.hpp>
// #include<ext/pb_ds/tree_policy.hpp>
// #include<ext/pb_ds/tag_and_trait.hpp>
// using namespace __gnu_pbds;
// #include<boost/multiprecision/cpp_int.hpp>
// namespace multiprecisioninteger = boost::multiprecision;
// using cint=multiprecisioninteger::cpp_int;
using namespace std;
using ll=long long;
#define double long double
using datas=pair<ll,ll>;
using ddatas=pair<double,double>;
using tdata=pair<ll,datas>;
using vec=vector<ll>;
using mat=vector<vec>;
using pvec=vector<datas>;
using pmat=vector<pvec>;
// using llset=tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>;
#define For(i,a,b) for(i=a;i<(ll)b;++i)
#define bFor(i,b,a) for(i=b,--i;i>=(ll)a;--i)
#define rep(i,N) For(i,0,N)
#define rep1(i,N) For(i,1,N)
#define brep(i,N) bFor(i,N,0)
#define brep1(i,N) bFor(i,N,1)
#define all(v) (v).begin(),(v).end()
#define allr(v) (v).rbegin(),(v).rend()
#define vsort(v) sort(all(v))
#define vrsort(v) sort(allr(v))
#define endl "\n"
#define eb emplace_back
#define printyes cout<<"Yes"<<endl
#define printno cout<<"No"<<endl
#define printYES cout<<"YES"<<endl
#define printNO cout<<"NO"<<endl
#define output(v) do{bool f=0;for(auto outi:v){cout<<(f?" ":"")<<outi;f=1;}cout<<endl;}while(0)
#define matoutput(v) do{for(auto outimat:v)output(outimat);}while(0)
const ll mod=1000000007;
// const ll mod=998244353;
const ll inf=1LL<<60;
const double PI = acos(-1);
const double eps = 1e-9;
template<class T> inline bool chmax(T& a,T b){bool x=a<b;if(x)a=b;return x;} 
template<class T> inline bool chmin(T& a,T b){bool x=a>b;if(x)a=b;return x;} 
template<class T> void print(T x,bool ech=1){cout<<x;if(ech)cout<<endl;}
template<class T1,class T2> void print(pair<T1,T2> x,bool ech=1){cout<<"{";print(x.first,0);cout<<",";print(x.second,0);cout<<"}";if(ech)cout<<endl;}
template<class T> void print(vector<T> v,bool ech=1){bool f=0;for(auto x:v){if(f)cout<<" ";print(x,0);f=1;}if(ech)cout<<endl;}
void startupcpp(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout<<fixed<<setprecision(15);
}
using P=pair<int,int>;
vector<P> lis[1024];
vector<P> merge(vector<P>& v,int val,int weight){
  vector<P> nv(v.size()),ans;
  int i,j=0,bf=-1;
  for(i=0;i<(int)v.size();++i){
    nv[i]=P(v[i].first+weight,v[i].second+val);
  }
  i=0;
  // print(nv);
  while(j<(int)v.size()){
    if(i==(int)v.size()||v[i].first>nv[j].first){
      if(nv[j].first>100000)break;
      if(chmax(bf,nv[j].second))ans.eb(nv[j]);
      ++j;
    }else if(v[i].first<nv[j].first){
      if(v[i].first>100000)break;
      if(chmax(bf,v[i].second))ans.eb(v[i]);
      ++i;
    }else{
      if(nv[j].first>100000)break;
      chmax(nv[j].second,v[i].second);
      if(chmax(bf,nv[j].second))ans.eb(nv[j]);
      ++i;++j;
    }
  }
  return ans;
}
int main(){
  startupcpp();
  lis[0].eb(P(0,0));
  int i,N,Q;
  cin>>N;
  vector<int> v(N),w(N);
  rep(i,N){
    cin>>v[i]>>w[i];
    if(i<1023)lis[i+1]=merge(lis[(i+1)>>1],v[i],w[i]);
  }
  cin>>Q;
  while(Q--){
    int u,L;
    cin>>u>>L;
    if(u<1024){
      auto itr=upper_bound(all(lis[u]),P(L+1,-1));--itr;
      print((*itr).second);
    }else{
      auto ch=lis[0];
      while(u>=1024){
        ch=merge(ch,v[u-1],w[u-1]);
        u>>=1;
      }
      int ans=0;
      for(P& x:ch){
        if(x.first>L)break;
        auto itr=upper_bound(all(lis[u]),P(L+1-x.first,-1));--itr;
        chmax(ans,(*itr).second+x.second);
      }
      print(ans);
    }
  }
  // }
}

Measured memory_bytes: 10760