#include <bits/stdc++.h>

// headers {{{
using namespace std;
// using namespace rel_ops;
 
const double pi=acos(-1.0);
const double eps=1e-11;
const int INF=0x7FFFFFFF;
template<class T> inline bool checkmin(T &a,T b){return b<a?a=b,1:0;}
template<class T> inline bool checkmax(T &a,T b){return b>a?a=b,1:0;}
template<class T> inline T sqr(T x){return x*x;}
template<class T> inline bool bit(T a, int b) {return (a>>b)&1;}
inline int popcount(int n) { return __builtin_popcount(n); }
inline int popcount(long long n) { return __builtin_popcountll(n); }
typedef long long int64;
typedef unsigned long long uint64;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<PII> VII;
#define MP(A,B) make_pair(A,B)
#define PB(X) push_back(X)
#define eb(args...) emplace_back(args)
#define rep(i,n) for(int i=0; i<(int)n; i++)
#define ff first
#define ss second
#define sz(g) int(g.size())
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
#ifdef LOCAL
#define dout cerr 
void out() { dout << endl; }
template<typename A, typename... B> void out(A a, B... b) { dout << ' ' << a; out(b...); }
#define dbg(args...) dout << __LINE__ << ":(" << #args << ") -> ", out(args)
template<class T>void show(T a, int n){for(int i=0; i<n; ++i) cout<<a[i]<<' '; cout<<endl;}
template<class T>void show(T a, int r, int l){for(int i=0; i<r; ++i)show(a[i],l);cout<<endl;}
constexpr bool local = true;
#else
#define dbg(...)
#define dout if(true);else cerr
#define out(...)
#define show(...)
constexpr bool local = false;
#endif
// }}}

int bound = 2048;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL); 
    int n, Q;
    while (cin >> n) {
        VI v(n+1), w(n+1);
        rep(i, n) {
            cin >> v[i+1] >> w[i+1];
            // v[i+1] = rand() + 1;
            // w[i+1] = rand() + 1;
        }
        cin >> Q;
        VI q(Q), L(Q), ans(Q);
        rep(i, Q) {
            // q[i]  = rand() % n + 1;
            // L[i] = rand() + 1;
            cin >> q[i] >> L[i];
        }
        map<int, VII> mp;
        rep(c, Q) {
            int x = q[c];
            VII s; 
            auto getS = [&]() {
                VII v2, vec;
                int msk = 1 << sz(s);
                if (sz(s) == 0) {
                    vec.eb(0, 0); return vec;
                }
                for (int i = 0; i < msk; i++) {
                    int val = 0, wei = 0;
                    int j = i;
                    while (j > 0) {
                        int b = __builtin_ffs(j) - 1;
                        // dbg(i, j, b);
                        val += s[b].ff;
                        wei += s[b].ss;
                        j -= 1<<b;
                    }
                    v2.eb(wei, val);
                }
                v2.eb(0, 0);
                sort(v2.begin(), v2.end());
                int premax = -1;
                for (int i = 0; i < sz(v2); i++) {
                    while (i+1 < sz(v2) && v2[i+1].ff == v2[i].ff) i++;
                    if (v2[i].ss <= premax) continue;
                    premax = v2[i].ss;
                    vec.PB(v2[i]);
                }
                // dbg(s, v2);
                // dbg(vec);
                return vec;
            };
            int kx = x;
            while (kx >= bound) kx >>= 1;
            if (!mp.count(kx)) {
                int tx = kx;
                while (tx >= 1) {
                    s.eb(v[tx], w[tx]); tx >>= 1;
                }
                // dbg(x, s);
                mp[kx] = getS();
            }
            auto &vec = mp[kx];
            s.clear();
            int k = x;
            while (k >= bound) {
                s.eb(v[k], w[k]); k >>= 1;
            }
            auto v2 = getS();
            int i = -1;
            int ans = 0;
            dbg(s, sz(v2));
            for (int j = sz(v2)-1; j>=0; j--) {
                while (i+1 < sz(vec) && vec[i+1].ff + v2[j].ff <= L[c]) {
                    i++;
                }
                if (i >= 0) checkmax(ans, v2[j].ss + vec[i].ss);
            }
            cout << ans << '\n';
        }
        
    }
    return 0;
}



Measured memory_bytes: 23200