import java.util.Scanner;
public class Main
{
public static class BST
{
private class Node
{
private int data;
private Node left;
private Node right;
}
private Node root;
BST(int []arr)
{
this.root=construct(arr,0,arr.length-1);
}
private Node construct(int []a, int low,int high)
{
if(low>high)
return null;
int mid=(low+high)/2;
Node nn=new Node();
nn.data=a[mid];
nn.left=this.construct(a,low,mid-1);
nn.right=this.construct(a,mid+1,high);
return nn;
}
public void preOrder()
{
preOrder(root);
}
private void preOrder(Node node)
{
if(node==null)
return;
System.out.print(node.data+" ");
preOrder(node.left);
preOrder(node.right);
}
public void range(int beg,int end)
{
range(root,beg,end);
}
private void range(Node node,int beg,int end)
{
if(node==null)
return;
range(node.left,beg,end);
if(node.data>=beg && node.data<=end)
System.out.print(node.data+" ");
range(node.right,beg,end);
}
}
public static void sort(int []a)
{
int small,pos;
for(int i=0;i<a.length-1;i++)
{
small=a[i];
pos=i;
for(int j=i+1;j<a.length;j++)
{
if(a[j]<small)
{small=a[j];
pos=j;
break;
}
}
int save=a[i];
a[i]=small;
a[pos]=save;
}
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t>0)
{
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
}
sort(a);
BST b=new BST(a);
int beg=sc.nextInt();
int end=sc.nextInt();
System.out.print("# Preorder : ");
b.preOrder();
System.out.println();
System.out.print("# Nodes within range are : ");
b.range(beg,end);
System.out.println();
t--;
}
}
}