#include<bits/stdc++.h>
using namespace std;
#define int long long
#define faster() ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define endl "\n"
map<string, string> myMap;
int idx = 1;
int j = 1;
class TheLoai {
public:
string ma, name;
friend istream& operator>>(istream& is, TheLoai& a) {
a.ma = "TL";
if (idx < 10) a.ma += "00" + to_string(idx);
else if (idx < 100) a.ma += "0" + to_string(idx);
else a.ma += to_string(idx);
getline(is, a.name);
myMap[a.ma] = a.name;
++idx;
return is;
}
};
int cv(string& s) {
int res = 0;
for (char i : s) res = res * 10 + (i - '0');
return res;
}
class Phim {
public:
string ma, name, date, tap, tl, code;
int ngay, thang, nam, soTap;
friend istream& operator>>(istream& is, Phim& a) {
a.code = "P";
if (j < 10) a.code += "00" + to_string(j);
else if (j < 100) a.code += "0" + to_string(j);
else a.code += to_string(j);
string s1, s2;
getline(is, a.ma);
a.tl = myMap[a.ma];
getline(is, a.date);
getline(is, a.name);
getline(is, a.tap);
stringstream ss2(a.date);
getline(ss2, s2, '/');
a.ngay = cv(s2);
getline(ss2, s2, '/');
a.thang = cv(s2);
getline(ss2, s2, '/');
a.nam = cv(s2);
a.soTap = cv(a.tap);
++j;
return is;
}
friend ostream& operator<<(ostream& os, Phim& a) {
os << a.code << ' ' << a.tl << ' ' << a.date << ' ' << a.name << ' ' << a.tap << endl;
return os;
}
};
bool cmp(Phim& a, Phim& b) {
if (a.nam != b.nam) return a.nam < b.nam;
if (a.thang != b.thang) return a.thang < b.thang;
if (a.ngay != b.ngay) return a.ngay < b.ngay;
if (a.name != b.name) return a.name < b.name;
return a.soTap > b.soTap;
}
void process(TheLoai mh[], int n, Phim a[], int m) {
sort(a, a + m, cmp);
}
void solve() {
int n, m;
cin >> n >> m;
cin.ignore();
TheLoai tl[100];
Phim p[1000];
for (int i = 0; i < n; i++) cin >> tl[i];
for (int i = 0; i < m; i++) cin >> p[i];
process(tl, n, p, m);
for (int i = 0; i < m; i++) cout << p[i];
}
signed main() {
faster();
int test = 1;
// cin >> test;
while (test--) solve();
return 0;
}
CgojaW5jbHVkZTxiaXRzL3N0ZGMrKy5oPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKI2RlZmluZSBpbnQgbG9uZyBsb25nCiNkZWZpbmUgZmFzdGVyKCkgaW9zOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOyBjaW4udGllKG51bGxwdHIpOyBjb3V0LnRpZShudWxscHRyKTsKI2RlZmluZSBlbmRsICJcbiIKCm1hcDxzdHJpbmcsIHN0cmluZz4gbXlNYXA7CmludCBpZHggPSAxOwppbnQgaiA9IDE7CgpjbGFzcyBUaGVMb2FpIHsKcHVibGljOgogICAgc3RyaW5nIG1hLCBuYW1lOwogICAgZnJpZW5kIGlzdHJlYW0mIG9wZXJhdG9yPj4oaXN0cmVhbSYgaXMsIFRoZUxvYWkmIGEpIHsKICAgICAgICBhLm1hID0gIlRMIjsKICAgICAgICBpZiAoaWR4IDwgMTApIGEubWEgKz0gIjAwIiArIHRvX3N0cmluZyhpZHgpOwogICAgICAgIGVsc2UgaWYgKGlkeCA8IDEwMCkgYS5tYSArPSAiMCIgKyB0b19zdHJpbmcoaWR4KTsKICAgICAgICBlbHNlIGEubWEgKz0gdG9fc3RyaW5nKGlkeCk7CiAgICAgICAgZ2V0bGluZShpcywgYS5uYW1lKTsKICAgICAgICBteU1hcFthLm1hXSA9IGEubmFtZTsKICAgICAgICArK2lkeDsKICAgICAgICByZXR1cm4gaXM7CiAgICB9Cn07CgppbnQgY3Yoc3RyaW5nJiBzKSB7CiAgICBpbnQgcmVzID0gMDsKICAgIGZvciAoY2hhciBpIDogcykgcmVzID0gcmVzICogMTAgKyAoaSAtICcwJyk7CiAgICByZXR1cm4gcmVzOwp9CgpjbGFzcyBQaGltIHsKcHVibGljOgogICAgc3RyaW5nIG1hLCBuYW1lLCBkYXRlLCB0YXAsIHRsLCBjb2RlOwogICAgaW50IG5nYXksIHRoYW5nLCBuYW0sIHNvVGFwOwogICAgZnJpZW5kIGl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