// editor4
#include<stdio.h>
#include<stdlib.h>

typedef struct node
{
    int data;
    struct node *next,*pre;
    
}node;
node *newnode,*head,*tail;
node *tem;
void create(int num)
{
    newnode=(node*)malloc(sizeof(node));
    newnode->data=num;
    newnode->next=NULL;
     newnode->pre=NULL;
     if(head==NULL)
     {
         head=newnode;
         tail=newnode;
         
     }
     else 
     {
         newnode->pre=tail;
         tail->next=newnode;
         tail=newnode;
     }
    tail->next=head;
    head->pre=tail;
}
void del(int k)
{itr=head->next;
    if(k==head->data)
    {
      tem=head;
      head=head->next;
      tail->next=head;
     tem=NULL;
    }
    else if(k==tail->data)
    {tem=tail;
        tail=tail->pre;
        head->pre=tail;
       tem=NULL;
        
    }
    else
    {node *tem1;
        for(tem=head->next;tem->data!=k;tem=tem->next)
    {
        tem1=tem;
    }
    tem1->next=tem->next;
    tem->next->pre=tem1;
    tem=NULL;
       
        
    }
}
void display()
{
    node *t=head;
  do{
      printf("%d ",t->data);
      t=t->next;
  }while(t!=head);
}
int main()
{
    int size,num;
    scanf("%d",&size);
    for(int i=0;i<size;i++)
    {
        scanf("%d",&num);
        create(num);
    }
    int k;
    scanf("%d",&k);
    del(k);
    if(tem !=NULL)
    {
        printf("Node not found");
        return 0;
    }
    display();
    
}