// editor5
#include<stdio.h>
#include<stdlib.h>
struct node{
  int data;
  struct node* next;
};
struct node* head=NULL;
struct node* temp;

void createlist(){
    struct node* newnode=(struct node*)malloc(sizeof(struct node));
    scanf("%d",&newnode->data);
    newnode->next=NULL;
    if(head==NULL){
        head=temp=newnode;
    }
    else
    {
        temp->next=newnode;
        temp=newnode;
    }
}
void printlist(){
    temp=head;
    while(temp!=NULL){
        printf("%d ",temp->data);
        temp=temp->next;
    }
    printf("\n");
}
void replace(int oldVal,int newVal){
    temp=head;
    int count;
    while(temp!=NULL && temp->data!=oldVal){
        temp=temp->next;
        count++;
    }
    if(count==n){
        printf("Value not found");
        exit(1);
    }
    if(temp!=NULL){
        temp->data=newVal;
    }
    printlist();
}

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        createlist();
    }
    int oldVal;
    int newVal;
    scanf("%d %d",&oldVal,&newVal);
    replace(oldVal,newVal);
}