#include <stdio.h>
#include <stdlib.h>
typedef struct node{
    int data;
    struct node *prev;
    struct node *next;
}nd;
nd *head=NULL,*tail,*newnode,*temp,*curr,*prev,*next;
int found=0;
void create(int val){
    newnode=(nd*)malloc(sizeof(nd));
    newnode->data=val;
    newnode->prev=NULL;
    newnode->next=NULL;
    if(head==NULL){
        head=newnode;
        tail=newnode;
    }
    tail->next=newnode;
    newnode->prev=tail;
    tail=newnode;
    tail->next=head;
}
void deleteval(int val){
    if(head->data==val){
        next=head->next;
        tail->next=head->next;
        next->prev=tail;
        head=next;
    }else if(tail->data==val){
        prev=tail->prev;
        head->prev=tail->prev;
        prev->next=head;
        tail=prev;
    }else{
        curr=head;
        while(curr->data!=val){
            if(curr->next==head){
                printf("Node not found");
                found=1;
                break;
            }
            curr=curr->next;
        }
        prev=curr->prev;
        next=curr->next;           
        if(curr->data==val){
            prev->next=next;
            next->prev=prev;
        }
    }
}
void display(){
    if(found==1){
        retrun ;
    }
    temp=head;
    while(temp!=NULL){
        printf("%d ",temp->data);
        if(temp->next==head){
            break;
        }
        temp=temp->next;
    }
}
int main(){
    int n,x,y;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&x);
        create(x);
    }
    scanf("%d",&y);
    deleteval(y);
    display();
}