/*
Jackson Simoneau
SI@UCF 2026 - Intro to Competitive Programming
Contest 2

Solution to Alphabetical Aristocrats: https://open.kattis.com/problems/alphabeticalaristocrats
*/

#include <bits/stdc++.h>
using namespace std;

int main() {
    int n; cin >> n;
    vector<pair<string, string>> res;
    string s;
    getline(cin, s);
    for(int i = 0; i < n; i++) {
        getline(cin, s);
        bool found = false;
        string cur = "";
        for(char c : s) {
            if(c >= 'A' && c <= 'Z') found = true;
            if(found) cur += c; 
        }
        res.push_back({cur, s});
    }
    sort(res.begin(), res.end());
    for(int i = 0; i < n; i++) {
        cout << res[i].second << endl;
    }
}