#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

template <typename Monoid>
struct RMQ {
  RMQ(int sz, const Monoid UNITY) : UNITY(UNITY) {
    init(sz);
    dat.assign((n << 1) - 1, UNITY);
  }

  RMQ(const vector<Monoid> &a, const Monoid UNITY) : UNITY(UNITY) {
    int a_sz = a.size();
    init(a_sz);
    dat.resize((n << 1) - 1);
    REP(i, a_sz) dat[n - 1 + i] = a[i];
    for (int i = n - 2; i >= 0; --i) dat[i] = max(dat[(i << 1) + 1], dat[(i << 1) + 2]);
  }

  void update(int node, Monoid val) {
    node += n - 1;
    dat[node] = val;
    while (node > 0) {
      node = (node - 1) >> 1;
      dat[node] = max(dat[(node << 1) + 1], dat[(node << 1) + 2]);
    }
  }

  Monoid query(int a, int b) { return query(a, b, 0, 0, n); }

  int find(int a, int b, Monoid val) { return find(a, b, val, 0, 0, n); }

  Monoid operator[](const int idx) const { return dat[idx + n - 1]; }

private:
  int n = 1;
  const Monoid UNITY;
  vector<Monoid> dat;

  void init(int sz) { while (n < sz) n <<= 1; }

  Monoid query(int a, int b, int node, int left, int right) {
    if (right <= a || b <= left) return UNITY;
    if (a <= left && right <= b) return dat[node];
    return max(query(a, b, (node << 1) + 1, left, (left + right) >> 1), query(a, b, (node << 1) + 2, (left + right) >> 1, right));
  }

  int find(int a, int b, Monoid val, int node, int left, int right) {
    if (dat[node] < val || right <= a || b <= left) return -1;
    if (right - left == 1) return node - (n - 1);
    int res_l = find(a, b, val, (node << 1) + 1, left, (left + right) >> 1);
    if (res_l != -1) return res_l;
    return find(a, b, val, (node << 1) + 2, (left + right) >> 1, right);
  }
};

int main() {
  const int X = 100000;
  int n; cin >> n;
  vector<int> v(n), w(n); REP(i, n) cin >> v[i] >> w[i];
  int border = (1 << 9) - 1;
  vector up(min(n, border), vector(X + 1, -INF));
  up[0][0] = 0;
  up[0][w[0]] = v[0];
  FOR(i, 1, up.size()) {
    int par = (i - 1) / 2;
    REP(j, X + 1) {
      chmax(up[i][j], up[par][j]);
      if (j + w[i] <= X) chmax(up[i][j + w[i]], up[par][j] + v[i]);
    }
  }
  REP(i, up.size()) FOR(j, 1, X + 1) chmax(up[i][j], up[i][j - 1]);
  int q; cin >> q;
  while (q--) {
    int ver, l; cin >> ver >> l; --ver;
    if (ver < border) {
      cout << up[ver][l] << '\n';
      continue;
    }
    vector<int> item;
    while (ver >= border) {
      item.emplace_back(ver);
      ver = (ver - 1) / 2;
    }
    int ans = 0;
    REP(i, 1 << item.size()) {
      int val = 0, wei = 0;
      REP(j, item.size()) {
        if (i >> j & 1) {
          val += v[item[j]];
          wei += w[item[j]];
        }
      }
      if (wei <= l) chmax(ans, up[ver][l - wei] + val);
    }
    cout << ans << '\n';
  }
  return 0;
}


Measured memory_bytes: 205948