#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main(){
  int N;
  cin >> N;
  const int H = 18;
  vector<int> V(N), W(N);
  for(int i = 0; i < N; ++i)
    cin >> V[i] >> W[i];
  

  int Q;
  cin >> Q;
  vector<vector<pair<int,int>>> q(N);
  for(int i = 0; i < Q; ++i){
    int idx, L;
    cin >> idx >> L;
    --idx;
    q[idx].emplace_back(i,L);
  }
  vector<int> ans(Q);
  int h = 10;
  vector dp(1<<(h+1), vector<pair<int,long long>>());
  dp[0] = {{0,0},{W[0],V[0]}};

  const long long INF = 1e18;
  auto dfs2 = [&](auto self, int v, vector<pair<int, long long>> T, const int p) -> void {
                int sz = T.size();
                for(int i = 0; i < sz; ++i){
                  auto e = T[i];
                  if(e.first+W[v] > 1e5) continue;
                  T.emplace_back(e.first+W[v],e.second+V[v]);
                }

                sort(T.begin(), T.end());
                for(size_t i = 1; i < T.size(); ++i)
                  T[i].second = max(T[i].second, T[i-1].second);
                T.erase(unique(T.begin(), T.end()), T.end());

                for(auto [idx, l] : q[v]){
                  long long a = 0;
                  for(auto e : T){
                    if(e.first > l) break;
                    pair<int, long long> r(l-e.first,INF);
                    a = max(a, (prev(upper_bound(dp[p].begin(), dp[p].end(), r)))->second + e.second);
                  }
                  ans[idx] = a;
                }
                
                if(2*v+1 < N) self(self,2*v+1,T,p);
                if(2*v+2 < N) self(self,2*v+2,T,p);
              };
  
  auto dfs1 = [&](auto self, int v, int hh) -> void {
                if(hh >= h){
                  vector<pair<int,long long>> ini = {{0,0LL}};
                  dfs2(dfs2,v,ini,(v-1)/2);
                  return;
                }
               for(auto [idx, l] : q[v]){
                 auto p = *(prev(upper_bound(dp[v].begin(), dp[v].end(), make_pair(l,INF))));
                 ans[idx] = p.second;
               }
               if(v*2+1 < N){
               // if(hh < h and v*2+1 < N){
                 int u = v*2+1;
                 // vector<pair<int, long long>>& dp_ = dp[u];
                 dp[u] = dp[v];
                 for(auto e : dp[v]){
                   if(e.first+W[u] > 1e5) continue;
                   dp[u].emplace_back(e.first+W[u],e.second+V[u]);
                 }
                 sort(dp[u].begin(), dp[u].end());
                 for(size_t i = 1; i < dp[u].size(); ++i)
                   dp[u][i].second = max(dp[u][i].second, dp[u][i-1].second);
                 dp[u].erase(unique(dp[u].begin(), dp[u].end()), dp[u].end());
                 self(self,u,hh+1);
               }
               // }else{
               //    vector<pair<int,long long>> ini = {{0,0LL}};
               //    dfs2(dfs2,v,ini,v);
               // }
               // if(hh < h and v*2+2 < N){
               if(v*2+2 < N){
                 int u = v*2+2;
                 dp[u] = dp[v];
                 for(auto e : dp[v]){
                   if(e.first+W[u] > 1e5) continue;
                   dp[u].emplace_back(e.first+W[u],e.second+V[u]);
                 }
                 sort(dp[u].begin(), dp[u].end());
                 for(size_t i = 1; i < dp[u].size(); ++i)
                   dp[u][i].second = max(dp[u][i].second, dp[u][i-1].second);
                 dp[u].erase(unique(dp[u].begin(), dp[u].end()), dp[u].end());
                 self(self,u,hh+1);
               }
             };
  dfs1(dfs1,0,0);
  
  for(auto a : ans)
    cout << a << "\n";
}


Measured memory_bytes: 59072