import sys T = int(sys.stdin.readline().strip()) for t in range (0, T): R, C = list(map(int, sys.stdin.readline().strip().split())) G = [] s = [-1 for i in range (0, 26)] e = [-1 for i in range (0, 26)] for r in range (R): x = sys.stdin.readline().strip() G.append(x) for c in range (C): if ord('a') <= ord(x[c]) <= ord("z"): s[ord(x[c]) - ord("a")] = (r, c) if ord('A') <= ord(x[c]) <= ord("Z"): e[ord(x[c]) - ord("A")] = (r, c) ans = [] for i in range (0, 26): if s[i] != -1: todo = set() visited = set() todo.add(s[i]) visited.add(s[i]) while todo: r, c = todo.pop() if ord('A') <= ord(G[r][c]) <= ord('Z'): ans.append(chr(ord('a') + i) + G[r][c]) if (r + 1, c) not in visited: rn = r - 1 while rn >= 0 and G[rn][c] not in "*#": rn -= 1 if (r - 1, c) not in todo: rs = r + 1 while rs < R and G[rs][c] not in "*#": rs += 1 if (r + 1, c) not in visited and (rn == -1 or G[rn][c] == "#"): for rx in range (r + 1, rs): if (rx, c) not in visited: visited.add((rx, c)) todo.add((rx, c)) if (r - 1, c) not in visited and (rs == R or G[rs][c] == "#"): for rx in range (rn + 1, r): if (rx, c) not in visited: visited.add((rx, c)) todo.add((rx, c)) if (r, c + 1) not in visited: cw = c - 1 while cw >= 0 and G[r][cw] not in "*#": cw -= 1 if (r, c - 1) not in visited: ce = c + 1 while ce < C and G[r][ce] not in "*#": ce += 1 if (r, c + 1) not in visited and (cw == -1 or G[r][cw] == "#"): for cx in range (c + 1, ce): if (r, cx) not in visited: visited.add((r, cx)) todo.add((r, cx)) if (r, c - 1) not in visited and (ce == C or G[r][ce] == "#"): for cx in range (cw + 1, c): if (r, cx) not in visited: visited.add((r, cx)) todo.add((r, cx)) if len(ans) == 0: ans = ["NONE"] print("Case #"+str(t+1)+":", *ans)