#include <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <bitset>
#include <complex>
#include <unistd.h>
#include <cassert>
#include <cctype>
#include <random>
#include <time.h>
#include <functional>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef pair<double, ll> pdlg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<ll, ll, ll, ll> tlglglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
typedef vector<ll> vll;
typedef vector< vector<ll> > matrix;
typedef unsigned int uint;
typedef unsigned long long ull;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template <class T> using VVV = V<VV<T>>;
#define REP(i, x, y) for(ll i = (ll)x; i < (ll)y; i++)
#define DREP(i, x, y, d) for(ll i = (ll)x; i < (ll)y; i += (ll)d)
#define PER(i, x, y) for(ll i = (ll)x; i > (ll)y; i--)
#define DPER(i, x, y, d) for(ll i = (ll)x; i > (ll)y; i -= (ll)d)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-12;
void yesno(bool ok, string yes = "Yes", string no = "No") {
    if (ok) {
        cout << yes << endl;
    } else {
        cout << no << endl;
    }
}

V<plglg> dp[1030];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N;
    cin >> N;
    ll Va[N], W[N];
    REP(i, 0, N) {
        cin >> Va[i] >> W[i];
    }
    ll div = min(1LL << 10, N);
    dp[0].push_back(plglg(0, 0));
    REP(i, 0, div) {
        ll par = (i + 1) / 2;
        ll now = i + 1;
        ll len = dp[par].size();
        REP(j, 0, len) {
            plglg tmp = dp[par][j];
            dp[now].push_back(tmp);
            dp[now].push_back(plglg(tmp.first + W[i], tmp.second + Va[i]));
        }
        sort(dp[now].begin(), dp[now].end());
        REP(i, 1, dp[now].size()) {
            dp[now][i].second = max(dp[now][i].second, dp[now][i - 1].second);
        }
    }
    // REP(i, 0, div + 1) {
    //     REP(j, 0, dp[i].size()) {
    //         cout << dp[i][j].first << " " << dp[i][j].second << " aaa ";
    //     }
    //     cout << endl;
    // }
    ll Q;
    cin >> Q;
    REP(i, 0, Q) {
        ll v, L;
        cin >> v >> L;
        if (v - 1 < div) {
            ll id = lower_bound(dp[v].begin(), dp[v].end(), plglg(L + 1, -1)) - dp[v].begin();
            plglg p = dp[v][id - 1];
            cout << p.second << endl;
        } else {
            V<plglg> vec[2];
            vec[0].push_back(plglg(0, 0));
            while(v - 1 >= div) {
                vec[1].clear();
                REP(j, 0, vec[0].size()) {
                    plglg tmp = vec[0][j];
                    vec[1].push_back(tmp);
                    vec[1].push_back(plglg(tmp.first + W[v - 1], tmp.second + Va[v - 1]));
                }
                swap(vec[0], vec[1]);
                ll par = v / 2;
                v = par;
            }
            ll ans = 0;
            REP(j, 0, vec[0].size()) {
                plglg p = vec[0][j];
                if (p.first <= L) {
                    ll id = lower_bound(dp[v].begin(), dp[v].end(), plglg(L - p.first + 1, -1)) - dp[v].begin();
                    plglg q = dp[v][id - 1];
                    ans = max(ans, p.second + q.second);
                }
            }
            cout << ans << endl;
        }
    }
}


Measured memory_bytes: 18564