// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")
 
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <algorithm>
#include <cassert>
#include <unordered_set>
#include <unordered_map>
#include <set>
#include <map>
#include <queue>
#include <random>
#include <numeric>
#include <time.h>
#include <stack>
#include <bitset>

#define prev asasddlsa
#define rank aljds

#define rep(i, l, r) for (int i = l; i < r; i++)
#define repb(i, r, l) for (int i = r; i >= l; i++)
 
using namespace std;
 
typedef long long ll;
typedef double dbl;
 
template<typename T>
void print(const vector<T>& s){
    for (auto x : s)
        cout << x << " ";
    cout << endl;
}
 
template<class T>   
void print(const T* s, int n){
    for (int i = 0; i < n; i++)
        cout << s[i] << ' ';
    cout << endl;
}

template<class T>
void print(vector<vector<T>> s){
    for (int i = 0; i < s.size(); i++)
        print(s[i]);
}

const int maxw = 1e5 + 7, maxn = (1 << 18) + 7;
const int lower = 11;

int dp[1 << lower][maxw];

int v[maxn], w[maxn];

int solve(){
    int n;
    cin >> n;
    // n = 1 + rand() % 10;
    for (int i = 0; i < n; i++){
        int val, weight;
        cin >> val >> weight;
        // val = rand() % 10, weight = rand() % 10;
        if (i < (1 << lower)){
            for (int j = 1; j < maxw; j++){
                int p = (i - 1) / 2;
                dp[i][j] = dp[i][j - 1];
                if (i > 0){
                    dp[i][j] = max(dp[i][j], dp[p][j]);
                    if (j >= weight)
                        dp[i][j] = max(dp[i][j], dp[p][j - weight] + val);
                }
                else if (j >= weight)
                    dp[i][j] = max(dp[i][j], val);
            }
        }
        v[i] = val, w[i] = weight;
    }
    int q;
    cin >> q;
    while (q--){
        int x, l;
        cin >> x >> l;
        // x = rand() % n + 1, l = rand() % 10;
        vector<int> anc;
        int cur = x - 1;
        while (cur >= (1 << lower)){
            anc.push_back(cur);
            cur = (cur - 1) / 2;
        }
        int answer = 0;
        for (int mask = 0; mask < (1 << anc.size()); mask++){
            int sumv = 0, sumw = 0;
            for (int j = 0; j < anc.size(); j++){
                if (mask & (1 << j))
                    sumw += w[anc[j]], sumv += v[anc[j]];
            }
            if (sumw <= l){
                sumv += dp[cur][l - sumw];
                answer = max(answer, sumv);
            }
        }

        // int correct = 0;
        // while (cur >= 0){
        //     anc.push_back(cur);
        //     cur = cur == 0 ? -1 : (cur - 1) / 2;
        // }
        // for (int mask = 0; mask < (1 << anc.size()); mask++){
        //     int sumv = 0, sumw = 0;
        //     for (int j = 0; j < anc.size(); j++){
        //         if (mask & (1 << j))
        //             sumw += w[anc[j]], sumv += v[anc[j]];
        //     }
        //     if (sumw <= l)
        //         correct = max(correct, sumv);
        // }
        // if (correct != answer){
        //     cout << n << endl;
        //     for (int i = 0; i < n; i++)
        //         cout << v[i] << ' ' << w[i] << endl;
        //     cout << x << ' ' << l << endl;
        // }
        // cout << correct << ' ' << answer << endl;
        // assert(correct == answer);

        cout << answer << '\n';
    }
    return 0;
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.precision(10);
    cout << fixed; 
    int t = 1;
    // cin >> t;
    for (int i = 0; i < t; i++){
        // cout << "Case #" << i + 1 << ": ";
        solve();
    }
    return 0;
}


Measured memory_bytes: 805756