### BUGGY CODE ###

#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#solve
def solve():
    n = II()
    vw = LIR(n)
    dp = [{i} for i in range(1 << n.bit_length() + 1)]
    for i in range(1, (1 << (n.bit_length() - 1))):
        dp[2 * i] |= dp[i]
        dp[2 * i + 1] |= dp[i]
    for i in range(1, n+1):
        t = dp[i]
        dp[i] = []
        for j in list(t):
            v, w = vw[j - 1]
            dp[i].append((v, w))
    for _ in range(II()):
        v, L = LI()
        t = dp[v]
        n1 = len(t) // 2
        n2 = len(t) - n1
        vw1 = t[:n1]
        vw2 = t[n1:]
        d1 = defaultdict(int)
        d2 = defaultdict(int)
        lis1 = []
        lis2 = []
        fullserch1 = itertools.product(range(2), repeat=n1)
        fullserch2 = itertools.product(range(2), repeat=n2)
        ans = 0
        for fulls in fullserch1:
            v, w = [0] * 2
            for i, full in enumerate(fulls):
                if full:
                    v += vw1[i][0]
                    w += vw1[i][1]
            d1[w] = max(d1[w], v)
        pre = -1
        l = list(d1.items())
        l.sort(key=lambda x: x[0])
        for key, value in l:
            if value > pre:
                lis1.append(key)
                pre = value

        for fulls in fullserch2:
            v, w = [0] * 2
            for i, full in enumerate(fulls):
                if full:
                    v += vw2[i][0]
                    w += vw2[i][1]
            d2[w] = max(d2[w], v)
        pre = -1
        l = list(d2.items())
        l.sort(key=lambda x: x[0])
        for key, value in l:
            if value > pre:
                lis2.append(key)
                pre = value

        for l1 in lis1:
            x = bisect_right(lis2, L - l1) - 1
            if x == -1:
                continue
            ans = max(ans, d1[l1]+d2[lis2[x]])

        print(ans)
    return


#main
if __name__ == '__main__':
    solve()


### FIXED CODE ###

#!/usr/bin/env python3
import sys

#solve
def solve():

    input = sys.stdin.buffer.readline
    LI = lambda: map(int, input().split())
    II = lambda: int(input())
    n = II()
    vw = [[]] * n
    M = 10 ** 5 + 1
    M0 = [0] * M
    XX = 1 << 10
    d = [[]] * XX
    d[0] = M0

    for i in range(1, min(XX, n + 1)):
        v, w = LI()
        vw[i - 1] = (v, w)
        di = d[i // 2][:]
        for j in range(M - 1, w - 1, -1):
            if di[j] < di[j - w] + v:
                di[j] = di[j - w] + v
        d[i] = di

    for i in range(XX, n + 1):
        v, w = LI()
        vw[i - 1] = (v, w)

    Vv = [i.bit_length() for i in range(1 << 18)]
    n = [pow(2, i) for i in range(18)]
    lentl = [i - 10 for i in range(19)]
    maskset = [[]] * n[10]

    for i in range(1, 11):
        for mask in range(n[i - 1], n[i]):
            a = mask & -mask
            b = Vv[a] - 1
            maska = mask - a
            maskset[mask - 1] = (mask, b, maska)

    for _ in range(II()):
        V, L = LI()

        if V < XX:
            print(d[V][L])
            continue

        n2 = lentl[Vv[V]]
        vw2 = [None] * n2

        for i in range(n2):
            vw2[i] = (vw[V - 1][0], vw[V - 1][1])
            V //= 2

        lw = [0] * n[n2]
        lv = [0] * n[n2]
        dv = d[V]
        ans = dv[L]

        for i in range(n[n2] - 1):
            mask, b, maska = maskset[i]
            v, w = vw2[b]
            ww = lw[maska] + w
            Lww = L - ww
            lw[mask] = ww
            if Lww < 0: continue
            vv = lv[maska] + v
            lv[mask] = vv
            tmp = dv[Lww] + vv
            if tmp > ans:
                ans = tmp
        print(ans)
    return

if __name__ == "__main__":
    solve()