#include
using namespace std;
class node {
public:
int data;
node * left;
node * right;
node (int val){
data=val;
left=right=NULL;
}
};
node * Tree(int arr[],int s,int e){
if(s>e){
return NULL;
}
int mid=(s+e)/2;
node * root=new node(arr[mid]);
root->left=Tree(arr,s,mid-1);
root->right=Tree(arr,mid+1,e);
return root;
}
node * remove(node *root,int val){
if(root==NULL){
return NULL;
}
else if(val < root->data){
root->left=remove(root->left,val);
return root;
}
else if(root->data==val){
if(root->right==NULL && root->left==NULL){
delete root;
return NULL;
}
else if(root->left==NULL && root->right!=NULL){
node * temp=root->right;
delete root;
return temp;
}
else if(root->right==NULL && root->left!=NULL){
node * temp=root->left;
delete root;
return temp;
}
node * temp=root->right;
while(temp->left!=NULL){
temp=temp->left;
}
root->data=temp->data;
root->right=remove(root->right,temp->data);
return root;
}
else{
root->right=remove(root->right,val);
}
return root;
}
void preorder(node * root){
if(root==NULL){
return;
}
cout<data<<" ";
preorder(root->left);
preorder(root->right);
return;
}
int main() {
int t;
cin>>t;
while(t–){
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
int d;
cin>>d;
int x;
node* root=Tree(arr,0,n-1);
for(int i=0;i<d;i++){
cin>>x;
root=remove(root,x);
}
preorder(root);
}
return 0;
}