/** Author : Aishwarya Mittal **/
/******** All Required Header Files ********/
#include<bits/stdc++.h>
using namespace std;
// SCAN
#define scd(t) scanf("%d",&t)
#define scd2(a,b) scanf("%d %d",&a, &b);
#define scd3(a,b,c) scanf("%d %d %d",&a, &b,&c);
#define sclld(t) scanf("%lld",&t)
// PRINT
#define prd(n) printf("%d",n)
#define prl(n) printf("%lld",n)
#define prdl(n) printf("%d\n",n)
#define prll(n) printf("%lld\n",n)
#define ps printf(" ")
#define pn printf("\n")
#define rep(i, j) for(int i=0;i<j;i++)
#define rup(i,a,b) for(int i=a;i<=b;i++)
#define rdn(i,a,b) for(int i=a;i>=b;i--)
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<vi> vvi;
typedef long long int ll;
#define pb push_back
#define mp make_pair
bool topoSort(int s, vi G[], stack<int>& stk, bool vis[], bool rec[]){
if(!vis[s]){
vis[s]=true;
rec[s]=true;
for(auto v: G[s]){
if(!vis[v] && topoSort(v,G,stk,vis,rec))
return true;
else if(rec[v])
return true;
}
stk.push(s);
}
rec[s]=false;
return false;
}
void solve(){
int r,c;
int size = 26;
vi G[26];
bool vis[26]={false};
bool present[26] = {false};
bool rec[26]={false};
stack<int> stk;
scd2(r,c);
string A[r];
rep(i,r)
cin>>A[i];
// mark if a char is present
rep(i,r){
rep(j,c){
present[A[i][j]-'A'] = true;
}
}
// form the graph using adjacency list for edges
rep(j,c){
rdn(i,r-2,0){
if(A[i+1][j]!=A[i][j])
G[A[i+1][j]-'A'].pb(A[i][j]-'A');
}
}
// remove duplicte edges from graph
rep(i,26){
if(G[i].size()>0){
std::sort(G[i].begin(), G[i].end());
auto ip = std::unique(G[i].begin(), G[i].end());
G[i].resize(std::distance(G[i].begin(), ip));
}
}
// flag to denote non stable wall i.e. cycle present in graph
// find order of blocks using topological sorting
bool flag = true;
rep(i,26){
if(!vis[i] && present[i]){
if(topoSort(i,G,stk,vis,rec)){
flag = false;
break;
}
}
}
if(flag){
while(!stk.empty()){
cout<<((char)(stk.top()+'A'));
stk.pop();
}
}
else
cout<<"-1";
cout<<"\n";
}
int main()
{
int t;
scd(t);
rup(tc,1,t)
{
printf("Case #%d: ",tc);
solve();
}
return 0;
}
LyoqIEF1dGhvciA6IEFpc2h3YXJ5YSBNaXR0YWwgKiovCiAKLyoqKioqKioqICAgQWxsIFJlcXVpcmVkIEhlYWRlciBGaWxlcyAqKioqKioqKi8KI2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCi8vIFNDQU4KI2RlZmluZSBzY2QodCkgc2NhbmYoIiVkIiwmdCkKI2RlZmluZSBzY2QyKGEsYikgc2NhbmYoIiVkICVkIiwmYSwgJmIpOwojZGVmaW5lIHNjZDMoYSxiLGMpIHNjYW5mKCIlZCAlZCAlZCIsJmEsICZiLCZjKTsKI2RlZmluZSBzY2xsZCh0KSBzY2FuZigiJWxsZCIsJnQpCgovLyBQUklOVAojZGVmaW5lIHByZChuKSBwcmludGYoIiVkIixuKQojZGVmaW5lIHBybChuKSBwcmludGYoIiVsbGQiLG4pCiNkZWZpbmUgcHJkbChuKSBwcmludGYoIiVkXG4iLG4pCiNkZWZpbmUgcHJsbChuKSBwcmludGYoIiVsbGRcbiIsbikKI2RlZmluZSBwcyBwcmludGYoIiAiKQojZGVmaW5lIHBuIHByaW50ZigiXG4iKQojZGVmaW5lIHJlcChpLCBqKSBmb3IoaW50IGk9MDtpPGo7aSsrKQojZGVmaW5lIHJ1cChpLGEsYikgZm9yKGludCBpPWE7aTw9YjtpKyspCiNkZWZpbmUgcmRuKGksYSxiKSBmb3IoaW50IGk9YTtpPj1iO2ktLSkKdHlwZWRlZiBwYWlyPGludCwgaW50PiBwaWk7CnR5cGVkZWYgdmVjdG9yPGludD4gdmk7CnR5cGVkZWYgdmVjdG9yPHN0cmluZz4gdnM7CnR5cGVkZWYgdmVjdG9yPHBpaT4gdmlpOwp0eXBlZGVmIHZlY3Rvcjx2aT4gdnZpOwp0eXBlZGVmIGxvbmcgbG9uZyBpbnQgbGw7CiNkZWZpbmUgcGIgcHVzaF9iYWNrCiNkZWZpbmUgbXAgbWFrZV9wYWlyCgoKYm9vbCB0b3BvU29ydChpbnQgcywgdmkgR1tdLCBzdGFjazxpbnQ+JiBzdGssIGJvb2wgdmlzW10sIGJvb2wgcmVjW10pewogICAgaWYoIXZpc1tzXSl7CiAgICAgICAgdmlzW3NdPXRydWU7CiAgICAgICAgcmVjW3NdPXRydWU7CiAgICAgICAgZm9yKGF1dG8gdjogR1tzXSl7CiAgICAgICAgICAgIGlmKCF2aXNbdl0gJiYgdG9wb1NvcnQodixHLHN0ayx2aXMscmVjKSkKICAgICAgICAgICAgICAgIHJldHVybiB0cnVlOwogICAgICAgICAgICBlbHNlIGlmKHJlY1t2XSkKICAgICAgICAgICAgICAgIHJldHVybiB0cnVlOwogICAgICAgIH0KICAgICAgICBzdGsucHVzaChzKTsKICAgIH0KICAgIHJlY1tzXT1mYWxzZTsKICAgIHJldHVybiBmYWxzZTsKfQp2b2lkIHNvbHZlKCl7CglpbnQgcixjOwoJaW50IHNpemUgPSAyNjsKICAgIHZpIEdbMjZdOwogICAgYm9vbCB2aXNbMjZdPXtmYWxzZX07CiAgICBib29sIHByZXNlbnRbMjZdID0ge2ZhbHNlfTsKICAgIGJvb2wgcmVjWzI2XT17ZmFsc2V9OwogICAgc3RhY2s8aW50PiBzdGs7CiAgICBzY2QyKHIsYyk7CiAgICBzdHJpbmcgQVtyXTsKICAgIHJlcChpLHIpCiAgICAgICAgY2luPj5BW2ldOyAgICAKICAgICAgIAogICAgLy8gbWFyayBpZiBhIGNoYXIgaXMgcHJlc2VudCAKICAgIHJlcChpLHIpewogICAgICAgIHJlcChqLGMpewogICAgICAgICAgICBwcmVzZW50W0FbaV1bal0tJ0EnXSA9IHRydWU7CiAgICAgICAgfQogICAgfQoKICAgIC8vIGZvcm0gdGhlIGdyYXBoIHVzaW5nIGFkamFjZW5jeSBsaXN0IGZvciBlZGdlcwogICAgcmVwKGosYyl7CiAgICAgICAgcmRuKGksci0yLDApewogICAgICAgICAgICBpZihBW2krMV1bal0hPUFbaV1bal0pCiAgICAgICAgICAgICAgICBHW0FbaSsxXVtqXS0nQSddLnBiKEFbaV1bal0tJ0EnKTsKICAgICAgICB9CiAgICB9CgoJLy8gcmVtb3ZlIGR1cGxpY3RlIGVkZ2VzIGZyb20gZ3JhcGgKICAgIHJlcChpLDI2KXsKICAgICAgICBpZihHW2ldLnNpemUoKT4wKXsKICAgICAgICAgICAgc3RkOjpzb3J0KEdbaV0uYmVnaW4oKSwgR1tpXS5lbmQoKSk7IAogICAgICAgICAgICBhdXRvIGlwID0gc3RkOjp1bmlxdWUoR1tpXS5iZWdpbigpLCBHW2ldLmVuZCgpKTsgCiAgICAgICAgICAgIEdbaV0ucmVzaXplKHN0ZDo6ZGlzdGFuY2UoR1tpXS5iZWdpbigpLCBpcCkpOyAKICAgICAgICB9CiAgICB9CiAgICAKICAgIC8vIGZsYWcgdG8gZGVub3RlIG5vbiBzdGFibGUgd2FsbCBpLmUuIGN5Y2xlIHByZXNlbnQgaW4gZ3JhcGgKICAgIC8vIGZpbmQgb3JkZXIgb2YgYmxvY2tzIHVzaW5nIHRvcG9sb2dpY2FsIHNvcnRpbmcKICAgIGJvb2wgZmxhZyA9IHRydWU7CiAgICByZXAoaSwyNil7CiAgICAgICAgaWYoIXZpc1tpXSAmJiBwcmVzZW50W2ldKXsKICAgICAgICAgICAgaWYodG9wb1NvcnQoaSxHLHN0ayx2aXMscmVjKSl7CiAgICAgICAgICAgICAgICBmbGFnID0gZmFsc2U7CiAgICAgICAgICAgICAgICBicmVhazsgCiAgICAgICAgICAgIH0KICAgICAgICB9ICAgICAKICAgIH0KCiAgICBpZihmbGFnKXsKICAgICAgICB3aGlsZSghc3RrLmVtcHR5KCkpewogICAgICAgIGNvdXQ8PCgoY2hhcikoc3RrLnRvcCgpKydBJykpOwogICAgICAgIHN0ay5wb3AoKTsKICAgICAgICB9CiAgICB9CiAgICBlbHNlCiAgICAgICAgY291dDw8Ii0xIjsKCiAgICBjb3V0PDwiXG4iOwp9CiAKaW50IG1haW4oKQp7CglpbnQgdDsKCXNjZCh0KTsKCXJ1cCh0YywxLHQpCgl7CgkJcHJpbnRmKCJDYXNlICMlZDogIix0Yyk7IAogICAgICAgIHNvbHZlKCk7Cgl9CiAgICByZXR1cm4gMDsKfSA=