All problems

Problem 751

Accounts Merge

Medium · 20 pts · Union Find, Graph, Hash Table, Sorting

Problem

Each account is a person's name followed by some email addresses. Two accounts belong to the same person if they share at least one email, and this is transitive, so chains of shared emails join many accounts. Accounts with the same name but no shared email are different people. Merge the accounts and print each person as the name followed by all their distinct emails in sorted order. Print the merged accounts sorted by their first email. Union-find over the emails does the grouping.

Input

The first line contains N. Each of the next N lines contains a name, a count K and then K emails.

Output

Print each merged account on its own line: the name, then its emails in sorted order, all separated by spaces. Order the lines by their first email.

Constraints

1 <= N <= 1000; 1 <= K <= 10; names and emails have no spaces; accounts that share an email have the same name

Example input

4
John 2 johnsmith@mail.com john_newyork@mail.com
John 2 johnsmith@mail.com john00@mail.com
Mary 1 mary@mail.com
John 1 johnnybravo@mail.com

Example output

John john00@mail.com john_newyork@mail.com johnsmith@mail.com
John johnnybravo@mail.com
Mary mary@mail.com

Explanation

The first two John accounts share johnsmith@mail.com, so they merge into John with three emails. Mary and the other John share nothing with anyone, so three accounts remain, ordered by their first email.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Minimum Multiplications to Reach End Next: Swim in Rising Water