#include <stdio.h>
#include <string.h>
#include <ctype.h>

// Function to check if a string has only letters and spaces
int isValidString(char *str) {
    for (int i = 0; str[i] != '\0'; i++) {
        if (!(isalpha(str[i]) || str[i] == ' ')) {
            return 0; // Invalid if digit or other special characters
        }
    }
    return 1;
}

int main() {
    int n;
    if (scanf("%d", &n) != 1 || n < 1 || n > 500) {
        printf("Invalid input");
        return 0;
    }

    getchar(); // consume newline

    char titles[505][105];
    char query[105];

    // Input book titles
    for (int i = 0; i < n; i++) {
        fgets(titles[i], sizeof(titles[i]), stdin);
        titles[i][strcspn(titles[i], "\n")] = '\0'; // remove newline

        if (!isValidString(titles[i])) {
            printf("Invalid input");
            return 0;
        }
    }

    // Input search query
    fgets(query, sizeof(query), stdin);
    query[strcspn(query, "\n")] = '\0';

    if (!isValidString(query)) {
        printf("Invalid input");
        return 0;
    }

    for(int i = 0; i <