#include
using namespace std;
class node
{ public:
int data;
node*next;
};
void insertattail(node*&head,int data)
{
if(head==NULL)
{
nodetemp=new node;
temp->data=data;
temp->next=head;
head=temp;
}
else
{
nodetemp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
node*n=new node;
temp->next=n;
n->next=NULL;
n->data=data;
}
}
void linkedlist(node*&head,int n)
{
for(int i=0;i<n;i++)
{
int data;
cin>>data;
insertattail(head,data);
}
}
node* merge(node*&head1,node*&head2)
{
nodep=head1;
nodeq=head2;
nodesorting=NULL;
if(p==NULL)
{
return q;
}
else if(q==NULL)
{
return p;
}
else
{
if(p->datadata)
{
sorting=p;
p=sorting->next;
}
else
{
sorting=q;
q=sorting->next;
}
}
nodenew_head=sorting;
while(p!=NULL&&q!=NULL)
{
if(p->datadata)
{
sorting->next=p;
sorting=p;
p=sorting->next;
}
else
{
sorting->next=q;
sorting=q;
q=sorting->next;
}
}
if(p==NULL)
{
sorting->next=q;
}
else
{
sorting->next=p;
}
return new_head;
}
void print(node*head)
{
if(head==NULL)
{
return;
}
else{
while(head!=NULL)
{
cout<<head->data<<" ";
head=head->next;
}
}
}
int main() {
nodehead1=NULL;
nodehead2=NULL;
int t;
cin>>t;
for(int i=0;i<t;i++)
{
int n1,n2;
cin>>n1;
linkedlist(head1,n1);
cin>>n2;
linkedlist(head2,n2);
node*head=merge(head1,head2);
print(head);
}
}