#include <iostream>
using namespace std;
class Binary_tree_node
{
public:
    int data;                                    
    Binary_tree_node *lChild, *rChild, *parents; 
    Binary_tree_node() : lChild(NULL), rChild(NULL) {}
    Binary_tree_node(int e) : data(e), lChild(NULL), rChild(NULL), parents(NULL) {}
};
class Binary_tree
{
    Binary_tree_node *root;
    
    void InsertNode(int data, Binary_tree_node *&r)
    {
        if (data > r->data && r->rChild)
        {
            InsertNode(data, r->rChild);
        }
        else if (data < r->data && r->lChild)
        {
            InsertNode(data, r->lChild);
        }
        else if (data > r->data && !r->rChild)
        {
            Binary_tree_node *s = new Binary_tree_node(data);
            r->rChild = s;
        }
        else if (data < r->data && !r->lChild)
        {
            Binary_tree_node *s = new Binary_tree_node(data);
            r->lChild = s;
        }
    }
    
    Binary_tree_node *getBefore(Binary_tree_node *s)
    {
        Binary_tree_node *p = s->lChild;
        while (p->rChild)
        {
            p = p->rChild;
        }
        return p;
    }
    
    void InOrder(Binary_tree_node *t)
    {
        if (t)
        {
            InOrder(t->lChild);
            cout << t->data << " ";
            InOrder(t->rChild);
        }
    }
    
    void Delete(int key, Binary_tree_node *&t)
    {
        if (key > t->data && t->rChild)
        {
            Delete(key, t->rChild);
        }
        else if (key < t->data && t->lChild)
        {
            Delete(key, t->lChild);
        }
        else if (key == t->data)
        {
            if (!t->lChild && !t->rChild) 
            {
                delete t;
                t = NULL;
            }
            else if (t->lChild && !t->rChild) 
            {
                Binary_tree_node *p = t;
                t = t->lChild;
                delete p;
            }
            else if (t->rChild && !t->lChild) 
            {
                Binary_tree_node *p = t;
                t = t->rChild;
                delete p;
            }
            else if (t->lChild && t->rChild) 
            {
                Binary_tree_node *p = getBefore(t);
                t->data = p->data;
                if (p->lChild)
                {
                    p->parents->rChild = p->lChild;
                }
                if (t->lChild == p) 
                {
                    t->lChild = NULL;
                }
                delete p;
            }
        }
    }
public:
    Binary_tree(int data) { root = new Binary_tree_node(data); }
    void Delete(int key)
    {
        Delete(key, root);
    }
    void Insert(int key)
    {
        InsertNode(key, root);
    }
    void InOrder()
    {
        InOrder(root);
        cout << endl;
    }
};
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, m;
        cin >> n;
        int data;
        cin >> data;
        Binary_tree bt(data);
        for (int i = 1; i < n; i++)
        {
            cin >> data;
            bt.Insert(data);
        }
        bt.InOrder();
        cin >> m;
        while (m--)
        {
            int key;
            cin >> key;
            bt.Delete(key);
            bt.InOrder();
        }
    }
    return 0;
}