#include <bits/stdc++.h>
using namespace std;
using lint     = long long;
const lint inf = 1LL << 60;
const lint mod = 1000000007;

template <class T>
bool chmax(T &a, const T &b) {
    return (a < b) ? (a = b, 1) : 0;
}
template <class T>
bool chmin(T &a, const T &b) {
    return (b < a) ? (a = b, 1) : 0;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    lint n;
    cin >> n;
    vector<lint> V(n + 1), W(n + 1);
    for (int i = 1; i < n + 1; ++i) {
        cin >> V[i] >> W[i];
    }
    lint q;
    cin >> q;
    vector<lint> v(q + 1), l(q + 1);
    vector<bool> calc(1000000, false);
    lint up = 1 << 10;
    vector<vector<pair<int, pair<int, int>>>> qs(1000000);
    for (int i = 0; i < q; ++i) {
        cin >> v[i] >> l[i];
        int c = v[i];
        // ls[c]      = {i, l[i]};
        bool added = false;
        if (c < (up)) {
            qs[c].push_back({i, {v[i], l[i]}});
            added = true;
        }
        while (c != 0) {
            if (!added && (c < (up))) {
                qs[c].push_back({i, {v[i], l[i]}});
                added = true;
            }
            if (c < (up)) {
                calc[c] = true;
            }
            c /= 2;
        }
    }
    // 1 - indexed!!!!!!

    vector<lint> ret(q, 0);
    function<void(int, map<lint, lint> &)> dfs = [&](int c, map<lint, lint> &dp) {
        map<lint, lint> to;
        to[0] = 0;
        for (auto &d : dp) {
            chmax(to[d.first], d.second);
            if (d.first + W[c] <= 100000)
                chmax(to[d.first + W[c]], d.second + V[c]);
        }
        if (qs[c].size() != 0) {
            lint mx             = 0;
            map<lint, lint> tod = to;
            tod[0]              = 0;
            for (auto it = tod.begin(); it != tod.end(); it = next(it)) {
                chmax(it->second, mx);
                chmax(mx, it->second);
            }
            for (auto &p : qs[c]) {
                vector<lint> vv, ww;
                lint curr = p.second.first;
                while (curr > c) {
                    vv.push_back(V[curr]);
                    ww.push_back(W[curr]);
                    curr /= 2;
                }
                int k  = vv.size();
                lint r = 0;
                for (int j = 0; j < 1 << k; ++j) {
                    lint vs = 0;
                    lint ws = 0;
                    for (int x = 0; x < k; ++x) {
                        if (j >> x & 1) {
                            vs += vv[x];
                            ws += ww[x];
                        }
                    }
                    if (ws <= p.second.second) {
                        auto it = tod.upper_bound(p.second.second - ws);
                        if (it == tod.begin())
                            continue;
                        lint mx = prev(it)->second;
                        chmax(r, vs + mx);
                    }
                }
                ret[p.first] = r;
            }
        }

        for (auto &v : {c * 2, c * 2 + 1}) {
            if ((v > n) || (v >= (up)) || !calc[v]) {
                continue;
            }
            dfs(v, to);
        }
    };
    map<lint, lint> vv;
    vv[0] = 0;
    dfs(1, vv);

    for (int i = 0; i < q; ++i) {
        cout << ret[i] << "\n";
    }
    return 0;
}

Measured memory_bytes: 35156