from bisect import bisect_left def answer(S): """ at most 100 of 100-character strings """ result = ["".join(sorted(S[0]))] for s in S[1:]: result.append(earliestPermutationAfter(s, result[-1])) if not result[-1]: return False, False return True, result def earliestPermutationAfter(s, target): if len(target) > len(s): target = target[:len(s)-1] + chr(ord(target[len(s)])+1) s = sorted(list(s)) result = [] for c in target: i = bisect_left(s, c) try: result.append(s[i]) s = s[:i]+s[i+1:] if result[-1] != c: return "".join(result+s) except: return False return "".join(result+s) T = int(input()) # read a line with a single integer for i in range(1, T + 1): P = int(input()) S = input().split(" ") poss, ans = answer(S) print(f'Case #{i}: {"POSSIBLE" if poss else "IMPOSSIBLE"}') if poss: print(" ".join(ans))