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')

