from heapq import heappush, heappop

n = eval(input())

h = [-10000]*4

for i in range(n):

    a = eval(input())

    heappush(h, -a)

    heappop(h)

h = [str(-a) for a in h]

ans = []

for i in range(3):

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

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

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

ans.sort()

print(ans[2])
n = eval(input())

h = sorted(eval(input()) for i in range(n))[:4]

h = list(map(str, h))

ans = []

for i in range(3):

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

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

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

print(sorted(ans)[2])
from heapq import heappush, heappop

n = eval(input())
h = [-10000] * 4
for i in range(n):
    a = eval(input())
    heappush(h, -a)
    heappop(h)
h = [str(-a) for a in h]
ans = []
for i in range(3):
    for j in range(i + 1, 4):
        ans.append(int(h[i] + h[j]))
        ans.append(int(h[j] + h[i]))
ans.sort()
print(ans[2])

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

-from heapq import heappush, heappop - -h = [-10000] * 4 -for i in range(n): -    a = eval(input()) -    heappush(h, -a) -    heappop(h) -h = [str(-a) for a in h] +h = sorted(eval(input()) for i in range(n))[:4] +h = list(map(str, h)) -ans.sort() -print(ans[2]) +print(sorted(ans)[2])