def fun(in_list, stack, out_list):
if len(in_list) == 0 and len(stack) == 0:
res.append(out_list)
# 有等待进站的火车时
if len(in_list) > 0:
fun(in_list[1:], stack + [in_list[0]], out_list)
# 有等待出站的列车,先出最后一列车
if len(stack) > 0:
fun(in_list, stack[:-1], out_list + [stack[-1]])
return res
while True:
try:
n = int(input())
order_list = list(map(int, input().split()))
res = []
result = fun(order_list, [], [])
for i in sorted(result):
print(' '.join(map(str, i)))
except:
break