#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <sstream>
#include <string>
#define _repargs(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rep(...) _repargs(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T>
inline void output(T a, int p = 0) {
    if(p) cout << fixed << setprecision(p)  << a << "\n";
    else cout << a << "\n";
}
// end of template

template <typename T> inline void voutput(T &v){
    rep(i, v.size()){
        if (i) cout << " " << v[i];
        else cout << v[i];
    }
    cout << endl;
}

const int H_DEPTH = 10; // 9

int main() {
    int N;
    cin >> N;
    vector<int> V(N), W(N);
    rep(i, N) cin >> V[i] >> W[i];
    int Q;
    cin >> Q;
    vector<int> vtx(Q), wgt(Q);
    rep(i, Q) {
        cin >> vtx[i] >> wgt[i];
        vtx[i]--;
    }
    
    if(N < (1 << H_DEPTH)) { // brute-force
        rep(i, Q) {
            vector<int> A;
            while(vtx[i] > 0) {
                A.pb(vtx[i]);
                vtx[i] = (vtx[i] - 1) / 2;
            }
            A.pb(0);
            int ans = 0;
            rep(j, (1 << A.size())) {
                int tv = 0;
                int tw = 0;
                rep(k, A.size()) {
                    if((j >> k) & 1) {
                        tw += W[A[k]];
                        tv += V[A[k]];
                    }
                }
                if(tw <= wgt[i]) ans = max(ans, tv);
            }
            output(ans);
        }
    }
    else {
        vector<vector<pair<int, int>>> top(1 << H_DEPTH); // w, v;
        rep(i, 1 << H_DEPTH) {
            int cv = i;
            vector<int> A;
            while(cv > 0) {
                A.pb(cv);
                cv = (cv - 1) / 2;
            }
            A.pb(0);
            vector<pair<int, int>> wv;
            rep(j, (1 << A.size())) {
                int tv = 0;
                int tw = 0;
                rep(k, A.size()) {
                    if((j >> k) & 1) {
                        tw += W[A[k]];
                        tv += V[A[k]];
                    }
                }
                wv.pb(mp(tw, tv));
            }
            sort(all(wv));
            vector<pair<int, int>> wvv;
            pair<int, int> cur = mp(-1, -1);
            rep(j, wv.size()) {
                if(cur.first == wv[j].first && cur.second < wv[j].second) {
                    wvv[wvv.size() - 1] = wv[j];
                    cur = wv[j];
                }
                else if(cur.second < wv[j].second) {
                    wvv.pb(wv[j]);
                    cur = wv[j];
                }
            }
            top[i] = wvv;
        }
        
        rep(i, Q) {
            if(vtx[i] < (1 << H_DEPTH)) {
                vector<int> A;
                while(vtx[i] > 0) {
                    A.pb(vtx[i]);
                    vtx[i] = (vtx[i] - 1) / 2;
                }
                A.pb(0);
                int ans = 0;
                rep(j, (1 << A.size())) {
                    int tv = 0;
                    int tw = 0;
                    rep(k, A.size()) {
                        if((j >> k) & 1) {
                            tw += W[A[k]];
                            tv += V[A[k]];
                        }
                    }
                    if(tw <= wgt[i]) ans = max(ans, tv);
                }
                output(ans);
            }
            else {
                vector<int> A;
                while(vtx[i] >= (1 << H_DEPTH)) {
                    A.pb(vtx[i]);
                    vtx[i] = (vtx[i] - 1) / 2;
                }
                int ans = 0;
                rep(j, (1 << A.size())) {
                    int tv = 0;
                    int tw = 0;
                    rep(k, A.size()) {
                        if((j >> k) & 1) {
                            tw += W[A[k]];
                            tv += V[A[k]];
                        }
                    }
                    if(tw > wgt[i]) continue;
                    
                    int l = 0, r = (int)top[vtx[i]].size(); // ok, ng
                    while(r - l > 1) {
                        int m = (l + r) / 2;
                        if(top[vtx[i]][m].first + tw <= wgt[i]) l = m;
                        else r = m;
                    }
                    ans = max(ans, top[vtx[i]][l].second + tv);
                }
                output(ans);
            }
        }
    }
    
    
    
    return 0;
}


Measured memory_bytes: 11424