while True:
    try:
        def dfs(wait, stack, leave,ans):
            if len(wait) == 0 and len(stack) == 0:
                ans.append(leave)

            if len(wait) > 0:  # 从等待队列中 入栈
                dfs(wait[1:], stack + [wait[0]], leave,ans)

            if len(stack) > 0:  # 出栈
                dfs(wait, stack[:-1], leave + [stack[-1]],ans)
            return ans
        n = int(input())
        s = list(map(int, input().split()))
        #n=3
        #s=[1,2,3]
        w=sorted(dfs(s,[], [],[]))
        for i in range(len(w)):
            for j in range(n):
                print(w[i][j],end=" ")
            print()
    except:
        break