// Template
#include <bits/stdc++.h>
#define rep_override(x, y, z, name, ...) name
#define rep2(i, n) for (int i = 0; i < (n); ++i)
#define rep3(i, l, r) for (int i = (l); i < (r); ++i)
#define rep(...) rep_override(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
using namespace std;
using ll = long long;
constexpr int inf = 1001001001;
constexpr ll INF = 3003003003003003003;
template <typename T> inline bool chmin(T& x, const T& y) {if (x > y) {x = y; return 1;} return 0;}
template <typename T> inline bool chmax(T& x, const T& y) {if (x < y) {x = y; return 1;} return 0;}
struct IOSET {IOSET() {cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(10);}} ioset;

// ---------------------------------------------------------------------
using pii = pair<int, int>;

vector<pii> mymerge(vector<pii> x, vector<pii> y) {
    vector<pii> ans;
    auto iterx = x.begin(), itery = y.begin();
    while (iterx != x.end() || itery != y.end()) {
        if (iterx == x.end()) {
            ans.push_back(*itery);
            ++itery;
            continue;
        }
        if (itery == y.end()) {
            ans.push_back(*iterx);
            ++iterx;
            continue;
        }
        if (*iterx < *itery) {
            ans.push_back(*iterx);
            ++iterx;
        } else {
            ans.push_back(*itery);
            ++itery;
        }
    }
    return ans;
}

int main() {
    int n;
    cin >> n;
    vector<int> v(n + 1, 0), w(n + 1, 0);
    rep(i, n) cin >> v[i + 1] >> w[i + 1];
    
    vector<int> h(n + 1, 0);
    rep(i, 2, n + 1) h[i] = h[i / 2] + 1;
    
    int k = max(1 << (h[n] / 2), 1);
    vector<vector<pii>> vec(k);
    vec[0] = {pii(0, 0)};
    rep(i, 1, k) {
        vector<pii> vec_ = vec[i / 2];
        for (pii &p: vec_) {
            p.first += w[i];
            p.second += v[i];
        }
        vec[i] = mymerge(vec[i / 2], vec_);
    }
    rep(i, 1, k) rep(j, 1, sz(vec[i])) chmax(vec[i][j].second, vec[i][j - 1].second);
    
    int q;
    cin >> q;
    rep(_, q) {
        int u, l;
        cin >> u >> l;
        if (u < k) {
            int ans = 0;
            for (pii p: vec[u]) {
                if (p.first <= l) chmax(ans, p.second);
            }
            cout << ans << "\n";
            continue;
        }
        
        vector<int> vs;
        while (u >= k) {
            vs.push_back(u);
            u /= 2;
        }
        reverse(all(vs));
        
        vector<vector<pii>> vec2(sz(vs) + 1);
        vec2[0] = {pii(0, 0)};
        rep(i, 1, sz(vs) + 1) {
            vector<pii> vec2_ = vec2[i - 1];
            for (pii &p: vec2_) {
                p.first += w[vs[i - 1]];
                p.second += v[vs[i - 1]];
            }
            vec2[i] = mymerge(vec2[i - 1], vec2_);
        }
        rep(i, 1, sz(vec2[sz(vs)])) chmax(vec2[sz(vs)][i].second, vec2[sz(vs)][i - 1].second);
        
        int now = sz(vec2[sz(vs)]) - 1;
        int ans = 0;
        rep(i, sz(vec[u])) {
            while (now >= 0) {
                if (vec[u][i].first + vec2[sz(vs)][now].first <= l) {
                    chmax(ans, vec[u][i].second + vec2[sz(vs)][now].second);
                    break;
                }
                --now;
            }
        }
        cout << ans << "\n";
    }
    return 0;
}


Measured memory_bytes: 6536