#include <bits/stdc++.h>
#define _overload3(_1,_2,_3,name,...)name
#define _rep(i,n)repi(i,0,n)
#define repi(i,a,b)for(int i=int(a),i##_len=(b);i<i##_len;++i)
#define MSVC_UNKO(x)x
#define rep(...)MSVC_UNKO(_overload3(__VA_ARGS__,repi,_rep,_rep)(__VA_ARGS__))
#define all(c)c.begin(),c.end()
#define write(x)cout<<(x)<<'\n'
using namespace std; using ll = long long; template<class T>using vv = vector<vector<T>>;
template<class T>auto vvec(int n, int m, T v) { return vv<T>(n, vector<T>(m, v)); }
constexpr int INF = 1 << 29, MOD = int(1e9) + 7; constexpr ll LINF = 1LL << 60;
struct aaa { aaa() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(10); }; }aaaa;

int main() {
    int N, Q;
    cin >> N;
    array<int, 1 << 18> V, W;
    fill(all(V), 0);
    fill(all(W), 0);
    rep(i, 1, N + 1) cin >> V[i] >> W[i];
    cin >> Q;
    vector<int> v(Q), L(Q);
    rep(i, Q) cin >> v[i] >> L[i];

    // 1(=2^0) 2-3(=2^1) 4-7(=2^2) 8-15(=2^3) ... (=2^5) (=2^6) ... (=2^11) (=2^12) ... (=2^17)

    array<vector<pair<int, int>>, 1 << 12> branch;
    constexpr int Lmax = 1e5;
    vector<pair<int, int>> items;
    constexpr auto comparer = [](auto&& l, auto&& r) {
        return l.first == r.first ? l.second > r.second : l.first < r.first;
    };
    constexpr auto unq = [](auto&& l, auto&& r) { return l.second >= r.second; };

    branch[0].emplace_back(0, 0);
    rep(d, 12) {
        rep(u, 1 << d, 1 << (d + 1)) {
            items.clear();
            rep(bit, 1 << (d + 1)) {
                int Vsum = 0, Wsum = 0;
                rep(b, d + 1) {
                    Vsum += (bit >> b & 1)* V[u >> b];
                    Wsum += (bit >> b & 1)* W[u >> b];
                }
                if (Wsum > Lmax) continue;
                items.emplace_back(Wsum, Vsum);
            }
            sort(all(items), comparer);
            items.erase(unique(all(items), unq), items.end());
            branch[u] = items;
        }
    }

    rep(q, Q) {
        int ans = 0;

        if (v[q] < 1 << 12) {
            ans = prev(lower_bound(all(branch[v[q]]), make_pair(L[q] + 1, 0)))->second;
        }
        else {
            int depth = 31 - __builtin_clz(v[q]);
            int d = depth - 12;
            int u_branch = v[q] >> (d + 1);

            items.clear();
            rep(bit, 1 << (d + 1)) {
                int Vsum = 0, Wsum = 0;
                rep(b, d + 1) {
                    Vsum += (bit >> b & 1)* V[v[q] >> b];
                    Wsum += (bit >> b & 1)* W[v[q] >> b];
                }
                if (Wsum > L[q]) continue;
                items.emplace_back(Wsum, Vsum);
            }
            sort(all(items), comparer);
            items.erase(unique(all(items), unq), items.end());
            reverse(all(items));

            int n = (int)branch[u_branch].size();
            int m = (int)items.size();
            for (int i = 0, j = 0; i < n && j < m; ++i) {
                while (j < m && branch[u_branch][i].first + items[j].first > L[q]) {
                    ++j;
                }
                if (j < m) ans = max(ans, branch[u_branch][i].second + items[j].second);
            }
        }

        write(ans);
    }
}


Measured memory_bytes: 76856