import java.util.*;
public class Main {
private class Node{
int data;
Node left;
Node right;
Node(int data, Node left, Node right){
this.data=data;
this.left=left;
this.right=right;
}
}
private Node root;
public Main(int[] arr){
this.root= constructBST(arr,0,arr.length-1);
}
private Node constructBST(int arr[], int low, int high){
if(low>high){
return null;
}
int mid=(low+high)/2;
Node node= new Node(arr[mid],null,null);
node.left= constructBST(arr,low,mid-1);
node.right=constructBST(arr,mid+1, high);
return node;
}
public void PreOrder(){
this.PreOrder_Traversal(this.root);
}
private void PreOrder_Traversal(Node node){
if(node==null){
return;
}
System.out.print(node.data+" ");
PreOrder_Traversal(node.left);
PreOrder_Traversal(node.right);
}
public ArrayList<Integer> InOrder(){
return this.InOrder(this.root);
}
private ArrayList<Integer> InOrder(Node node){
if(node==null){
return new ArrayList<Integer>();
}
ArrayList<Integer> ans= new ArrayList<Integer>();
ArrayList<Integer> leftList= InOrder(node.left);
for(int l:leftList){
ans.add(l);
}
ans.add(node.data);
ArrayList<Integer> rightList= InOrder(node.right);
for(int r:rightList){
ans.add(r);
}
return ans;
}
public int max(){
return this.max(this.root);
}
private int max(Node node){
while(node.right!=null){
node=node.right;
}
return node.data;
}
public void removeNode(int item){
this.removeNode(this.root, null, false, false, item);
}
public void removeNode(Node node, Node parent, boolean isLeft, boolean isRight, int item){
if(node==null){
return;
}
if(item<node.data){
removeNode(node.left,node,true,false,item);
}else if(item>node.data){
removeNode(node.right, node, false, true, item);
}else{
if(node.left==null && node.right==null){
if(isLeft){
parent.left=null;
}else{
parent.right=null;
}
}else if(node.left==null && node.right!=null){
if(isLeft){
parent.left=node.right;
}else{
parent.right=node.right;
}
}else if(node.left!=null && node.right==null){
if(isLeft){
parent.left=node.left;
}else{
parent.right=node.left;
}
}else{
ArrayList<Integer> inorderList= InOrder();
int index= inorderList.indexOf(node.data);
int newData= inorderList.get(index+1);
node.data=newData;
removeNode(node.right,node,true,false,newData);
}
}
}
public static void main(String args[]) {
Scanner sc= new Scanner(System.in);
int tests= sc.nextInt();
for(int test=0; test<tests; test++){
int size=sc.nextInt();
int arr[]= new int[size];
for(int i=0; i<size; i++){
arr[i]=sc.nextInt();
}
Arrays.sort(arr);
int size2= sc.nextInt();
int remArray[]= new int[size2];
for(int i=0; i<size2; i++){
remArray[i]=sc.nextInt();
}
Main tree= new Main(arr);
for(int a:remArray){
tree.removeNode(a);
}
tree.PreOrder();
}
}
}