Problem:
Score : 700 points

Problem Statement

We have a rooted binary tree with N vertices, where the vertices are numbered 1 to N.
Vertex 1 is the root, and the parent of Vertex i (i \geq 2) is Vertex \left[ \frac{i}{2} \right].

Each vertex has one item in it. The item in Vertex i has a value of V_i and a weight of W_i.
Now, process the following query Q times:

Given are a vertex v of the tree and a positive integer L.
Let us choose some (possibly none) of the items in v and the ancestors of v so that their total weight is at most L.
Find the maximum possible total value of the chosen items.

Here, Vertex u is said to be an ancestor of Vertex v when u is an indirect parent of v, that is, there exists a sequence of vertices w_1,w_2,\ldots,w_k (k\geq 2) where w_1=v, w_k=u, and w_{i+1} is the parent of w_i for each i.

Constraints

All values in input are integers.

1 \leq N < 2^{18}

1 \leq Q \leq 10^5

1 \leq V_i \leq 10^5

1 \leq W_i \leq 10^5

For the values v and L given in each query, 1 \leq v \leq N and 1 \leq L \leq 10^5.

Input

Let v_i and L_i be the values v and L given in the i-th query.
Then, Input is given from Standard Input in the following format:

N
V_1 W_1
:
V_N W_N
Q
v_1 L_1
:
v_Q L_Q

Output

For each integer i from 1 through Q,
the i-th line should contain the response to the i-th query.

Sample Input 1

3
1 2
2 3
3 4
3
1 1
2 5
3 5

Sample Output 1

0
3
3

In the first query, we are given only one choice: the item with (V, W)=(1,2). Since L = 1, we cannot actually choose it, so our response should be 0.

In the second query, we are given two choices: the items with (V, W)=(1,2) and (V, W)=(2,3). Since L = 5, we can choose both of them, so our response should be 3.

Sample Input 2

15
123 119
129 120
132 112
126 109
118 103
115 109
102 100
130 120
105 105
132 115
104 102
107 107
127 116
121 104
121 115
8
8 234
9 244
10 226
11 227
12 240
13 237
14 206
15 227

Sample Output 2

256
255
250
247
255
259
223
253

Solution in C (GCC 9.2.1) (judge verdict: Wrong Answer)

#include<stdio.h>
long long R=1,C=1,H[2000010],N[2000010],f[2000010];
long long MAX(long long  a,long long b){return a>b?a:b;}
//評価関数（いまはMIN）
int hyouka(int a,int b){
  if(C<b)return 1;
  if(C<a||b==0)return 0;
  return N[H[a]]<N[H[b]]?1:0;
}
//挿入関数
void hin(long long a){
  int i=C++;
  for(N[H[0]=R]=a;hyouka(0,i/2);i/=2)H[i]=H[i/2];
  H[i]=R++;
}
//取り出す関数
int hout(){
  int rt=H[1],i,j=2,k=H[--C];
  for(i=1;hyouka(i,C);i=j)H[i]=H[j=i*2+1-hyouka(i*2,i*2+1)];
  H[j/2]=k;
  return rt;
}
int ub(long long l,long long r,long long n,long long *d){
  int m=(l+r)/2;
  if(l==r)return m;
  return n<d[m]?ub(l,m,n,d):ub(m+1,r,n,d);
}
int lb(long long l,long long r,long long n,long long *d){
  int m=(l+r)/2;
  if(l==r)return m;
  return n<=d[m]?lb(l,m,n,d):lb(m+1,r,n,d);
}
long long n,a[100010],b[100010],c[100010],d[100010],v[500010],w[500050],ans[100010];
void g(long long x,int y){//printf("%d\n",x);
  long long p=(1<<y),i,j,l,r;
  if(x-1>=n)return ;
  //printf("p%d\n",p);
  for(i=0;i<p;i++){
    v[i+p]=v[i]+a[x-1];
    w[i+p]=w[i]+b[x-1];
  }
  l=lb(0,n,x,c);
  r=ub(0,n,x,c);//printf("%d %d %d\n",l,r,2*p);
  for(i=l;i<r;i++){
    for(j=0;j<2*p;j++){//printf("vd:%d %d\n",v[j],w[j]);
      if(w[j]<=d[i])ans[i]=MAX(ans[i],v[j]);
    }
  }//printf("aaaa\n");
  g(x*2  ,y+1);
  g(x*2+1,y+1);
}
int main(){
  long long i,e,m;
  scanf("%lld",&n);
  for(i=0;i<n;i++)scanf("%lld %lld",&a[i],&b[i]);
  scanf("%lld",&m);
  for(i=0;i<m;i++){
    scanf("%lld %lld",&e,&f[R]);
    hin(e);
  }
  for(i=0;i<m;i++){
    e=hout();
    c[i]=N[e];
    d[i]=f[e];
  }
  //for(i=0;i<m;i++)printf("%d %d\n",c[i],d[i]);
  //printf("aa\n");
  v[0]=w[0]=0;
  g(1,0);
  for(i=0;i<m;i++)printf("%lld\n",ans[i]);
  return 0;
}