Under construction

Still being built. Things may change

Game Sort: Part 1

Interview

Time limit5sMemory limit1024 MB

Summary
Given P strings, rearrange the letters inside each one so the resulting list is in non-decreasing lexicographic order, or report that it cannot be done.
Level

Medium5 of 10

Topics
String, Greedy
Solved
No attempts yet

Problem

Note: The main parts of the statements of the problems Game Sort: Part 1 and Game Sort: Part 2 are identical, except for the last paragraph. The problems can otherwise be solved independently.

Amir and Badari are playing a sorting game. The game starts with a string S\mathbf{S} and an integer P\mathbf{P} chosen by an impartial judge. Amir must split S\mathbf{S} into exactly P\mathbf{P} contiguous non-empty parts (substrings). For example, if S\mathbf{S} is CODEJAM and P=3\mathbf{P} = 3, Amir can split it as [COD, EJA, M] or [CO, D, EJAM]. Amir cannot split it as [COD, EJAM], [COD, JA, M], [EJA, COD, M], or [CODE, EJA, M].

Then Badari must rearrange the letters within each part so that the list of parts is sorted in non-decreasing lexicographical order. If she can, Badari wins. Otherwise, Amir wins. Given the partition Amir made, can you help Badari win, or say that it is not possible?

Input

The first line contains the number of test cases T\mathbf{T}. T\mathbf{T} test cases follow. Each test case has two lines. The first line contains a single integer P\mathbf{P}, the number of parts Amir made. The second line contains P\mathbf{P} strings S_1,S_2,…,S_P\mathbf{S\_1}, \mathbf{S\_2}, \dots, \mathbf{S\_P}, representing the parts in order.

Output

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy is either POSSIBLE if Badari can win, or IMPOSSIBLE if she cannot. If she can win, output a second line containing t_1 t_2 … t_Pt\_1\, t\_2\, \dots\, t\_{\mathbf{P}}, where t_it\_i is a rearrangement of the letters of S_i\mathbf{S\_i}, and t_it\_i is lexicographically earlier than or equal to t_i+1t\_{i+1} for all ii. If there are multiple solutions, you may output any one of them.

Limits

1≤T≤1001 \le \mathbf{T} \le 100.

For all ii, each character of S_i\mathbf{S\_i} is an English uppercase letter from A through Z.

Hints

In Sample Case #1, Badari could also win in 5 other ways. Two of them are [CO, JED, MA] and [CO, EJD, MA].

In Sample Case #2, Badari can win simply by leaving all parts as Amir gave them. Other ways are also possible.

In Sample Case #3, Amir has guaranteed a win for himself by leaving Badari no winning option.

Examples1

  1. Example 1

    Input
    3
    3
    CO DEJ AM
    3
    CODE JA M
    2
    ABABABAB AAA
    
    Expected output
    Case #1: POSSIBLE
    CO DEJ MA
    Case #2: POSSIBLE
    CODE JA M
    Case #3: IMPOSSIBLE