#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<utility>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e15;
constexpr long long MOD = 1'000'000'007; 

struct fast_io {
	fast_io(){
		std::cin.tie(nullptr);
		std::ios::sync_with_stdio(false);
	};
} fio;


vector<int> V, W;
vector<int> v(20), w(20);
vector<vector<pair<int,int>>> HOGE; 

const int NUM = 11;

int solve(int nV, int L){
	int ans = 0;
	int N = 0, tv = nV;
	bool flag = false;
	int ind = 0;
	
	while(tv){
		if(HOGE[tv].size()) {
			flag = true;
			ind = tv;
		}
		tv /= 2;
		N++;
	}
	
	if(flag == true) {// cout<<"<><>"<<" ind = "<<ind<<" N = "<<N<<" "<<HOGE[ind].size()<<endl;
		int n = NUM+1;
		int  m = N-n;
		//cout<<"n = "<<n<<" m = "<<m<<endl;
		vector<pair<int,int>>  fuga(1<<m);
		
		// for(int i = ind+1; i < N; i++){
			// w[i] = W[nV-1];
			// v[i] = V[nV-1];
			// nV /= 2;
		// }
		 for(int i = 0; i < m; i++){ //cout<<"i = "<<i<<" nv = "<<nV<<endl;
			w[i] = W[nV-1];
			v[i] = V[nV-1];
			// cout<<v[i]<<" "<<w[i]<<endl;
			nV /= 2;
		}
		
		for(int i = 0; i < 1<<m; i++){
			for(int j = 0; j < m; j++){
				if(1&(i>>j)){
					fuga[i].first += w[j];
					fuga[i].second += v[j];
				}
			}
		}
		
		sort(fuga.begin(),fuga.end());
		
		for(int i = 0; i < 1<<m; i++){
			if(i != (1<<m)-1){
				fuga[i+1].second = max(fuga[i].second,fuga[i+1].second);
			}
			// cout<<i<<" fuga "<<fuga[i].first<<" "<<fuga[i].second<<endl;
		}
		
		for(int i = 0; i < 1<<m; i++){
			pair<int,int> p = {L-fuga[i].first,INF};
			int x = lower_bound(HOGE[ind].begin(), HOGE[ind].end(), p) - HOGE[ind].begin() - 1;
			if(p.first >= 0) {
				ans = max(HOGE[ind][x].second+fuga[i].second,ans);
				
			}
		}
	} else {
		
		int n = N/2 , m = N-n;
		vector<pair<int,int>> hoge(1<<n), fuga(1<<m);
		
		// cout<<"N = "<<N<<" n = "<<n<<" m = "<<m<<endl;
		
		for(int i = 0; i < N; i++){
			w[i] = W[nV-1];
			v[i] = V[nV-1];
			nV /= 2;
			// cout<<V[nV-1]<<" "<<W[nV-1]<<endl;
		}
		
		for(int i = 0; i < 1<<n; i++){
			for(int j = 0; j < n; j++){
				if(1&(i>>j)){
					hoge[i].first += w[j];
					hoge[i].second += v[j];
				}
			}
		}
		
		sort(hoge.begin(),hoge.end());
		
		for(int i = 0; i < 1<<n; i++){
			if(i != (1<<n)-1){
				hoge[i+1].second = max(hoge[i].second,hoge[i+1].second);
			}
		}
		
		for(int i = 0; i < 1<<m; i++){
			for(int j = 0; j < m; j++){
				if(1&(i>>j)){
					fuga[i].first += w[j+n];
					fuga[i].second += v[j+n];
				}
			}
		}
		
		sort(fuga.begin(),fuga.end());
		
		for(int i = 0; i < 1<<m; i++){
			if(i != (1<<m)-1){
				fuga[i+1].second = max(fuga[i].second,fuga[i+1].second);
			}
		}
		
		for(int i = 0; i < 1<<m; i++){
			pair<int,int> p = {L-fuga[i].first,INF};
			int x = lower_bound(hoge.begin(), hoge.end(), p) - hoge.begin() - 1;
			if(p.first >= 0)ans = max(hoge[x].second+fuga[i].second,ans);
		}
		
	}
	return ans;
	
}


vector<int> tV, tW;

void rec(int cur, int N, int depth){// cout<<"depth = "<<depth<<" "<<tV.size()<<endl;
	
	if(cur > N) return ;
	
	tV.push_back(V[cur-1]);
	tW.push_back(W[cur-1]);
	
	if(depth == NUM) {
		
		// for(int i = 0; i < tV.size(); i++){
			// cout<<tV[i]<<" ";
			
		// }
		// cout<<endl;
		
		int n = depth+1;
		// int n = tV.size();
		
		HOGE[cur].resize(1<<n);
		
		for(int i = 0; i < 1<<n; i++){
			for(int j = 0; j < n; j++){
				if(1&(i>>j)){
					HOGE[cur][i].first += tW[j];
					HOGE[cur][i].second += tV[j];
				}
			}
		}
		
		sort(HOGE[cur].begin(),HOGE[cur].end());
		
		for(int i = 0; i < 1<<n; i++){
			if(i != (1<<n)-1){
				HOGE[cur][i+1].second = max(HOGE[cur][i].second,HOGE[cur][i+1].second);
			}
		}
		
		// for(int i = 0; i < HOGE[cur].size(); i++){
			// cout<<HOGE[cur][i].first<<" ";
		// }
		// cout<<endl;
		
		tV.pop_back();
		tW.pop_back();
		
		return ;
	}
		
	
	
	rec(cur * 2, N, depth+1);
	
	rec(cur * 2 + 1, N, depth+1);
	
	tV.pop_back();
	tW.pop_back();
	
}

signed main(){
	cout<<fixed<<setprecision(10);
	
	int N;
	int Q;
	
	cin>>N;
	
	V.resize(N);
	W.resize(N);
	HOGE.resize(1<<12);
	
	for(int i = 0; i < N; i++){
		cin>>V[i]>>W[i];
	}
	
	rec(1, N, 0);
	
	cin>>Q;
	
	for(int i = 0; i < Q; i++){ //cout<<"i = "<<i<<endl;
		int v, L;
		
		cin>>v>>L;
		
		cout<<solve(v, L)<<endl;
	}
	
	return 0;
}

Measured memory_bytes: 138612