password
 
% Code Jam to I/O 2016 for Women, Problem D. Password Security
% https://code.google.com/codejam/contest/8274486/dashboard#s=p3
% in Picat, by N.F. Zhou, 04/23/2016

% to use: picat password < input_file > output_file

import util, sat.

main =>
    T = read_line().to_int(),
    foreach (TC in 1..T)
        _ = read_line().to_int(),
        L = read_line().split(),
        L1 = [S : S in L, len(S) == len(S.sort_remove_dups())],
        printf("Case #%w: ", TC),
        not not do_case(TC,L)                        
    end.

do_case(TC,L),member([_],L) =>
    println("IMPOSSIBLE").
do_case(TC,L) ?=>
    Vs = new_array(26),
    Vs :: 1..26,
    all_distinct(Vs),
    foreach(S in L)
        Tuple = {ord(C)-ord('A')+1 : C in S},
        foreach(I in 1..26-len(S)+1)
            VTuple = {Vs[I+J] : J in 0..len(S)-1},
            table_notin(VTuple,[Tuple])
        end
    end,
    solve(Vs),
    foreach (I in 1..26)
        printf("%c",chr(Vs[I]+ord('A')-1))
    end,
    nl.
do_case(TC,_L) =>
    println("IMPOSSIBLE").