#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
struct node {
string id;
int a, c, m, e;
int ra, rc, rm, re;
};
bool cmpa(node &a, node &b){return a.a > b.a;}
bool cmpc(node &a, node &b){return a.c > b.c;}
bool cmpm(node &a, node &b){return a.m > b.m;}
bool cmpe(node &a, node &b){return a.e > b.e;}
vector<node> v;
map<string, int> ma;
int main(){
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
node t;
cin >> t.id >> t.c >> t.m >> t.e;
t.a = (t.c + t.m + t.e) / 3;
v.push_back(t);
}
sort(v.begin(), v.end(), cmpa);
v[0].ra = 1;
for (int i = 1; i < v.size(); i++){
if(v[i-1].a == v[i].a) v[i].ra = v[i-1].ra;
else v[i].ra = i + 1;
}
sort(v.begin(), v.end(), cmpc);
v[0].rc = 1;
for (int i = 1; i < v.size(); i++){
if(v[i-1].c == v[i].c) v[i].rc = v[i-1].rc;
else v[i].rc = i + 1;
}
sort(v.begin(), v.end(), cmpm);
v[0].rm = 1;
for (int i = 1; i < v.size(); i++){
if(v[i-1].m == v[i].m) v[i].rm = v[i-1].rm;
else v[i].rm = i + 1;
}
sort(v.begin(), v.end(), cmpe);
v[0].re = 1; ma[v[0].id] = 1;
for (int i = 1; i < v.size(); i++){
if(v[i-1].e == v[i].e) v[i].re = v[i-1].re;
else v[i].re = i + 1;
ma[v[i].id] = i + 1;
}
for (int j = 0; j < m; j++) {
string t;
cin >> t;
if(ma[t]){
int i = ma[t] - 1;
if(v[i].ra <= v[i].rc && v[i].ra <= v[i].rm && v[i].ra <= v[i].re)
cout << v[i].ra << ' ' << 'A' << endl;
else if(v[i].rc < v[i].ra && v[i].rc <= v[i].rm && v[i].rc <= v[i].re)
cout << v[i].rc << ' ' << 'C' << endl;
else if(v[i].rm < v[i].ra && v[i].rm < v[i].rc && v[i].rm <= v[i].re)
cout << v[i].rm << ' ' << 'M' << endl;
else if(v[i].re < v[i].ra && v[i].re < v[i].rc && v[i].re < v[i].rm)
cout << v[i].re << ' ' << 'E' << endl;
}else{
cout << "N/A\n";
}
}
return 0;
}