#include<bits/stdc++.h>
#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)
#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)
#define foreach(i, n) for(auto &i:(n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll << (x))
#define method(name,res,...) function<res (__VA_ARGS__) > name = [&](__VA_ARGS__) -> res
using ll = long long;
//const ll MOD = (ll)1e9+7;
const ll MOD = 998244353;
const int INF = (ll)1e9+7;
const ll INFLL = (ll)1e18;
using namespace std;
template<class t>
using vvector = vector<vector<t>>;
template<class t>
using vvvector = vector<vector<vector<t>>>;
template<class t>
using priority_queuer = priority_queue<t, vector<t>, greater<t>>;
template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}
template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}
#ifdef DEBUG
#define debug(x) cout<<"LINE "<<__LINE__<<": "<<#x<<" = "<<x<<endl;
#else
#define debug(x) (void)0
#endif

namespace templates{
  ll modpow(ll x, ll b){
    ll res = 1;
    while(b){
      if(b&1)res = res * x % MOD;
      x = x * x % MOD;
      b>>=1;
    }
    return res;
  }

  ll modinv(ll x){
    return modpow(x, MOD-2);
  }

  bool was_output = false;
  template<class t>
  void output(t a){
    if(was_output)cout << " ";
    cout << a;
    was_output = true;
  }
  void outendl(){
    was_output = false;
    cout << endl;
  }
  ll in(){
    ll res;
    scanf("%lld", &res);
    return res;
  }

  template<class t>
  istream& operator>>(istream&is, vector<t>&x){
    for(auto &i:x)is >> i;
    return is;
  }

  template<class t, class u>
  istream& operator>>(istream&is, pair<t, u>&x){
    is >> x.first >> x.second;
    return is;
  }

  template<class t>
  void in(t&x){
    cin >> x;
  }

  template<class t>
  t in(){
    t res; cin >> res; return res;
  }

  template<class t>
  void out(t x){
    cout << x;
  }

  template<class t>
  vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){
    sort(line.begin(),line.end(),comp);
    return line;
  }

  template<class t>
  vector<t> reversed(vector<t> line){
    reverse(line.begin(),line.end());
    return line;
  }
}

using namespace templates;



int main(){
  int n;
  scanf("%d",&n);
  vector<pii> line(n+1);
  rep(i,1,n+1){
    scanf("%d%d",&line[i].second,&line[i].first);
  }

  method(out2,vector<pii>,int from){
    if(from==0){
      vector<pii> res;
      res.emplace_back(0,0);
      return res;
    }
    vector<pii> left = out2(from>>2);
    vector<pii> right(left.size());
    int ls = left.size();
    rep(i,ls){
      right[i].first = left[i].first + line[from].first;
      right[i].second = left[i].second + line[from].second;
    }

    left.emplace_back(INF,INF);
    right.emplace_back(INF,INF);
    int cnt = 0;
    {
      int l = 0;
      int r = 0;
      int last_val = -1;
      rep(i,ls*2){
        if(left[l].first<right[r].first){
          if(last_val<left[l].second){
            ++cnt;
            last_val = left[l].second;
          }
          ++l;
        }else{
          if(last_val<right[r].second){
            ++cnt;
            last_val = right[r].second;
          }
          ++r;
        }
      }
    }

    vector<pii> res(cnt);
    int l = 0;
    int r = 0;
    int last_val = -1;
    int itr=0;
    rep(i,ls*2){
      if(left[l].first<right[r].first){
        if(last_val<left[l].second){
          res[itr++] = left[l];
          last_val = left[l].second;
        }
        ++l;
      }else{
        if(last_val<right[r].second){
          res[itr++] = right[r];
          last_val = right[r].second;
        }
        ++r;
      }
    }
    return res;
  };

  method(func,ll,int from,ll use){
    vector<pii> lu = out2(from);
    vector<pii> ru = out2(from>>1);
    ll res = 0;
    int ls = lu.size();
    int rs = ru.size();
    int r = 0;
    per(i,ls){
      while(r+1<rs and lu[i].first+ru[r+1].first<=use)++r;
      if(lu[i].first+ru[r].first<=use){
        chmax(res,lu[i].second+ru[r].second);
      }
    }
    return res;
  };


  int q;
  scanf("%d",&q);
  rep(i,q){
    int v;
    int l;
    scanf("%d%d",&v,&l);
    cout << func(v,l) << endl;
  }
  return 0;
}


Measured memory_bytes: 5612