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: Wrong Answer)

#pragma GCC target("sse,sse2,sse3,sse4,sse4.1,sse4.2,avx,avx2")
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define all(x) x.begin(), x.end()

template <typename T1, typename T2> inline void chkmin(T1 &x, const T2 & y) {if (x > y) x = y;}
template <typename T1, typename T2> inline void chkmax(T1 &x, const T2 & y) {if (x < y) x = y;}


namespace fast {
	/** Fast input-output */
	 
	template <class T = int> inline T readInt();						
	inline double readDouble();
	inline int readUInt();					 
	inline int readChar(); // first non-blank character
	inline void readWord(char *s); 
	inline bool readLine(char *s); // do not save '\n'
	inline bool isEof();
	inline int getChar(); 
	inline int peekChar();
	inline bool seekEof();
	inline void skipBlanks();
	 
	template <class T> inline void writeInt(T x, char end = 0, int len = -1);
	inline void writeChar(int x); 
	inline void writeWord(const char *s);
	inline void writeDouble(double x, int len = 0);
	inline void flush();
	 
	static struct buffer_flusher_t {
		~buffer_flusher_t() {
			flush();
		}
	} buffer_flusher;
	 
	/** Read */
	 
	static const int buf_size = 4096;
	 
	static unsigned char buf[buf_size];
	static int buf_len = 0, buf_pos = 0;
	 
	inline bool isEof() {
		if (buf_pos == buf_len) {
			buf_pos = 0, buf_len = fread(buf, 1, buf_size, stdin);
			if (buf_pos == buf_len)
				return 1;
		}
		return 0;
	}
	 
	inline int getChar() { 
		return isEof() ? -1 : buf[buf_pos++];
	}
	 
	inline int peekChar() { 
		return isEof() ? -1 : buf[buf_pos];
	}
	 
	inline bool seekEof() { 
		int c;
		while ((c = peekChar()) != -1 && c <= 32)
			buf_pos++;
		return c == -1;
	}
	 
	inline void skipBlanks() {
		while (!isEof() && buf[buf_pos] <= 32U)
			buf_pos++;
	}
	 
	inline int readChar() {
		int c = getChar();
		while (c != -1 && c <= 32)
			c = getChar();
		return c;
	}
	 
	inline int readUInt() {
		int c = readChar(), x = 0;
		while ('0' <= c && c <= '9')
			x = x * 10 + c - '0', c = getChar();
		return x;
	}
	 
	template <class T>
	inline T readInt() {
		int s = 1, c = readChar();
		T x = 0;
		if (c == '-')
			s = -1, c = getChar();
		else if (c == '+')
			c = getChar();
		while ('0' <= c && c <= '9')
			x = x * 10 + c - '0', c = getChar();
		return s == 1 ? x : -x;
	}
	 
	inline double readDouble() {
		int s = 1, c = readChar();
		double x = 0;
		if (c == '-')
			s = -1, c = getChar();
		while ('0' <= c && c <= '9')
			x = x * 10 + c - '0', c = getChar();
		if (c == '.') {
			c = getChar();
			double coef = 1;
			while ('0' <= c && c <= '9')
				x += (c - '0') * (coef *= 1e-1), c = getChar();
		}
		return s == 1 ? x : -x;
	}
	 
	inline void readWord(char *s) { 
		int c = readChar();
		while (c > 32)
			*s++ = c, c = getChar();
		*s = 0;
	}
	 
	inline bool readLine(char *s) { 
		int c = getChar();
		while (c != '\n' && c != -1)
			*s++ = c, c = getChar();
		*s = 0;
		return c != -1;
	}
	 
	/** Write */
	 
	static int write_buf_pos = 0;
	static char write_buf[buf_size];
	 
	inline void writeChar(int x) {
		if (write_buf_pos == buf_size)
			fwrite(write_buf, 1, buf_size, stdout), write_buf_pos = 0;
		write_buf[write_buf_pos++] = x;
	}
	 
	inline void flush() {
		if (write_buf_pos) {
			fwrite(write_buf, 1, write_buf_pos, stdout), write_buf_pos = 0;
			fflush(stdout);
		}
	}
	 
