#include <bits/stdc++.h>
#define db(x) cout << (x) << '\n';
#define all(v) (v).begin(),(v).end()
#define allr(v) (v).rbegin(),(v).rend()
#define f first
#define s second
using namespace std;
typedef long long ll;
typedef long double ld;
//typedef pair<ll,ll> pii;
typedef pair<int,int> pii;
typedef pair<double,ll> pdi;
typedef pair<ld,ld> pdd;
typedef pair<double,ll> pdi;
typedef pair<double,pii> pdp;
typedef pair<string,ll> psi;
typedef pair<ll,string> pls;
typedef pair<string,string> pss;
typedef pair<ll,pii> pip;
typedef pair<pii,pii> ppp;
typedef complex<ld> point;
#define mod (ll)(1e9+7)
#define eps (1e-9)
#define oo (ll)(0x3f3f3f3f)
//#define pi acos(-1)
#define MAXN (ll)(1<<18)

int w[MAXN],v[MAXN];

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

    int n;
    cin >> n;
    for(int i=1;i<=n;i++)
        cin >> v[i] >> w[i];

    int q;
    cin >> q;
    while(q--)
    {
        int u,l;
        cin >> u >> l;
        vector<pii> p1,p2;
        while(u)
        {
            if(p1.size()<9)
                p1.push_back(pii(w[u],v[u]));
            else
                p2.push_back(pii(w[u],v[u]));
            u>>=1;
        }

        vector<pii> s1,s2;
        s1.push_back(pii(0,0));
        s2.push_back(pii(0,0));
        for(int i=0;i<p1.size();i++)
        {
            int wi=p1[i].f;
            int vi=p1[i].s;
            vector<pii> ss1(s1.size());
            for(int j=0;j<(1<<i);j++)
                ss1[j]=pii(s1[j].f+wi,s1[j].s+vi);
            vector<pii> ns1(1<<(i+1));
            int pi=0;
            int pj=0;
            while(pi<(1<<i) && pj<(1<<i))
            {
                if(s1[pi].first<ss1[pj].first)
                {
                    ns1[pi+pj]=s1[pi];
                    pi++;
                }
                else
                {
                    ns1[pi+pj]=ss1[pj];
                    pj++;
                }
            }
            while(pi<(1<<i))
            {
                ns1[pi+pj]=s1[pi];
                pi++;
            }
            while(pj<(1<<i))
            {
                ns1[pi+pj]=ss1[pj];
                pj++;
            }
            s1=ns1;
        }

        for(int i=0;i<p2.size();i++)
        {
            int wi=p2[i].f;
            int vi=p2[i].s;
            vector<pii> ss1(s2.size());
            for(int j=0;j<(1<<i);j++)
                ss1[j]=pii(s2[j].f+wi,s2[j].s+vi);
            vector<pii> ns1(1<<(i+1));
            int pi=0,pj=0;
            while(pi<(1<<i) && pj<(1<<i))
            {
                if(s2[pi].first<ss1[pj].first)
                {
                    ns1[pi+pj]=s2[pi];
                    pi++;
                }
                else
                {
                    ns1[pi+pj]=ss1[pj];
                    pj++;
                }
            }
            while(pi<(1<<i))
            {
                ns1[pi+pj]=s2[pi];
                pi++;
            }
            while(pj<(1<<i))
            {
                ns1[pi+pj]=ss1[pj];
                pj++;
            }
            s2=ns1;
        }

        for(int i=1;i<s1.size();i++)
            s1[i].second=max(s1[i].second,s1[i-1].second);
        for(int i=1;i<s2.size();i++)
            s2[i].second=max(s2[i].second,s2[i-1].second);
        int res=0;
        int pp1=0,pp2=s2.size()-1;
        while(pp1<s1.size())
        {
            while(pp2>=0 && s2[pp2].first+s1[pp1].first>l)
                pp2--;
            if(pp2!=-1)
                res=max(res,s1[pp1].second+s2[pp2].second);
            pp1++;
        }
        db(res)
    }

    return 0;
}
/**
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
**/


Measured memory_bytes: 5728