#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i,n) for (auto& i: n)

template<class T> inline bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> inline bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
void init() {cin.tie(0);ios::sync_with_stdio(false);cout << fixed << setprecision(15);}

#ifdef DEBUG
template <class T, class N> void verr(const T& a, const N& n) { rep(i, n) cerr << a[i] << " "; cerr << "\n" << flush; }
ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << "\n" << flush; }
template<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?" ":"\n") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); }
#endif

const ll INF = 5e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//
ll V[1 << 18], W[1 << 18];
int main() {
    init();
    ll N;
    cin >> N;
    rep(i, N) cin >> V[i] >> W[i];

    ll Q;
    cin >> Q;
    pair<ll, ll> m1[1 << 10], m2[1 << 10], C[1 << 10];
    ll vv[20], ww[20];

    rep(_, Q){
        ll v, L;
        cin >> v >> L;

        ll vi = 0;
        while(v){
            vv[vi]=V[v - 1], ww[vi] = W[v - 1];
            v /= 2, vi++;
        }

        ll s1 = vi / 2, s2 = (vi + 1) / 2;
        // 1
        m1[0] = m2[0] = {0, 0};
        rep(i,s1){
            ll tw = ww[i], tv = vv[i], ti = 0, ci = 0;
            rep(j, 1<<i){
                while(ti < 1<<i and m1[ti].first + tw < m1[j].first){
                    C[ci] = {m1[ti].first + tw, m1[ti].second + tv};
                    ci++, ti++;
                }
                C[ci] = {m1[j].first, m1[j].second};
                ci++;
            }
            while(ti < 1<<i){
                C[ci] = {m1[ti].first + tw, m1[ti].second + tv};
                ci++, ti++;
            }
            rep(j, 1 << i + 1) m1[j] = C[j];
        }

        // 2
        rep(i,s2){
            ll tw = ww[s1 + i], tv = vv[s1 + i], ti = 0, ci = 0;
            rep(j, 1<<i){
                while(ti < 1<<i and m2[ti].first + tw < m2[j].first){
                    C[ci] = {m2[ti].first + tw, m2[ti].second + tv};
                    ci++, ti++;
                }
                C[ci] = {m2[j].first, m2[j].second};
                ci++;
            }
            while(ti < 1<<i){
                C[ci] = {m2[ti].first + tw, m2[ti].second + tv};
                ci++, ti++;
            }
            rep(j, 1 << i + 1) m2[j] = C[j];
        }

        m2[1 << s2] = {INF, INF};
        ll t = 0;
        rep(i, 1<<s1){
            if (!chmax(t, m1[i].second))
                m1[i].second = t;
        }
        t = 0;
        rep(i, 1<<s2){
            if(!chmax(t,m2[i].second))
                m2[i].second = t; 
        }

        

        ll ans = 0;
        ll i2 = 0;
        while (m1[0].first + m2[i2 + 1].first <= L) i2++;

        rep(i1, 1<<s1){
            if (m1[i1].first > L) break;
            while (m1[i1].first + m2[i2].first > L) i2--;
            chmax(ans, m1[i1].second + m2[i2].second);
        }
        cout << ans << '\n';
    }
}


Measured memory_bytes: 7732