#include <bits/stdc++.h>

using namespace std;

#define INF 100000000
#define YJ 1145141919
#define INF_INT_MAX 2147483647
#define INF_LL 9223372036854775
#define INF_LL_MAX 9223372036854775807
#define EPS 1e-10
#define MOD 1000000007
#define MOD9 998244353
#define Pi acos(-1)
#define LL long long
#define ULL unsigned long long
#define LD long double

#define int long long

using II = pair<int, int>;

int gcd(int a, int b) { return b != 0 ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a * b / gcd(a, b); }
int extgcd(int a, int b, int &x, int &y) { int g = a; x = 1; y = 0; if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x; return g; }

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(a)  begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())

const int MAX_N = 1 << 18 + 1;
const int MAX_Q = 100005;
int N;
int V[MAX_N], W[MAX_N];
int Q;
int v[MAX_Q], L[MAX_Q];

const int DEPTH = 9;
vector<II> VL1, VL2;

unordered_map<int, vector<II>> VL2_MAP;
int memoPos;

int solve(int pos, int maxW, int depth = 0) {
  if (depth == 0) {
    VL1.clear();
    VL2.clear();
    VL1.emplace_back(II(0,0));
    VL1.emplace_back(II(W[pos], V[pos]));
  } else if (depth == DEPTH) {
    if (VL2_MAP.count(pos)) {
      VL2 = VL2_MAP[pos];
      return solve(1, maxW, -1);
    }
    memoPos = pos;
    VL2.emplace_back(II(0,0));
    VL2.emplace_back(II(W[pos], V[pos]));
  } else if (0 < depth) {
    if (depth < DEPTH) {
      int length = VL1.size();
      REP(i,length) {
        VL1.emplace_back(II(W[pos] + VL1[i].first, V[pos] + VL1[i].second));
      }
    } else {
      int length = VL2.size();
      REP(i,length) {
        VL2.emplace_back(II(W[pos] + VL2[i].first, V[pos] + VL2[i].second));
      }
    }
  }
  
  if (pos != 1) {
    return solve(pos/2, maxW, depth+1);
  } else {
    if (VL2.size() == 0) {
      int ans = 0;
      for(const II& v : VL1) {
        if (v.first <= maxW) {
          ans = max(ans, v.second);
        }
      }
      return ans;
    }

    if (depth != -1) {
      sort(begin(VL2), end(VL2), [](const II& lh, const II& rh) {
          if (lh.first != rh.first) {
            return lh.first < rh.first;
          } else {
            return lh.second > rh.second;
          }
      });
      FOR(i,1,VL2.size()) {
        if (VL2[i].second < VL2[i-1].second) {
          VL2[i].second = VL2[i-1].second;
        }
      }
      VL2_MAP[memoPos] = VL2;
    }

    // cerr << "target w:" << maxW << endl;
    // for(auto v : VL1) {
    //   fprintf(stderr, "(%lld, %lld) ", v.first, v.second);
    // }
    // cerr << endl;
    // for(auto v : VL2) {
    //   fprintf(stderr, "(%lld, %lld) ", v.first, v.second);
    // }
    // cerr << endl;
    
    int ans = 0;
    REP(i, VL1.size()) {
      II vl1 = VL1[i];
      int vl2Index = upper_bound(begin(VL2), end(VL2), II(maxW - vl1.first, INF_LL)) - begin(VL2);
      if (vl2Index == 0) {
        continue;
      }
      ans = max(ans, vl1.second + VL2[vl2Index-1].second);
    }

    return ans;
  }
}

signed main()
{
  cin >> N;
  REP(n,N) {
    cin >> V[n+1] >> W[n+1];
  }

  cin >> Q;
  REP(q,Q) {
    cin >> v[q] >> L[q];
    if (v[q] == 1) {
      if (W[1] <= L[q]) {
        cout << V[1] << endl;
      } else {
        cout << 0 << endl;
      }
      continue;
    }
    cout << solve(v[q], L[q]) << endl;
  }

  return 0;
}


Measured memory_bytes: 12060