// skip until line 65
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <istream>
#include <iterator>
#include <sstream>
#include <list>
#include <map>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <iomanip>
using namespace std;

#define BR "\n"
#define REP(i, n) for(int (i)=0;(i)<(n);++(i))
#define ALL(cont) begin(cont),end(cont)
#define AS_MOD(a, b) ((((a) % (b) ) + (b)) % (b))
#define MO(a) AS_MOD(a,mod)
#define FEACH(it, cont) for(auto (it) = begin(cont); it!=end(cont);++it)
#define FEACHR(it, cont) for(auto (it) = rbegin(cont); it!=rend(cont);++it)
#define pb push_back
#define pob pop_back
#define fi first
#define se second
#define getll() ([](){ll s;scanf("%lld", &s);return s;})()
#define getld() ([](){ld s;scanf("%Lf", &s);return s;})()
#define prl(P) printf("%lld", P)
#define prd(P) printf("%.10Lf", P)
#define pr(P) printf(P)
#define MAX(V, T) V=max(V,T)
#define MIN(V, T) V=min(V,T)

#define DumpArray(cont, from, to) for(int __i=from;__i<to;++__i) {cout<<"(" <<__i<<": " << cont[__i]<<")";} cout<<endl;
#define DumpPairs(cont, from, to) for(int __i=from;__i<to;++__i) {cout<<"(" <<__i<<": " << cont[__i].fi <<"," << cont[__i].se<<")";} cout<<endl;
/*
 * ***_bound(6)
 * 5 6 6 6 6 6 7 7 7
 *   ^lower    ^upper
 * void reverse(c.begin(), c.end())
 *
 * graph
 * seg tree
 * dp
 * brute force
 */
struct Comparator{
    int operator()(int l, int r) const {
        return r<l;
    }
};
struct Vec;
//set<int, Comparator> S;

// solution below
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> P;
ll N;
vector<P> WV(4*100000);
vector<P> ar1[512+10];
vector<P> ar2(512);
vector<P> vals(20);
#define TEN 10
ll solve(ll v,ll L) {
    vals.clear();
    vector<ll> tmp;
    while(v>=1) {
        tmp.pb(v);
        vals.pb(WV[v]);
        v/=2;
    }
    reverse(ALL(tmp));
    reverse(ALL(vals));
    ar2.clear();
    if (vals.size()<TEN) {
        for(int i=0;i< (1<<((int)vals.size()));++i) {
            P t(0,0);
            for(int j=0;j<vals.size();++j) {
                if ((i>>j)&1) {
                    t.fi+=vals[j].fi;
                    t.se+=vals[j].se;
                }
            }
            ar2.pb(t);
        }
        ll ret=0;
        for(P p:ar2) {
            if (p.fi<=L)
                ret=max(ret,p.se);
        }
        return ret;
    }
    // pack ar1,ar2
    ar2.pb(P(0,0));
    for(int i=0;i< (1<<((int)vals.size()-TEN));++i) {
        P t(0,0);
        for(int j=0;j<vals.size()-TEN;++j) {
            if ((i>>j)&1) {
                t.fi+=vals[j+TEN].fi;
                t.se+=vals[j+TEN].se;
            }
        }
        ar2.pb(t);
    }
    //
    ll ret=0;
    ll tn=1<<(TEN-1);
    /*cout<<vals.size()<<endl;
    DumpArray(tmp,0,tmp.size());
    DumpPairs(ar2,0,ar2.size());
    cout<<tmp[TEN-1]<<endl;*/
    for(P p:ar2) {
        P ans=p;
        auto it=lower_bound(ALL(ar1[tmp[TEN-1]-tn]), P(L - p.fi + 1, 0), ([](P l, P r) -> bool {
            return l.fi == r.fi ? l.se < r.se : l.fi < r.fi;
        }));
        if (it!=ar2.begin()) {
            --it;
            ans.fi+=it->fi;
            ans.se+=it->se;
        }
        if (ans.fi<=L)
            ret=max(ret,ans.se);
    }
    return ret;
}
void prepare(int v) {
    int ov=v;
    vals.clear();
    while(v>=1) {
        vals.pb(WV[v]);
        v/=2;
    }
    ll tn=1<<(TEN-1);
    for(int i=0;i< (1<<vals.size());++i) {
        P t(0,0);
        for(int j=0;j<TEN;++j) {
            if ((i>>j)&1 && j<vals.size()) {
                t.fi+=vals[j].fi;
                t.se+=vals[j].se;
            }
        }
        ar1[ov-tn].pb(t);
    }
    sort(ALL(ar1[ov-tn]), ([](P l, P r) -> bool {
        return l.fi == r.fi ? l.se < r.se : l.fi < r.fi;
    }) );
    vector<P> n;
    n.pb(ar1[ov-tn][0]);
    for(int i=1;i<ar1[ov-tn].size();++i){
        if (ar1[ov-tn][i].se>n[n.size()-1].se)
            n.pb(ar1[ov-tn][i]);
    }
    ar1[ov-tn]=n;
}
int main() {
    cout.precision(15);
    cout << fixed;
    cin >> N;
    REP(i,N)
        cin >> WV[i+1].se>>WV[i+1].fi;
    for(int i=(1<<(TEN-1));i<(1<<(TEN));++i)
        if (i<=N)
            prepare(i);

    /*for(int i=0;i<(1<<(TEN-1));++i) {
        for(int j=0;j<8;++j)
        cout<<"("<<ar1[i][j].fi <<" "<<ar1[i][j].se<<")";
        cout<<endl;
    }*/
    ll Q;
    cin >> Q;
    REP(i,Q) {
        ll v,L;
        cin >> v>>L;
        //cout<< "F"<<endl;
        ll ans=solve(v,L);
        cout <<ans<<endl;
        //cout<< "F"<<endl;
    }
}


Measured memory_bytes: 17888