//#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,abm,mmx,avx,avx2,popcnt,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
/*#pragma GCC optimize("section-anchors")
#pragma GCC optimize("profile-values,profile-reorder-functions,tracer")
#pragma GCC optimize("vpt")
#pragma GCC optimize("rename-registers")
#pragma GCC optimize("move-loop-invariants")
#pragma GCC optimize("unswitch-loops")
#pragma GCC optimize("function-sections")
#pragma GCC optimize("data-sections")
#pragma GCC optimize("branch-target-load-optimize")
#pragma GCC optimize("branch-target-load-optimize2")
#pragma GCC optimize("btr-bb-exclusive")*/
//#pragma comment(linker, "/STACK:367077216")
#define _CRT_SECURE_NO_WARNINGS
#include <chrono>
#include <set>
#include <map>
#include <deque>
#include <string>
#include <cstdint>
#include <cmath>
#include <queue>
#include <cassert>
#include <random>
#include <bitset>
#include <iomanip>
#include <numeric>
#include <time.h>//////////////
#include <ctime>
#include <string>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
//++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++
//#define endl '\n'
#define mp make_pair
#define pbc push_back
#define pob pop_back()
#define empb emplace_back
#define queuel queue<long long>
#define sqr(a) ((a) * (a))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pin(p) cin >> p.first >> p.second;
#define uniq(a) sort(all(a));(a).resize(unique(all(a)) - a.begin());
#define rev(v) reverse(v.begin(), v.end());
#define sout(s, c) for (auto i : s) cout << i << c;
#define pout(p) cout << p.first << " " << p.second;
#define er(v, l, r) erase(v.begin() + l, v.begin() + r);
#define vin(v) for (ll i = 0; i < v.size(); ++i) cin >> v[i];
#define vout(v, c) for (int i = 0; i < v.size(); ++i) cout << v[i] << c;
#define pushi(v, a) for (int i = 0; i < a.size(); ++i) v.push_back(a[i]);
#define fastio() ios_base::sync_with_stdio(0); cout.tie(0); cin.tie(0); srand(time(NULL))
#define dab(v) for(auto i:v)cout<<i<<' ';
#define sp system("pause")
//++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++
using namespace std;
//++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
//++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++
const ld EPS = 1e-9;
const ld EPS2 = 1e-6;
const ld PI = acos(-1);
ll mod = (ll)81273918 * 10000000 + 3623713;
const int MOD7 = 1000000007;
const int MOD9 = 1000000009;
const int a228 = 18;
const int inf = 1e9;
const ll kekmod = 1791791791;
const ll bestmod = 1148822869;
int n, q;
const int MAXN = 3e5 + 1;
int dd[MAXN];
int va[MAXN];
int w[MAXN];
vector<int> mxs[MAXN];
vector<int> weis[MAXN];
int tdp = 0;
void dfs(int v, int kek)
{
	if (v > n) return;
	dd[v] = kek;
	dfs(2 * v, kek + 1);
	dfs(2 * v + 1, kek + 1);
}
signed main()
{
	fastio();
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i) scanf("%d%d", &va[i], &w[i]);
	dfs(1, 1);
	scanf("%d", &q);
	while ((1 << tdp) < n)++tdp;
	for (int i = 1; i <= n; ++i)
	{
		if (dd[i] == tdp / 2 + 1)
		{
			int am = tdp / 2 + 1;
			vector<pair<int, int>> norm;
			for (int mask = 0; mask < (1 << am); ++mask)
			{
				int v = i;
				int tw = 0, tv = 0;
				int aam = 0;
				while (1)
				{
					if (mask & (1 << aam))
					{
						tw += w[v];
						tv += va[v];
					}
					++aam;
					if (v == 1) break;
					v /= 2;
				}
				norm.pbc({ tw, tv });
			}
			sort(all(norm), [&](pair<int, int> a, pair<int, int> b) { return (a.first == b.first ? a.second > b.second : a.first < b.first);});
			vector<pair<int, int>> kek;
			for (auto j : norm)
			{
				if (!kek.size()) kek.pbc(j);
				else
				{
					if (kek.back().first != j.first) kek.pbc(j);
				}
			}
			mxs[i].resize(kek.size());
			weis[i].resize(kek.size());
			int nmx = 0;
			for (int j = 0; j < kek.size(); ++j)
			{
				nmx = max(nmx, kek[j].second);
				mxs[i][j] = nmx;
				weis[i][j] = kek[j].first;
			}
		}
	}
	for (int i = 0; i < q; ++i)
	{
		int v, l;
		scanf("%d%d", &v, &l);
		if (dd[v] <= tdp / 2 + 1)
		{
			int am = dd[v];
			int ans = 0;
			int tv = v;
			for (int mask = 0; mask < (1 << am); ++mask)
			{
				v = tv;
				int tw = 0, tv = 0;
				int aam = 0;
				while (1)
				{
					if (mask & (1 << aam))
					{
						tw += w[v];
						if (tw > l) break;
						tv += va[v];
					}
					++aam;
					if (v == 1) break;
					v /= 2;
				}
				if (tw <= l)
				{
					ans = max(ans, tv);
				}
			}
			printf("%d\n", ans);
		}
		else
		{
			int ans = 0;
			int tv = v;
			int am = dd[v] - tdp / 2 - 1;
			for (int mask = 0; mask < (1 << am); ++mask)
			{
				v = tv;
				int tw = 0, tv = 0;
				int aam = 0;
				while (1)
				{
					if (mask & (1 << aam))
					{
						tw += w[v];
						if (tw > l) break;
						tv += va[v];
					}
					++aam;
					v /= 2;
					if (dd[v] == tdp / 2 + 1) break;
				}
				int kek = l - tw;
				if (kek < 0) continue;
				int x = upper_bound(all(weis[v]), kek) - weis[v].begin();
				if (x == 0) ans = max(ans, tv);
				else
				{
					ans = max(ans, tv + mxs[v][x - 1]);
				}
			}
			printf("%d\n", ans);
		}
	}
//	sp;
}

Measured memory_bytes: 26132