#K61047. Organize Books by Custom Sorting

    ID: 31222 Type: Default 1000ms 256MiB

Organize Books by Custom Sorting

Organize Books by Custom Sorting

You are given a list of book IDs for several test cases. Each book ID is represented as a string and may contain leading zeros. The task is to sort these book IDs according to the following criteria, in order of priority:

  1. The sum of the digits in the book ID, i.e. (\sum_{i=1}^{|s|} s_i) where (s_i) is the (i)-th digit of the book ID.
  2. The number of digits (i.e. the length of the string).
  3. Lexicographical order (dictionary order).

For each test case, you will output the sorted list of book IDs on a new line. The input is provided via standard input and output should be printed to standard output.

For example, if a test case has 3 book IDs: "103", "002", and "21", the sorted order would be "002 21 103".

inputFormat

The first line of input contains an integer (T), the number of test cases. For each test case:

  • The first line contains an integer (N), the number of book IDs.
  • The second line contains (N) space-separated strings representing the book IDs.

outputFormat

For each test case, output one line containing the sorted book IDs separated by a single space.## sample

2
3
103 002 21
4
123 234 345 456
002 21 103

123 234 345 456

</p>