#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
#define ll long long int
#define ull unsigned long long int
#define ld long double
#define endl '\n'
#define loop(a,b,c) for(ll i=a;i<=b;i+=c)
#define intarr(arr,n) ll arr[n];for(ll i=0;i<n;i++)cin>>arr[i]
#define inparr(arr,n) for(ll i=0;i<n;i++)cin>>arr[i]
#define inpvec(vec,n) for(ll i=0;i<n;i++){ll var;cin>>var;vec.push_back(var);}
#define pb push_back
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define mod 1000000007
#define newline cout<<endl
#define ump unordered_map<ll,ll>
#define vec vector<ll>
#define mkp make_pair
#define disp(var1,var2) cout<<var1<<" "<<var2<<endl;
#define all(v) v.begin(),v.end()
#define cout(var) cout<<var<<endl
#define mod2 998244353
#define fbo find_by_order /// find kth smallest by passing k-1
#define ook order_of_key //// find no of smaller items less than k
#define teensort(a,r,g,b) a[0]=r;a[1]=g;a[2]=b;sort(a,a+3);
#define ss second
#define ff first
#define displayarray(a,n) for(ll i=0;i<n;i++)cout<<a[i]<<" "; cout<<endl;
#define pi pair<ll,ll>
#define meramax 1e18
#define meramin -1e18
#define percentile 1000000007
#define PI 3.14159265358979323846
vector<string> v;
void bfs(auto g)
{
string str="";
string ans="";
auto *visited = new bool[28];
for(ll i=0;i<28;i++)
visited[i] = false;
list<ll> queue;
visited[27] = true;
queue.push_back(27);
str+="27";
list<ll>::iterator i;
while(!queue.empty())
{
auto s = queue.front();
if(s==27)
{
str+="27";
}
else
{
char ch='A'+s;
str+=ch;
}
queue.pop_front();
for (i = g[s].begin(); i != g[s].end(); ++i)
{
if (!visited[*i])
{
visited[*i] = true;
queue.push_back(*i);
}
}
}
for(ll i=0;i<str.length();)
{
if(i+1<str.length() && str[i]=='2' && str[i+1]=='7')
{
i+=2;
continue;
}
else
{
ans+=str[i];
i++;
}
}
v.pb(ans);
//cout(ans);
return;
}
void AcDegaYe()
{
ll r,c;
cin>>r>>c;
char a[r+1][c+1];
unordered_map<char,ll> h;
for(ll i=1;i<=r;i++)
{
for(ll j=1;j<=c;j++)
{
cin>>a[i][j];
h[a[i][j]]++;
}
}
ll neeche[26];
for(ll i=0;i<26;i++)neeche[i]=27;
for(ll i=1;i<=r-1;i++)
{
for(ll j=1;j<=c;j++)
{
if(a[i][j]!=a[i+1][j])
{
neeche[a[i][j]-'A']=a[i+1][j]-'A';
}
}
}
unordered_map<ll,list<ll>> g;
ll f=0;
for(ll i=0;i<26;i++)
{
char ch='A'+i;
if(h.count(ch)==1)
{
if(neeche[neeche[i]]==i)
{
f=1;
break;
}
g[i].pb(neeche[i]);
g[neeche[i]].pb(i);
}
}
if(f)
{
v.pb("-1");
}
else
{
bfs(g);
}
}
int main()
{
fastio
ll t;
cin>>t;
//ll t=1;
while(t--)
{
AcDegaYe();
}
for(ll i=1;i<=v.size();i++)
{
cout<<"Case #"<<i<<": "<<v[i-1]<<endl;
}
cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNpbmNsdWRlIDxleHQvcGJfZHMvYXNzb2NfY29udGFpbmVyLmhwcD4gCiNpbmNsdWRlIDxleHQvcGJfZHMvdHJlZV9wb2xpY3kuaHBwPiAKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKdXNpbmcgbmFtZXNwYWNlIF9fZ251X3BiZHM7CnR5cGVkZWYgdHJlZTxpbnQsIG51bGxfdHlwZSwgbGVzczxpbnQ+LCByYl90cmVlX3RhZywgdHJlZV9vcmRlcl9zdGF0aXN0aWNzX25vZGVfdXBkYXRlPiBwYmRzOwojZGVmaW5lIGxsIGxvbmcgbG9uZyBpbnQKI2RlZmluZSB1bGwgdW5zaWduZWQgbG9uZyBsb25nIGludAojZGVmaW5lIGxkIGxvbmcgZG91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ICAgICAgICAgIGlmICghdmlzaXRlZFsqaV0pIAogICAgICAgICAgICB7IAogICAgICAgICAgICAgICAgdmlzaXRlZFsqaV0gPSB0cnVlOyAKICAgICAgICAgICAgICAgIHF1ZXVlLnB1c2hfYmFjaygqaSk7IAogICAgICAgICAgICB9IAogICAgICAgIH0gCiAgICB9IAogICAgZm9yKGxsIGk9MDtpPHN0ci5sZW5ndGgoKTspCiAgICB7CiAgICAgICAgaWYoaSsxPHN0ci5sZW5ndGgoKSAmJiBzdHJbaV09PScyJyAmJiBzdHJbaSsxXT09JzcnKQogICAgICAgIHsKICAgICAgICAgICAgaSs9MjsKICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgfQogICAgICAgIGVsc2UKICAgICAgICB7CiAgICAgICAgICAgIGFucys9c3RyW2ldOwogICAgICAgICAgICBpKys7CiAgICAgICAgfQogICAgfQogICAgdi5wYihhbnMpOwogICAgLy9jb3V0KGFucyk7CiAgICByZXR1cm47Cn0Kdm9pZCBBY0RlZ2FZZSgpCnsKICAgIGxsIHIsYzsKICAgIGNpbj4+cj4+YzsKICAgIGNoYXIgYVtyKzFdW2MrMV07CiAgICB1bm9yZGVyZWRfbWFwPGNoYXIsbGw+IGg7CiAgICBmb3IobGwgaT0xO2k8PXI7aSsrKQogICAgewogICAgICAgIGZvcihsbCBqPTE7ajw9YztqKyspCiAgICAgICAgewogICAgICAgICAgICBjaW4+PmFbaV1bal07CiAgICAgICAgICAgIGhbYVtpXVtqXV0rKzsKICAgICAgICB9CiAgICB9CiAgICBsbCBuZWVjaGVbMjZdOwogICAgZm9yKGxsIGk9MDtpPDI2O2krKyluZWVjaGVbaV09Mjc7CiAgICBmb3IobGwgaT0xO2k8PXItMTtpKyspCiAgICB7CiAgICAgICAgZm9yKGxsIGo9MTtqPD1jO2orKykKICAgICAgICB7CiAgICAgICAgICAgIGlmKGFbaV1bal0hPWFbaSsxXVtqXSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgbmVlY2hlW2FbaV1bal0tJ0EnXT1hW2krMV1bal0tJ0EnOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgdW5vcmRlcmVkX21hcDxsbCxsaXN0PGxsPj4gZzsKICAgIGxsIGY9MDsKICAgIGZvcihsbCBpPTA7aTwyNjtpKyspCiAgICB7CiAgICAgICAgY2hhciBjaD0nQScraTsKICAgICAgICBpZihoLmNvdW50KGNoKT09MSkKICAgICAgICB7CiAgICAgICAgICAgIGlmKG5lZWNoZVtuZWVjaGVbaV1dPT1pKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBmPTE7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgICAgICBnW2ldLnBiKG5lZWNoZVtpXSk7CiAgICAgICAgICAgIGdbbmVlY2hlW2ldXS5wYihpKTsKICAgICAgICB9CiAgICB9CiAgICBpZihmKQogICAgewogICAgICAgIHYucGIoIi0xIik7CiAgICB9CiAgICBlbHNlCiAgICB7CiAgICAgICAgYmZzKGcpOwogICAgfQp9CmludCBtYWluKCkKeyAgIAogICAgCiAgICBmYXN0aW8KICAgIGxsIHQ7CiAgICBjaW4+PnQ7CiAgICAvL2xsIHQ9MTsKICAgIHdoaWxlKHQtLSkKICAgIHsKICAgICAgICBBY0RlZ2FZZSgpOwogICAgfQogICAgZm9yKGxsIGk9MTtpPD12LnNpemUoKTtpKyspCiAgICB7CiAgICAgICAgY291dDw8IkNhc2UgIyI8PGk8PCI6ICI8PHZbaS0xXTw8ZW5kbDsKICAgIH0KICAgIGNlcnIgPDwgIlxuVGltZSBlbGFwc2VkOiAiIDw8IDEwMDAgKiBjbG9jaygpIC8gQ0xPQ0tTX1BFUl9TRUMgPDwgIm1zXG4iOwogICAgcmV0dXJuIDA7Cn0=