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