Problem:
Score : 700 points

Problem Statement

We have a rooted binary tree with N vertices, where the vertices are numbered 1 to N.
Vertex 1 is the root, and the parent of Vertex i (i \geq 2) is Vertex \left[ \frac{i}{2} \right].

Each vertex has one item in it. The item in Vertex i has a value of V_i and a weight of W_i.
Now, process the following query Q times:

Given are a vertex v of the tree and a positive integer L.
Let us choose some (possibly none) of the items in v and the ancestors of v so that their total weight is at most L.
Find the maximum possible total value of the chosen items.

Here, Vertex u is said to be an ancestor of Vertex v when u is an indirect parent of v, that is, there exists a sequence of vertices w_1,w_2,\ldots,w_k (k\geq 2) where w_1=v, w_k=u, and w_{i+1} is the parent of w_i for each i.

Constraints

All values in input are integers.

1 \leq N < 2^{18}

1 \leq Q \leq 10^5

1 \leq V_i \leq 10^5

1 \leq W_i \leq 10^5

For the values v and L given in each query, 1 \leq v \leq N and 1 \leq L \leq 10^5.

Input

Let v_i and L_i be the values v and L given in the i-th query.
Then, Input is given from Standard Input in the following format:

N
V_1 W_1
:
V_N W_N
Q
v_1 L_1
:
v_Q L_Q

Output

For each integer i from 1 through Q,
the i-th line should contain the response to the i-th query.

Sample Input 1

3
1 2
2 3
3 4
3
1 1
2 5
3 5

Sample Output 1

0
3
3

In the first query, we are given only one choice: the item with (V, W)=(1,2). Since L = 1, we cannot actually choose it, so our response should be 0.

In the second query, we are given two choices: the items with (V, W)=(1,2) and (V, W)=(2,3). Since L = 5, we can choose both of them, so our response should be 3.

Sample Input 2

15
123 119
129 120
132 112
126 109
118 103
115 109
102 100
130 120
105 105
132 115
104 102
107 107
127 116
121 104
121 115
8
8 234
9 244
10 226
11 227
12 240
13 237
14 206
15 227

Sample Output 2

256
255
250
247
255
259
223
253

Solution in C++ (GCC 9.2.1) (judge verdict: Runtime Error)


// https://scrapbox.io/hitsujirere-memo/

/*****/

#define TRUE true
#define FALSE false

#include <bits/stdc++.h>

/**/
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
/**/

using namespace std;
using ll = long long;
using ld = long double;
using pint = pair<int, int>;
using pll = pair<long long, long long>;
template <class T>
using vec = vector<T>;
template <class T>
using vec2 = vector<vector<T>>;
template <class T>
using vec3 = vector<vector<vector<T>>>;

constexpr int INF = numeric_limits<int>::max();
constexpr ll INFL = numeric_limits<ll>::max();
constexpr ll MOD = 1000000007; // 10^9+7

#ifdef TRUE

#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define rep1(i, n) for (ll i = 1, i##_len = (n); i <= i##_len; ++i)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; --i)
#define rrep1(i, n) for (ll i = (n); i > 0; --i)
#define step(i, a, n) for (ll i = (a), i##_len = (a) + (n); i < i##_len; ++i)
#define rstep(i, a, n) for (ll i = (a) + (n)-1, i##_len = (a); i >= i##_len; --i)
#define range(i, a, b) for (ll i = (a), i##_len = (b); i < i##_len; ++i)
#define rrange(i, a, b) for (ll i = (b)-1, i##_len = (a); i >= i##_len; --i)

std::string strMulti(const char c, const int n)
{
	std::string out = "";
	for (int i = 0; i < n; i++)
	{
		out += c;
	}
	return out;
}
std::string strMulti(const std::string t, const int n)
{
	std::string out = "";
	for (int i = 0; i < n; i++)
	{
		out += t;
	}
	return out;
}
std::string tab(const int n)
{
	return strMulti('\t', n);
}
std::string join(const vector<string> &v, const char *delim = 0)
{
	std::string s;
	if (!v.empty())
	{
		s += v[0];
		for (decltype(v.size()) i = 1, c = v.size(); i < c; ++i)
		{
			if (delim)
				s += delim;
			s += v[i];
		}
	}
	return s;
}

