#include <iostream>
#include <vector>
#include <string>
#include <map>
using namespace std;
int main(void)
{
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int a,b,c,d;
string str;
cin>>a;
for(d=1;d<=a;d++)
{
map<int,vector<string> > ans;
b=10;
while(b--)
{
cin>>str>>c;
ans[c].push_back(str);
}
cout<<"Case #"<<d<<":\n";
for(auto &i:ans.rbegin()->second)
cout<<i<<"\n";
}
return 0;
}