//Problem link>>
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2904
#include <bits/stdc++.h>
using namespace std ;
typedef struct
{
string name ;
int ap , dp ;
} team ;
team player[11] ;
int main ()
{
int t , i ,j ,it=1 ;
cin>>t ;
while (t--)
{
for (i=0 ; i<10 ; i++) cin>>player[i].name>>player[i].ap>>player[i].dp ;
for (i=0 ; i<9 ; i++)
{
for (j=i+1; j<10 ; j++ )
{
if (player[i].ap<player[j].ap) swap(player[i] , player[j]) ;
else if (player[i].ap==player[j].ap)
{
if (player[i].dp>player[j].dp) swap(player[i] , player[j]) ;
else if (player[i].dp==player[j].dp)
{
if (player[i].name>player[j].name) swap(player[i] , player[j] ) ;
}
}
}
}
for (i=0 ; i<4 ; i++)
{
for (j=i+1 ; j<5 ; j++ )
if (player[i].name>player[j].name) swap (player[i] , player[j]) ;
}
for (i=5 ; i<9 ; i++)
{
for (j=i+1 ; j<10 ; j++ )
if (player[i].name>player[j].name) swap (player[i] , player[j]) ;
}
cout<<"Case "<<it<<":"<<endl ;
it++ ;
for (i=0 ; i<5 ; i++)
{
if (i==0) cout<<"("<<player[i].name<<"," ;
else if (i==4) cout<<" "<<player[i].name<<")" ;
else cout<<" "<<player[i].name<<"," ;
}
cout<<endl ;
for (i=5 ; i<10 ; i++)
{
if (i==5) cout<<"("<<player[i].name<<"," ;
else if (i==9) cout<<" "<<player[i].name<<")" ;
else cout<<" "<<player[i].name<<"," ;
}
cout<<endl ;
}
return 0 ;
}