#include
#include
using namespace std;
class node{
public:
int data;
nodeleft;
noderight;
node(int d){
data=d;
left=NULL;
right=NULL;
}
};
node* buildBalancedBST(int ar[],int s,int e){
//base condition
if(s>e){
return NULL;
}
//recursion condition
int m=(s+e)/2;
node* newNode=new node(ar[m]);
newNode->left=buildBalancedBST(ar,s,m-1);
newNode->right=buildBalancedBST(ar,m+1,e);
return newNode;
}
void preOrderPrint(node*root){
if(root==NULL){
return;
}
cout<data<<" ";
preOrderPrint(root->left);
preOrderPrint(root->right);
return;
}
int main() {
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int ar[n];
for(int i=0;i<n;i++){
cin>>ar[i];
}
sort(ar,ar+n);
node* root=buildBalancedBST(ar,0,n-1);
int k1,k2;
cin>>k1;
cin>>k2;
cout<<"# Preorder : ";
preOrderPrint(root);
cout<<endl;
int s=0,e=n-1;
int m=(s+e)/2;
while(ar[m]!=k1){
if(ar[m]<k1){
s=m+1;
}
else{
e=m-1;
}
m=(s+e)/2;
}
cout<<"# Nodes within range are : ";
while(ar[m]!=k2){
cout<<ar[m]<<" ";
m++;
}
cout<<ar[m]<<endl;
}
}