### BUGGY CODE ###

import sys
from operator import itemgetter
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
            continue
        else:
            rw.append(Aw[idx])
            rv.append(Av[idx])
            idx += 1

    return rv, rw

def mergedown(Av, Aw, Ai, vw):
    v, w = vw
    resv, resw, resi = [], [], []
    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])
            resi.append(Ai[idx])
            idx += 1
            continue
        else:
            if Aw[jdx] - w < 0:
                jdx += 1
                continue
            resv.append(Av[jdx] + v)
            resw.append(Aw[jdx] - w)
            resi.append(Ai[jdx])
            jdx += 1
    return resv, resw, resi

def calc(Av, Aw, Ai, Bv, Bw, Bi):
    rv, rw, ri = [], [], []
    N = len(Av)
    M = len(Bv)
    adx = 0
    bdx = 0
    for _ in range(N+M):
        if adx == N:
            rv.append(Bv[bdx])
            rw.append(Bw[bdx])
            ri.append(Bi[bdx])
            bdx += 1
            continue
        if bdx == M:
            rv.append(Av[adx])
            rw.append(Aw[adx])
            ri.append(Ai[adx])
            adx += 1
            continue
        if Aw[adx] < Bw[bdx]:
            rv.append(Av[adx])
            rw.append(Aw[adx])
            ri.append(Ai[adx])
            adx += 1
        else:
            rv.append(Bv[bdx])
            rw.append(Bw[bdx])
            ri.append(Bi[bdx])
            bdx += 1
    return rv, rw, ri
  
limit = 10**5
N = int(readline())
VW = [None] + [tuple(map(int, readline().split())) for _ in range(N)]

BS = min(N+1, 1<<13)
Q = int(readline())
VL = [None] + [tuple(map(int, readline().split())) for _ in range(Q)]

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

table = [[] for _ in range(N+1)]
tv = [[] for _ in range(N+1)]
tw = [[] for _ in range(N+1)]
ti = [[] for _ in range(N+1)]
for i in range(1, Q+1):
    v, l = VL[i]
    table[v].append((0, l, i))
for i in range(1, N+1):
    if table[i]:    
        table[i].sort(key = itemgetter(1))
        tv[i], tw[i], ti[i] = map(list, zip(*table[i]))

cnt = 0
for i in range(N, BS-1, -1):
    if tv[i]:
        cnt += 2*len(tv[i])
        tv[i//2], tw[i//2], ti[i//2] = calc(*mergedown(tv[i], tw[i], ti[i], VW[i]), tv[i//2], tw[i//2], ti[i//2])

ans = [0]*(Q+1)
for i in range(1, BS):
    if tv[i]:
        rv = [Rv[i][0]]
        rw = [Rw[i][0]]
        for j in range(1, len(Rv[i])):
            if rv[-1] < Rv[i][j]:
                rv.append(Rv[i][j])
                rw.append(Rw[i][j])
        for k in range(len(tv[i])-1, -1, -1):
            v, rew, idx = tv[i][k], tw[i][k], ti[i][k]
            while rw[-1] > rew:
                rw.pop()
                rv.pop()
            ans[idx] = max(ans[idx], v+rv[-1])
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:])))