#include <vector>
#include <algorithm>
#include <unistd.h>

struct IO {
  static const int bufsize=1<<25;
  char ibuf[bufsize], obuf[bufsize];
  char *ip, *op;
  IO(): ip(ibuf), op(obuf) { for(int t = 0, k = 0; (k = read(STDIN_FILENO, ibuf+t, sizeof(ibuf)-t)) > 0; t+=k); }
  ~IO(){ for(int t = 0, k = 0; (k = write(STDOUT_FILENO, obuf+t, op-obuf-t)) > 0; t+=k); }
  long long scan_int(){
    long long x=0;
    bool neg=false;
    for(;*ip<'+';ip++) ;
    if(*ip=='-'){ neg=true; ip++;}
    else if(*ip=='+') ip++;
    for(;*ip>='0';ip++) x = 10*x+*ip-'0';
    if(neg) x = -x;
    return x;
  }
  void put_int(long long x, char c=0){
    static char tmp[20];
    if(x==0) *op++ = '0';
    else {
      int i;
      if(x<0){
        *op++ = '-';
        x = -x;
      }
      for(i=0; x; i++){
        tmp[i] = x % 10;
        x /= 10;
      }
      for(i--; i>=0; i--)
        *op++ = tmp[i]+'0';
    }
    if(c) *op++ = c;
  }
  void put_double(double x, char c=0){
    unsigned y;
    const int mask = (1<<24) - 1;
    put_int(x);
    *op++ = '.';
    x = x - (int) x;
    if(x < 0) x = -x;
    y = x * (1<<24);

    for(int i=0;i<7;i++){
      y *= 10;
      *op++ = '0' + (y>>24);
      y &= mask;
    }
    if(c) *op++ = c;
  }
  inline char scan_char(){ return *ip++; }
  inline void put_char(char c){ *op++ = c; }
  inline char *scan_string(){ char *s = ip; while(*ip!='\n'&&*ip!=' ') ip++; *ip++='\0'; return s;}
  inline void put_string(const char *s, char c=0){ while(*s) *op++=*s++; if(c) *op++=c;}
} io;


using u32 = unsigned int;
using u64 = unsigned long long int;

int n;
int V[1<<18];
int W[1<<18];

constexpr int K = 11;

std::vector<std::pair<u32, u64> > s[(1<<K)];

void update(std::vector<std::pair<u32, u64> >& p, std::vector<std::pair<u32, u64> >& s, int i){
  auto j = p.begin();
  auto k = j;
  s.reserve(p.size() << 1);
  while(1) {
    if(j == p.end()){
      while(k != p.end() && k->first + W[i] <= 100000){
        s.emplace_back(k->first + W[i], k->second + V[i]);
        ++k;
      }
      break;
    }
    if(k == p.end() || k->first + W[i] > 100000){
      s.insert(s.end(), j, p.end());
      break;
    }
    if(j->first <= k->first + W[i] && j->second >= k->second + V[i]) ++k;
    else if(j->first >= k->first + W[i] && j->second <= k->second + V[i]) ++j;
    else if(j->first <= k->first + W[i]) s.push_back(*j++);
    else s.emplace_back(k->first + W[i], k->second + V[i]), ++k;
  }
}

int main(){
  n = io.scan_int();
  for(int i = 0; i < n; ++i) V[i] = io.scan_int(), W[i] = io.scan_int();
  int q;
  q = io.scan_int();

  s[0].emplace_back(0, 0);
  for(int i = 1; i <= std::min((1<<K)-1, n); ++i){
    update(s[i/2], s[i], i-1);
/*
io.put_int(i, ' ');
io.put_int(s[i].size(), '\n');
for(auto x: s[i]) io.put_string(" ("), io.put_int(x.first), io.put_string(", "), io.put_int(x.second, ')');
io.put_char('\n');
*/
  }

  while(q--){
    u32 v, l;
    v = io.scan_int();
    l = io.scan_int();
    std::vector<std::pair<u32, u64> > t[2];
    t[0].emplace_back(0, 0);
    while(v >= (1<<K)){
      update(t[0], t[1], v-1);
      t[0] = std::move(t[1]);
      t[1].clear();
      v >>= 1;
    }
/*
io.put_int(v, ' ');
io.put_int(t[0].size(), '\n');
for(auto x: t[0]) io.put_string(" ("), io.put_int(x.first), io.put_string(", "), io.put_int(x.second, ')');
io.put_char('\n');
*/
    u64 best = 0;
    auto j = s[v].rbegin();
    for(auto i = t[0].begin(); i != t[0].end() && i->first <= l; ++i){
      while(i->first + j->first > l) ++j;
      if(best <= i->second + j->second) best = i->second + j->second;
    }
    io.put_int(best, '\n');
  }
  return 0;
}


Measured memory_bytes: 51040