#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <memory>
#include <complex>
#include <numeric>
#include <cstdio>
#include <iomanip>
#include <random>

#define REP(i,m,n) for(int i=int(m);i<int(n);i++)
#define RREP(i,m,n) for(int i=int(n)-1;i>=int(m);--i)
#define EACH(i,c) for (auto &(i): c)
#define all(c) begin(c),end(c)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort(begin(c),end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
//#define int long long

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x)  cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s) do{}while(0)
#define dump(x) do{}while(0)
#define BR 
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int,int>;
using VP = vector<PII>;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

void solve() {
    LL n,q;
    cin >> n;
    VLL v(n), w(n);
    REP(i,0,n) cin >> v[i] >> w[i];
    cin >> q;

    VVLL as(512,VLL(100000+1));
    REP(k,0,512) {
        int u = k + 511;
        VI ps;
        while (u > 0) {
            ps.push_back(u);
            u = (u-1)/2;
        }
        ps.push_back(0);
        int len = ps.size();
        REP(p,0,1<<len) {
            LL sumw = 0, sumv = 0;
            REP(j,0,len) {
                if (!(p & (1<<j))) continue;
                sumw += w[ps[j]];
                sumv += v[ps[j]];
            }
            if (sumw > 100000) continue;
            chmax(as[k][sumw], sumv);
        }
        REP(i,1,100000+1) chmax(as[k][i], as[k][i-1]);
    }

    int u,l;
    REP(z,0,q) {
        cin >> u >> l;
        --u;
        LL ans = 0;
        if (u < 1023) {
            VI ps;
            while (u > 0) {
                ps.push_back(u);
                u = (u-1)/2;
            }
            ps.push_back(0);
            int len = ps.size();
            VLL minv(l+1,0);
            REP(p,0,1<<(len-len/2)) {
                LL sumw = 0, sumv = 0;
                REP(j,0,len-len/2) {
                    if (!(p & (1<<j))) continue;
                    sumw += w[ps[j+len/2]];
                    sumv += v[ps[j+len/2]];
                }
                if (sumw > l) continue;
                chmax(minv[sumw], sumv);
            }
            REP(i,1,l+1) chmax(minv[i], minv[i-1]);

            // REP(i,0,l+1) cout << minv[i] << " ";
            // cout << endl;

            REP(p,0,1<<(len/2)) {
                LL sumw = 0, sumv = 0;
                REP(j,0,len/2) {
                    if (!(p & (1<<j))) continue;
                    sumw += w[ps[j]];
                    sumv += v[ps[j]];
                }
                if (sumw > l) continue;
                chmax(ans, sumv + minv[l-sumw]);
            }
        } else {
            VI ps;
            while (u > 1022) {
                ps.push_back(u);
                u = (u-1)/2;
            }
            int len = ps.size();
            REP(p,0,1<<(len)) {
                LL sumw = 0, sumv = 0;
                REP(j,0,len) {
                    if (!(p & (1<<j))) continue;
                    sumw += w[ps[j]];
                    sumv += v[ps[j]];
                }
                if (sumw > l) continue;
                chmax(ans, sumv + as[u-511][l-sumw]);
            }
        }
        cout << ans << endl;
    }
}

signed main() {
    solve();
    
    return 0;
}

Measured memory_bytes: 409556