#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

struct Node {
    int data;
    struct Node* next;
};

struct Node* head = NULL;

int isValid(char str[]) {
    for (int i = 0; str[i]; i++) {
        if (!isdigit(str[i]) && !(i == 0 && str[i] == '-'))
            return 0;
    }
    return 1;
}

void insertAtEnd(int value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = value;
    newNode->next = NULL;

    if (head == NULL) {
        head = newNode;
        return;
    }

    struct Node* temp = head;
    while (temp->next != NULL)
        temp = temp->next;
    temp->next = newNode;
}

int replaceAll(int oldVal, int newVal) {
    struct Node* temp = head;
    int found = 0;
    while (temp != NULL) {
        if (temp->data == oldVal) {
            temp->data = newVal;
            found = 1;
        }
        temp = temp->next;
    }
    return found;
}

void display() {

struct Node* temp = head;
    while (temp != NULL) {
        printf("%d", temp->data);
        if (temp->next != NULL)
            printf(" ");
        temp = temp->next;
    }
}

int main() {
    char input[100];
    int n;

    scanf("%s", input);
    if (!isValid(input)) {
        printf("Invalid input\n");
        return 0;
    }
    n = atoi(input);

    for (int i = 0; i < n; i++) {
        scanf("%s", input);
        if (!isValid(input)) {
            printf("Invalid input\n");
            return 0;
        }
        insertAtEnd(atoi(input));
    }

    char oldStr[100], newStr[100];
    scanf("%s %s", oldStr, newStr);
    if (!isValid(oldStr) || !isValid(newStr)) {
        printf("Invalid input\n");
        return 0;
    }

    int oldVal = atoi(oldStr);
    int newVal = atoi(newStr);

    if (!replaceAll(oldVal, newVal)) {
        printf("Value not found\n");
        return 0;
    }

    display();
    return 0;
}