n=int(eval(input()));

ans=0;

p=[0]*22222;

for w,s,v in sorted([list(map(int,input().split()))for _ in[0]*n],key=lambda a:a[0]+a[1]):

 for j in range(s,-1,-1):

  p[j+w]=max(p[j+w],p[j]+v);

  ans=max(ans,p[j+w]);

print(ans)

p=[0]*6**6

for w,s,v in sorted([list(map(int,input().split()))for _ in[0]*int(eval(input()))],key=lambda a:a[0]+a[1]):

 for j in range(s,-1,-1):p[j+w]=max(p[j+w],p[j]+v)

print((max(p)))
n = int(eval(input()))
ans = 0
p = [0] * 22222
for w, s, v in sorted(
    [list(map(int, input().split())) for _ in [0] * n], key=lambda a: a[0] + a[1]
):
    for j in range(s, -1, -1):
        p[j + w] = max(p[j + w], p[j] + v)
        ans = max(ans, p[j + w])
print(ans)

p = [0] * 6**6
for w, s, v in sorted(
    [list(map(int, input().split())) for _ in [0] * int(eval(input()))],
    key=lambda a: a[0] + a[1],
):
    for j in range(s, -1, -1):
        p[j + w] = max(p[j + w], p[j] + v)
print((max(p)))

-n = int(eval(input())) -ans = 0 -p = [0] * 22222 +p = [0] * 6**6 -    [list(map(int, input().split())) for _ in [0] * n], key=lambda a: a[0] + a[1] +    [list(map(int, input().split())) for _ in [0] * int(eval(input()))], +    key=lambda a: a[0] + a[1], -        ans = max(ans, p[j + w]) -print(ans) +print((max(p)))