#include <bits/stdc++.h>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define sz(x) static_cast<int>((x).size())

using LL = long long;
template<class T, size_t D>
struct vec : vector<vec<T, D - 1>> {
  template<class... Args>
  vec(size_t n = 0, Args... args)
    : vector<vec<T, D - 1>>(n, vec<T, D - 1>(args...)) {}
};
template<class T>
struct vec<T, 1> : vector<T> {
  template<class... Args>
  vec(Args... args)
    : vector<T>(args...) {}
};

template<class T>
inline bool asMn(T& a, const T& b) { return a > b ? a = b, true : false; }
template<class T>
inline bool asMx(T& a, const T& b) { return a < b ? a = b, true : false; }
inline int nex(int i, int n) { return i == n - 1 ? 0 : i + 1; }
inline int pre(int i, int n) { return !i ? n - 1 : i - 1; }

const int MAGIC = 10;
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());

int main() {
  ios_base::sync_with_stdio(0); cin.tie(0);

  int n; cin >> n;
  vec<int, 1> v(n), w(n);
  for (int i = 0; i < n; ++i) {
    cin >> v[i] >> w[i];
  }

  vec<pair<int, int>, 2> query(n);
  int q; cin >> q;
  for (int i = 0; i < q; ++i) {
    int u, l; cin >> u >> l; --u;
    query[u].emplace_back(l, i);
  }
  for (int u = 0; u < n; ++u) {
    sort(all(query[u]));
  }

  vec<int, 2> adjEdge(n);
  vec<int, 1> par(n, -1), dep(n);
  for (int i = 2; i <= n; ++i) {
    adjEdge[(i >> 1) - 1].emplace_back(i - 1);
    par[i - 1] = (i >> 1) - 1;
    dep[i - 1] = __lg(i);
  }

  vec<int, 1> ans(q);

  array<int, static_cast<int>(1e5) + 1> mxV2;
  function<void(int)> solve = [&](int u) {
    if (dep[u] <= MAGIC) {
      if (dep[u] == MAGIC) {
        fill(all(mxV2), 0);
      }

      vec<int, 1> anc;
      for (int i = u; ~i; i = par[i]) {
        anc.emplace_back(i);
      }

      vec<pair<int, int>, 1> ret;
      for (int mask = 0; mask < 1 << sz(anc); ++mask) {
        int sumV = 0, sumW = 0;
        for (int i = 0; i < sz(anc); ++i) {
          if (mask >> i & 1) {
            sumV += v[anc[i]];
            sumW += w[anc[i]];
          }
        }
        if (sumW < sz(mxV2)) {
          ret.emplace_back(sumW, sumV);
          asMx(mxV2[sumW], sumV);
        }
      }
      sort(all(ret));

      int iRet = 0, mxV = 0;
      for (auto& i : query[u]) {
        while (iRet < sz(ret) && ret[iRet].first <= i.first) {
          asMx(mxV, ret[iRet].second);
          ++iRet;
        }
        ans[i.second] = mxV;
      }

      if (dep[u] == MAGIC) {
        for (int i = 1; i < sz(mxV2); ++i) {
          asMx(mxV2[i], mxV2[i - 1]);
        }
      }
    } else {
      vec<int, 1> anc;
      for (int i = u; dep[i] > MAGIC; i = par[i]) {
        anc.emplace_back(i);
      }

      for (auto& iQ : query[u]) {
        for (int mask = 0; mask < 1 << sz(anc); ++mask) {
          int sumV = 0, sumW = 0;
          for (int i = 0; i < sz(anc); ++i) {
            if (mask >> i & 1) {
              sumV += v[anc[i]];
              sumW += w[anc[i]];
            }
          }

          if (iQ.first - sumW >= 0) {
            asMx(ans[iQ.second], mxV2[iQ.first - sumW] + sumV);
          }
        }
      }
    }

    for (auto& i : adjEdge[u]) {
      solve(i);
    }
  };
  solve(0);

  for (auto& i : ans) {
    cout << i << '\n';
  }

  return 0;
}

Measured memory_bytes: 27748