#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<pair<int, int> > vpii;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<ll> vll;
#define INF 0x3f3f3f3f
#define MOD 998244353LL
#define EPS 0.00001
#define f first
#define s second
#define pb push_back
#define mp make_pair

#define FOR(i, a, b) for (int i=(a); i<=(signed)(b); i++)
#define F0R(i, a) for (int i=0; i<(signed)(a); i++)
#define RFOR(i, a, b) for (int i=(a); i >= b; i--)

#define MN 262150
#define LAY 9
int n;
int v[MN], w[MN];
int q;
int ans[260][100005];

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    cin >> n;
    FOR(i, 1, n) cin >> v[i] >> w[i];
    if(n>=(1<<LAY)){
        FOR(i, 1<<(LAY-1), (1<<LAY)-1){
            //cout << i << ":\n";
            int loc = i-(1<<(LAY-1));
            vpii res;
            int x = i;
            while(x != 0){
                res.pb({v[x], w[x]});
                x /= 2;
            }
            F0R(bm, 1<<LAY){
                int cst=0, val=0;
                F0R(j, LAY){
                    if((1<<j)&bm){
                        cst += res[j].s; val += res[j].f;
                    }
                }
                //cout << "got " << cst << " " << val << "\n";
                if(cst <= 100000) ans[loc][cst] = max(ans[loc][cst], val);
            }
            FOR(j, 1, 100000){
                ans[loc][j] = max(ans[loc][j], ans[loc][j-1]);
            }
        }
    }
    cin >> q;
    F0R(_, q){
        int cv, ml;
        cin >> cv >> ml;
        if(cv < (1<<LAY)){
            vpii res;
            int x = cv;
            while(x != 0){
                res.pb({v[x], w[x]});
                x /= 2;
            }
            int fin = 0;
            int cst=0, val=0;
            F0R(bm, 1<<(res.size())){
                //cout << "trying " << bm << " ";
                //cout << cst << " " << val << "\n";
                if(cst<=ml){
                    fin = max(fin, val);
                }
                F0R(j, res.size()){
                    if((1<<j)&(bm+1)){
                        cst += res[j].s; val += res[j].f;
                        break;
                    } else{
                        cst -= res[j].s; val -= res[j].f;
                    }
                }
            }
            cout << fin << "\n";
        } else{
            vpii res;
            int x = cv;
            while(x >= (1<<LAY)){
                res.pb({v[x], w[x]});
                x /= 2;
            }
            //cout << cv << " to " << x << ":\n";
            int fin = 0;
            int cst=0, val=0;
            F0R(bm, 1<<(res.size())){
                //cout << "trying " << cst << " " << val << "\n";
                if(cst <= ml){
                    int loc = x-(1<<(LAY-1));
                    int ot = ans[loc][ml-cst];
                    //cout << loc << " " << ml-cst << " " << ot << "\n";
                    fin = max(fin, val+ot);
                }
                F0R(j, res.size()){
                    if((1<<j)&(bm+1)){
                        cst += res[j].s; val += res[j].f;
                        break;
                    } else{
                        cst -= res[j].s; val -= res[j].f;
                    }
                }
            }
            cout << fin << "\n";
        }
    }

    return 0;
}


Measured memory_bytes: 105712