const int isDebugMode = 0;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<long long, long long> P;
struct edge{long long to,cost;};
const int inf = 1 << 27;
const long long INF = 1LL << 60;
const int COMBMAX = 1001001;
const long long MOD = 1000000007;
const long long dy[] = {-1, 0, 0, 1};
const long long dx[] = {0, -1, 1, 0};
string abc = "abcdefghijklmnopqrstuvwxyz";
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define eachdo(v, e) for (const auto &e : (v))
#define all(v) (v).begin(), (v).end()
#define lower_index(v, e) (long long)distance((v).begin(), lower_bound((v).begin(), (v).end(), e))
#define upper_index(v, e) (long long)distance((v).begin(), upper_bound((v).begin(), (v).end(), e))
long long mpow(long long a, long long n, long long mod = MOD){long long res = 1; while(n > 0){if(n & 1)res = res * a % mod; a = a * a % mod; n >>= 1;} return res;}
void pt(){cout << endl; return;}
template<class Head> void pt(Head&& head){cout << head; pt(); return;}
template<class Head, class... Tail> void pt(Head&& head, Tail&&... tail){cout << head << " "; pt(forward<Tail>(tail)...);}
void dpt(){if(!isDebugMode) return; cout << endl; return;}
template<class Head> void dpt(Head&& head){if(!isDebugMode) return; cout << head; pt(); return;}
template<class Head, class... Tail> void dpt(Head&& head, Tail&&... tail){if(!isDebugMode) return; cout << head << " "; pt(forward<Tail>(tail)...);}
template<class T> void debug(T v){if(!isDebugMode) return; rep(i, v.size()) cout << v[i] << " " ; cout << endl;}
template<class T> void debug2(T v){if(!isDebugMode) return; rep(i, v.size()){rep(j, v[i].size()) cout << v[i][j] << " " ; cout << endl;}}
template<class T1, class T2> inline bool chmin(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2> inline bool chmax(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2> long long gcd(T1 a, T2 b){return a % b ? gcd(b , a % b) : b;}
template<class T1, class T2> void reduce(T1 &a, T2 &b){ll g = gcd(a, b); a /= g; b /= g; return;}
bool valid(long long H, long long W, long long h, long long w) { return 0 <= h && h < H && 0 <= w && w < W; }

const int MAX_DEPTH = 11;   
const int MAX_L = 100100;

int main(){
    int N; cin >> N;
    vector<int> val(N), wet(N);
    rep(i, N) cin >> val[i] >> wet[i];
    int restore = min(N, 1 << MAX_DEPTH);
    dpt(restore);
    vector<vector<int>> dp(restore, vector<int>(MAX_L + 1, 0));
    // case i = 0
    rep(j, MAX_L + 1){
        if(wet[0] <= j) dp[0][j] = val[0];
    }
    for(int i = 1; i < restore; i++){
        rep(j, MAX_L + 1){
            if(j > 0) dp[i][j] = dp[i][j - 1];
            chmax(dp[i][j], dp[(i - 1) >> 1][j]);
            if(0 <= j - wet[i]) chmax(dp[i][j], dp[(i - 1) >> 1][j - wet[i]] + val[i]);
        }
    }
    int Q; cin >> Q;
    vector<pair<int, int>> queries(Q);
    vector<int> anss;
    rep(i, Q) cin >> queries[i].first >> queries[i].second;

    for(const auto &query : queries){
        int node = query.first - 1;
        int L = query.second;
        if(node < restore){
            anss.push_back(dp[node][L]);
            continue;
        }
        int ans = 0;
        vector<pair<int, int>> list;
        while(restore <= node){
            list.push_back(make_pair(wet[node], val[node]));
            node = (node - 1) >> 1;
        }
        int listSize = list.size();
        int listcom = 1 << listSize;
        rep(i, listcom){
            int com_wet = 0;
            int com_val = 0;
            rep(j, listSize){
                if(i >> j & 1){
                    com_wet += list[j].first;
                    com_val += list[j].second;
                }
            }
            if(L < com_wet) continue;
            chmax(ans, com_val + dp[node][L - com_wet]);
        }
        anss.push_back(ans);
    }
    rep(i, Q) pt(anss[i]);
    return 0;
}

Measured memory_bytes: 809900