Group of Interest X37740


Statement
 

pdf   zip

html

The UPF Hacklab offers students a space where they can share knowledge to develop interests and skills in a multidisciplinary way. But each time an interest group is created, all members of Hacklab receive an email, spamming students who are not part of the interest group.

In order to automatize the mailing process, Alice has decided to create a repository of students together with their interests.

Input

The input consists of several test cases. Each test case starts with an integer N ≤ 500 of students. The following N lines contain the name of each student, the number of topics 1 ≤ In ≤ 10 he or she is interested in and the corresponding names of the topics.

Then, an integer M ≤ 500 of meetings will take place in the Hacklab. Each meeting will start with the word “Union” or “Intersection”, followed by the number of topics 1 ≤ T ≤ 10 and the name of each topic. “Union” means that all students interested in any of the T topics are invited, while “Intersection” means that only students interested in all T topics are invited.

Output

For each test case, output the number of students we have to send email to and their respective names in lexicographical order.

Public test cases
  • Input

    3
    John 1 Science
    Antoine 2 Science Math
    Bob 2 Literature Math
    2
    Intersection 2 Science Math
    Union 2 Science Literature
    

    Output

    1 Antoine
    3 Antoine Bob John
    
  • Information
    Author
    Javier Segovia
    Language
    English
    Official solutions
    C++
    User solutions