import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
Main m=new Main();
int t=sc.nextInt();
while(t–>0){
int n1=sc.nextInt();
int a1[]=new int[n1];
for(int i=0;i<n1;i++){
a1[i]=sc.nextInt();
}
int n2=sc.nextInt();
int a2[]=new int[n2];
for(int i =0;i<n2;i++){
a2[i]=sc.nextInt();
}
BST tree=m.new BST(a1);
tree.remove(a2);
tree.preorder();
}
}
class BST{
class Node{
Node left;
Node right;
int data;
}
Node root;
public BST(int a1[]){
root=this.construct(a1);
}
public Node construct(int a1[]){
for(int i=0;i<a1.length;i++){
this.root=construct(a1[i],this.root);
}
return root;
}
public Node construct(int val ,Node root){
if(root==null)
{
Node nn=new Node();
nn.data=val;
return nn;
}
if(val<root.data){
root.left=construct(val,root.left);
}
else if(val>root.data){
root.right=construct(val,root.right);
}
return root;
}
public int max() {
return this.max(this.root);
}
private int max(Node root) {
if(root==null)
return -1;
if(root.right==null)
return root.data;
else {
return max(root.right);
}
}
public boolean find(int data) {
return this.find(this.root, data);
}
private boolean find(Node node,int data) {
if(node==null)
return false;
if(node.data==data)
return true;
if(node.data<data) {
return find(node.right,data);
}
else {
return find(node.left,data);
}
}
public void remove(int a2[]) {
for(int i=0;i<a2.length;i++){
if(this.find(a2[i])){
this.remove(this.root, null,false,a2[i]);
}
else{
continue;
}
}
}
//ilc mean is left child , it is true for left, false for right
private void remove(Node root,Node parent, boolean ilc,int item) {
if(root==null)
return ;
if(item>root.data) {
remove(root.right,root,false,item);
}
else if(item<root.data) {
remove(root.left,root,true,item);
}
else {
if(root.left==null&&root.right==null) {
if(ilc) {
parent.left=null;
}
else {
parent.right=null;
}
}
else if(root.left==null&&root.right!=null) {
if(ilc) {
parent.left=root.right;
}
else {
parent.right=root.right;
}
}
else if(root.left!=null&&root.right==null) {
if(ilc) { //ilc tell , we need to add left to the parent or right to the parent
parent.left=root.left;
}
else {
parent.right=root.left;
}
}
else {
if(root.right.left!=null){
int max=max(root.right.left);
root.data=max;
remove(root.right.left,root.right,true,max);
}else{
int max=max(root.right);
root.data=max;
remove(root.right,root,false,max);
}
}
}
}
public void preorder(){
this.preorder(this.root);
}
private void preorder(Node node){
if(node!=null){
System.out.print(node.data+" ");
preorder(node.left);
preorder(node.right);
}
}
}
}