import sys
from collections import defaultdict
#cycle checking function
def cycle_exists(G):
color = { u : "white" for u in G }
found_cycle = [False]
for u in G:
if color[u] == "white":
dfs_visit(G, u, color, found_cycle)
if found_cycle[0]:
break
return found_cycle[0]
def dfs_visit(G, u, color, found_cycle):
if found_cycle[0]:
return
color[u] = "gray"
for v in G[u]:
if color[v] == "gray":
found_cycle[0] = True
return
if color[v] == "white":
dfs_visit(G, v, color, found_cycle)
color[u] = "black"
t=int(input())
for ii in range(t):
mat=[]
char_dict=dict()
r,c=map(int,input().split())
for j in range(r):
col=[i for i in input()]
mat.append(col)
graph=defaultdict(set)
#creates the graph and a dictionary to store all the unique characters
for j in range(r-2,-1,-1):
for k in range(c):
char_dict[mat[j][k]]=0
char_dict[mat[j+1][k]]=0
if mat[j][k]!=mat[j+1][k]:
graph[mat[j][k]].add(mat[j+1][k])
if mat[j+1][k] not in graph:
graph[mat[j+1][k]]=set()
#if r==1, simply print all the unique elements in the row
if r==1:
col=''.join(map(str,set(col)))
sys.stdout.write("Case #"+str(ii+1)+':'+' '+col+'\n')
#if cycle exists, print -1
elif (cycle_exists(graph)):
sys.stdout.write("Case #"+str(ii+1)+':'+' '+str(-1)+'\n')
#topological sort
else:
OBSERVE = 0
CHECK = 1
topsort=[]
for i in char_dict:
if char_dict[i]==0:
stack = [(OBSERVE, i, 0)]
while len(stack):
state, vertex, parent = stack.pop()
char_dict[vertex]=1
if state == OBSERVE:
stack.append((CHECK, vertex, parent))
for child in graph[vertex]:
if char_dict[child]!=1:
stack.append((OBSERVE, child, vertex))
else:
topsort.append(vertex)
topsort=''.join(map(str,topsort))
sys.stdout.write("Case #"+str(ii+1)+':'+' '+topsort+'\n')
aW1wb3J0IHN5cwpmcm9tIGNvbGxlY3Rpb25zIGltcG9ydCBkZWZhdWx0ZGljdAojY3ljbGUgY2hlY2tpbmcgZnVuY3Rpb24KZGVmIGN5Y2xlX2V4aXN0cyhHKTogICAgICAgICAgICAgICAgICAgICAKICAgIGNvbG9yID0geyB1IDogIndoaXRlIiBmb3IgdSBpbiBHICB9ICAKICAgIGZvdW5kX2N5Y2xlID0gW0ZhbHNlXSAgICAgICAgICAgICAgICAKICAgIGZvciB1IGluIEc6ICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICBpZiBjb2xvclt1XSA9PSAid2hpdGUiOgogICAgICAgICAgICBkZnNfdmlzaXQoRywgdSwgY29sb3IsIGZvdW5kX2N5Y2xlKQogICAgICAgIGlmIGZvdW5kX2N5Y2xlWzBdOgogICAgICAgICAgICBicmVhawogICAgcmV0dXJuIGZvdW5kX2N5Y2xlWzBdCmRlZiBkZnNfdmlzaXQoRywgdSwgY29sb3IsIGZvdW5kX2N5Y2xlKToKICAgIGlmIGZvdW5kX2N5Y2xlWzBdOiAgICAgICAgICAgICAgICAgICAgICAgICAgCiAgICAgICAgcmV0dXJuCiAgICBjb2xvclt1XSA9ICJncmF5IiAgICAgICAgICAgICAgICAgICAgICAgICAgIAogICAgZm9yIHYgaW4gR1t1XTogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKICAgICAgICBpZiBjb2xvclt2XSA9PSAiZ3JheSI6ICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICBmb3VuZF9jeWNsZVswXSA9IFRydWUgICAgICAgCiAgICAgICAgICAgIHJldHVybgogICAgICAgIGlmIGNvbG9yW3ZdID09ICJ3aGl0ZSI6ICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICBkZnNfdmlzaXQoRywgdiwgY29sb3IsIGZvdW5kX2N5Y2xlKQogICAgY29sb3JbdV0gPSAiYmxhY2siICAKICAgIAp0PWludChpbnB1dCgpKQpmb3IgaWkgaW4gcmFuZ2UodCk6CiAgICBtYXQ9W10KICAgIGNoYXJfZGljdD1kaWN0KCkKICAgIHIsYz1tYXAoaW50LGlucHV0KCkuc3BsaXQoKSkKICAgIGZvciBqIGluIHJhbmdlKHIpOgogICAgICAgIGNvbD1baSBmb3IgaSBpbiBpbnB1dCgpXQogICAgICAgIG1hdC5hcHBlbmQoY29sKQogICAgZ3JhcGg9ZGVmYXVsdGRpY3Qoc2V0KQogICAgI2NyZWF0ZXMgdGhlIGdyYXBoIGFuZCBhIGRpY3Rpb25hcnkgdG8gc3RvcmUgYWxsIHRoZSB1bmlxdWUgY2hhcmFjdGVycwogICAgZm9yIGogaW4gcmFuZ2Uoci0yLC0xLC0xKToKICAgICAgICBmb3IgayBpbiByYW5nZShjKToKICAgICAgICAgICAgY2hhcl9kaWN0W21hdFtqXVtrXV09MAogICAgICAgICAgICBjaGFyX2RpY3RbbWF0W2orMV1ba11dPTAKICAgICAgICAgICAgaWYgbWF0W2pdW2tdIT1tYXRbaisxXVtrXToKICAgICAgICAgICAgICAgIGdyYXBoW21hdFtqXVtrXV0uYWRkKG1hdFtqKzFdW2tdKQogICAgICAgICAgICAgICAgaWYgbWF0W2orMV1ba10gbm90IGluIGdyYXBoOgogICAgICAgICAgICAgICAgICAgIGdyYXBoW21hdFtqKzFdW2tdXT1zZXQoKQoJI2lmIHI9PTEsIHNpbXBseSBwcmludCBhbGwgdGhlIHVuaXF1ZSBlbGVtZW50cyBpbiB0aGUgcm93CiAgICBpZiByPT0xOgogICAgICAgIGNvbD0nJy5qb2luKG1hcChzdHIsc2V0KGNvbCkpKQogICAgICAgIHN5cy5zdGRvdXQud3JpdGUoIkNhc2UgIyIrc3RyKGlpKzEpKyc6JysnICcrY29sKydcbicpCiAgICAjaWYgY3ljbGUgZXhpc3RzLCBwcmludCAtMQogICAgZWxpZiAoY3ljbGVfZXhpc3RzKGdyYXBoKSk6CiAgICAgICAgc3lzLnN0ZG91dC53cml0ZSgiQ2FzZSAjIitzdHIoaWkrMSkrJzonKycgJytzdHIoLTEpKydcbicpCiAgICAjdG9wb2xvZ2ljYWwgc29ydAogICAgZWxzZToKICAgICAgICBPQlNFUlZFID0gMAogICAgICAgIENIRUNLID0gMQogICAgICAgIHRvcHNvcnQ9W10KICAgICAgICBmb3IgaSBpbiBjaGFyX2RpY3Q6CiAgICAgICAgICAgIGlmIGNoYXJfZGljdFtpXT09MDoKICAgICAgICAgICAgICAgIHN0YWNrID0gWyhPQlNFUlZFLCBpLCAwKV0KICAgICAgICAgICAgICAgIHdoaWxlIGxlbihzdGFjayk6CiAgICAgICAgICAgICAgICAgICAgc3RhdGUsIHZlcnRleCwgcGFyZW50ID0gc3RhY2sucG9wKCkKICAgICAgICAgICAgICAgICAgICBjaGFyX2RpY3RbdmVydGV4XT0xCiAgICAgICAgICAgICAgICAgICAgaWYgc3RhdGUgPT0gT0JTRVJWRToKICAgICAgICAgICAgICAgICAgICAgICAgc3RhY2suYXBwZW5kKChDSEVDSywgdmVydGV4LCBwYXJlbnQpKQogICAgICAgICAgICAgICAgICAgICAgICBmb3IgY2hpbGQgaW4gZ3JhcGhbdmVydGV4XToKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIGNoYXJfZGljdFtjaGlsZF0hPTE6CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgc3RhY2suYXBwZW5kKChPQlNFUlZFLCBjaGlsZCwgdmVydGV4KSkKICAgICAgICAgICAgICAgICAgICBlbHNlOgogICAgICAgICAgICAgICAgICAgICAgICAgICAgdG9wc29ydC5hcHBlbmQodmVydGV4KQogICAgICAgIHRvcHNvcnQ9Jycuam9pbihtYXAoc3RyLHRvcHNvcnQpKQogICAgICAgIHN5cy5zdGRvdXQud3JpdGUoIkNhc2UgIyIrc3RyKGlpKzEpKyc6JysnICcrdG9wc29ydCsnXG4nKQoK