#include "bits/stdc++.h"
#define YES "YES"
#define NO "NO"
#define Yes "Yes"
#define No "No"
#define DEFAULT LL ans=solve();\
 	if(ans==NONE){\
 	}else{\
 		cout << ans << endl;\
 	}
#define YESNO three(solve(),(OUT(YES),1),(OUT(NO),0))
#define YesNo three(solve(),OUT(Yes),OUT(No))
#define ECHO OUT(solve())
#define three(A,B,C) ((A)?(B):(C))
#define FOR(i,a,b)  for(LL i=(a);i< (LL)(b);i++)
#define EFOR(i,a,b) for(LL i=(a);i<=(LL)(b);i++)
#define RFOR(i,a,b) for(LL i=(b-1);i>=(LL)(a);i--)
#define REP(i,b) FOR(i,zero,b)
#define rep REP
#define EREP(i,b) EFOR(i,zero,b)
#define RREP(i,b) RFOR(i,zero,b)
#define ALL(c) c.begin(),c.end()
#define UNIQUE(c) sort(ALL(c));c.erase(unique(ALL(c)),c.end())
#define MAX(c) (*max_element(ALL(c)))
#define MIN(c) (*min_element(ALL(c)))
#define MP make_pair
#define FI first
#define SE second
#define SI(x) (LL(x.size()))
#define PB push_back
#define DEBUG(a) OUT(a)
#define DEBUG2(a,b) OUT2(a,b)
#define cat cout << __LINE__ << endl
#define OUT(a) cout << (a) << endl
#define OUT2(a,b) cout << (a) <<" "<<(b) << endl
#define zero 0LL
#define all ALL
#define pb emplace_back
#define eb pb
using namespace std;
template<typename T> inline void maximize(T &a, T b) { a = max(a, b); }
template<typename T> inline void minimize(T &a, T b) { a = min(a, b); }
template<typename T> inline bool middle(T a, T b, T c) { return b <= a && a <= c; }
template<class T> inline bool MX(T &l, const T &r) { return l < r ? l = r, 1 : 0; }
template<class T> inline bool MN(T &l, const T &r) { return l > r ? l = r, 1 : 0; }
typedef int LL;
typedef double ld;
typedef int ut;
typedef vector<ut> VI;
typedef vector<VI> VII;
typedef pair<ut, ut> pr;
typedef pair<ut, pr> ppr;
typedef vector<pr> Vpr;
typedef vector<ppr> Vppr;
typedef tuple<int, int, int, int> tapu;
typedef vector<tapu> Vtapu;
typedef priority_queue<pr, Vpr > PQ;
inline void outputVI(VI x) { REP(i, SI(x)) { cout << three(i, " ", "") << x[i]; }OUT(""); }
const int SIZE1 = 5e5 + 1000;
const int SIZE2 = 5010;
const int SIZE3 = 430;
const int SIZE = SIZE1;
const int MAPSIZE = 40;
const LL p998 = 998244353;
const LL p107 = 1000000007;
const LL p = p107;
const LL INF = 1LL << 30;
const long double EPS = 1e-7;
const LL NONE=-2;
#define endl "\n"
ut N, M,K,Q, D, H;
LL W[SIZE];
LL V[SIZE];
Vpr reals[SIZE];
LL solve(){
	cin >> N;
	LL border=1<<9;
	EFOR(i,1,N){
		cin >> V[i] >> W[i];	
		if(i==1){
			reals[i].pb(0,0);
			reals[i].pb(W[i],V[i]);
		}else if(i<border or i>=border*2){
			Vpr v;
			LL sie=reals[i/2].size();
			v.resize(sie);
			REP(j,sie){
					v[j]=pr(reals[i/2][j].first+W[i],reals[i/2][j].second+V[i]);
			}
			int n1=0,n2=0;
			reals[i].pb(0,0);
			while(min(n1,n2)!=sie){
				pr tar;
				if(n1!=sie and( n2==sie or reals[i/2][n1].first<v[n2].first)){
					tar=reals[i/2][n1];
					n1++;
				}else{
					tar=v[n2];
					n2++;
				}
				if(reals[i].back().first==tar.first and reals[i].back().second<tar.second) reals[i].pop_back();
				if(reals[i].back().second<tar.second) reals[i].pb(tar);
			}
		}else{
			reals[i].pb(0,0);
			reals[i].pb(W[i],+V[i]);	
		}
	}
	EFOR(i,1,N){
		if(i<border) reverse(ALL(reals[i]));
	}
	cin >> Q;
	REP(i,Q){
		LL v,L;
		LL ans=0;
		cin >> v >> L;
		if(v<border){
			for(auto x:reals[v]){
				if(x.first<=L){
					ans=x.second;
					break;
				}
			}
		}else{
			LL anc=v;
			while(anc>=border){
				anc/=2;
			}

			LL now=0;
			for(auto x:reals[v]){
				if(x.first>L) continue;
				while(x.first+reals[anc][now].first>L) now++;
				MX(ans,x.second+reals[anc][now].second);
			}
		}
		cout << ans <<endl;
	}
	return NONE;
}
 signed main() {
 
	ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(10);
	LL T=1;

	REP(i,T){
		DEFAULT;

	}
	
//	YesNo;
//	YESNO;
//	three(solve(),OUT("First"),OUT("Second"));
	//cin >> N;
	return 0;
 
}


Measured memory_bytes: 721652