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

using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();

template <typename T1, typename T2>
bool chmin(T1& a, const T2& b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <typename T1, typename T2>
bool chmax(T1& a, const T2& b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

class Prints {
private:
    class __Prints {
    public:
        __Prints(const char* sep, const char* term) : sep(sep), term(term) {}
        template <class... Args>
        void operator()(const Args&... args) const { print(args...); }

    private:
        const char *sep, *term;
        void print() const { std::cout << term; }
        void print_rest() const { std::cout << term; }
        template <class T, class... Tail>
        void print(const T& head, const Tail&... tail) const { std::cout << head, print_rest(tail...); }
        template <class T, class... Tail>
        void print_rest(const T& head, const Tail&... tail) const { std::cout << sep << head, print_rest(tail...); }
    };

public:
    Prints() {}
    __Prints operator()(const char* sep = " ", const char* term = "\n") const { return __Prints(sep, term); }
};

Prints prints;

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

constexpr int MX_W = 100000;
int dp1[100010][600];

void solve() {
    int N;
    cin >> N;
    Vi V(N + 1), W(N + 1);
    for (int i = 1; i <= N; i++) {
        cin >> V[i] >> W[i];
    }

    int th;
    for (int i = 0; i < N; i++) {
        if (i * i >= N) {
            th = i;
            break;
        }
    }

    for (int i = 1; i <= th; i++) {
        for (int j = 0; j <= MX_W; j++) {
            chmax(dp1[j][i], dp1[j][i >> 1]);
            if (j - W[i] >= 0)
                chmax(dp1[j][i], dp1[j - W[i]][i >> 1] + V[i]);
        }
    }

    int Q;
    cin >> Q;
    vector<Pii> d;
    while (Q--) {
        int v, L;
        cin >> v >> L;
        d.clear();
        int idx = v;
        while (idx > th) {
            d.emplace_back(V[idx], W[idx]);
            idx >>= 1;
        }
        int res = 0;
        int sz = (int)d.size();
        for (int mask = 0; mask < (1 << sz); mask++) {
            int tv = 0, tw = 0;
            for (int i = 0; i < sz; i++) {
                if (mask >> i & 1) {
                    tv += d[i].first;
                    tw += d[i].second;
                }
            }
            if (tw > L) continue;
            chmax(res, tv + dp1[L - tw][idx]);
        }
        prints()(res);
    }
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

int main() {
#ifdef FASTIO
    cin.tie(nullptr), cout.tie(nullptr);
    ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
    ifstream ifs("./in_out/input.txt");
    cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
    ofstream ofs("./in_out/output.txt");
    cout.rdbuf(ofs.rdbuf());
#endif
    cout << setprecision(18) << fixed;
    solve();
    cout << flush;
    return 0;
}


Measured memory_bytes: 239772