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

void init(); //ノードの初期化
void insert(int ); //ノードの追加
struct Node* listSearch(int );
void deleteNode(struct Node *);
void deleteFirst();
void deleteLast();
void deleteKey(int );

struct Node{

  int key;
  struct Node *prev;
  struct Node *next;

};

struct Node *NIL;

int main(){

  int sumcommand,i,key;
  char command[10];

  init();

  scanf("%d",&sumcommand);

  for(i = 0; i < sumcommand; i++){

    scanf("%s",command);

    if((strcmp("insert",command)) == 0){

      scanf("%d",&key);
      insert(key);
    

    }

    else if((strcmp("delete",command)) == 0){

      scanf("%d",&key);
      deleteKey(key);
    }

    else if((strcmp("deleteFirst",command))==0){

      deleteFirst();
    }

    else if((strcmp("deleteLast",command))== 0){

      deleteLast();

    }

  }

  return 0;

}

void init(){

  NIL = (struct Node *)malloc(sizeof(struct Node));

  NIL->next  = NIL;
  NIL->prev = NIL;

}

void insert(int key){

  struct Node *x;

  x = (struct Node *) malloc(sizeof(struct Node));

  x -> key = key;

  NIL->next->prev = x;

  NIL->next = x;

  x->prev = NIL;

}

struct Node* listSearch(int key){

  struct Node *y;

  y = NIL->next;

  while(y != NIL && y->key != key){

    y = y->next;

  }

  return y;

}

void deleteNode(struct Node* x){

  if(x == NIL){

    return;

  }

  x->prev->next = x->next;
  x->next->prev = x->prev;

  free(x);

}

void deleteFirst(){

  deleteNode(NIL->next);

}

void deleteLast(){

  deleteNode(NIL->prev);

}

void deleteKey(int key){

  struct Node* deletenode;

  deletenode = listSearch(key);

  deleteNode(deletenode);

}