#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
    int N;
    cin >> N;
    vector<ll> V(N+1), W(N+1);
    int umax = (1<<12);
    
    for (int i = 1; i <= N; i++) cin >> V[i] >> W[i];
    vector<vector<pair<ll, ll>>> dp(N+1);
    dp[0].push_back({0, 0});
    for (int i = 1; i <= min(N, umax-1); i++) {
        vector<pair<ll, ll>> A;
        for (auto p : dp[i/2]) {
            if (p.first + W[i] <= 100000) A.push_back({p.first + W[i], p.second + V[i]});
        }
        int s1 = dp[i/2].size(), s2 = A.size();
        int pos1 = 0, pos2 = 0;
        while (pos1 < s1 || pos2 < s2) {
            if (pos1 == s1) {
                dp[i].push_back(A[pos2]);
                pos2++;
            }else if (pos2 == s2) {
                dp[i].push_back(dp[i/2][pos1]);
                pos1++;
            }else {
                if (dp[i/2][pos1].first < A[pos2].first) {
                    dp[i].push_back(dp[i/2][pos1]);
                    pos1++;
                }else if (dp[i/2][pos1].first > A[pos2].first){
                    dp[i].push_back(A[pos2]);
                    pos2++;
                }else {
                    if (dp[i/2][pos1].second >= A[pos2].second) {
                        dp[i].push_back(dp[i/2][pos1]);
                    }else {
                        dp[i].push_back(A[pos2]);
                    }
                    pos1++;
                    pos2++;
                }
            }
        }
        
        for (int j = 1; j < (int)dp[i].size(); j++) {
            dp[i][j].second = max(dp[i][j].second, dp[i][j-1].second);
        }
    }
    
    int Q;
    cin >> Q;
    for (int z = 0; z < Q; z++) {
        int v, L;
        cin >> v >> L;
        if (v <= umax-1) {
            pair<ll, ll> p = {L, 0};
            auto it = lower_bound(dp[v].begin(), dp[v].end(), p);
            if (it == dp[v].end() || (*it).first > L) it--;
            cout << (*it).second << "\n";
        }
        else {
            int t = v;
            vector<pair<ll, ll>> hdp = {{0, 0}};
            while (t >= umax) {
                vector<pair<ll, ll>> A, ndp;
                for (auto p : hdp) {
                    if (p.first + W[t] <= 100000) A.push_back({p.first + W[t], p.second + V[t]});
                }
                int s1 = hdp.size(), s2 = A.size();
                int pos1 = 0, pos2 = 0;
                while (pos1 < s1 || pos2 < s2) {
                    if (pos1 == s1) {
                        ndp.push_back(A[pos2]);
                        pos2++;
                    }else if (pos2 == s2) {
                        ndp.push_back(hdp[pos1]);
                        pos1++;
                    }else {
                        if (hdp[pos1].first < A[pos2].first) {
                            ndp.push_back(hdp[pos1]);
                            pos1++;
                        }else if (hdp[pos1].first > A[pos2].first){
                            ndp.push_back(A[pos2]);
                            pos2++;
                        }else {
                            if (hdp[pos1].second >= A[pos2].second) {
                                ndp.push_back(hdp[pos1]);
                            }else {
                                ndp.push_back(A[pos2]);
                            }
                            pos1++;
                            pos2++;
                        }
                    }
                }
                swap(hdp, ndp);
                t /= 2;
            }
            int pos3 = dp[t].size()-1;
            ll ans = 0;
            for (int i = 0; i < (int)hdp.size(); i++) {
                if (hdp[i].first > L) break;
                while (pos3 >= 1 && dp[t][pos3].first + hdp[i].first > L) pos3--;
                ans = max(ans, dp[t][pos3].second + hdp[i].second);
            }
            cout << ans << "\n";
        }
    }
    
    
    return 0;
}

Measured memory_bytes: 187212