Print disconnected graph in tabular format

You are a class teacher and you notice few things about your students:

  1. Relationship between students is reflective
    i.e. A hates B <=> B hates A

  2. There are 3 kind of students

    • friendly : these students get along with anyone, even with the troublemakers.
    • trouble makers : love to pick fights
            B
           /
      A - X
           \
            C
      * X is a troublemaker here, (s)he does not get along with A, B and C. 
      * A, B and C don't have any conflict among them
    • internal conflict : everyone hates everyone
           Q
         / | \
        P -+- S
         \ | /
           R
      * Each one of P, Q, R and S hates other three.

You have to select a team for upcoming sports event. To avoid any conflict within the team, you decided to pick students such that there are no two students in the team who hate each other.

You had interview with each student and came up with list of non compatible students for each of your students.

You have to write an algorithm (focused on time-efficiency) to sort this data in rows So that you can select any one part from each row of output for your team (see examples).

Input

Line 1 : N (Number of students, 0 < N <= 26)
Next 2*N lines; for each ith student of N :

  • n (number of students ith student does not like, 0 <= n < N)
  • string of length n, containing n Capital english latters where A ~ student-0, B ~ student-1 ... Z ~ student-25

Examples

Example 1

Input:

> 3
> 1
> C
> 0
> 
> 1
> A

Visualization:

    Student | Does not like
    --------+--------------
     A      | C
     B      | 
     C      | A
	 
            C 
           / 
          A     B
		  
Output:

> A|C
> B

Example 2

Input:

> 8
> 2
> CE
> 1
> G
> 2
> AE
> 0
>
> 2
> AC
> 0
>
> 2
> BH
> 1
> G

Visualization:

    Student | Does not like
    --------+--------------
     A      | C, E
     B      | G 
     C      | A, E 
     D      | 
     E      | A, C 
     F      | 
     G      | B, H
     H      | G

            A     B - G   
           / \         \      D    F
          C - E         H

Output:

> A|C|E
> BH|G
> D
> F

Example 3

Input:

> 9
> 0
> 
> 1
> C
> 1
> B
> 5
> EFGHI
> 1
> D
> 1
> D
> 1
> D
> 1
> D
> 1
> D

Visualization:

    Student | Does not like
    --------+--------------
     A      | 
     B      | C 
     C      | B
     D      | E, F, G, H, I
     E      | D 
     F      | D
     G      | D
     H      | D
     I      | D
                      F G
            C         |/    
    A      /      E - D - H 
          B           |     
                      I

Output:

> A
> B|C
> D|EFGHI
Comments (3)