#include <cstdio>
#include <cstring>
#include <cmath>
#include <cassert>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <iostream>
#include <numeric>

/* debug macros */
#ifdef WAFDAYO
#define DBG_LINE() {std::cerr<<"\e[2m[L"<<__LINE__<<"]\e[m ";}
#define DBG_PRINT(s,t,u) {std::cerr<<(s)<<" \e[2m=\e[m \e[1m"<<(t)<<"\e[m"<<(u);}
#define SELECT_7TH(x1,x2,x3,x4,x5,x6,x7,...) x7
#define dbg1(x1) DBG_PRINT(#x1,x1,std::endl)
#define dbg2(x1,x2) DBG_PRINT(#x1,x1,", ")dbg1(x2)
#define dbg3(x1,x2,x3) DBG_PRINT(#x1,x1,", ")dbg2(x2,x3)
#define dbg4(x1,x2,x3,x4) DBG_PRINT(#x1,x1,", ")dbg3(x2,x3,x4)
#define dbg5(x1,x2,x3,x4,x5) DBG_PRINT(#x1,x1,", ")dbg4(x2,x3,x4,x5)
#define dbg6(x1,x2,x3,x4,x5,x6) DBG_PRINT(#x1,x1,", ")dbg5(x2,x3,x4,x5,x6)
#define dbg(...) DBG_LINE()\
SELECT_7TH(__VA_ARGS__,dbg6,dbg5,dbg4,dbg3,dbg2,dbg1)(__VA_ARGS__)
#else
#define dbg(...) {}
#endif

/* utility functions */
struct read_item{read_item(){};
template<class T>operator T(){T t;std::cin>>t;return t;}};
char splf(int i,int n){return(i+1<n)?' ':'\n';};
template<bool up>
struct _RI{int i;_RI(int a):i(a){}
int operator*(){return i;}void operator++(){i+=(up?1:-1);}
bool operator!=(const _RI& r){return up?i<r.i:i>=r.i;}};
template<bool up>
struct _RX{_RI<up> x,y;_RI<up> begin(){return x;}_RI<up> end(){return y;}
_RX(int a,int b):x(up?a:(a-1)),y(b){}_RX(int a):_RX(up?0:a,up?a:0){}};
typedef _RX<true> range;
typedef _RX<false> revrange;

/* types and constants */
typedef int64_t i64;
typedef uint64_t u64;
// const i64 inf = (i64)1.05e18;
// const int inf = (int)1.05e9;

using namespace std;

void compute_next_table(vector<pair<int,i64>>& parent, vector<pair<int,i64>>& table, int w, i64 v, int max_w) {

	static vector<pair<int,i64>> temp;

	temp.clear();
	temp.reserve(parent.size() * 2);

	for(auto& p_wv : parent) {
		int pw = p_wv.first;
		i64 pv = p_wv.second;
		int nw = pw + w;
		i64 nv = pv + v;
		temp.push_back({pw, pv});
		temp.push_back({nw, nv});
	}

	sort(temp.begin(), temp.end());
	table.clear();
	int last_w = -1;
	i64 last_v = -1;

	for(auto& t_wv : temp) {
		int tw = t_wv.first;
		i64 tv = t_wv.second;
		if(tw > max_w) {
			break;
		}
		if(last_v >= tv) {
			continue;
		}
		if(last_w == tw) {
			assert(table.size() > 0);
			table[table.size() - 1].second = tv;
		} else {
			table.push_back({tw, tv});
		}
		last_w = tw;
		last_v = tv;
	}
}

i64 find_max_v(vector<pair<int,i64>>& table, int w) {
	pair<int, i64> lo = {w + 1, 0};
	auto it = lower_bound(table.begin(), table.end(), lo) - table.begin() - 1;
	return table[it].second;
}

int go_parent(int x) {
	return ((x - 1) >> 1);
}

int main() {

	cin.tie(0);
	ios::sync_with_stdio(false);

	const int N = read_item();
	vector<int> Vs(N), Ws(N);
	for(auto i : range(N)) {
		Vs[i] = read_item();
		Ws[i] = read_item();
	}
	const int Q = read_item();
	vector<int> Xs(Q), Ls(Q);
	for(auto i : range(Q)) {
		Xs[i] = read_item();
		Xs[i] -= 1;
		Ls[i] = read_item();
	}
	int max_l = 0;
	for(auto l : Ls) {
		max_l = max(max_l, l);
	}

	int max_node_num = min(N, (1 << 11));
	vector<vector<pair<int,i64>>> tables(max_node_num);

	tables[0].push_back({0, 0});
	tables[0].push_back({Ws[0], Vs[0]});

	for(auto i : range(1, max_node_num)) {
		int parent = go_parent(i);
		auto& par_table = tables[parent];
		auto& table = tables[i];
		compute_next_table(par_table, table, Ws[i], Vs[i], max_l);
	}

	vector<pair<int,i64>> q_table, temp;

	for(auto q_i : range(Q)) {

		int x = Xs[q_i];
		int l = Ls[q_i];

		if(x < max_node_num) {
			auto ans = find_max_v(tables[x], l);
			printf("%lld\n", ans);
			continue;
		}

		q_table.clear();
		q_table.push_back({0, 0});
		q_table.push_back({Ws[x], Vs[x]});

		x = go_parent(x);
		while(x >= max_node_num) {
			compute_next_table(q_table, temp, Ws[x], Vs[x], l);
			swap(q_table, temp);
			x = go_parent(x);
		}

		i64 ans = 0;

		for(auto& q_wv : q_table) {
			int qw = q_wv.first;
			i64 qv = q_wv.second;
			if(qw > l) {
				continue;
			}
			int rem_w = l - qw;
			auto rem_v = find_max_v(tables[x], rem_w);
			i64 v = qv + rem_v;
			ans = max(ans, v);
		}

		printf("%lld\n", ans);
	}

	return 0;
}

/* wafdayo~~~ */


Measured memory_bytes: 49556