#include <stdio.h>
#include <stdlib.h>
struct Node {
    int data;
    struct Node *prev, *next;
};
struct Node* createNode(int data) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    if (newNode == NULL) {
        printf("Memory allocation failed");
        exit(0);
    }
    newNode->data = data;
    newNode->prev = NULL;
    newNode->next = NULL;
    return newNode;
}
void printList(struct Node* head) {
    struct Node* temp = head;
    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("\n");
}
void deleteLastNode(struct Node** head) {
    if (*head == NULL) 
    return;
    struct Node* temp = *head;
    while (temp->prev == NULL) {
        temp = temp->next;
        if (temp->prev == NULL) {
            free(temp);
            *head = NULL;
            return;
        }
        temp->prev->next = NULL;
        free(temp);
    }
    int main() {
        int n;
        scanf("%d", &n);
        if (n < 0) {
            printf("Invalid input");
            return 0;
        }
        struct Node *head = NULL, *tail = NULL;
        for (int i = 0; i < n; i++) {
            int value;
            scanf("%d", &value);
        struct Node* newNode = createNode(value);
        if (head == NULL) {
            head = newNode;
            tail = newNode;
        } else {
            tail->next = newNode;
            newNode->prev = tail;
            tail = newNode;
        }
    }
    printList(head);
    deleteLastNode(&head);
    printList(head);
    return 0;
}