#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct Node{
    int data;
    struct Node* prev;
    struct Node* next;
};

struct Node* createNode(int data){
    struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));
    newNode->data = data;
    newNode->prev = NULL;
    newNode->next=NULL;
    return newNode;
}
void printList(struct Node* head){
    struct Node* temp=head;
    while(temp !=NULL){
        printf("%d ",temp->data);
        temp=temp->next;
    }
    printf("\n");
}
int main(){
    int n;
    scanf("%d",&n);
     
     if(n<0||n>10){
         printf("Invalid input\n");
         return 0;
     }
     struct Node *head=NULL,*tail=NULL;
     for(int i=0; i<n;i++){
         int val;
         scanf("%d",&val);
         struct Node* newNode=ceateNode(val);
         if(head==NULL){
             head=newNode;
             tail=newNode;
         }else{
             tail->next=newNode;
             newNode->prev=tail;
             tail=newNode;
         }
     }
     int targetID,newID;
     scanf("%d %d",&targetID,&newID);
     printList(head);
     
     struct Node* curr=head;
     while(curr !=NULL && curr->data!=targetID){
         curr=curr->next;
     }
     if(curr == NULL){
         printf("Invalid input\n");
         return 0;
     }
     struct Node* newNode=createNode(newID);
     newNode->next=curr->next;
     newNode->prev=curr;
     
     if(curr->next !=NULL){
         curr->next->prev=newNode;
     }
     curr->next=newNode;
     
     printList(head);
     return 0;
}