#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; 
        if (isalpha(name[i]))
            hasAlpha = 1; 
    }
    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; 
        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; 
    free(temp);
}


void removeBack() {
    if (tail == NULL) return;
    struct Node* temp = tail;
    tail = tail->prev;
    if (tail != NULL)
        tail->next = NULL;
    else
        head = NULL;
    free(temp);
}

int main() {
    int n;
    scanf("%d", &n);
    getchar(); 

    for (int i = 0; i < n; i++) {
        char line[150];
        fgets(line, sizeof(line), stdin);
        line[strcspn(line, "\n")] = '\0'; 

        char cmd[20], container[101] = "";
        int count = sscanf(line, "%s %[^\n]", cmd, container);

        if (strcasecmp(cmd, "addFront") == 0 || strcasecmp(cmd, "addBack") == 0) {
            if (count != 2 || !isValidName(container)) {
                printf("Invalid input.\n");
                return 0;
            }
            if (strcasecmp(cmd, "addFront") == 0)
                addFront(container);
            else
                addBack(container);
        }
        else if (strcasecmp(cmd, "removeFront") == 0) {
            removeFront();
        }
        else if (strcasecmp(cmd, "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("  "); 
        }
        printf("\n");
    }

    return 0;
}