#C43. Group Anagrams

    ID: 47822 Type: Default 1000ms 256MiB

Group Anagrams

Group Anagrams

You are given a list of words. Your task is to group the words that are anagrams of each other. Two words are anagrams if and only if they consist of the same characters in a different order (the comparison is case-sensitive).

After grouping the words, sort each group by lexicographical order ignoring case. Then, sort all groups by the lexicographical order (ignoring case) of the first word in each group. Finally, output each group on a separate line with words separated by a single space.

Note: If the input list is empty (i.e. n = 0), no output should be produced.

The lexicographical comparisons should be performed in a case-insensitive manner when sorting the words within each group and when ordering the groups.

For example, given the input:

6
eat tea tan ate nat bat

One valid output is:

ate eat tea
bat
nat tan

inputFormat

The first line contains an integer n (0 ≤ n ≤ 1000), representing the number of words.

The second line contains n words separated by spaces. If n is 0, the input will be empty.

outputFormat

Output the anagram groups, one group per line. Within each group, the words should be sorted in lexicographical order (ignoring case) and separated by a single space. The groups themselves must be sorted (ignoring case) according to the first word of each group.

## sample
6
eat tea tan ate nat bat
ate eat tea

bat nat tan

</p>