#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using vint = vector<int>;
using vvint = vector<vint>;
using vvvint = vector<vvint>;
using vdouble = vector<double>;
using vvdouble = vector<vdouble>;
using vvvdouble = vector<vvdouble>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using uint = unsigned int;
using ull = unsigned long long;

template<typename T> using uset = unordered_set<T>;
template<typename T1, typename T2> using umap = unordered_map<T1, T2>;

constexpr int INF = (1 << 30) - 1;
constexpr ll LLINF = 1LL << 60;
constexpr int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
constexpr int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr char el = '\n';
constexpr int mod = 1000000007;
constexpr int mod2 = 998244353;
template<typename T> T gcd(T a, T b) { return (b ? gcd(b, a % b) : a); }
template<typename T> T lcm(T a, T b) { return (a / gcd(a, b) * b); }
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) { return (a > b && (a = b, true)); }
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) { return (a < b && (a = b, true)); }
template<typename T>
bool isin(T y, T x, T h, T w) { return (0 <= y && 0 <= x && y < h && x < w); }
template<typename T>
bool isin1(T y, T x, T h, T w) { return (0 < y && 0 < x && y <= h && x <= w); }

template<typename T>
ostream& operator <<(ostream &os, vector<T> &v) {
	os << v[0];
	for (int i = 1; i < v.size(); i++) os << " " << v[i];
	return (os);
}

template<typename T>
istream& operator >>(istream &is, vector<T> &v) {
	for (auto &u : v) is >> u;
	return (is);
}

template<typename T1, typename T2>
istream& operator >>(istream &is, pair<T1, T2> &p) {
	return (is >> p.first >> p.second);
}

int N;
vint V, W;
vector<vector<Pi>> A(1024);

int half(int u, int L) {
	vint v, w;
	int k = u;
	while (k > 0) {
		v.push_back(V[k]);
		w.push_back(W[k]);
		k >>= 1;
	}
	int n = v.size();
	vector<Pi> A;
	int hal = n/2;
	//cout << " " << v << endl;
	//cout << " " << w << endl;

	for (int b = 0; b < (1 << hal); b++) {
		int vsum = 0, wsum = 0;
		for (int i = 0; i < hal; i++) {
			if ((b>>i)&1) {
				vsum += v[i];
				wsum += w[i];
			}
		}
		if (wsum <= L) A.emplace_back(wsum, -vsum);
	}
	sort(begin(A), end(A));	

	vector<Pi> B;
	for (int b = 0; b < (1 << (n-hal)); b++) {
		int vsum = 0, wsum = 0;
		for (int i = 0; i < n-hal; i++) {
			if ((b>>i)&1) {
				vsum += v[i+hal];
				wsum += w[i+hal];
			}
		}
		if (wsum <= L) B.emplace_back(wsum, -vsum);
	}
	sort(begin(B), end(B));

	for (int i = 0; i < A.size(); i++) {
		A[i].second = -A[i].second;
		if (i > 0) chmax(A[i].second, A[i-1].second);
		//cout << A[i].first << " " << A[i].second << "  ";
	}
	//cout << endl;

	for (int i = 0; i < B.size(); i++) {
		B[i].second = -B[i].second;
		if (i > 0) chmax(B[i].second, B[i-1].second);
		//cout << B[i].first << " " << B[i].second << "  ";
	}
	
	int j = (int)B.size() - 1;
	int maxv = 0;
	for (int i = 0; i < A.size(); i++) {
		int vs = A[i].second, ws = A[i].first;
		while (j >= 0 && ws + B[j].first > L) j--;
		if (j >= 0) {
			chmax(maxv, vs + B[j].second);
		}
	}
	return (maxv);
}

int half2(int u, int L) {
	vint v, w;
	int k = u;
	while (k >= 1024) {
		v.push_back(V[k]);
		w.push_back(W[k]);
		k >>= 1;
	}
	int vv = k;

	int n = v.size();

	vector<Pi> B;
	for (int b = 0; b < (1 << n); b++) {
		int vsum = 0, wsum = 0;
		for (int i = 0; i < n; i++) {
			if ((b>>i)&1) {
				vsum += v[i];
				wsum += w[i];
			}
		}
		if (wsum <= L) B.emplace_back(wsum, vsum);
	}
	
	int maxv = 0;
	for (int i = 0; i < B.size(); i++) {
		int vs = B[i].second, ws = B[i].first;
		auto pos = upper_bound(begin(A[vv]), end(A[vv]), Pi(L - ws, INF)) - 1 - begin(A[vv]);
		chmax(maxv, A[vv][pos].second + vs);
	}
	return (maxv);
}

void Main() {
	cin >> N;
	V = vint(N+1), W = vint(N+1);
	for (int i = 1; i <= N; i++) {
		cin >> V[i] >> W[i];
	}
	
	for (int u = 512; u < 1024; u++) {
		vint v, w;
		int k = u;
		while (k > 0) {
			v.push_back(V[k]);
			w.push_back(W[k]);
			k >>= 1;
		}
		int n = v.size();
		
		for (int b = 0; b < 1024; b++) {
			int vsum = 0, wsum = 0;
			for (int i = 0; i < n; i++) {
				if ((b>>i)&1) {
					vsum += v[i];
					wsum += w[i];
				}
			}
			A[u].emplace_back(wsum, -vsum);
		}
		sort(begin(A[u]), end(A[u]));	

		for (int i = 0; i < A.size(); i++) {
			A[u][i].second = -A[u][i].second;
			if (i > 0) chmax(A[u][i].second, A[u][i-1].second);
		}
	}

	int Q; cin >> Q;
	while (Q--) {
		int u, L; cin >> u >> L;
		if (u < 1024) cout << half(u, L) << el;
		else cout << half2(u, L) << el;
	}
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(0);
	cout << fixed << setprecision(20);
	Main();
	return (0);
}


Measured memory_bytes: 9500