#include <iostream>
#include <iomanip>
#include <map>
#include <set>
#include <cmath>
#include <vector>
#include <climits>
#include <string>
#include <algorithm>
#include <sstream>
#include <numeric>
using namespace std;

vector<string> split(string haystack, string needle) {
    vector<string> result;
    int startPos = 0;
    int foundPos = haystack.find(needle, startPos);
    while (foundPos != string::npos) {
        int count = foundPos - startPos;
        string token = haystack.substr(startPos, count);
        if (!token.empty()) {
            result.push_back(token);
        }
        startPos = foundPos + needle.length();
        foundPos = haystack.find(needle, startPos);
    }
    string token = haystack.substr(startPos, haystack.length() - startPos);
    if (!token.empty()) {
        result.push_back(token);
    }
    return result;
}

string nameConvert(const string& name) {
    const string DELIMITER = " ";
    stringstream builder;
    auto parts = split(name, DELIMITER);
    for (auto part : parts) {
        part[0] = toupper(part[0]);
        transform(part.begin() + 1, part.end(), part.begin() + 1, ::tolower);
        builder << part << DELIMITER;
    }
    return builder.str();
}

string dateConvert(const string& date) {
    const string DELIMITER = "/";
    stringstream builder;
    auto parts = split(date, DELIMITER);
    builder << setw(2) << setfill('0') << parts[0] << DELIMITER;
    builder << setw(2) << setfill('0') << parts[1] << DELIMITER;
    builder << parts[2];
    return builder.str();
}

class TeacherIDGenerator {
private:
    inline static int index = 1;
    inline static const string PREFIX = "GV";

public:
    static string next() {
        stringstream builder;
        builder << PREFIX << setw(2) << setfill('0') << index;
        index++;
        return builder.str();
    }
};

class Teacher {
private:
    string id, name, major;
public:
    Teacher() : name(""), major("") {
        id = TeacherIDGenerator::next();
    };
    Teacher(string name, string major) : name(name), major(major) {
        id = TeacherIDGenerator::next();
    };

public:
    void setName(string name) { this->name = name; }
    void setMajor(string major) { this->major = major; }

public:
    string toString() {
        stringstream builder;
        builder << id << " " << name << " ";

        auto parts = split(major, " ");
        for (int i = 0;i < parts.size();i++) {
            builder << (char) toupper(parts[i][0]);
        }
        return builder.str();
    }
public:
    bool operator < (const Teacher& other) const {

        string first_name = split(name, " ").back();
        string other_name = split(other.name, " ").back();

        if (first_name != other_name) {
            return first_name < other_name;
        }
        return id < other.id;
    }
};

class TeacherFromKeyboardProvider {
public:
    static Teacher next() {
        string buffer;
        Teacher t;

        getline(cin, buffer);
        t.setName(buffer);

        getline(cin, buffer);
        t.setMajor(buffer);

        return t;
    }
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;cin >> n;
    cin.ignore();
    vector<Teacher> teachers;
    for (int i = 0;i < n;i++) {
        Teacher t = TeacherFromKeyboardProvider::next();
        teachers.push_back(t);
    }

    sort(teachers.begin(), teachers.end());

    for (auto teacher : teachers) {
        cout << teacher.toString() << endl;
    }
    return 0;
}

