PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FW0000038. Nhóm phương án liên thông (Test)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Nhóm phương án liên thông (Test)

Test

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

Each record gives five alternatives and one chosen letter. Add directed edges from the chosen letter to each of the other five alternatives and output strongly connected letter groups.

Input

Integer nn, followed by nn lines of six uppercase letters: five alternatives and the selected answer as the sixth letter.

Output

Print each strongly connected component on its own line with alphabetically sorted letters, and sort components by their first letter.

Subtasks

  • Subtask 1 (40 points): n≤12n\le12; 1≤n≤1001\le n\le100; each letter belongs to A–Z; repeated letters are allowed.
  • Subtask 2 (60 points): 1≤n≤1001\le n\le100; each letter belongs to A–Z; repeated letters are allowed.

Examples

Input

4
A B C D E C
F C H I J J
K B H I F I
K C E B J K

Output

A
B
C
D
E
F
H
I J K

Explanation

Construct directed edges from the chosen letter (sixth character) to the five alternatives. Only I, J, K are mutually reachable; other active letters are singleton components.