<reponame>KishanMishra1/SDE-s-Sheet-<gh_stars>0
from collections import deque
import itertools
class TreeNode:
    def __init__(self,key):
        self.left = None
        self.right = None
        self.data = key

def insertNode(root,node):
    if root is None:
        root = node
    else:
        if root.data < node.data:
            if root.right is None:
                root.right = node
            else:
                insertNode(root.right, node)
        else:
            if root.left is None:
                root.left = node
            else:
                insertNode(root.left, node)


def zigzag(A):
    h = height(A)
    res = []
    up = 1
    for i in range(1, h + 1):
        temp = []
        temp = levelorder(A, i, temp)
        res.append(temp)
    return res


def levelorder(A, level, temp):
    if A == None:
        return
    if level == 1:
        temp.append(A.data)
    else:
        levelorder(A.left, level - 1, temp)
        levelorder(A.right, level - 1, temp)
    return temp


def height(A):
    if A == None:
        return 0
    else:
        l = height(A.left)
        r = height(A.right)
        if l > r:
            return l + 1
        else:
            return r + 1




for _ in range(int(input())):
    n=int(input())
    p=[]
    k=list(map(int,input().split()))
    r=TreeNode(k[0])
    for i in range(1,len(k)):
        insertNode(r,TreeNode(k[i]))


    #p=list(itertools.chain.from_iterable(zigzag(r)))
    '''for i in p:
        print(i,end="\n")
    print()'''
    for i in zigzag(r):
        print(*i)
    print()
