#K41282. Frequency-based String Sorting

    ID: 26831 Type: Default 1000ms 256MiB

Frequency-based String Sorting

Frequency-based String Sorting

You are given several test cases. Each test case contains a list of strings, and your task is to sort each list according to a custom order. For a given string \(s\), first compute the frequency of each character. Then, form a sorted list of these frequencies, \(f(s) = [c_1, c_2, \dots, c_k]\), where each \(c_i\) is a frequency count and the list is in increasing order.

The strings should be sorted primarily by their \(f(s)\) value, and in case of a tie, by the string itself in lexicographic order.

inputFormat

The input is read from standard input (stdin). The first line contains an integer \(T\), the number of test cases. Each test case starts with an integer \(N\) on a new line, which is followed by \(N\) lines each containing a single string.

outputFormat

For each test case, output one line containing the sorted strings separated by a single space. The output is written to standard output (stdout).

## sample
2
3
banana
apple
pear
4
a
ab
aa
b
pear apple banana

a b ab aa

</p>