#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cwchar>
#include <cwctype>
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <codecvt>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <shared_mutex>
#include <boost/format.hpp>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/optional.hpp>

#ifdef DEBUG
#include "libs/debug.cpp"
#define DEBUG 1
#else
#define DEBUG 0
#endif

#define rep(i, n) repr((i), 0, (n))
#define repr(i, a, t) reps((i), (a), (t), 1)
#define reps(i, a, t, s) for (long long (i) = (a); (i) < (long long)(t); (i) += (s))
#define rrep(i, n) rrepr((i), (n), 0)
#define rrepr(i, a, t) rreps((i), (a), (t), 1)
#define rreps(i, a, t, s) for (long long (i) = (a) - 1; (i) >= (long long)(t); (i) -= (s))
#define each(v, c) for (auto &&(v) : (c))
#define all(c) (c).begin(), (c).end()

using namespace std;

// コンテナのメンバ関数を使わないと O(N)
template<typename T>
typename set<T>::iterator lower_bound(typename set<T>::iterator l, typename set<T>::iterator r, const T &v);
template<typename T>
typename set<T>::iterator upper_bound(typename set<T>::iterator l, typename set<T>::iterator r, const T &v);
template<typename S, typename T>
typename map<S, T>::iterator lower_bound(typename map<S, T>::iterator l, typename map<S, T>::iterator r, const T &v);
template<typename S, typename T>
typename map<S, T>::iterator upper_bound(typename map<S, T>::iterator l, typename map<S, T>::iterator r, const T &v);

namespace mp = boost::multiprecision;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using ml = mp::cpp_int;
template<typename T>
using optional = boost::optional<T>;
const auto none = boost::none;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;

auto main() -> int {
  cout << fixed << setprecision(10);
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  ifstream ifs;
  if (DEBUG) {
    ifs.open("src/_in.txt");
    cin.rdbuf(ifs.rdbuf());
  }

  ll N;
  cin >> N;
  vector<tuple<ll, ll>> VW(N);
  rep (i, N) {
    cin >> get<0>(VW[i]) >> get<1>(VW[i]);
  }
  ll Q;
  cin >> Q;
  vector<tuple<ll, ll>> VL(Q);
  rep (i, Q) {
    cin >> get<0>(VL[i]) >> get<1>(VL[i]);
  }
  VW.insert(VW.begin(), make_tuple(0, 0));

  const ll BIT = 1 << 10;
  vector<vector<ll>> dp1(BIT, vector<ll>(100100, 0));
  repr (i, 1, BIT) {
    if (i > N) break;
    auto[v, w] = VW[i];
    dp1[i] = dp1[i >> 1];
    rrep (j, dp1[i].size() - w) {
      dp1[i][j + w] = max(dp1[i][j + w], dp1[i >> 1][j] + v);
    }
  }

  each (vl, VL) {
    auto[v, l] = vl;
    if (v < BIT) {
      cout << dp1[v][l] << "\n";
      continue;
    }

    ll p = v;
    vector<ll> children;
    while (p >= BIT) {
      children.push_back(p);
      p >>= 1;
    }

    ll ans = 0;
    rep (choices, 1 << children.size()) {
      ll ws = 0;
      ll vs = 0;
      rep (j, children.size()) {
        if (choices >> j & 1) {
          auto[v1, w1] = VW[children[j]];
          vs += v1;
          ws += w1;
        }
      }
      if (ws <= l) {
        ans = max(ans, vs + dp1[p][l - ws]);
      }
    }
    cout << ans << "\n";
  }
}

Measured memory_bytes: 810044