eolymp
bolt
Спробуйте наш новий інтерфейс для відправки розв'язків
Задачі

The Writer`s Club

The Writer`s Club

Ліміт часу 1 секунда
Ліміт використання пам'яті 64 MiB

Forget Facebook, forget Second Life, The Writers’ Club is the next big thing on the Internet. The Writers’ Club is where both writers and lovers of short stories get to meet, publish, read, and discuss the writers’ promising contributions. As any other Internet virtual community, the website gives readers the facility to create a list of their favorite writers. The website maintainers have noticed that readers tend to favor writers already favored by the readers’ favorite writers. For example, if John likes the stories written by Alice, then many of the readers who likes John’s stories also like Alice’s. Needless to say, John’s readers tend to also admire Alice’s favorite writers too.

The website would like to start a recommendation service based on this observation. Continuing with the above example, the website would like to recommend Alice (and her favorite writers, and their favorite writers, and so on) to all of John’s readers. Of course, the recommendation service has to be smart enough not to recommend a writer already favored by the reader receiving the recommendation.

Вхідні дані

Your program will be tested on one or more test cases. The first line of a test case specifies two positive integers: T and N where T is the total number of users (not exceeding 100000) and N is the number of writers (not exceeding 100). N line follows, one for each writer. Each line is made of one or more names, the first being the name of a writer, and the remaining being the names of admirers of that writer. For example:

writerreader_1reader_2reader_d

Where writer is the writer’s name, reader_1reader_d are the names of the writer’s admirers. A name is a sequence of one or more lower-case letters, no longer than 16 characters. Names are unique throughout the test case. Names are separated by one or more spaces.

The last line of the input file is made of two zero’s.

Вихідні дані

For each test case, the first line of the output should be:

— CASE k

Where k is the test case number (starting at 1), and '-' is the minus sign.

Following that, there should be zero or more lines, one line for each writer being recommended. The format is:

writerreader_1reader_2...

Where writer is the name of the writer to be recommended for reader_1, reader_2, and so on. The output lines should be sorted alphabetically according to writers, and for each writer, readers should also be sorted alphabetically. No writer should be recommended for himself. All names in the output are separated by a single space.

Приклад

Вхідні дані #1
7 3
john paul ringo
alice paul john
bob  alice sunny cher
0 0
Вихідні дані #1
--- CASE 1
alice ringo
bob john paul ringo