ZOJ Problem Set - 3674
As we known, Searching in Wiki is an useful way for everyone who wants to get information. Wiki, a website which allows its users to add, modify, or delete its content via a web browser, is famous for its huge information. You can find almost anything you heard in Wiki.
But sometimes you may get into trouble because of these huge information. It's hard to find key words from so many characters, that means you will spend a lot of time understanding what it describes. To solve this question, wiki provides some tips for each word. The tips for one word describe the meaning of this word briefly, so you can understand this word quickly through these tips. A tip consists only of 'a' to 'z' and 'A' to 'Z'. It's a convenient application.
This time you get a task from your teacher to search information for given words. It's a boring work, so you think of Wiki immediately. You get tips for each word from Wiki, and now you can answer questions the teacher may ask tomorrow morning easily. But to make sure, you decide to test yourself before tomorrow.
You prepare some queries for the test, each query contains some words given before and you should find out all the common tips of words in this query (A common tip means all the words in the query have this tip). In order to check your answer, you need to write a program now.
There are multiple test cases.
Each case begins with an integer n ( 1 <=n <=100 ), indicating the number of words given. Next N*2 lines, each two lines describe a word and its tips. For each two lines, the first line gives an word ( the word is no longer than 30 characters) , and the second line contains some tips for this word (the string is no longer than 200 characters), each two words are separated by one space.
The N*2+2 line contains an integer m ( 1 <=m <= 100 ), indicating the number of queries. Next m lines, each line contains some words, each two words are separated by one space.( the string is no longer than 200 characters)
Process to the end of input.
For each query, print one line with all the common tips of the words in query, and each two words are separated by one space. (The common tips should be printed in alphabet order) If no tips satisfy the previous request, print one line with "NO".
4 fish agile animal horse swift animal eagle fierce animal Kyuubee alien incubator 2 fish horse eagle fish horse eagle Kyuubee
Author: CHEN, Henghong
Contest: ZOJ Monthly, November 2012