#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <stack>
#include <cassert>
#include <map>
#include <numeric>
#include <cstring>
#include <set>
#include <ctime>
#include <queue>
#include <cmath>
#include <iomanip>
#include <iterator>

using namespace std;

clock_t timeStart, timeFinish;

void timeBegin() {
    timeStart = clock();
}

void timeEnd() {
    timeFinish = clock();
}

void timeDuration() {
    double time_taken = double(timeFinish - timeStart) / double(CLOCKS_PER_SEC);
    cout << "Time taken by program is : " << fixed << time_taken << setprecision(5);
    cout << " sec " << endl;
}

class InputReader {
public:
    InputReader() {
        input_file = stdin;
        cursor = 0;
        fread(buffer, SIZE, 1, input_file);
    }
    InputReader(const char *file_name) {
        input_file = fopen(file_name, "r");
        cursor = 0;
        fread(buffer, SIZE, 1, input_file);
    }
    inline InputReader &operator >>(int &n) {
        while((buffer[cursor] < '0' || buffer[cursor] > '9') && buffer[cursor] != '-') {
            advance();
        }
        int sign = 1;
        if (buffer[cursor] == '-') {
            sign = -1;
            advance();
        }
        n = 0;
        while('0' <= buffer[cursor] && buffer[cursor] <= '9') {
            n = n * 10 + buffer[cursor] - '0';
            advance();
        }
        n *= sign;
        return *this;
    }
private:
    FILE *input_file;
    static const int SIZE = 1 << 17;
    int cursor;
    char buffer[SIZE];
    inline void advance() {
        ++ cursor;
        if(cursor == SIZE) {
            cursor = 0;
            fread(buffer, SIZE, 1, input_file);
        }
    }
};

const int MAXDEPTH = 12;
const int MAXLOG = 18;
const int MAXN = 1 << 18;

int v[1 + MAXN], w[1 + MAXN];
int a[1 + MAXLOG], b[1 + MAXLOG];
vector<pair<int, int> > sums[1 << MAXDEPTH];
int bit[1 + MAXN];

void precompute(int n) {
    for (int i = 0; (1 << i) <= n; i++) {
        bit[1 << i] = i;
    }
    sums[1].emplace_back(make_pair(0, 0));
    sums[1].emplace_back(make_pair(w[1], v[1]));
    for (int i = 2; i <= n && i < (1 << MAXDEPTH); i++) {
        int p = 0, q = 0;
        while (p < sums[i / 2].size() || q < sums[i / 2].size()) {
            if (p < sums[i / 2].size() && (q == sums[i / 2].size() || sums[i / 2][p].first <= sums[i / 2][q].first + w[i])) {
                sums[i].emplace_back(sums[i / 2][p]);
                p++;
            } else {
                sums[i].emplace_back(make_pair(sums[i / 2][q].first + w[i], sums[i / 2][q].second + v[i]));
                q++;
            }
        }
        for (int j = 1; j < sums[i].size(); j++) {
            sums[i][j].second = max(sums[i][j].second, sums[i][j - 1].second);
        }
    }
}

int search(int node, int limit) {
    int left = 0, right = sums[node].size(), answer = 0;
    while (left <= right) {
        int middle = (left + right) / 2;
        if (sums[node][middle].first <= limit) {
            answer = max(answer, sums[node][middle].second);
            left = middle + 1;
        } else {
            right = middle - 1;
        }
    }
    return answer;
}

pair<int, int> sum[1 + MAXN];

int compute(int node, int limit) {
    if (node < (1 << MAXDEPTH)) {
        return search(node, limit);
    }
    vector<pair<int, int> > p;
    while (node >= (1 << MAXDEPTH)) {
        p.emplace_back(make_pair(w[node], v[node]));
        node /= 2;
    }
    int answer = search(node, limit);
    sum[0] = make_pair(0, 0);
    for (int i = 1; i < (1 << p.size()); i++) {
        if (i == 61) {
            i++;
            i--;
        }
        int j = i - (i & -i);
        if (j == 0) {
            sum[i] = p[bit[i]];
        } else {
            sum[i] = make_pair(sum[j].first + sum[i - j].first, sum[j].second + sum[i - j].second);
        }
        if (sum[i].first <= limit) {
            answer = max(answer, sum[i].second + search(node, limit - sum[i].first));
        }
    }
    return answer;
}

int main() {
    timeBegin();
    InputReader cin;
    //ofstream cout("output.txt");
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> v[i] >> w[i];
    }
    precompute(n);
    int q;
    cin >> q;
    for (int i = 1; i <= q; i++) {
        int node, limit;
        cin >> node >> limit;
        cout << compute(node, limit) << "\n";
    }
    //timeDuration();
    return 0;
}






Measured memory_bytes: 93180