#include <iostream>
using namespace std;

struct Node {
    int data;
    Node* next;
};

Node* createLinkedList(int arr[], int n) {
    if (n == 0) return nullptr;

    Node* head = new Node{arr[0], nullptr};
    Node* tail = head;

    for (int i = 1; i < n; i++) {
        Node* newNode = new Node{arr[i], nullptr};
        tail->next = newNode;
        tail = newNode;
    }

    return head;
}

bool replaceValues(Node* head, int oldVal, int newVal) {
    bool found = false;
    Node* temp = head;

    while (temp != nullptr) {
        if (temp->data == oldVal) {  // Required condition
            temp->data = newVal;
            found = true;
        }
        temp = temp->next;
    }

    return found;
}

void printList(Node* head) {
    Node* temp = head;
    while (temp != nullptr) {
        cout << temp->data;
        if (temp->next != nullptr) cout << " ";
        temp = temp->next;
    }
    cout << endl;
}

int main() {
    int n;
    cin >> n;

    if (n < 0 || n > 1000) {
        cout << "Invalid input" << endl;
        return 0;
    }

    int* arr = new int[n];

    for (int i = 0; i < n; i++) {
        if (!(cin >> arr[i])) {
            cout << "Invalid input" << endl;
            delete[] arr;
            return 0;
        }
    }

    int oldVal, newVal;
    if (!(cin >> oldVal >> newVal)) {
        cout << "Invalid input" << endl;
        delete[] arr;
        return 0;
    }

    Node* head = createLinkedList(arr, n);

    if (replaceValues(head, oldVal, newVal)) {
        printList(head);
    } else {
        cout << "Value not found" << endl;
    }

    // Cleanup
    Node* temp;
    while (head != nullptr) {
        temp = head;
        head = head->next;
        delete temp;
    }

    delete[] arr;
    return 0;
}