class Tnode:#树的结构定义
    def __init__(self, data, left: None, right: None):
        self.data = data
        self.left = left
        self.right = right


def tpx(T: Tnode, v):#二叉排序
    if T == None:
        return
    if v < T.data:
        if T.left == None:
            T.left = Tnode(v, None, None)
        else:
            tpx(T.left, v)
    if v > T.data:
        if T.right == None:
            T.right = Tnode(v, None, None)
        else:
            tpx(T.right, v)

#前序、中序、后序输出
def pre(T: Tnode, pr):
    if not T:
        return
    pr.append(T.data)
    pre(T.left, pr)
    pre(T.right, pr)


def mid(T: Tnode, mi):
    if not T:
        return
    mid(T.left, mi)
    mi.append(T.data)
    mid(T.right, mi)


def post(T: Tnode, po):
    if not T:
        return
    post(T.left, po)
    post(T.right, po)
    po.append(T.data)


while True:
    try:
        n = int(input())
        value = list(map(int, input().split(" ")))
        T = Tnode(value[0], None, None)
        for i in range(1, len(value)):
            tpx(T, value[i])
        pr, mi, po = [], [], []
        pre(T, pr)
        mid(T, mi)
        post(T, po)
        for i in range(len(mi) - 1):
            print(pr[i], end=" ")
        print(pr[len(mi) - 1])
        for i in range(len(mi) - 1):
            print(mi[i], end=" ")
        print(mi[len(mi) - 1])
        for i in range(len(mi) - 1):
            print(po[i], end=" ")
        print(po[len(mi) - 1])
    except:
        break