#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <list>
#include <ctime>
#include <sstream>
#include <queue>
#include <stack>
#include <random>
#include <bitset>
#include <numeric>
#include <cassert>
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;
#define rep(x, b, e) for(int x=(b); x<(e); ++x)
#define ford(x, b, e) for(int x=((int)(b))-1; x>=(e); --x)
#define all(c) c.begin(),c.end()
#define sz(x) ((int)((x).size()))
#define pb push_back
#define st first
#define nd second
#define mp(x,y) make_pair(x,y)
typedef short int sint;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it) {
	*this << ((it != d.b) ? ", " : "") << *it;
  }
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

const int nax = (1 << 18) + 1;
const int kax = 1e5;
int v[nax], w[nax];
int q;
vector<pii> que[nax];
int res[kax];
vector<pii> opts[nax];
int n;

vector<pii> improve(vector<pii> &inp) {
	sort(all(inp));
	int wsk = 0, nast = 0;
	vector<pii> res;
	for (auto i : inp) {
		if (res.size() > 0 && res.back().nd >= i.nd) {
			continue;
		}
		res.pb(i);
	}
	return res;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin >> n;
	rep(i,1,n+1) {
		cin >> v[i] >> w[i];
	}
	cin >> q;
	rep(i, 0, q) {
		int vv, c;
		cin >> vv >>c;
		que[vv].pb({c, i});
	}
	opts[1].pb({0,0});
	if (w[1] <= kax) {
		opts[1].pb({w[1], v[1]});
	}
	int level = 0;
	rep(node, 1, n+1) {
		if ((node&(node-1)) == 0) {
			++level;
		}
		if (level <= 12) {
			// just binsearch
			for (auto query : que[node]) {
				int pos = upper_bound(opts[node].begin(), opts[node].end(), mp(query.st + 1, -1)) - opts[node].begin() - 1;
				res[query.nd] = opts[node][pos].nd;
			}
			// left and right
			if (2*node <= n) {
				opts[2*node] = opts[node];
				for (auto opt : opts[node]) {
					if (opt.st + w[2*node] <= kax) {
						opts[2*node].pb({opt.st + w[2*node], opt.nd + v[2*node]});
					}
				}
				opts[2*node] = improve(opts[2*node]);
			}
			if (2*node + 1<=n) {
				opts[2*node + 1] = opts[node];
				for (auto opt : opts[node]) {
					if (opt.st + w[2*node + 1] <= kax) {
						opts[2*node + 1].pb({opt.st + w[2*node + 1], opt.nd + v[2*node + 1]});
					}
				}
				opts[2*node + 1] = improve(opts[2*node + 1]);
			}
		} else {
			if (que[node].size() > 0) {
				vector<pii> pars;
				int wsk = node;
				rep(kt, 12, level) {
					pars.pb({w[wsk], v[wsk]});
					wsk /= 2;
				}
				vector<pii> smallOpts;
				rep(m, 0, (1 << sz(pars))) {
					int cw = 0, cv = 0;
					rep(j, 0, sz(pars)) {
						if (m & (1<<j)) {
							cw += pars[j].st;
							cv += pars[j].nd;
						}
					}
					smallOpts.pb({cw, cv});
				}
				smallOpts = improve(smallOpts);
				for (auto query : que[node]) {
					for (auto smallOpt : smallOpts) {
						if (smallOpt.st > query.st) {
							continue;
						}
						int newL = query.st - smallOpt.st;
						int pos = upper_bound(opts[wsk].begin(), opts[wsk].end(), mp(newL + 1, -1)) - opts[wsk].begin() - 1;
						res[query.nd] = max(res[query.nd], smallOpt.nd + opts[wsk][pos].nd);
					}
				}
			}
		}
	}
	rep(i,0,q) {
		cout << res[i] << endl;
	}
}

Measured memory_bytes: 298520