Under construction

Still being built. Things may change

I Hate SPAM, but Some People Love It

Time limit1sMemory limit128 MB

Summary
Simulate SPAM spreading through a friendship graph, then for each person report the attribute earned from every message based on how many friends they forwarded it to.
Level

Medium5 of 10

Topics
Graph, BFS, Simulation, Implementation
Solved
No attempts yet

Statement

Unfortunately, SPAM messages are becoming more and more common. Some of them can multiply, because they ask you to forward them to everyone you know. Some SPAM messages wish you good luck, others promise you will become rich, and others simply remind you how important it is to tell your friends how much you care. Here is a typical SPAM message.

From: Alice
To: Bob, Mary, Julia, Paul

Hi, this is a good-luck email. I wish for you to become a millionaire, but
that is up to you. If you
* send this email to 10 or more people, you will become a millionaire;
* send this email to 5 or more people, you will become rich;
* send this email to fewer than 5 people, you will stay poor.
As I said, it is up to you. Write your email and get rich! :-)

Alice

People usually react to a SPAM message in one of two ways:

  • they delete it immediately without even reading it (they hate SPAM); or
  • they forward it to everyone they know (they love SPAM).

In this problem we assume that everyone loves SPAM, so everyone who receives a message forwards it to all of the friends whose email address they know. Nobody, however, forwards the same message more than once, and nobody sends a message to themselves.

Each SPAM message produces a different effect depending on how many friends you forward it to. A SPAM message defines two threshold values T1T_1 and T2T_2 (T1≤T2T_1 \le T_2) and three attributes A1A_1, A2A_2, and A3A_3. Let TT be the number of messages a person forwards for a given SPAM (that is, the number of friends they send it to; if they never receive that SPAM, then T=0T = 0). The person acquires the attribute:

  • A1A_1 if T<T1T < T_1;
  • A2A_2 if T1≤T<T2T_1 \le T < T_2;
  • A3A_3 if T≥T2T \ge T_2.

You are given a group of people and, for each person, the set of friends whose email address they know. You are also given a set of distinct SPAM messages, each with its thresholds, its three attributes, and the person who originates it. For each person, determine the attributes they acquire from every SPAM message. You may assume that every SPAM originator has at least one friend.

Input

The input contains several test cases. The first line of a test case contains an integer NN, the number of people in the group (2≤N≤202 \le N \le 20). People are identified by integers from 11 to NN.

The next NN lines describe friendships: the ii-th of these lines lists the friends of person ii — the people whose email address person ii knows. Each such line is a list of integers FF (1≤F≤N1 \le F \le N, F≠iF \ne i) terminated by a single 00.

Then comes the description of the SPAM messages (at most 100100 of them), one per line. Each line contains an integer PP — the person who originates the SPAM (1≤P≤N1 \le P \le N) — followed by the two threshold values T1T_1 and T2T_2, and the three attributes A1A_1, A2A_2, and A3A_3 (each attribute is a single word of at most 2020 letters). The list of SPAM messages ends with a line containing a single 00.

Finally, NN lines follow; the ii-th contains the name of person ii (a single word of at most 2020 letters).

The end of the input is indicated by N=0N = 0.

Output

For each test case, output one line for each person, in the order the people appear in the input. Each line contains the person's name followed by a colon and a space, and then the attributes that person acquired, in the same order as the SPAM messages appear in the input. Each attribute is followed by a single space.

Examples1

  1. Example 1

    Input
    5
    2 3 0
    1 3 5 4 0
    5 0
    0
    4 1 0
    1 2 4 poor rich millionaire
    5 3 10 sad normal happy
    0
    Bob
    Paul
    Mary
    Alice
    Julia
    6
    2 0
    1 3 0
    1 2 4 0
    1 2 3 5 0
    1 2 3 4 0
    1 3 4 0
    1 2 4 red green blue
    1 2 4 dumb normal smart
    6 3 5 ugly bad good
    0
    Peter
    Paul
    Victoria
    John
    Julia
    Anne
    0 0
    
    Expected output
    Bob: rich sad 
    Paul: millionaire normal 
    Mary: poor sad 
    Alice: poor sad 
    Julia: rich sad 
    Peter: red dumb ugly 
    Paul: green normal ugly 
    Victoria: green normal bad 
    John: blue smart bad 
    Julia: blue smart bad 
    Anne: red dumb bad