N = int(eval(input()))

HPs = [tuple(map(int, input().split())) for i in range(N)]



HPs.sort(key=lambda x: x[0] + x[1])



INF = float('inf')

dp = [0]

for H, P in HPs:

    if dp[-1] != INF:

        dp += [INF]

    dp = list(map(min, list(zip([0] + [hgt + P if hgt <= H else INF for hgt in dp], dp))))



dp += [INF]

print((dp.index(INF) - 1))

N = int(eval(input()))

HPs = [tuple(map(int, input().split())) for i in range(N)]



HPs.sort(key=lambda x: x[0] + x[1])



dp = {0: 0}

for H, P in HPs:

    for num, minH in list(dp.items()):

        if H >= minH:

            if num + 1 in dp:

                dp[num + 1] = min(dp[num + 1], minH + P)

            else:

                dp[num + 1] = minH + P



print((max(dp.keys())))

N = int(eval(input()))
HPs = [tuple(map(int, input().split())) for i in range(N)]
HPs.sort(key=lambda x: x[0] + x[1])
INF = float("inf")
dp = [0]
for H, P in HPs:
    if dp[-1] != INF:
        dp += [INF]
    dp = list(
        map(min, list(zip([0] + [hgt + P if hgt <= H else INF for hgt in dp], dp)))
    )
dp += [INF]
print((dp.index(INF) - 1))

N = int(eval(input()))
HPs = [tuple(map(int, input().split())) for i in range(N)]
HPs.sort(key=lambda x: x[0] + x[1])
dp = {0: 0}
for H, P in HPs:
    for num, minH in list(dp.items()):
        if H >= minH:
            if num + 1 in dp:
                dp[num + 1] = min(dp[num + 1], minH + P)
            else:
                dp[num + 1] = minH + P
print((max(dp.keys())))

-INF = float("inf") -dp = [0] +dp = {0: 0} -    if dp[-1] != INF: -        dp += [INF] -    dp = list( -        map(min, list(zip([0] + [hgt + P if hgt <= H else INF for hgt in dp], dp))) -    ) -dp += [INF] -print((dp.index(INF) - 1)) +    for num, minH in list(dp.items()): +        if H >= minH: +            if num + 1 in dp: +                dp[num + 1] = min(dp[num + 1], minH + P) +            else: +                dp[num + 1] = minH + P +print((max(dp.keys())))