import sys
readline = sys.stdin.readline

def mergeup(A, vw):
    v, w = vw
    res = []
    N = len(A)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if idx == N:
            if A[jdx][1] + w > limit:
                break
            res.append((A[jdx][0] + v, A[jdx][1] + w))
            jdx += 1
            continue
        if A[idx][1] <=  A[jdx][1] + w:
            res.append(A[idx])
            idx += 1
        else:
            if A[jdx][1] + w > limit:
                break
            res.append((A[jdx][0] + v, A[jdx][1] + w))
            jdx += 1
    return res

def mergedown(A, vw):
    v, w = vw
    res = []
    N = len(A)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if jdx == N:
            res.append(A[idx])
            idx += 1
            continue
        if A[idx][1] <=  A[jdx][1] - w:
            res.append(A[idx])
            idx += 1
        else:
            if A[jdx][1] - w < 0:
                jdx += 1
                continue
            res.append((A[jdx][0] + v, A[jdx][1] - w))
            jdx += 1
    return res

def calc(A, B):
    res = []
    N = len(A)
    M = len(B)
    adx = 0
    bdx = 0
    for _ in range(N+M):
        if adx == N:
            res.append(B[bdx])
            bdx += 1
            continue
        if bdx == M:
            res.append(A[adx])
            adx += 1
            continue
        if A[adx][1] < B[bdx][1]:
            res.append(A[adx])
            adx += 1
        else:
            res.append(B[bdx])
            bdx += 1
    return res

limit = 10**5
N = int(readline())
BS = min(N, 1<<12)
VW = [None] + [tuple(map(int, readline().split())) for _ in range(N)]
Q = int(readline())
VL = [None] + [tuple(map(int, readline().split())) for _ in range(N)]

R = [None]*(N+1)
R[0] = [(0, 0)]
for i in range(1, BS+1):
    R[i] = mergeup(R[i//2], VW[i])

table = [[] for _ in range(N+1)]

for i in range(1, Q+1):
    v, l = VL[i]
    K = [(0, l)]
    while v > BS:
        K = mergedown(K, VW[v])
        v >>= 1
    K = [(v, rw, i) for v, rw in K]
    table[v] = calc(table[v], K)    

ans = [0]*(Q+1)
for i in range(1, BS+1):
    if table[i]:
        res = [R[i][0]]
        for j in range(1, len(R[i])):
            if res[-1][0] < R[i][j][0]:
                res.append(R[i][j])
        
        for k in range(len(table[i])-1, -1, -1):
            v, rw, idx = table[i][k]
            while res[-1][1] > rw:
                res.pop()
            ans[idx] = max(ans[idx], v+res[-1][0])

print('\n'.join(map(str, ans[1:])))
        
    

### FIXED CODE ###

import sys
readline = sys.stdin.readline
def mergeup(Av, Aw, vw):
    v, w = vw
    rv, rw = [], []
    N = len(Av)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if idx == N or Aw[idx] > Aw[jdx] + w:
            if Aw[jdx] + w > limit:
                break
            rw.append(Aw[jdx]+w)
            rv.append(Av[jdx]+v)
            jdx += 1
        else:
            rw.append(Aw[idx])
            rv.append(Av[idx])
            idx += 1

    return rv, rw

def mergedown(Av, Aw, vw):
    v, w = vw
    resv, resw = [], []
    N = len(Av)
    idx = 0
    jdx = 0
    for _ in range(2*N):
        if jdx == N or Aw[idx] <= Aw[jdx] - w:
            resv.append(Av[idx])
            resw.append(Aw[idx])
            idx += 1
        else:
            if Aw[jdx] - w < 0:
                jdx += 1
                continue
            resv.append(Av[jdx] + v)
            resw.append(Aw[jdx] - w)
            jdx += 1
    return resv, resw


limit = 10**5
N = int(readline())
VW = [None] + [tuple(map(int, readline().split())) for _ in range(N)]
BS = min(N, 1<<11)
Q = int(readline())
VL = [None] + [tuple(map(int, readline().split())) for _ in range(Q)]


Rv = [[0] for _ in range(BS+1)]
Rw = [[0] for _ in range(BS+1)]
for i in range(1, BS):
    for vm, wm in zip(*mergeup(Rv[i//2], Rw[i//2], VW[i])):
        if Rv[i][-1] < vm:
            Rv[i].append(vm)
            Rw[i].append(wm)
    
ans = [0]*(Q+1)
for i in range(1, Q+1):
    v, l = VL[i]
    Kv, Kw = [0], [l]
    while v >= BS:
        Kv, Kw = mergedown(Kv, Kw, VW[v])
        v >>= 1
    cnt = len(Rv[v])-1
    ctr = 0
    for j in range(len(Kv)-1, -1, -1):
        kv, kw = Kv[j], Kw[j]
        while kw < Rw[v][cnt]:
            cnt -= 1
        ctr = max(ctr, kv+Rv[v][cnt])
    ans[i] = ctr

print('\n'.join(map(str, ans[1:])))