#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

struct Node {
    char name[101];
    struct Node* prev;
    struct Node* next;
};

struct Node* head = NULL;
struct Node* tail = NULL;

int isValidName(char* name) {
    int hasAlpha = 0;
    for (int i = 0; name[i]; i++) {
        if (!(isalpha(name[i]) || name[i] == ' '))
            return 0; // invalid char
        if (isalpha(name[i]))
            hasAlpha = 1; // ensure at least one alphabet
    }
    return hasAlpha;
}

void addFront(char* name) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    strcpy(newNode->name, name);
    newNode->prev = NULL;
    newNode->next = head;

    if (head == NULL) {
        head = tail = newNode;
    } else {
        head->prev = newNode;  // required by problem
        head = newNode;
    }
}

void addBack(char* name) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    strcpy(newNode->name, name);
    newNode->next = NULL;
    newNode->prev = tail;

    if (tail == NULL) {
        head = tail = newNode;
    } else {
        tail->next = newNode;
        tail = newNode;
    }
}

void removeFront() {
    if (head == NULL) return;
    struct Node* temp = head;
    head = head->next;
    if (head != NULL)
        head->prev = NULL;
    else
        tail = NULL; // list became empty
    free(temp);
}

void removeBack() {
    if (tail == NULL) return;
    struct Node* temp = tail;
    tail = tail->prev;
    if (tail != NULL)
        tail->next = NULL;
    else
        head = NULL; // list became empty
    free(temp);
}

int main() {
    int n;
    scanf("%d", &n);

    for (int i = 0; i < n; i++) {
        char operation[20];
        char container[101];
        int args = scanf("%s", operation);

        if (args != 1) continue;

        if (strcasecmp(operation, "addFront") == 0 || strcasecmp(operation, "addBack") == 0) {
            scanf(" %[^\n]", container);
            if (!isValidName(container)) {
                printf("Invalid input.\n");
                return 0;
            }
            if (strcasecmp(operation, "addFront") == 0)
                addFront(container);
            else
                addBack(container);
        } else if (strcasecmp(operation, "removeFront") == 0) {
            removeFront();
        } else if (strcasecmp(operation, "removeBack") == 0) {
            removeBack();
        }
    }

    if (head == NULL) {
        printf("Empty\n");
    } else {
        struct Node* current = head;
        while (current) {
            printf("%s", current->name);
            current = current->next;
            if (current) printf("  "); // two spaces as in sample
        }
        printf("\n");
    }

    return 0;
}