#K96287. Group Anagrams

    ID: 39053 Type: Default 1000ms 256MiB

Group Anagrams

Group Anagrams

You are given a series of test cases. In each test case, you will be provided with a list of strings. Your task is to group the anagrams together for each test case.

An anagram is defined as a word formed by rearranging the letters of another word. Mathematically, two words S1 and S2 are anagrams if \[ \text{sort}(S_{1}) = \text{sort}(S_{2}) \] where \(\text{sort}(\cdot)\) denotes the operation of sorting the characters in non-decreasing order.

For each test case, you must output a single line consisting of the original words, but grouped by their anagram sets, preserving the order of the first appearance of each anagram group.

inputFormat

The input begins with an integer T (1 ≤ T ≤ 100), the number of test cases. Each test case starts with an integer N (1 ≤ N ≤ 1000), denoting the number of strings. The next line contains N space-separated words. Each word consists of lowercase English letters.

outputFormat

For each test case, print a single line that contains the words grouped by their anagrams. Words within the same group appear in the same order as in input. Different anagram groups are concatenated in the order in which their first member appears in the list. Separate words with a single space.

## sample
2
6
bat tab cat act tac dog
4
listen silent enlist inlets
bat tab cat act tac dog

listen silent enlist inlets

</p>