#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
    struct Node* prev;
};


void insertAfter(struct Node* target, int newID) {
    if (target == NULL) {
        printf("Invalid input\n");
        return;
    }

    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    newNode->data = newID;
    newNode->next = target->next;
    newNode->prev = target;

    if (target->next != NULL) {
        target->next->prev = newNode;
    }

    target->next = newNode;
void printList(struct Node* head) {
    struct Node* temp = head;
    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("\n");
}

int main() {
    int n, targetID, newID;
    scanf("%d", &n);

    if (n <= 0) {
        printf("Invalid input\n");
        return 0;
    }

    struct Node *head = NULL, *temp, *target = NULL;
    for (int i = 0; i < n; i++) {
        int rideID;
        scanf("%d", &rideID);

        temp = (struct Node*)malloc(sizeof(struct Node));
        temp->data = rideID;
        temp->next = NULL;
        temp->prev = NULL;

        if (head == NULL) {
            head = temp;
        } else {
            struct Node* last = head;
            while (last->next != NULL) {
                last = last->next;
            }
            last->next = temp;
            temp->prev = last;
        }
    }
    scanf("%d %d", &targetID, &newID);
    temp = head;
    while (temp != NULL) {
        if (temp->data == targetID) {
            target = temp;
            break;
        }
        temp = temp->next;
    }

    
    printList(head);

    /
        insertAfter(target, newID);
        printList(head);
    } else {
        printf("Invalid input\n");
    }

    return 0;
}