string to_string(string &s) { return '"' + s + '"'; }
string to_string(char &c) { return string({'\'', c, '\''}); }
template <class T1, class T2>
string to_string(pair<T1, T2> p)
{
	return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <class Tuple, size_t... I>
string _to_string_tuple(Tuple &&t, std::index_sequence<I...>)
{
	vector<string> texts = {to_string(std::get<I>(t))...};
	return "(" + join(texts, ", ") + ")";
}
template <class... Args>
string to_string(tuple<Args...> t)
{
	using Tuple = tuple<Args...>;
	return _to_string_tuple(std::forward<Tuple>(t),
							std::make_index_sequence<std::tuple_size<std::decay_t<Tuple>>::value>{});
}
template <class T>
string to_string(vector<T> ar)
{
	vector<string> texts(ar.size());
	for (size_t i = 0; i < ar.size(); ++i)
	{
		texts[i] = to_string(ar[i]);
	}
	return "{" + join(texts, ", ") + "}";
}
template <class T>
string to_string(initializer_list<T> il)
{
	vector<string> texts(il.size());
	size_t i = 0;
	for (T v : il)
	{
		texts[i] = to_string(v);
		i++;
	}
	return "{" + join(texts, ", ") + "}";
}

#ifdef DEBUG_BUILD
#define debugvar(x) debugln(#x, (x))
#define debugvartab(x, t) debugln(tab(t) + #x, (x))
void debug(string name)
{
	std::cerr << name;
}
void debugln(string name)
{
	std::cerr << name << '\n';
}
template <class T>
void debug(string name, T v)
{
	std::cerr << name << " = " << to_string(v);
}
template <class T>
void debugln(string name, T v)
{
	std::cerr << name << " = " << to_string(v) << '\n';
}
template <class T>
void debug(string name, initializer_list<T> il)
{
	std::cerr << name << " = " << to_string(il);
}
template <class T>
void debugln(string name, initializer_list<T> il)
{
	std::cerr << name << " = " << to_string(il) << '\n';
}
template <class... Args>
void debug(string name, Args... args)
{
	std::cerr << name << " = " << to_string(tuple<Args...>(args...));
}
template <class... Args>
void debugln(string name, Args... args)
{
	std::cerr << name << " = " << to_string(tuple<Args...>(args...)) << '\n';
}
#else
#define debugvar(x)
#define debugvartab(x, t)
void debug(string name)
{
}
void debugln(string name) {}
template <class T>
void debug(string name, T v) {}
template <class T>
void debugln(string name, T v) {}
template <class T>
void debug(string name, initializer_list<T> il) {}
template <class T>
void debugln(string name, initializer_list<T> il) {}
template <class... Args>
void debug(string name, Args... args) {}
template <class... Args>
void debugln(string name, Args... args) {}
#endif

#define all(x) (x).begin(), (x).end()
#define pair(a, b) make_pair(a, b)

template <class T>
bool chmax(T &a, const T b)
{
	if (a < b)
	{
		a = b;
		return true;
	}
	return false;
}
template <class T>
bool chmin(T &a, const T b)
{
	if (a > b)
	{
		a = b;
		return true;
	}
	return false;
}
template <class T>
T divup(const T a, const T b)
{
	if (a % b == 0)
	{
		return a / b;
	}
	return a / b + 1;
}
template <class T>
bool cmp_2nd(pair<T, T> a, pair<T, T> b)
{
	if (a.second != b.second)
	{
		return a.second < b.second;
	}
	return a.first < b.first;
}
ll modpow(ll x, ll n, const ll &p)
{
	ll ret = 1;
	while (n > 0)
	{
		if (n & 1)
		{
			(ret *= x) %= p;
		}
		(x *= x) %= p;
		n >>= 1;
	}
	return ret;
}
template <class T>
T modinv(T a, const T &p)
{
	T b = p, u = 1, v = 0;
	while (b)
	{
		T t = a / b;
		a -= t * b;
		swap(a, b);
		u -= t * v;
		swap(u, v);
	}
	u %= p;
	if (u < 0)
	{
		u += p;
	}
	return u;
}
template <class T>
T math_P(T m, T n)
{
	T ret = 1;
	for (T i = m; i > m - n; i--)
	{
		ret *= i;
	}
	return ret;
}
template <class T>
T math_C(T m, T n)
{
	T ret = math_P(m, n);
	for (T i = 2; i <= n; i++)
	{
		ret /= i;
	}
	return ret;
}
ll extended_euclidean(ll u, ll v)
{
	ll r0 = u;
	ll r1 = v;
	ll s0 = 1;
	ll s1 = 0;
	ll t0 = 0;
	ll t1 = 1;
	while (r1 != 0)
	{
		ll q = r0 / r1;
		ll r = r0 - q * r1;
		ll s = s0 - q * s1;
		ll t = t0 - q * t1;
		r0 = r1;
		s0 = s1;
		t0 = t1;
		r1 = r;
		s1 = s;
		t1 = t;
	}
	if (t0 < 0)
	{
		return t0 + u;
	}
	else
	{
		return t0;
	}
}
ll math_C_mod(ll n, ll c, const ll &p)
{
	ll upe = 1;
	ll dow = 1;
	for (ll i = 1; i < c + 1; ++i)
	{
		upe = upe * n % p;
		dow = dow * i % p;
		n -= 1;
	}
	return (upe * extended_euclidean(p, dow)) % p;
}
template <class T>
T math_gcd(T a, T b)
{
	if (b == 0)
	{
		return a;
	}
	else
	{
		return math_gcd(b, a % b);
	}
}
template <class T>
T math_lcm(T a, T b) { return a / math_gcd(a, b) * b; }
template <class T>
T SumStep(T a, T n, T d) { return n * (2 * a + (n - 1) * d) / 2; }
template <class T>
T SumRange(T a, T b, T d) { return SumStep(a, (b - a) / d, d); }

#endif

constexpr ll P10_5 = 100000;

/*****/

void Main()
{
	ll N;
	cin >> N;
	// Value, Weight
	vec<pll> P(N);
	rep(i, N) cin >> P[i].first >> P[i].second;
	// dp[P番号][重さ]
	vec2<ll> dp(N, vec<ll>(P10_5 + 10));
	dp[0][P[0].second] = P[0].first;
	range(i, 1, N)
	{
		debugvar(i);
		int from = (i - 1) / 2;
		ll V = P[i].first;
		ll W = P[i].second;
		rep(w, P10_5 + 1)
		{
			dp[i][w] = dp[from][w];
		}
		chmax(dp[i][W], V);
		rep(w, P10_5 + 1 - W)
		{
			debugvar(w);
			chmax(dp[i][w + W], dp[from][w] + V);
		}
	}
	//rep(i, N) debugvar(dp[i]);

	ll Q;
	cin >> Q;
	rep(iQ, Q)
	{
		ll u, L;
		cin >> u >> L;
		u--;
		ll max = 0;
		rep(v, L + 1)
		{
			chmax(max, dp[u][v]);
		}
		cout << max << endl;
	}
}

/*****/

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

	std::cout << std::fixed << std::setprecision(10);

	Main();
	std::cerr << flush;
	std::cout << flush;

	return 0;
}