// 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));
    if(scanf("%d",&newnode->data)==0){
        printf("Invalid input\n");
        exit(1);
    }
    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,int n){
    temp=head;
    int count=0;
    for(int i=0;i<n;i++){
        while(temp!=NULL && temp->data!=oldVal){
            temp=temp->next;
            count++;
        }
    }
    if(count==n)
    {
        printf("Value not found");
        exit(1);
    }
    while(temp!=NULL&&temp->data==oldVal){
        temp->data=newVal;
        temp=temp->next;
    }
    printlist();
}
int main()
{
    int n;
    scanf("%d",&n);
    if(n<0){
        printf("Invalid input\n");
        return 0;
    }
    for(int i=0;i<n;i++){
        createlist();
    }
    int oldVal;
    int newVal;
    scanf("%d %d",&oldVal,&newVal);
    replace(oldVal,newVal,n);