#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#ifdef ENABLE_DEBUG
#define dump(a) cerr<<#a<<"="<<a<<endl
#define dumparr(a,n) cerr<<#a<<"["<<n<<"]="<<a[n]<<endl
#else
#define dump(a) 
#define dumparr(a,n) 
#endif
#define FOR(i, a, b) for(ll i = (ll)a;i < (ll)b;i++)
#define For(i, a) FOR(i, 0, a)
#define REV(i, a, b) for(ll i = (ll)b-1LL;i >= (ll)a;i--)
#define Rev(i, a) REV(i, 0, a)
#define REP(a) For(i, a)
#define SIGN(a) (a==0?0:(a>0?1:-1))

typedef long long int ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<ll, ll> pll;
typedef pair<ll,pll> ppll;
typedef vector<ll> vll;
typedef long double ld;
typedef pair<ld,ld> pdd;

const ll INF=(1LL<<60);
#if __cplusplus<201700L
ll gcd(ll a, ll b) {
  a=abs(a);
  b=abs(b);
  if(a==0)return b;
  if(b==0)return a;
  if(a < b) return gcd(b, a);
  ll r;
  while ((r=a%b)) {
    a = b;
    b = r;
  }
  return b;
}
#endif
template<class T>
bool chmax(T& a,const T& b){
  if(a<b){
    a=b;
    return true;
  }
  return false;
}
template<class T>
bool chmin(T& a,const T& b){
  if(a>b){
    a=b;
    return true;
  }
  return false;
}
template<class S,class T>
std::ostream& operator<<(std::ostream& os,pair<S,T> a){
  os << "(" << a.first << "," << a.second << ")";
  return os;
}
template<class T>
std::ostream& operator<<(std::ostream& os,vector<T> a){
  os << "[ ";
  REP(a.size()){
    os<< a[i] << " ";
  }
  os<< " ]";
  return os;
}


void solve(long long N, std::vector<long long> V, std::vector<long long> W, long long Q, std::vector<long long> v, std::vector<long long> L){
  ll Lmax=*max_element(L.begin(),L.end());
  vector<vector<ll>> dp((1<<9));
  dp[0].resize(W[0]+1);
  dp[0][W[0]]=V[0];
  vector<ll> ans(Q);
  For(iii,N){
    if(iii>=(1<<9))break;
    if(iii!=0){
      ll parent=(iii+1)/2-1;
      dp[iii].resize(min((ull)Lmax+1,dp[parent].size()+W[iii]));
      FOR(i,0,dp[parent].size()){
        if(i>0)chmax(dp[iii][i],max(dp[iii][i-1],dp[parent][i]));
        else chmax(dp[iii][i],dp[parent][i]);
        if(dp[iii].size()>i+W[iii])chmax(dp[iii][i+W[iii]],dp[parent][i]+V[iii]);
      }
      FOR(i,dp[parent].size(),dp[iii].size()){
        chmax(dp[iii][i],dp[iii][i-1]);
      }
    }
  }
  For(i,Q){
    ll ans=0;
    if(v[i]<(1<<9)){
      ans=dp[v[i]][min((ll)dp[v[i]].size()-1,L[i])];
    }else{
      deque<tuple<ll,ll,ll>> q;
      q.push_back(make_tuple(0,v[i],L[i]));
      while (!q.empty())
      {
        ll curscore,v,L;
        tie(curscore,v,L)=q.back();q.pop_back();
        if(v<(1<<9)){
          ans=max(ans,dp[v][min(L,(ll)dp[v].size()-1)]+curscore);
        }else{
          q.push_front(make_tuple(curscore,(v+1)/2-1,L));
          if(L>W[v])q.push_front(make_tuple(curscore+V[v],(v+1)/2-1,L-W[v]));
          if(L==W[v])ans=max(ans,curscore+V[v]);
        }
      }
    }
    printf("%lld\n",ans);
  }
}

int main(){
  cout<<setprecision(1000);
    long long N;
    scanf("%lld",&N);
    std::vector<long long> V(N);
    std::vector<long long> W(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&V[i]);
        scanf("%lld",&W[i]);
    }
    long long Q;
    scanf("%lld",&Q);
    std::vector<long long> v(Q);
    std::vector<long long> L(Q);
    for(int i = 0 ; i < Q ; i++){
        scanf("%lld",&v[i]);
        scanf("%lld",&L[i]);
        --v[i];
    }
    solve(N, std::move(V), std::move(W), Q, std::move(v), std::move(L));
    return 0;
}


Measured memory_bytes: 411152