#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

// Helper function to check if string is integer number
int is_integer(char *str) {
    int i = 0;
    if (str == '-') i++; // Handle negative
    for (; str[i] != '\0'; i++) {
        if (!isdigit(str[i])) return 0;
    }
    return (i > 0);
}

int main() {
    char buffer;

    // Read n and validate
    if (!fgets(buffer, sizeof(buffer), stdin)) {
        printf("Invalid input");
        return 0;
    }
    buffer[strcspn(buffer, "\n")] = 0;
    if (!is_integer(buffer)) {
        printf("Invalid input");
        return 0;
    }
    int n = atoi(buffer);

    // Read array
    if (!fgets(buffer, sizeof(buffer), stdin)) {
        printf("Invalid input");
        return 0;
    }
    buffer[strcspn(buffer, "\n")] = 0;
    int arr, idx = 0;
    char *token = strtok(buffer, " ");
    while (token != NULL) {
        if (!is_integer(token)) {
            printf("Invalid input");
            return 0;
        }
        arr[idx++] = atoi(token);
        token = strtok(NULL, " ");
    }
    if (idx != n) {
        printf("Invalid input");
        return 0;
    }

    // Read oldVal and newVal
    if (!fgets(buffer, sizeof(buffer), stdin)) {
        printf("Invalid input");
        return 0;
    }
    buffer[strcspn(buffer, "\n")] = 0;
    int vals[2], vdx = 0;
    token = strtok(buffer, " ");
    while (token != NULL) {
        if (!is_integer(token)) {
            printf("Invalid input");
            return 0;
        }
        vals[vdx++] = atoi(token);
        token = strtok(NULL, " ");
    }
    if (vdx != 2) {
        printf("Invalid input");
        return 0;
    }
    int oldVal = vals, newVal = vals[1];

    // Replace logic
    int found = 0;
    for (int i = 0; i < n; i++) {
        if (arr[i] == oldVal) {
            arr[i] = newVal;
            found = 1;
        }
    }
    if (!found) {
        printf("Value not found");
    } else {
        for (int i = 0; i < n; i++) {
            printf("%d", arr[i]);
            if (i != n - 1) printf(" ");
        }
    }
    return 0;
}