#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int key;
    struct Node* next;
} Node;

Node* createNode(int key) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->key = key;
    newNode->next = NULL;
    return newNode;
}

void append(Node** head, int key) {
    Node* newNode = createNode(key);
    if (*head == NULL) {
        *head = newNode;
        return;
    }
    Node* temp = *head;
    while (temp->next != NULL)
        temp = temp->next;
    temp->next = newNode;
}

void printList(Node* head) {
    while (head != NULL) {
        printf("%d ", head->key);
        head = head->next;
    }
    printf("\n");
}

Node* mergeSorted(Node* a, Node* b) {
    Node dummy;
    Node* tail = &dummy;
    dummy.next = NULL;

    while (a != NULL && b != NULL) {
        if (a->key <= b->key) {
            tail->next = a;
            a = a->next;
        } else {
            tail->next = b;
            b = b->next;
        }
        tail = tail->next;
    }


    if (a != NULL) tail->next = a;
    if (b != NULL) tail->next = b;

    return dummy.next;
}

int main() {
    Node* a = NULL;
    Node* b = NULL;

    int n, m, val;

    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &val);
        append(&a, val);
    }

    scanf("%d", &m);

    for (int i = 0; i < m; i++) {
        scanf("%d", &val);
        append(&b, val);
    }

   
    printList(a);

    printList(b);

    Node* ans = mergeSorted(a, b);

    printList(ans);

    return 0;
}