// 一遍过的,还是挺开心的,比较代码量挺大的,这个题不涉及到任何算法,锻炼你的理解能力和打码水平
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
#include <unordered_map>
using namespace std;
const int N = 1e4 + 10;
int n,m;
typedef pair<string,int> PSI;
vector<pair<string,int>>va;
vector<pair<string,int>>vb;
vector<pair<string,int>>vc;
unordered_map<string,pair<int,int>>mp1;
unordered_map<string,unordered_map<string,int>>mp2;
void print(int i,char c,vector<PSI>v)
{
cout << "Case " << i << ": " << 1 << " " << c << endl;
if(v.size())
for(auto &x : v)
cout << x.first << ' ' << x.second << endl;
else
cout << "NA" << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin >> n >> m;
string str;
int score;
for(int i = 0;i < n;i ++)
{
cin >> str >> score;
char c = str[0];
string site;
for(int i = 1;i < 4;i ++)
site += str[i];
string date;
for(int i = 4;i < 10;i ++)
date += str[i];
string id;
for(int i = 10;i < 13;i ++)
id += str[i];
if(c == 'A'){
va.push_back({str,score});
}
else if(c == 'B'){
vb.push_back({str,score});
}
else{
vc.push_back({str,score});
}
int sums = mp1[site].second + 1;
int sumf = mp1[site].first + score;
mp1[site] = make_pair(sumf,sums);
mp2[date][site] ++;
}
sort(va.begin(),va.end(),[](PSI p1,PSI p2){
if(p1.second == p2.second){
return p1.first < p2.first;
}
return p1.second > p2.second;
});
sort(vb.begin(),vb.end(),[](PSI p1,PSI p2){
if(p1.second == p2.second){
return p1.first < p2.first;
}
return p1.second > p2.second;
});
sort(vc.begin(),vc.end(),[](PSI p1,PSI p2){
if(p1.second == p2.second){
return p1.first < p2.first;
}
return p1.second > p2.second;
});
for(int i = 1;i <= m;i ++)
{
int op;
cin >> op;
string str;
if(op == 1){
char c;
cin >> c;
if(c == 'A'){
print(i,c,va);
}
else if(c == 'B'){
print(i,c,vb);
}
else{
print(i,c,vc);
}
}
else if(op == 2){
cin >> str;
cout << "Case " << i << ": 2 " << str << endl;
if(mp1[str].second)
cout << mp1[str].second << ' ' << mp1[str].first << endl;
else
cout << "NA" << endl;
}
else
{
cin >> str;
cout << "Case " << i << ": 3 " << str << endl;
vector<PSI>v;
for(auto &x : mp2[str])
{
v.push_back({x.first,x.second});
}
sort(v.begin(),v.end(),[](PSI p1,PSI p2){
if(p1.second == p2.second){
return p1.first < p2.first;
}
return p1.second > p2.second;
});
if(v.size()){
for(auto &x : v){
cout << x.first << ' ' << x.second << endl;
}
}else{
cout << "NA" << endl;
}
}
}
return 0;
}