#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ini(arr, val) memset(arr, (val), sizeof(arr))
#define loop(i,n) for(ll i=0; i<n; i++)
#define loop1(i,n) for(ll i=1; i<=n; i++)
#define all(a) (a).begin(),(a).end()
#define exist(s,e) (s.find(e)!=s.end())
#define dbg(x) cout << #x << " = " << x << endl
#define pt(x) cout<<x<<"\n"
#define pts(x) cout<<x<<" "
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define inf (int)1e9
#define infll 1e18
#define eps 1e-9
#define PI 3.1415926535897932384626433832795
#define mod 1000000007
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define test int t; cin>>t; while(t--)
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef map<int,int> mii;
typedef set<int> si;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<pll> vpll;
typedef vector<vl> vvl;
typedef map<ll,ll> mll;
typedef set<ll> sl;
int dirx[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int diry[] = { -1, 0, 1, -1, 1, -1, 0, 1 };
//===========================DEBUG======================//
#define XOX 1
vector<string> vec_splitter(string s) {
s += ',';
vector<string> res;
while(!s.empty()) {
res.push_back(s.substr(0, s.find(',')));
s = s.substr(s.find(',') + 1);
}
return res;
}
void debug_out(
vector<string> __attribute__ ((unused)) args,
__attribute__ ((unused)) int idx,
__attribute__ ((unused)) int LINE_NUM) { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, int LINE_NUM, Head H, Tail... T) {
if(idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") ";
stringstream ss; ss << H;
cerr << args[idx] << " = " << ss.str();
debug_out(args, idx + 1, LINE_NUM, T...);
}
#ifdef XOX
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
#else
#define debug(...) 42
#endif
//================================================================//
//////////////////////////////////////////////////////////////////////////////////////////
// main starts
//////////////////////////////////////////////////////////////////////////////////////////
int const lmt=1e5+5;
string s[100];
int adj[26][26];
stack<int> st;
int vis[26];
int pre[26];
bool ok;
void dfs(int p){
// debug(char(p+'A'));
// if(!ok)return;
vis[p]=1;
for(int i=0; i<26; i++){
if(!pre[i] || !adj[p][i])continue;
if(vis[i]==0)dfs(i);
else if(vis[i]==1)ok=0;
}
vis[p]=2;
st.push(p);
}
int main(){
// #ifndef ONLINE_JUDGE
// freopen("../input.txt", "r", stdin);
// freopen("../output.txt", "w", stdout);
// #endif
fast
int T=1;
test{
ll r,c;
cin>>r>>c;
loop(i,r){
cin>>s[i];
}
ini(adj,0);
ini(vis,0);
ok=1;
ini(pre,0);
while(st.size()){st.pop();}
for(int j=0; j<c; j++){
for(int i=0; i<r-1; i++){
int a = s[i+1][j]-'A';
int b = s[i][j]-'A';
pre[a]=1;
pre[b]=1;
if(a==b)continue;
adj[a][b]=1;
}
}
for(int i=0; i<26; i++) if(pre[i] && !vis[i]){
dfs(i);
// debug(char(i+'A'));
}
cout<< "Case #" << T++ << ": ";
if(!ok)pts("-1");
else while(!st.empty()){
cout<<char(st.top() + 'A');
st.pop();
}
cout<<"\n";
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojZGVmaW5lIGxsIGxvbmcgbG9uZwojZGVmaW5lIGluaShhcnIsIHZhbCkgbWVtc2V0KGFyciwgKHZhbCksIHNpemVvZihhcnIpKQojZGVmaW5lIGxvb3AoaSxuKSAgZm9yKGxsIGk9MDsgaTxuOyBpKyspCiNkZWZpbmUgbG9vcDEoaSxuKSAgZm9yKGxsIGk9MTsgaTw9bjsgaSsrKQoKI2RlZmluZSBhbGwoYSkgICAgICAoYSkuYmVnaW4oKSwoYSkuZW5kKCkKI2RlZmluZSBleGlzdChzLGUpICAocy5maW5kKGUpIT1zLmVuZCgpKQojZGVmaW5lIGRiZyh4KSA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5wdXQudHh0IiwgInIiLCBzdGRpbik7CiAgICAvLyBmcmVvcGVuKCIuLi9vdXRwdXQudHh0IiwgInciLCBzdGRvdXQpOwogICAgLy8gI2VuZGlmCiAgICBmYXN0CiAgICBpbnQgVD0xOwogICAgdGVzdHsKCiAgICAgICAgbGwgcixjOwogICAgICAgIGNpbj4+cj4+YzsKICAgICAgICAKICAgICAgICBsb29wKGkscil7CiAgICAgICAgICAgIGNpbj4+c1tpXTsKICAgICAgICB9CiAgICAgICAgaW5pKGFkaiwwKTsKICAgICAgICBpbmkodmlzLDApOwogICAgICAgIG9rPTE7CiAgICAgICAgaW5pKHByZSwwKTsKICAgICAgICB3aGlsZShzdC5zaXplKCkpe3N0LnBvcCgpO30KCiAgICAgICAgZm9yKGludCBqPTA7IGo8YzsgaisrKXsKICAgICAgICAgICAgZm9yKGludCBpPTA7IGk8ci0xOyBpKyspewogICAgICAgICAgICAgICAgaW50IGEgPSBzW2krMV1bal0tJ0EnOwogICAgICAgICAgICAgICAgaW50IGIgPSBzW2ldW2pdLSdBJzsKICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgcHJlW2FdPTE7CiAgICAgICAgICAgICAgICBwcmVbYl09MTsKICAgICAgICAgICAgICAgIGlmKGE9PWIpY29udGludWU7CiAgICAgICAgICAgICAgICBhZGpbYV1bYl09MTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICBmb3IoaW50IGk9MDsgaTwyNjsgaSsrKSBpZihwcmVbaV0gJiYgIXZpc1tpXSl7CiAgICAgICAgICAgIGRmcyhpKTsKICAgICAgICAgICAgLy8gZGVidWcoY2hhcihpKydBJykpOwogICAgICAgIH0KICAgICAgICBjb3V0PDwgIkNhc2UgIyIgPDwgVCsrIDw8ICI6ICI7CiAgICAgICAgCiAgICAgICAgaWYoIW9rKXB0cygiLTEiKTsKICAgICAgICBlbHNlIHdoaWxlKCFzdC5lbXB0eSgpKXsKICAgICAgICAgICAgY291dDw8Y2hhcihzdC50b3AoKSArICdBJyk7CiAgICAgICAgICAgIHN0LnBvcCgpOwogICAgICAgIH0KCiAgICAgICAgY291dDw8IlxuIjsKICAgIH0KfQ==
Main.java:1: error: illegal character: '#'
#include<bits/stdc++.h>
^
Main.java:1: error: class, interface, or enum expected
#include<bits/stdc++.h>
^
Main.java:5: error: illegal character: '#'
#define ll long long
^
Main.java:5: error: class, interface, or enum expected
#define ll long long
^
Main.java:6: error: illegal character: '#'
#define ini(arr, val) memset(arr, (val), sizeof(arr))
^
Main.java:7: error: illegal character: '#'
#define loop(i,n) for(ll i=0; i<n; i++)
^
Main.java:7: error: class, interface, or enum expected
#define loop(i,n) for(ll i=0; i<n; i++)
^
Main.java:7: error: class, interface, or enum expected
#define loop(i,n) for(ll i=0; i<n; i++)
^
Main.java:8: error: illegal character: '#'
#define loop1(i,n) for(ll i=1; i<=n; i++)
^
Main.java:8: error: class, interface, or enum expected
#define loop1(i,n) for(ll i=1; i<=n; i++)
^
Main.java:8: error: class, interface, or enum expected
#define loop1(i,n) for(ll i=1; i<=n; i++)
^
Main.java:10: error: illegal character: '#'
#define all(a) (a).begin(),(a).end()
^
Main.java:11: error: illegal character: '#'
#define exist(s,e) (s.find(e)!=s.end())
^
Main.java:12: error: illegal character: '#'
#define dbg(x) cout << #x << " = " << x << endl
^
Main.java:12: error: illegal character: '#'
#define dbg(x) cout << #x << " = " << x << endl
^
Main.java:13: error: illegal character: '#'
#define pt(x) cout<<x<<"\n"
^
Main.java:14: error: illegal character: '#'
#define pts(x) cout<<x<<" "
^
Main.java:16: error: illegal character: '#'
#define mp make_pair
^
Main.java:17: error: illegal character: '#'
#define pb push_back
^
Main.java:18: error: illegal character: '#'
#define F first
^
Main.java:19: error: illegal character: '#'
#define S second
^
Main.java:22: error: illegal character: '#'
#define inf (int)1e9
^
Main.java:23: error: illegal character: '#'
#define infll 1e18
^
Main.java:24: error: illegal character: '#'
#define eps 1e-9
^
Main.java:25: error: illegal character: '#'
#define PI 3.1415926535897932384626433832795
^
Main.java:26: error: illegal character: '#'
#define mod 1000000007
^
Main.java:29: error: illegal character: '#'
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
^
Main.java:29: error: class, interface, or enum expected
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
^
Main.java:29: error: class, interface, or enum expected
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
^
Main.java:30: error: illegal character: '#'
#define test int t; cin>>t; while(t--)
^
Main.java:30: error: class, interface, or enum expected
#define test int t; cin>>t; while(t--)
^
Main.java:30: error: class, interface, or enum expected
#define test int t; cin>>t; while(t--)
^
Main.java:30: error: class, interface, or enum expected
#define test int t; cin>>t; while(t--)
^
Main.java:33: error: class, interface, or enum expected
typedef vector<int> vi;
^
Main.java:34: error: class, interface, or enum expected
typedef vector<string> vs;
^
Main.java:35: error: class, interface, or enum expected
typedef vector<pii> vpii;
^
Main.java:36: error: class, interface, or enum expected
typedef vector<vi> vvi;
^
Main.java:37: error: class, interface, or enum expected
typedef map<int,int> mii;
^
Main.java:38: error: class, interface, or enum expected
typedef set<int> si;
^
Main.java:40: error: class, interface, or enum expected
typedef pair<ll, ll> pll;
^
Main.java:41: error: class, interface, or enum expected
typedef vector<ll> vl;
^
Main.java:42: error: class, interface, or enum expected
typedef vector<string> vs;
^
Main.java:43: error: class, interface, or enum expected
typedef vector<pll> vpll;
^
Main.java:44: error: class, interface, or enum expected
typedef vector<vl> vvl;
^
Main.java:45: error: class, interface, or enum expected
typedef map<ll,ll> mll;
^
Main.java:46: error: class, interface, or enum expected
typedef set<ll> sl;
^
Main.java:49: error: class, interface, or enum expected
int dirx[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
^
Main.java:50: error: class, interface, or enum expected
int diry[] = { -1, 0, 1, -1, 1, -1, 0, 1 };
^
Main.java:55: error: illegal character: '#'
#define XOX 1
^
Main.java:55: error: class, interface, or enum expected
#define XOX 1
^
Main.java:58: error: class, interface, or enum expected
vector<string> res;
^
Main.java:59: error: class, interface, or enum expected
while(!s.empty()) {
^
Main.java:61: error: class, interface, or enum expected
s = s.substr(s.find(',') + 1);
^
Main.java:62: error: class, interface, or enum expected
}
^
Main.java:64: error: class, interface, or enum expected
}
^
Main.java:68: error: class, interface, or enum expected
__attribute__ ((unused)) int LINE_NUM) { cerr << endl; }
^
Main.java:71: error: class, interface, or enum expected
if(idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") ";
^
Main.java:72: error: class, interface, or enum expected
stringstream ss; ss << H;
^
Main.java:72: error: class, interface, or enum expected
stringstream ss; ss << H;
^
Main.java:73: error: class, interface, or enum expected
cerr << args[idx] << " = " << ss.str();
^
Main.java:74: error: class, interface, or enum expected
debug_out(args, idx + 1, LINE_NUM, T...);
^
Main.java:75: error: class, interface, or enum expected
}
^
Main.java:76: error: illegal character: '#'
#ifdef XOX
^
Main.java:77: error: illegal character: '#'
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
^
Main.java:77: error: illegal character: '#'
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __LINE__, __VA_ARGS__)
^
Main.java:78: error: illegal character: '#'
#else
^
Main.java:79: error: illegal character: '#'
#define debug(...) 42
^
Main.java:80: error: illegal character: '#'
#endif
^
Main.java:90: error: class, interface, or enum expected
string s[100];
^
Main.java:91: error: class, interface, or enum expected
int adj[26][26];
^
Main.java:92: error: class, interface, or enum expected
stack<int> st;
^
Main.java:93: error: class, interface, or enum expected
int vis[26];
^
Main.java:94: error: class, interface, or enum expected
int pre[26];
^
Main.java:95: error: class, interface, or enum expected
bool ok;
^
Main.java:96: error: class, interface, or enum expected
void dfs(int p){
^
Main.java:100: error: class, interface, or enum expected
for(int i=0; i<26; i++){
^
Main.java:100: error: class, interface, or enum expected
for(int i=0; i<26; i++){
^
Main.java:100: error: class, interface, or enum expected
for(int i=0; i<26; i++){
^
Main.java:103: error: class, interface, or enum expected
if(vis[i]==0)dfs(i);
^
Main.java:104: error: class, interface, or enum expected
else if(vis[i]==1)ok=0;
^
Main.java:105: error: class, interface, or enum expected
}
^
Main.java:107: error: class, interface, or enum expected
st.push(p);
^
Main.java:108: error: class, interface, or enum expected
}
^
Main.java:117: error: class, interface, or enum expected
test{
^
Main.java:120: error: class, interface, or enum expected
cin>>r>>c;
^
Main.java:122: error: class, interface, or enum expected
loop(i,r){
^
Main.java:124: error: class, interface, or enum expected
}
^
Main.java:126: error: class, interface, or enum expected
ini(vis,0);
^
Main.java:127: error: class, interface, or enum expected
ok=1;
^
Main.java:128: error: class, interface, or enum expected
ini(pre,0);
^
Main.java:129: error: class, interface, or enum expected
while(st.size()){st.pop();}
^
Main.java:129: error: class, interface, or enum expected
while(st.size()){st.pop();}
^
Main.java:131: error: class, interface, or enum expected
for(int j=0; j<c; j++){
^
Main.java:131: error: class, interface, or enum expected
for(int j=0; j<c; j++){
^
Main.java:132: error: class, interface, or enum expected
for(int i=0; i<r-1; i++){
^
Main.java:132: error: class, interface, or enum expected
for(int i=0; i<r-1; i++){
^
Main.java:134: error: class, interface, or enum expected
int b = s[i][j]-'A';
^
Main.java:136: error: class, interface, or enum expected
pre[a]=1;
^
Main.java:137: error: class, interface, or enum expected
pre[b]=1;
^
Main.java:138: error: class, interface, or enum expected
if(a==b)continue;
^
100 errors