using System;
using System.Collections.Generic;
using System.IO;
using CompLib.Util;

public class Program
{
    private int N;
    private int[] V, W;
    private int Q;

    // 頂点iで重さがj以下での価値最大
    private long[] D;

    public void Solve()
    {
        var sc = new Scanner();
        N = sc.NextInt();
        V = new int[N];
        W = new int[N];
        for (int i = 0; i < N; i++)
        {
            V[i] = sc.NextInt();
            W[i] = sc.NextInt();
        }

        int p = Math.Min(N + 1, 1 << 10);
        D = new long[p * 100001];
        SetD(1, 1);
        Q = sc.NextInt();
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) {AutoFlush = false});
        for (int i = 0; i < Q; i++)
        {
            Query(sc);
        }

        Console.Out.Flush();
    }

    int C(int i, int j)
    {
        return i * (100001) + j;
    }

    void SetD(int i, int depth)
    {
        if (i > N || depth > 10)
        {
            return;
        }

        for (int j = 0; j <= 100000; j++)
        {
            D[C(i, j)] = D[C(i / 2, j)];
        }

        for (int j = 100000 - W[i - 1]; j >= 0; j--)
        {
            D[C(i, j + W[i - 1])] = Math.Max(D[C(i, j + W[i - 1])], D[C(i / 2, j)] + V[i - 1]);
        }

        SetD(i * 2, depth + 1);
        SetD(i * 2 + 1, depth + 1);
    }

    void Query(Scanner sc)
    {
        int v = sc.NextInt();
        int L = sc.NextInt();

        int depth = 0;
        int cp = v;
        while (cp > 0)
        {
            depth++;
            cp /= 2;
        }

        if (depth <= 10)
        {
            Console.WriteLine(D[C(v, L)]);
            return;
        }

        long ans = 0;
        var ls = new List<S>();
        ls.Add(new S(0, 0));
        int cur = v;
        for (int i = 0; i < depth - 10; i++, cur /= 2)
        {
            int len = ls.Count;
            for (int j = 0; j < len; j++)
            {
                if (ls[j].W + W[cur - 1] <= L)
                {
                    ls.Add(new S(ls[j].V + V[cur - 1], ls[j].W + W[cur - 1]));
                }
            }
        }

        foreach (S pair in ls)
        {
            ans = Math.Max(ans, pair.V + D[C(cur, L - pair.W)]);
        }

        Console.WriteLine(ans);
    }

    public static void Main(string[] args) => new Program().Solve();
}

struct S
{
    public int V, W;

    public S(int v, int w)
    {
        V = v;
        W = w;
    }
}

namespace CompLib.Util
{
    using System;
    using System.Linq;

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            while (_index >= _line.Length)
            {
                _line = Console.ReadLine().Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            _line = Console.ReadLine().Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}

Measured memory_bytes: 844036