#include <bits/stdc++.h>
using namespace std;
class SinhVien{
private:
string msv, ten, lop, email;
public:
friend istream& operator >> (istream &in, SinhVien &a);
friend ostream& operator << (ostream &out, SinhVien a);
string getLOP();
string getMA();
};
istream& operator >> (istream &in , SinhVien &a){
getline(in, a.msv);
getline(in, a.ten);
getline(in, a.lop);
getline(in, a.email);
return in;
}
ostream& operator << (ostream &out, SinhVien a){
out << a.msv << " " << a.ten << " " << a.lop << " " << a.email << endl;
return out;
}
string SinhVien::getLOP(){
return this->lop;
}
string SinhVien::getMA(){
return this->msv;
}
bool cmp(SinhVien a, SinhVien b){
if(a.getLOP()==b.getLOP()){
return a.getMA() < b.getMA();
}
return a.getLOP() < b.getLOP();
}
void sapxep(SinhVien ds[], int n){
sort(ds,ds+n,cmp);
}
int main(){
int n; cin >> n;
cin.ignore();
SinhVien a[1001];
for(int i = 0; i < n; i++) cin >> a[i];
sapxep(a,n);
for(int i = 0; i < n; i++) cout << a[i];
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpjbGFzcyBTaW5oVmllbnsKICAgIHByaXZhdGU6CiAgICAgICAgc3RyaW5nIG1zdiwgdGVuLCBsb3AsIGVtYWlsOwogICAgcHVibGljOgogICAgICAgIGZyaWVuZCBpc3RyZWFtJiBvcGVyYXRvciA+PiAoaXN0cmVhbSAmaW4sIFNpbmhWaWVuICZhKTsKICAgICAgICBmcmllbmQgb3N0cmVhbSYgb3BlcmF0b3IgPDwgKG9zdHJlYW0gJm91dCwgU2luaFZpZW4gYSk7CiAgICAgICAgc3RyaW5nIGdldExPUCgpOwogICAgICAgIHN0cmluZyBnZXRNQSgpOwp9OwoKCmlzdHJlYW0mIG9wZXJhdG9yID4+IChpc3RyZWFtICZpbiAsIFNpbmhWaWVuICZhKXsKICAgIGdldGxpbmUoaW4sIGEubXN2KTsKICAgIGdldGxpbmUoaW4sIGEudGVuKTsKICAgIGdldGxpbmUoaW4sIGEubG9wKT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