#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define loop(i,n) for(ll i=0;i<(n);i++)
#define FOR(i,a,b) for(ll i=(a);i<=(b);i++)
#define FORD(i,a,b) for(ll i=(a);i>=(b);i--)
#define cases ll T=0;cin>>T;while(T--)
#define ff first
#define ss second
#define all(v) v.begin(),v.end()
#define END "\n"
#define pb push_back
#define go(c,itr) for(auto itr=(c).begin(); itr!=(c).end(); itr++)
#define back(c,itr) for(auto itr=(c).rbegin(); itr!=(c).rend(); itr++)
#define inf 9e18
#define MOD 1000000007
#define MODU 998244353
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define MAXN 200001
const string alpha = "abcdefghijklmnopqrstuvwxyz";
vector<ll> adj[26];
vector<ll> topsort, indegree(26);
unordered_map<ll, ll> flag;
void kahn(ll n)
{
priority_queue<ll> q;
ll ctr = 0;
FOR(i, 0, n - 1)
{
if (indegree[i] == 0 && flag[i] == 1)
q.push(i);
if (flag[i] == 1)
++ctr;
}
while (!q.empty())
{
ll node = q.top();
q.pop();
topsort.pb(node);
for (auto child : adj[node])
{
--indegree[child];
if (indegree[child] == 0)
q.push(child);
}
}
if (topsort.size() != ctr)
cout << "-1";
else
for (auto itr : topsort)
cout << char(itr + 65);
cout << END;
}
signed main() {
// #ifndef ONLINE_JUDGE
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
// #endif
fast
ll t;
cin >> t;
FOR(x, 1, t)
{
ll r, c;
cin >> r >> c;
vector<vector<char>> a(r, vector<char>(c));
loop(i, r)
{
loop(j, c)
{
cin >> a[i][j];
}
}
if (r == 1)
{
cout << "Case #" << x << ": ";
set<char> ch;
loop(i, r)
loop(j, c)
ch.insert(a[i][j]);
go(ch, itr) cout << *itr;
cout << END;
}
else
{
unordered_map<ll, ll> fl[26];
loop(i, 26)
{
loop(j, c)
{
loop(k, r - 1)
{
if (a[k][j] == char(i + 65))
{
if (a[k + 1][j] != char(i + 65) && fl[(ll(a[k + 1][j]) - 65)][i] == 0)
{
adj[(ll(a[k + 1][j]) - 65)].pb(i);
++indegree[i];
fl[(ll(a[k + 1][j]) - 65)][i] = 1;
}
}
flag[ll(a[k][j]) - 65] = flag[ll(a[k + 1][j]) - 65] = 1;
}
}
}
// loop(i, 26)
// {
// if (flag[i])
// {
// cout << char(i + 65) << " : ";
// go(adj[i], itr)
// {
// cout << char(*itr + 65) << " ";
// }
// cout << END;
// }
// }
cout << "Case #" << x << ": "; kahn(26);
loop(i, 26)
{
indegree[i] = 0;
adj[i].clear();
fl[i].clear();
}
topsort.clear();
flag.clear();
}
}
return 0;
}
I3ByYWdtYSBHQ0Mgb3B0aW1pemUoMikKI3ByYWdtYSBHQ0Mgb3B0aW1pemUoMykKI3ByYWdtYSBHQ0Mgb3B0aW1pemUoIk9mYXN0LG5vLXN0YWNrLXByb3RlY3Rvcix1bnJvbGwtbG9vcHMsZmFzdC1tYXRoLE8zIikKI3ByYWdtYSBHQ0MgdGFyZ2V0KCJhdngsYXZ4MixmbWEiKQojaW5jbHVkZTxiaXRzL3N0ZGMrKy5oPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKdHlwZWRlZiB1bnNpZ25lZCBsb25nIGxvbmcgdWxsOwojZGVmaW5lIERFQlVHKHgpIGNvdXQgPDwgJz4nIDw8ICN4IDw8ICc6JyA8PCB4IDw8IGVuZGw7CiNkZWZpbmUgbG9vcChpLG4pIGZvcihsbCBpPTA7aTwobik7aSsrKQojZGVmaW5lIEZPUihpLGEsYikgZm9yKGxsIGk9KGEpO2k8PShiKTtpKyspCiNkZWZpbmUgRk9SRChpLGEsYikgZm9yKGxsIGk9KGEpO2k+PShiKTtpLS0pCiNkZW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