using System;

namespace AtCoder {
    class KnapsackQTree {
        const int LIMIT = 2048;

        static int N;
        static int[] value, weight;

        static int[][] totalWeightMemo = new int[LIMIT + 1][];
        static int[][] maxValueMemo = new int[LIMIT + 1][];

        class Item : IComparable<Item> {
            public int weight, value;
            public Item(int weight, int value) {
                this.weight = weight;
                this.value = value;
            }
            public int CompareTo(Item other) {
                return weight.CompareTo(other.weight);
            }
        }

        static int solve(int v, int weightLimit) {
            if (v >= LIMIT) {
                int ans = solve(v / 2, weightLimit);
                if (weightLimit >= weight[v]) {
                    ans = Math.Max(ans, solve(v / 2, weightLimit - weight[v]) + value[v]);
                }
                return ans;
            }

            int[] totalWeight = totalWeightMemo[v];
            int[] maxValue = maxValueMemo[v];
            if (totalWeight[totalWeight.Length - 1] <= weightLimit) {
                return maxValue[maxValue.Length - 1];
            }

            int L = 0, R = totalWeight.Length - 1;
            while (R - L > 1) {
                int mid = (L + R) / 2;
                if (totalWeight[mid] > weightLimit) {
                    R = mid;
                } else {
                    L = mid;
                }
            }

            return maxValue[L];
        }

        public static void Main(string[] args) {
            N = int.Parse(Console.ReadLine());
            value = new int[N + 1];
            weight = new int[N + 1];
            for (int i = 1; i <= N; i++) {
                var parts = Console.ReadLine().Split(' ');
                value[i] = int.Parse(parts[0]);
                weight[i] = int.Parse(parts[1]);
            }

            for (int i = 1; i <= N && i < LIMIT; i++) {
                if (i == 1) {
                    totalWeightMemo[i] = new int[] { 0, weight[1] };
                    maxValueMemo[i] = new int[] { 0, value[1] };
                } else {
                    int p = i / 2;
                    Item[] items = new Item[2 * totalWeightMemo[p].Length];
                    for (int j = 0; j < totalWeightMemo[p].Length; j++) {
                        items[2 * j] = new Item(totalWeightMemo[p][j], maxValueMemo[p][j]);
                        items[2 * j + 1] = new Item(totalWeightMemo[p][j] + weight[i], maxValueMemo[p][j] + value[i]);
                    }
                    Array.Sort(items);
                    int len = 0;
                    for (int j = 0; j < items.Length; j++) {
                        if (j == 0 || items[j].weight > items[j - 1].weight) {
                            len++;
                        }
                    }
                    int[] totalWeight = new int[len];
                    int[] maxValue = new int[len];
                    int maxSoFar = 0, pos = 0;
                    for (int j = 0; j < items.Length; j++) {
                        if (j > 0 && items[j].weight > items[j - 1].weight) {
                            totalWeight[pos] = items[j - 1].weight;
                            maxValue[pos++] = maxSoFar;
                        }
                        maxSoFar = Math.Max(maxSoFar, items[j].value);
                    }
                    totalWeight[pos] = items[items.Length - 1].weight;
                    maxValue[pos++] = maxSoFar;
                    totalWeightMemo[i] = totalWeight;
                    maxValueMemo[i] = maxValue;
                }
            }

            int Q = int.Parse(Console.ReadLine());
            for (int q = 0; q < Q; q++) {
                var parts = Console.ReadLine().Split(' ');
                int v = int.Parse(parts[0]);
                int weightLimit = int.Parse(parts[1]);
                Console.WriteLine(solve(v, weightLimit));
            }
        }
    }
}


Measured memory_bytes: 71828