#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

const int MOD = 1'000'000'007;
const int INF = 1'000'000'000; //1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10;

template<class T, class S> inline bool chmax(T &a, const S &b){
    if(a < b){
        a = b; return true;
    }
    return false;
}
template<class T, class S> inline bool chmin(T &a, const S &b){
    if(b < a){
        a = b; return true;
    }
    return false;
}







inline int grayCode(int i){
    return i ^ (i >> 1);
}

inline int invGrayCode(int j){
    for(int k(0); k < 5; ++k){// 2pow(5) = 32
        j = j ^ (j >> (1<<k));
    }
    return j;
}

std::unordered_map<int, int> twoPow{{1, 0}, {2, 1}, {4, 2}, {8, 3}, {16, 4}, {32, 5}, {64, 6}, {128, 7}, {256, 8}, {512, 9}, {1024, 10}, {2048, 11}, {4096, 12}, {8192, 13}, {16384, 14}, {32768, 15}, {65536, 16}, {131072, 17}, {262144, 18}, {524288, 19}};

int main(){
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int N; std::cin >> N;
    std::vector<int> V(N+1), W(N+1);
    for(int i(1); i <= N; ++i) std::cin >> V[i] >> W[i];
    int n(0);
    for(; n <= 18; ++n){
        if((1 << n) > N) break;
    }
    int Q, mxL; std::cin >> Q;
    std::vector<int> v(Q), L(Q);
    for(int i(0); i < Q; ++i){
        std::cin >> v[i] >> L[i];
        chmax(mxL, L[i]);
    }
    int n2(n / 2);
    std::vector<std::vector<int>> dp((1 << n2), std::vector<int>(mxL+1, 0));
    for(int i(1), i_len(1 << n2); i < i_len; ++i){
        for(int j(1); j <= mxL; ++j){
            dp[i][j] = dp[i/2][j];
            if(j >= W[i]) chmax(dp[i][j], dp[i/2][j-W[i]] + V[i]);
        }
    }
    for(int qq(0); qq < Q; ++qq){
        if(v[qq] < (1 << n2)){
            std::cout << dp[v[qq]][L[qq]] << "\n";
            continue;
        }
        int vx(v[qq]), n1(0);
        {
            int a(1 << n2);
            while(vx >= a){
                ++n1; vx >>= 1;
            }
        }
        int sw(0), sv(0), prvj(0), ans(0);
        if(sw <= L[qq]){
            ans = dp[vx][L[qq]];
        }
        for(int i(1), i_len(1 << n1); i < i_len; ++i){
            int j(i ^ (i >> 1));
            int changebit(j ^ prvj);
            int a(v[qq] >> (twoPow[changebit]));
            if(j & changebit){
                sw += W[a];
                sv += V[a];
            }else{
                sw -= W[a];
                sv -= V[a];
            }
            if(sw <= L[qq]){
                chmax(ans, dp[vx][L[qq]-sw] + sv);
            }
            prvj = j;
        }
        std::cout << ans << "\n";
    }
    return 0;
}


Measured memory_bytes: 207280