#include <stdio.h>

#define tsort int
#define smaxn 100000 //最大要素数
#define smxbit 17 //要素のうち最大の数値のビット数
#define snobit 15 //1-smxbitの間で調整、増やせば速いがメモリを消費する
#define rev 0     //0で昇順、1で降順
#define snosc (1<<(snobit+1))

int sort(tsort *vlist,int *plist,int n){
  int h,i,j,k,l,m,s,p,qlist[smaxn],c[snosc][3],ss;
#if rev
  ss=snosc-1;
#else
  ss=0;
#endif
  tsort vj,vk,mx[snosc];
  for(i=0;i<snosc;i++){
    c[i][0]=-1;
    c[i][1]=-1;
  }
  for(i=0;i<n;i++){
    l=(int)(vlist[i]>>(smxbit-snobit))&(snosc-1)^(1<<snobit);
    if(c[l][0]==-1){
      c[l][0]=i;
      c[l][2]=i;
      mx[l]=vlist[i];
    }else{
      qlist[c[l][1]]=i;
#if rev
      if(vlist[i]<mx[l]){
#else
      if(vlist[i]>mx[l]){
#endif
        c[l][2]=i;
        mx[l]=vlist[i];
      }
    }
    c[l][1]=i;
    plist[i]=-1;
  }
#if rev
  for(h=snosc-1;h>=0;h--){
#else
  for(h=0;h<snosc;h++){
#endif
    s=c[h][0];
    if(s==-1){
#if rev
      if(h==ss) ss--;
      if(h!=snosc-1) c[h][1]=c[h+1][1];
#else
      if(h==ss) ss++;
      if(h!=0) c[h][1]=c[h-1][1];
#endif
      continue;
    }
    qlist[c[h][1]]=-1;
    while(qlist[s]!=-1){
      i=s;
      p=-1;
      while(i!=-1){
        j=i;
        k=qlist[j];
        if(k==-1) break;
        i=qlist[k];
#if rev
        if(vlist[j]<vlist[k])
#else
        if(vlist[j]>vlist[k])
#endif
        {
          if(p==-1){
            s=k;
          }else{
            qlist[p]=k;
          }
          l=j;
          j=k;
          k=l;
        }
        qlist[j]=i;
        p=j;
        vj=vlist[j];
        vk=vlist[k];
        while(k!=-1){
#if rev
          if((j==-1)|(vj<vk))
#else
          if((j==-1)|(vj>vk))
#endif
          {
            l=plist[k];
            plist[k]=plist[m];
            plist[m]=k;
            m=k;
            k=l;
            vk=vlist[k];
          }else{
            m=j;
            j=plist[j];
            vj=vlist[j];
          }
        }
      }
    }
    if(h==ss){
      c[h][0]=s;
    }else{
#if rev
      plist[c[h+1][1]]=s;
#else
      plist[c[h-1][1]]=s;
#endif
    }
    for(i=c[h][2];i!=-1;i=plist[i]) j=i;
    c[h][1]=j;
  }
  return(c[ss][0]);
}

//最小値・最大値
//型は可変
#define tmnmx int
tmnmx max(tmnmx p0,tmnmx p1){
  return(p0>p1?p0:p1);
}

int main(){
  int n,q,i,j,d[20][2],v[270000],w[270000],c[20][100005]={},p[100005],l[100005],f[270000]={};
  int a[100005],b[270005][3]={},r[100005][3],s[100005],t,u[100005],cr=1,plist[100005],g;
  scanf("%d",&n);
  for(i=1;i<=n;i++){
    scanf("%d",&v[i]);
    scanf("%d",&w[i]);
  }
  scanf("%d",&q);
  for(i=0;i<q;i++){
    scanf("%d",&p[i]);
    scanf("%d",&l[i]);
    for(j=p[i];j>0;j>>=1){
      if(f[j]>=l[i]) break;
      f[j]=l[i];
    }
    if(b[p[i]][0]==0){
      b[p[i]][1]=cr;
      g=1;
    }else{
      r[b[p[i]][2]][1]=cr;
      g=0;
    }
    r[cr][0]=l[i];
    r[cr][1]=0;
    r[cr][2]=i;
    b[p[i]][2]=cr;
    b[p[i]][0]++;
    if(g) cr++;
  }
  i=1;
  d[i][0]=0;
  d[i][1]=1;
  while(i>0){
    if(d[i][1]>n){
      d[i][0]=3;
    }else if(f[d[i][1]]==0){
      d[i][0]=3;
    }
    switch(d[i][0]){
      case 0:
        d[i][0]++;
        t=0;
        for(j=b[d[i][1]][1];j!=0;j=r[j][1]){
          s[t]=r[j][0];
          u[t]=j;
          t++;
        }
        t=sort(&s,&plist,t);
        c[i][0]=0;
        for(j=1;j<=f[d[i][1]];j++){
          if(w[d[i][1]]<=j){
            c[i][j]=max(c[i-1][j-w[d[i][1]]]+v[d[i][1]],c[i-1][j]);
          }else{
            c[i][j]=c[i-1][j];
          }
          while(t!=-1){
            if(r[u[t]][0]==j){
              a[r[u[t]][2]]=c[i][j];
              t=plist[t];
            }else{
              break;
            }
          }
        }
        break;
      case 1:
        d[i][0]++;
        i++;
        d[i][0]=0;
        d[i][1]=d[i-1][1]<<1;
        break;
      case 2:
        d[i][0]++;
        i++;
        d[i][0]=0;
        d[i][1]=(d[i-1][1]<<1)+1;
        break;
      default:
        i--;
        break;
    }
  }
  for(i=0;i<q;i++){
    printf("%d\n",a[i]);
  }
  return(0);
}