	template <class T> 
	inline void writeInt(T x, char end, int output_len) {
		if (x < 0)
			writeChar('-'), x = -x;
	 
		char s[24];
		int n = 0;
		while (x || !n)
			s[n++] = '0' + x % 10, x /= 10;
		while (n < output_len)
			s[n++] = '0';
		while (n--)
			writeChar(s[n]);
		if (end)
			writeChar(end);
	}
	 
	inline void writeWord(const char *s) {
		while (*s)
			writeChar(*s++);
	}
	 
	inline void writeDouble(double x, int output_len) {
		if (x < 0)
			writeChar('-'), x = -x;
		int t = (int)x;
		writeInt(t), x -= t;
		writeChar('.');
		for (int i = output_len - 1; i > 0; i--) {
			x *= 10;
			t = std::min(9, (int)x);
			writeChar('0' + t), x -= t;
		}
		x *= 10;
		t = std::min(9, (int)(x + 0.5));
		writeChar('0' + t);
	}
}

using namespace fast;

const int MAXLOG = 18;
const int MAXN = ((1 << MAXLOG) + 228) * 20;
const int BUBEN = 12;
int n;
int v[MAXN], w[MAXN];

void read() {
	n = readInt();
	for (int i = 1; i <= n; ++i) {
		v[i] = readInt();
		w[i] = readInt();
	}
}

int getHeight(int v) {
	int ans = 0;
	while (v) {
		ans++;
		v /= 2;
	}
	return ans;
}

pair<int, int> fans[MAXN];
int topFans[MAXN];
int start[MAXN];
int revOrder[MAXN];

int have[MAXLOG];
int topHave;

void build(int v) {
	topHave = 0;
	while (v) {
		have[topHave++] = v;
		v /= 2;
	}
	reverse(have, have + topHave);
} 

void precalc() {
	int pos = 0;
	for (int V = 0; V < n; ++V) {
		if (getHeight(V) != BUBEN) continue;
		revOrder[V] = pos;
		build(V);
		topFans[pos] = 0;
		if (pos > 0) start[pos] = start[pos - 1] + topFans[pos - 1];
		for (int mask = 0; mask < (1 << topHave); ++mask) {
			int sum = 0;
			int now = 0;
			for (int bit = 0; bit < topHave; ++bit) {
				if ((mask >> bit) & 1) {
					sum += w[have[bit]];
					now += v[have[bit]];
				}
			}
			fans[start[pos] + topFans[pos]++] = {sum, now};
		}
		sort(fans + start[pos], fans + start[pos] + topFans[pos]);
		for (int i = 1; i < topFans[pos]; ++i) {
			chkmax(fans[start[pos] + i].second, fans[start[pos] + i - 1].second);
		}
		++pos;
	}
}

void stupid(int l) {
	int ans = 0;
	for (int mask = 0; mask < (1 << topHave); mask++) {
		int sum = 0;
		int now = 0;
		for (int bit = 0; bit < topHave; bit++) {
			if ((mask >> bit) & 1) {
				sum += w[have[bit]];
				now += v[have[bit]];
			}
			if (sum > l) break;
		}
		if (sum <= l) {
			chkmax(ans, now);
		} 
	}
	writeInt(ans, '\n');
}

void smart(int l) {	
	int szL = BUBEN;
	int szR = topHave - szL;
	int pos = revOrder[have[BUBEN - 1]];
	int ans = 0;
	for (int mask = 0; mask < (1 << szR); ++mask) {
		int sum = 0;
		int now = 0;
		for (int bit = 0; bit < szR; ++bit) {
			if ((mask >> bit) & 1) {
				sum += w[have[bit + szL]];
				now += v[have[bit + szL]];
			}
			if (sum > l) break;
		}
		if (sum > l) continue;
		chkmax(ans, now);
		int mypos = lower_bound(fans + start[pos], fans + start[pos] + topFans[pos], make_pair(l - sum + 1, 0)) - fans - start[pos];
		mypos--;
		if (mypos >= 0 && mypos < topFans[pos]) {
			chkmax(ans, now + fans[start[pos] + mypos].second);
		}
	}
	writeInt(ans, '\n');
}

void solve() {
	int v, l;
	v = readInt();
	l = readInt();
	build(v);
	if (topHave <= BUBEN) {
		stupid(l);
	} else {
		smart(l);
	}
}

void run() {
	precalc();
	int q;
	q = readInt();
	while (q--) {
		solve();
	}
}

signed main() {
	read();
	run();
	return 0;
}