// title:Level Order Traversal
#include <bits/stdc++.h>
using namespace std;
struct BSTNode{
int val;
BSTNode *left, *right;
BSTNode(int val){
this->val = val;
this->left = this->right = NULL;
}
};
struct BSTNode * insert(struct BSTNode * root,int n)
{
if(root==NULL)
return new BSTNode(n);
else if(n<root->val)
root->left=insert(root->left,n);
else if(root->val<n)
root->right=insert(root->right,n);
return root;
}
void level(struct BSTNode *root)
{
if(root!=NULL)
{
queue<BSTNode*>q;
q.push(root);
while(q.size()!=0)
{
int l=q.size();
// cout<<"1";
for(int i=0;i<l;i++)
{
// cout<<"aqghsz";
BSTNode *x=q.front();
q.pop();
cout<<x->val<<" ";
if(x->left)
q.push(x->left);
if(x->right)
q.push(x->right);
}
cout<<"\n";
}
}
}
int main()
{
int tq;
cin>>tq;
while(tq--)
{
int n;
cin>>n;
struct BSTNode *root=NULL;
for(int i=0;i<n;i++)
{
int x;
cin>>x;
root=insert(root,x);
}
level(root);
cout<<"\n";
}
}
Ly8gdGl0bGU6TGV2ZWwgT3JkZXIgVHJhdmVyc2FsIAojaW5jbHVkZSA8Yml0cy9zdGRjKysuaD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpzdHJ1Y3QgQlNUTm9kZXsKCWludCB2YWw7CglCU1ROb2RlICpsZWZ0LCAqcmlnaHQ7CglCU1ROb2RlKGludCB2YWwpewoJCXRoaXMtPnZhbCA9IHZhbDsKCQl0aGlzLT5sZWZ0ID0gdGhpcy0+cmlnaHQgPSBOVUxMOwoJfQp9OwpzdHJ1Y3QgQlNUTm9kZSAqIGluc2VydChzdHJ1Y3QgQlNUTm9kZSAqIHJvb3QsaW50IG4pCnsKICAgIGlmKHJvb3Q9PU5VTEwpCiAgICByZXR1cm4gbmV3IEJTVE5vZGUobik7CiAgICBlbHNlIGlmKG48cm9vdC0+dmFsKQogICAgcm9vdC0+bGVmdD1pbnNlcnQocm9vdC0+bGVmdCxuKTsKICAgIGVsc2UgaWYocm9vdC0+dmFsPG4pCiAgICByb290LT5yaWdodD1pbnNlcnQocm9vdC0+cmlnaHQsbik7CiAgICByZXR1cm4gcm9vdDsKfQp2b2lkIGxldmVsKHN0cnVjdCBCU1ROb2RlICpyb290KQp7CiAgICAKICAgIGlmKHJvb3QhPU5VTEwpCiAgICB7CiAgICAgICAgcXVldWU8QlNUTm9kZSo+cTsKICAgICAgICBxLnB1c2gocm9vdCk7CiAgICAgICAgd2hpbGUocS5zaXplKCkhPTApCiAgICAgICAgewogICAgICAgICAgICBpbnQgbD1xLnNpemUoKTsKICAgICAgICAgICAgLy8gY291dDw8IjEiOwogICAgICAgICAgICBmb3IoaW50IGk9MDtpPGw7aSsrKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIC8vICBjb3V0PDwiYXFnaHN6IjsKICAgICAgICAgICAgICAgICBCU1ROb2RlICp4PXEuZnJvbnQoKTsKICAgICAgICAgICAgICAgICBxLnBvcCgpOwogICAgICAgICAgICAgICAgIGNvdXQ8PHgtPnZhbDw8IiAiOwogICAgICAgICAgICAgICAgIGlmKHgtPmxlZnQpCiAgICAgICAgICAgICAgICAgcS5wdXNoKHgtPmxlZnQpOwogICAgICAgICAgICAgICAgIGlmKHgtPnJpZ2h0KQogICAgICAgICAgICAgICAgIHEucHVzaCh4LT5yaWdodCk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgY291dDw8IlxuIjsKICAgICAgICB9CiAgICB9Cn0KaW50IG1haW4oKQp7CiAgICBpbnQgdHE7CiAgICBjaW4+PnRxOwogICAgd2hpbGUodHEtLSkKICAgIHsKICAgICAgICBpbnQgbjsKICAgICAgICBjaW4+Pm47CiAgICAgICAgc3RydWN0IEJTVE5vZGUgKnJvb3Q9TlVMTDsKICAgICAgICBmb3IoaW50IGk9MDtpPG47aSsrKQogICAgICAgIHsKICAgICAgICAgICAgaW50IHg7CiAgICAgICAgICAgIGNpbj4+eDsKICAgICAgICAgICAgcm9vdD1pbnNlcnQocm9vdCx4KTsKICAgICAgICB9CiAgICAgICAKICAgICAgICBsZXZlbChyb290KTsKICAgICAgICBjb3V0PDwiXG4iOwogICAgfQp9