a = list(map(str,sorted(eval(input()) for i in range(eval(input())))[:4]))

ans = []

for i in range(3):

	for j in range(i+1,4):

		ans.append(int(a[i]+a[j]))

		ans.append(int(a[j]+a[i]))

print(sorted(ans)[2])
a = list(map(str,sorted(int(input()) for i in range(eval(input())))[:4]))

ans = []

for i in range(3):

	for j in range(i+1,4):

		ans.append(int(a[i]+a[j]))

		ans.append(int(a[j]+a[i]))

print(sorted(ans)[2])
a = list(map(str, sorted(eval(input()) for i in range(eval(input())))[:4]))
ans = []
for i in range(3):
    for j in range(i + 1, 4):
        ans.append(int(a[i] + a[j]))
        ans.append(int(a[j] + a[i]))
print(sorted(ans)[2])

a = list(map(str, sorted(int(input()) for i in range(eval(input())))[:4]))
ans = []
for i in range(3):
    for j in range(i + 1, 4):
        ans.append(int(a[i] + a[j]))
        ans.append(int(a[j] + a[i]))
print(sorted(ans)[2])

-a = list(map(str, sorted(eval(input()) for i in range(eval(input())))[:4])) +a = list(map(str, sorted(int(input()) for i in range(eval(input())))[:4]))