#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

typedef struct Node {
    char song[105];
    struct Node *next;
} Node;
Node* newNode(char *song) {
    Node temp = (Node)malloc(sizeof(Node));
    strcpy(temp->song, song);
    temp->next = NULL;
    return temp;
}
int invalidString(char *s) {
    if (strlen(s) == 0) return 1;
    for (int i = 0; s[i]; i++) {
        if (isspace(s[i])) return 1;  
    }
    return 0;
}
int main() {
    int n;
    if (scanf("%d", &n) != 1 || n < 1 || n > 100) {
        printf("Invalid input");
        return 0;
    }
    Node *head = NULL, *tail = NULL;
    char song[105];
    for (int i = 0; i < n; i++) {
        if (scanf("%s", song) != 1 || invalidString(song)) {
            printf("Invalid input");
            return 0;
        }
        Node *temp = newNode(song);
        if (!head) {
            head = tail = temp;
        } else {
            tail->next = temp;
            tail = temp;
        }
    }
    tail->next = head;
     char delSong[105];
    if (scanf("%s", delSong) != 1 || invalidString(delSong)) {
        printf("Invalid input");
        return 0;
    }
    Node *current = head, *prev = tail;
    int found = 0;
    do {
        if (strcmp(current->song, delSong) == 0) {
            found = 1;
            if (current == head && current == tail) {
                     printf("Playlist is empty");
                return 0;
            }
            if (current == head) head = head->next;
            if (current == tail) tail = prev;
            prev->next = current->next;
            free(current);
            break;
        }
        prev = current;
        current = current->next;
    } while (current != head);

    if (!found) {
        printf("Song not found");
        return 0;
    }
    current = head;
    int first = 1;
    do {
        if (!first) printf(" ");
        printf("%s", current->song);
        first = 0;
        current = current->next;
    } while (current != head);

    return 0;
}