#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#include <numeric>
#include <random>
#include <unordered_map>
#include <unordered_set>
#define all(a) a.begin(),a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#define debug(x) cerr << __LINE__ << ' ' << #x << ':' << x << '\n'
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
template<class T> using pque = priority_queue<T, vector<T>, greater<T>>;
constexpr int inf = 1000000010;
constexpr ll INF = 1000000000000000010;
constexpr int mod1e9 = 1000000007;
constexpr int mod998 = 998244353;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
constexpr ll ten(int n) { return n ? 10 * ten(n - 1) : 1; };
int dx[] = { 1,0,-1,0,1,1,-1,-1 }; int dy[] = { 0,1,0,-1,1,-1,1,-1 };
void fail() { cout << "-1\n"; exit(0); }
template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; }
template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; }
template<class T> istream &operator >> (istream &s, vector<T> &v) { for (auto &e : v) s >> e; return s; }
template<class T> ostream &operator << (ostream &s, const vector<T> &v) { for (auto &e : v) s << e << ' '; return s; }

struct fastio {
	fastio() {
		cin.tie(0); cout.tie(0);
		ios::sync_with_stdio(false);
		cout << fixed << setprecision(20);
		cerr << fixed << setprecision(20);
	}
}fastio_;

void change(vector<pair<int, int>> &v) {
	int maxval = -1;
	vector<pair<int, int>> tmp;
	for (pair<int, int> p : v) {
		if (p.second > maxval) {
			tmp.pb({ p.first,p.second });
			maxval = p.second;
		}
	}
	v = tmp;
}

int s = 1024;

int main() {
	int n;
	cin >> n;
	vector<int> v(n + 1), w(n + 1);
	for (int i = 1; i <= n; i++) cin >> v[i] >> w[i];
	vector<vector<pair<int, int>>> a(s, vector<pair<int, int>>());
	a[0].pb({ 0,0 });
	for (int i = 1; i <= min(n, s - 1); i++) {
		for (pair<int, int> p : a[i / 2]) {
			a[i].pb({ p.first, p.second });
			a[i].pb({ p.first + w[i],p.second + v[i] });
		}
		sort(all(a[i]));
		change(a[i]);
	}
	int q;
	cin >> q;
	while (q--) {
		int ver, l;
		cin >> ver >> l;
		if (ver < s) {
			int ans = 0;
			for (pair<int, int> p : a[ver]) {
				if (p.first <= l) chmax(ans, p.second);
			}
			cout << ans << '\n';
		}
		else {
			vector<pair<int, int>> pre, check;
			pre.pb({ 0,0 });
			while (ver >= s) {
				check.clear();
				for (pair<int, int> p : pre) {
					check.pb({ p.first,p.second });
					check.pb({ p.first + w[ver],p.second + v[ver] });
				}
				pre = check;
				ver >>= 1;
			}
			sort(all(check));
			change(check);
			int ans = 0;
			for (pair<int, int> p : a[ver]) {
				int rem = l - p.first;
				if (rem < 0) continue;
				auto idx = upper_bound(all(check), pair<int, int>(rem + 1, 0)) - check.begin();
				idx--;
				chmax(ans, p.second + check[idx].second);
			}
			cout << ans << '\n';
		}
	}
}

Measured memory_bytes: 10892