#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#  include <intrin.h>
#  define __builtin_popcount __popcnt
#endif

#define _USE_MATH_DEFINES

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <unordered_map>
#include <stack>
#include <list>
#include <math.h>
#include <bitset>
#include <iterator>
#include <map>
#include <iomanip>
#include <functional>
#include <string>
#include <algorithm>
#include <queue>
#include <random>
#include <numeric>
#include <set>
#include <cassert>
#include <cmath>
#include <cinttypes>

using namespace std;

template<class T> ostream& operator<<(ostream& os, const vector<T>& v) { for (auto i = begin(v); i != end(v); i++) os << *i << (i == end(v) - 1 ? "" : " "); return os; }
template<class T> istream& operator>>(istream& is, vector<T>& v) { for (auto i = begin(v); i != end(v); i++) is >> *i; return is; }
struct fast_io {
	fast_io& operator>> (int& target) { scanf("%" PRId32, &target);  return *this; }
	fast_io& operator>> (unsigned int& target) { scanf("%" PRIu32, &target);  return *this; }
	fast_io& operator>> (long long& target) { scanf("%" PRId64, &target);  return *this; }
	fast_io& operator>> (unsigned long long& target) { scanf("%" PRIu64, &target);  return *this; }
	template<typename T> fast_io& operator>> (vector<T>& target) { for (auto& value : target) { *this >> value; } return *this; }
	fast_io& operator<< (const int value) { printf("%" PRId32, value); return *this; }
	fast_io& operator<< (const unsigned int value) { printf("%" PRIu32, value); return *this; }
	fast_io& operator<< (const long long value) { printf("%" PRId64, value); return *this; }
	fast_io& operator<< (const unsigned long long value) { printf("%" PRIu64, value); return *this; }
	fast_io& operator<< (const string& value) { printf("%s", value.c_str()); return *this; }
	template<typename T> fast_io& operator<< (const vector<T>& target) { for (auto& value : target) { *this << value << " "; } return *this; }
}; fast_io fio;

template<class ForwardIt, class UnaryPredicate>
ForwardIt first_true(ForwardIt first, ForwardIt last, UnaryPredicate predicate) {
	ForwardIt it;
	typename std::iterator_traits<ForwardIt>::difference_type count, step; count = std::distance(first, last);
	while (count > 0) {
		it = first;	step = count / 2;
		std::advance(it, step);
		if (!predicate(*it)) { first = ++it; count -= step + 1; }
		else count = step;
	}
	return first;
}

#define MOD 1000000007
#define pii pair<int,int>
#define pll pair<long long, long long>
#define all(x) (x).begin(), (x).end()
#define sort_all(x) sort(all(x))
#define make_unique(x) (x).erase(unique(all(x)), (x).end())

vector<int> G[1 << 19];
vector<int> v, w;
vector<pii> Ls[1 << 19];
vector<int> ans;

vector<pii> step(vector<pii>& orig, int w, int v)
{
	vector<pii> orig2;
	orig2.reserve(orig.size() + 1);
	orig2.push_back({ w,v });
	for (auto x : orig)
	{
		orig2.push_back({ x.first + w,x.second + v });
	}
	vector<pii> ans;
	int i = 0, j = 0;
	while (true) {
		bool b1 = i < orig.size(), b2 = j < orig2.size();
		if (!b1 && !b2)
			break;
		pii *cand;
		if (b1 && (!b2 || orig[i] < orig2[j]))
		{
			cand = &orig[i]; i++;
		}
		else
		{
			cand = &orig2[j]; j++;
		}
		if (ans.size() == 0 || ans.back().second < cand->second)
			ans.push_back(*cand);
	}

	return ans;
}

int handle_ans(int i, int L, vector<pii>& first, vector<pii>& second)
{
	int max_ans = 0;
	auto it = first_true(first.begin(), first.end(), [L](pii& a) {
		return a.first > L;
		});
	if (it != first.begin())
	{
		it--;
		max_ans = it->second;
	}
	if (second.size() != 0)
	{
		for (auto& x : second)
		{
			if (x.first <= L) max_ans = max(max_ans, x.second);
			if (x.first > L) continue;
			int rem = L - x.first;
			if (rem <= 0) continue;

			auto it = first_true(first.begin(), first.end(), [rem](pii& a) {
				return a.first > rem;
				});
			if (it != first.begin())
			{
				it--;
				max_ans = max(max_ans, x.second + it->second);
			}
		}
	}
	return max_ans;
}

void dfs(int i, int level, vector<pii> &first, vector<pii> &second)
{
	if (level <= 9)
	{
		vector<pii> new_first = step(first, w[i], v[i]);
		for (auto x : Ls[i])
		{
			ans[x.first] = handle_ans(i, x.second, new_first, second);
		}
		for (int u : G[i])
		{
			dfs(u, level + 1, new_first, second);
		}
	}
	else
	{
		vector<pii> new_second = step(second, w[i], v[i]);
		for (auto x : Ls[i])
		{
			ans[x.first] = handle_ans(i, x.second, first, new_second);
		}
		for (int u : G[i])
		{
			dfs(u, level + 1, first, new_second);
		}
	}
}


int main() {

#if defined(_DEBUG)
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	int n;
	fio >> n;

	for (int i = 1; i < n; i++)
	{
		int parent = (i + 1) / 2 - 1;
		G[parent].push_back(i);
	}

	v.resize(n); w.resize(n);
	for (int i = 0; i < n; i++)
	{
		fio >> v[i] >> w[i];
	}

	int q;
	fio >> q;
	ans.resize(q);
	for (int i = 0; i < q; i++)
	{
		int v, L;
		fio >> v >> L;
		v--;
		Ls[v].push_back({ i,L });
	}

	vector<pii> a, b;
	dfs(0, 0,a,b);
	for (int x : ans)
		fio << x << "\n";

	return 0;
}

Measured memory_bytes: 37484