#pragma GCC optimize ("O2")
#pragma GCC target ("avx2")
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int CM = 1 << 17, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
	if (ci - owa > 0) {
		memcpy(cn, owa, CL);
		ci -= CM;
		fread(cn + CL, 1, CM, stdin);
	}
	ll tmp = *(ll*)ci;
	int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
	tmp = tmp << dig & ma1;
	tmp = tmp * 10 + (tmp >> 8) & ma2;
	tmp = tmp * 100 + (tmp >> 16) & ma3;
	tmp = tmp * 10000 + (tmp >> 32) & ma4;
	ci += 72 - dig >> 3;
	return tmp;
}

const int dm = 1 << 21;
char dn[dm], * di = dn;
inline void putint(int X) {
	if (X == 0) {
		*di++ = '0';
		*di++ = '\n';
		return;
	}
	int keta = 0;
	char C[10];
	while (X) {
		*(C + keta) = '0' + X % 10;
		X /= 10;
		keta++;
	}
	for (int i = keta - 1; i >= 0; i--)* di++ = (*(C + i));
	*di++ = '\n';
}

int V[300000], W[300000];
int vmae[512][512], wmae[512][512];

int main() {
	//cin.tie(0);
	//ios::sync_with_stdio(false);


	int N = getint();
	rep1(i, N) {
		V[i] = getint();
		W[i] = getint();
	}

	rep1(i, 511) {
		int k = 31 - __builtin_clz(i);
		int K1 = 1 << k;
		int j = i / 2;

		int t1 = 0;
		int t2 = 0;
		int t3 = 0;
		while (t1 < K1 || t2 < K1) {
			if (t1 == K1) {
				vmae[i][t3] = vmae[j][t2] + V[i];
				wmae[i][t3] = wmae[j][t2] + W[i];
				t2++;
			}
			else if (t2 == K1) {
				vmae[i][t3] = vmae[j][t1];
				wmae[i][t3] = wmae[j][t1];
				t1++;
			}
			else if (wmae[j][t1] > wmae[j][t2] + W[i]) {
				vmae[i][t3] = vmae[j][t2] + V[i];
				wmae[i][t3] = wmae[j][t2] + W[i];
				t2++;
			}
			else {
				vmae[i][t3] = vmae[j][t1];
				wmae[i][t3] = wmae[j][t1];
				t1++;
			}
			t3++;
		}
	}
	rep1(i, 511) {
		int k = 32 - __builtin_clz(i);
		int K1 = 1 << k;
		int saidai = 0;
		rep(j, K1) {
			if (saidai < vmae[i][j]) saidai = vmae[i][j];
			vmae[i][j] = saidai;
		}
	}

	int Q = getint();

	rep(q, Q) {
		int P = getint(), L = getint();

		if (P < 512) {
			int k = 32 - __builtin_clz(P);
			int K1 = 1 << k;
			int saidai = 0;
			rep(j, K1) {
				if (wmae[P][j] <= L) saidai = vmae[P][j];
				else break;
			}
			putint(saidai);
			continue;
		}

		int vato[2][512] = {}, wato[2][512] = {};
		auto v1 = vato[0];
		auto v2 = vato[1];
		auto w1 = wato[0];
		auto w2 = wato[1];
		int k = 0;
		while (P >= 512) {
			int v = V[P];
			int w = W[P];

			int K1 = 1 << k;

			int t1 = 0;
			int t2 = 0;
			int t3 = 0;
			while (t1 < K1 || t2 < K1) {
				if (t1 == K1) {
					v2[t3] = v1[t2] + v;
					w2[t3] = w1[t2] + w;
					t2++;
				}
				else if (t2 == K1) {
					v2[t3] = v1[t1];
					w2[t3] = w1[t1];
					t1++;
				}
				else if (w1[t1] > w1[t2] + w) {
					v2[t3] = v1[t2] + v;
					w2[t3] = w1[t2] + w;
					t2++;
				}
				else {
					v2[t3] = v1[t1];
					w2[t3] = w1[t1];
					t1++;
				}
				t3++;
			}
			swap(v1, v2);
			swap(w1, w2);
			P /= 2;
			k++;
		}

		int K1 = 1 << k;
		int saidai = 0;

		int p = 511;
		rep(j, K1) {
			while (p >= 0 && w1[j] + wmae[P][p] > L) p--;
			if (p < 0) break;
			if (saidai < v1[j] + vmae[P][p]) saidai = v1[j] + vmae[P][p];
		}

		putint(saidai);

	}

	fwrite(dn, 1, di - dn, stdout);

	Would you please return 0;
}

Measured memory_bytes: 8448