#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma region template
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vvll = vector<vi>;
using vvvll = vector<vvll>;
using vld = vector<ld>;
using vvld = vector<vld>;
using vvvld = vector<vvld>;
using vs = vector<string>;
using pii = pair<int, int>;
using vp = vector<pii>;
template <typename T>
using pqrev = priority_queue<T, vector<T>, greater<T>>;
#define rep(i, n) for (int i = 0, i##_end = (n); i < i##_end; i++)
#define repb(i, n) for (int i = (n)-1; i >= 0; i--)
#define repr(i, a, b) for (int i = (a), i##_end = (b); i < i##_end; i++)
#define reprb(i, a, b) for (int i = (b)-1, i##_end = (a); i >= i##_end; i--)
#define ALL(a) (a).begin(), (a).end()
#define SZ(x) ((int)(x).size())
//*
constexpr ll MOD = 1e9 + 7;
/*/
constexpr ll MOD = 998244353;
//*/
constexpr ll INF = 1e+18;
constexpr ld EPS = 1e-12L;
constexpr ld PI = 3.14159265358979323846L;
constexpr ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
constexpr ll LCM(ll a, ll b) { return a / GCD(a, b) * b; }
template <typename S, typename T>
constexpr bool chmax(S &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename S, typename T>
constexpr bool chmin(S &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#ifdef OJ_LOCAL
#include "dump.hpp"
#else
#define dump(...) ((void)0)
#endif
template <typename T>
bool print_(const T &a) {
  cout << a;
  return true;
}
template <typename T>
bool print_(const vector<T> &vec) {
  for (auto &a : vec) {
    cout << a;
    if (&a != &vec.back()) {
      cout << " ";
    }
  }
  return false;
}
template <typename T>
bool print_(const vector<vector<T>> &vv) {
  for (auto &v : vv) {
    for (auto &a : v) {
      cout << a;
      if (&a != &v.back()) {
        cout << " ";
      }
    }
    if (&v != &vv.back()) {
      cout << "\n";
    }
  }
  return false;
}
void print() { cout << "\n"; }
template <typename Head, typename... Tail>
void print(Head &&head, Tail &&... tail) {
  bool f = print_(head);
  if (sizeof...(tail) != 0) {
    cout << (f ? " " : "\n");
  }
  print(forward<Tail>(tail)...);
}
#pragma endregion

void Pr(bool f){
  cout << (f ? "Yes" : "No") << "\n";
  exit(0);
}

int vt[2][20];
int wt[2][20];
pii x1[1 << 20];
pii x2[1 << 20];

// v1をソートし、v2を対応する位置に移動する
template <typename T, typename U>
void pairSort(vector<T> &v1, vector<U> &v2, bool rev = false) {
  int n = v1.size();
  vector<pair<T, U>> p(n);
  for (int i = 0; i < n; i++) {
    p[i] = pair<T, U>(v1[i], v2[i]);
  }
  if (rev) {
    sort(p.begin(), p.end(), [&](const auto &l, const auto &r) {
      if (l.first == r.first)
        return l.second > r.second;
      else
        return l.first < r.first;
    });
  } else {
    sort(p.begin(), p.end());
  }
  for (int i = 0; i < n; i++) {
    v1[i] = p[i].first;
    v2[i] = p[i].second;
  }
}


int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  int n;
  cin >> n;
  vi v(n+1), w(n+1);
  repr(i, 1, n+1){
    cin >> v[i] >> w[i];
  }
  int q;
  cin >> q;
  vi qid(q);
  iota(ALL(qid), 0ll);
  vp qq(q);
  rep(i, q){
    cin >> qq[i].first >> qq[i].second;
  }
  pairSort(qq, qid);
  vi ans(q, 0);
  int pre = -1;
  int pree = -1;
  rep(it, q){
    int xx = qq[it].first, W = qq[it].second;
    vi c(2, 0);
    int cc = 0;
    {
      int xxx = xx;
      while(xxx){
        cc++;
        xxx >>= 1;
      }
    }
    c[1] = cc/2;
    c[0] = cc-c[1];
    int xxx = xx;
    if(pree != xx)rep(i, c[0]){
      vt[0][i] = v[xxx];
      wt[0][i] = w[xxx];
      xxx >>= 1;
    }
    if(pre != (xx >> c[0]))rep(i, c[1]){
      vt[1][i] = v[xxx];
      wt[1][i] = w[xxx];
      xxx >>= 1;
    }
    int bitN = 1 << c[0];
    if(pree != xx){
      rep(i, bitN){
        x1[i].first = 0;
        x1[i].second = 0;
      }
      rep(i, bitN){
        rep(j, c[0]){
          if((i >> j) & 1){
            x1[i].first += wt[0][j];
            x1[i].second += vt[0][j];
          }
        }
      }
      sort(x1, x1+bitN);
    }
    int bitN2 = 1 << c[1];
    if(pre != (xx >> c[0])){
      rep(i, bitN2){
        x2[i].first = 0;
        x2[i].second = 0;
      }
      rep(i, bitN2){
        rep(j, c[1]){
          if((i >> j) & 1){
            x2[i].first += wt[1][j];
            x2[i].second += vt[1][j];
          }
        }
      }
      sort(x2, x2+bitN2);
      repr(i, 1, bitN2){
        chmax(x2[i].second, x2[i-1].second);
      }
    }
    int j = bitN2-1;
    rep(i, bitN){
      while(j > 0 && x1[i].first+x2[j].first > W){
        j--;
      }
      if(x1[i].first+x2[j].first <= W)
        chmax(ans[qid[it]], x1[i].second+x2[j].second);
    }
    pre = xx >> c[0];
    pree = xx;
  }
  for(auto&& e: ans){
    print(e);
  }
}


Measured memory_bytes: 7720