#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rrep(i,b,a) for(ll i=b;i>=a;i--)
#define fori(i,a) for(auto i : a )
#define all(a) begin(a), end(a)
#define mset(a,b) memset(a,b,sizeof(a))
#define mcopy(a,b) memcopy(a,b,sizeof(a))
#define pb push_back
#define f first
#define s second
#define ll long long
string getString(char x) {
string s(1, x);
return s;
}
void solve1(){
vector<pair<ll,string>> v(10001);
rep(i,1,10001){
cin>>v[i].f>>v[i].s;
}
sort(all(v));
vector< set<string> > v1(100);
rep(i,1,10001){
v1[v[i].f].insert(v[i].s);
}
string s="";
rep(i,1,11){
if(i==1)s+=*v1[i].begin();
else{
for(auto j:v1[i]){
if(v1[i-1].find(j)==v1[i-1].end()){
if(i!=10) s+=j;
else{
s=getString(j[1])+s;
}
break;
}
}
}
}
cout<<s<<endl;
}
void solve3(vector<pair<ll,string>> v){
map<char,int> m1;
set<char> s1;
for(auto i:v){
for(auto j:i.s){
s1.insert(j);
}
}
set<char> s2=s1;
for(auto i:v){
s2.erase(i.s[0]);
m1[i.s[0]]++;
}
string g="";
char fi;
int mx=0;
for(auto i:s2){
// if(mx>m1[i]){
// mx=m1[i];
fi=i;
// }
}
g+=getString(fi);
s1.erase(fi);
s2.erase(fi);
m1.erase(fi);
vector<pair<int,char>> v1;
for(auto i:s2) m1[i]=0;
for(auto i:m1){
v1.pb({i.s,i.f});
}
// sort(v1.begin(),v1.end());
sort(v1.begin(),v1.end(),greater<pair<int,char>>());
for(auto i:v1) g+=getString(i.s);
cout<<g<<endl;
}
void solve2(){
vector<pair<ll,string>> v(10001);
ll flag=0;
rep(i,1,10001){
cin>>v[i].f>>v[i].s;
if(v[i].f!=-1) flag=1;
}
auto comp=[]( const pair<ll,string> & x , const pair<ll,string> &y ){return x.s.length()<y.s.length();};
sort(all(v),comp);
// solve3(v);
if(flag==0)solve3(v);
else{
set<char> s1;
for(auto i:v){
for(auto j:i.s){
s1.insert(j);
}
}
vector<pair<ll,string>> v1;
map<ll,set<char>> m1;
rep(i,1,10001){
if(v[i].f!=-1){
v1.pb({v[i].f,v[i].s});
}
}
rep(i,0,v1.size()){
if(to_string(v1[i].f).length()==v1[i].s.length()) m1[to_string(v1[i].f)[0]-'0'].insert(v1[i].s[0]);
}
string g="";
g+=getString(*m1[1].begin());
// cout<<g<<endl;
rep(i,2,10){
for(auto j:m1[i]){
if(m1[i-1].find(j)==m1[i-1].end()){
g+= getString(j);
}
}
}
// cout<<g<<endl;
for(auto i:s1){
if(g.find(i)==string::npos){
g=getString(i)+g;
break;
}
}
cout<<g<<endl;
}
}
void solve(){
ll u;
cin>>u;
if(u==2) solve1();
else solve2();
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ll t,k=1;
cin>>t;
while(t--){
cout<<"Case #"<<k++<<": ";
solve();
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojZGVmaW5lIHJlcChpLGEsYikgZm9yKGxsIGk9YTtpPGI7aSsrKQojZGVmaW5lIHJyZXAoaSxiLGEpIGZvcihsbCBpPWI7aT49YTtpLS0pCiNkZWZpbmUgZm9yaShpLGEpIGZvcihhdXRvIGkgOiBhICkKI2RlZmluZSBhbGwoYSkgYmVnaW4oYSksIGVuZChhKQojZGVmaW5lIG1zZXQoYSxiKSBtZW1zZXQoYSxiLHNpemVvZihhKSkKI2RlZmluZSBtY29weShhLGIpIG1lbWNvcHkoYSxiLHNpemVvZihhKSkKI2RlZmluZSBwYiBwdXNoX2JhY2sKI2RlZmluZSBmIGZpcnN0CiNkZWZpbmUgcyBzZWNvbmQKI2RlZmluZSBsbCBsb25nIGxvbmcKc3RyaW5nIGdldFN0cmluZyhjaGFyIHgpIHsKICAgIHN0cmluZyBzKDEsIHgpOwogICAgcmV0dXJuIHM7Cn0Kdm9pZCBzb2x2ZTEoKXsKICB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