#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
#ifdef __GNUC__
#include <ext/hash_map>
#define unordered_map __gnu_cxx::hash_map
#else
#include <unordered_map>
#endif
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

static string ones[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
static string tens[] = {"", "X", "XX", "XXX", "XL", "L"};

class KingSort { public:
	vector <string> getSortedList(vector <string> kings)
	{
		vector< pair<string,int> > kk;
		for(int i=0; i<kings.size(); ++i)
			kk.push_back( parse(kings[i]) );
		sort(kk.begin(), kk.end());
		for(int i=0; i<kings.size(); ++i)
			kings[i] = encode(kk[i]);
		return kings;
	}

	pair<string, int> parse(const string& s)
	{
		stringstream ss(s);
		string name, ord;
		ss >> name >> ord;
		return make_pair(name, roman(ord));
	}

	int roman(const string& s)
	{
		for(int i=0; i<=s.size(); ++i)
		{
			int ti = find(&tens[0], &tens[6], s.substr(0, i)) - &tens[0];
			int oi = find(&ones[0], &ones[10], s.substr(i)) - &ones[0];
			if( oi == 10 || ti == 6 )
				continue;
			return oi + ti*10;
		}
		return -99999;
	}

	string encode(const pair<string,int>& p)
	{
		stringstream ss;
		ss << p.first << " " << tens[p.second/10] << ones[p.second%10];
		return ss.str();
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const vector <string>& Expected, const vector <string>& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, KingSort().getSortedList(kings));}
int main(){

CASE(0)
	string kings_[] = {"Louis IX", "Louis VIII"};
	  vector <string> kings(kings_, kings_+sizeof(kings_)/sizeof(*kings_)); 
	string __[] = {"Louis VIII", "Louis IX" };
	  vector <string> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(1)
	string kings_[] = {"Louis IX", "Philippe II"};
	  vector <string> kings(kings_, kings_+sizeof(kings_)/sizeof(*kings_)); 
	string __[] = {"Louis IX", "Philippe II" };
	  vector <string> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(2)
	string kings_[] = {"Richard III", "Richard I", "Richard II"};
	  vector <string> kings(kings_, kings_+sizeof(kings_)/sizeof(*kings_)); 
	string __[] = {"Richard I", "Richard II", "Richard III" };
	  vector <string> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(3)
	string kings_[] = {"John X", "John I", "John L", "John V"};
	  vector <string> kings(kings_, kings_+sizeof(kings_)/sizeof(*kings_)); 
	string __[] = {"John I", "John V", "John X", "John L" };
	  vector <string> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(4)
	string kings_[] = {"Philippe VI", "Jean II", "Charles V", "Charles VI", "Charles VII", "Louis XI"};
	  vector <string> kings(kings_, kings_+sizeof(kings_)/sizeof(*kings_)); 
	string __[] = {"Charles V", "Charles VI", "Charles VII", "Jean II", "Louis XI", "Philippe VI" };
	  vector <string> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(5)
	string kings_[] = {"Philippe II", "Philip II"};
	  vector <string> kings(kings_, kings_+sizeof(kings_)/sizeof(*kings_)); 
	string __[] = {"Philip II", "Philippe II" };
	  vector <string> _(__, __+sizeof(__)/sizeof(*__)); 
END
/*
CASE(6)
	string kings_[] = ;
	  vector <string> kings(kings_, kings_+sizeof(kings_)/sizeof(*kings_)); 
	string __[] = ;
	  vector <string> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(7)
	string kings_[] = ;
	  vector <string> kings(kings_, kings_+sizeof(kings_)/sizeof(*kings_)); 
	string __[] = ;
	  vector <string> _(__, __+sizeof(__)/sizeof(*__)); 
END
*/
}
// END CUT HERE
