n = int(input())
stack = list(map(int, input().split()))
additional = []
while stack:
cur = stack.pop()
while additional and cur < additional[-1]:
stack.append(additional.pop())
additional.append(cur)
print(*additional[::-1])
n = int(input())
stack = list(map(int, input().split()))
additional = []
while stack:
cur = stack.pop()
while additional and cur < additional[-1]:
stack.append(additional.pop())
additional.append(cur)
print(*additional[::-1])