#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
using namespace __gnu_pbds;
//typedef tree<
//        pair<long long, int>, // change type
//        null_type,
//        less<pair<long long, int> >, // change type
//        rb_tree_tag,
//        tree_order_statistics_node_update>
//        ordered_set;

template<class T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;


vector<string> vec_splitter(string s) {
    s += ',';
    vector<string> res;
    while(!s.empty()) {
        res.push_back(s.substr(0, s.find(',')));
        s = s.substr(s.find(',') + 1);
    }
    return res;
}
void debug_out(
        vector<string> __attribute__ ((unused)) args,
        __attribute__ ((unused)) int idx,
        __attribute__ ((unused)) int LINE_NUM) { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
    if(idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") ";
    stringstream ss; ss << H;
    cerr << args[idx] << " = " << ss.str();
    debug_out(args, idx + 1, LINE_NUM, T...);
}

#ifdef LOCAL
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) ;
#endif


typedef long long ll;
typedef long double ld;
#define rep(i, start, end) for(int i = start; i < end; ++i)
#define sz(x) (int)((x).size())
#define pb push_back
#define all(x) x.begin(), x.end()
#define clr(d, v) memset(d, v, sizeof(d))
#define pii pair<int, int>
#define X first
#define Y second
//#define debug(x) cerr << #x << " : " << (x) << " "
const double PI = 3.14159265358979323846;
const double eps = (1e-9);
//int isGreater(long double x, long double y)
//{
//    if (abs(x - y) < eps)
//        return 0;
//    if (x > y)
//        return 1;
//    return -1;
//}

const int MAX_W = 100001;
const int MX = 1024;
int mem[MX][MAX_W];
const int MAX_N = (1<<18) + 1;
int v[MAX_N], w[MAX_N];
int n;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
//    freopen("AC.txt", "w", stdout);
#endif
    cin >> n;
    for (int i = 1; i <= n; ++i)
        cin >> v[i] >> w[i];

    for (int i = 1; i < MX; ++i)
    {
        for (int j = 0; j < MAX_W; ++j)
        {
            mem[i][j] = mem[i>>1][j];
            if (j >= w[i])
                mem[i][j] = max(mem[i][j], mem[i>>1][j - w[i]] + v[i]);
        }
    }

    int q;
    cin >> q;
    vector<int> nodes;
    while(q--)
    {
        int u, L;
        cin >> u >> L;
        nodes.clear();
        while(sz(nodes) < 8 && u >= 1)
        {
            nodes.push_back(u);
            u /= 2;
        }

        int best = 0;
        for (int msk = 0; msk < (1<<sz(nodes)); ++msk)
        {
            int tempValue = 0, tempWeight = L;
            for (int j = 0; j < sz(nodes); ++j)
            {
                if ((1<<j)&msk)
                {
                    tempWeight -= w[nodes[j]];
                    tempValue += v[nodes[j]];
                    if (tempWeight < 0)
                        break;
                }
            }
            if (tempWeight >= 0)
                best = max(best, tempValue + mem[u][tempWeight]);
        }
        cout << best << '\n';
    }

}

Measured memory_bytes: 405332