#include
#include
#include
using namespace std;
bool compare( string x, string y){
if( x[0] == y[0]){
return x.length() > y.length();
}
else{
return x < y;
}
}
int main() {
int n;
cin >>n;
string s[100];
cin.ignore();
for( int i = 0; i <=n; i++){
getline( cin, s[i]);
}
sort( s , s + n, compare);
for( int i = 0; i < n; i++){
cout << s[i] << endl;
}
return 0;
}