def hcrz(wait, instack, out):
    if wait:
        hcrz(wait[1:], instack + [wait[0]], out)
    if instack:
        hcrz(wait, instack[:-1], out + [instack[-1]])
    if not wait and not instack:
        out_list.append(" ".join(map(str, out)))
data_list = []
 
for i in sys.stdin:
    data_list.append(i.strip())
new_list = []
for j in range(1,int(len(data_list)/2+1)):
    new_list.append(list(map(int,data_list[2*j-1].split(" "))))
for i in new_list:
    out_list = []
    hcrz(i,[],[])
    out_list.sort()
    for j in out_list:
        print(j)