#pragma warning(disable:4996)
#include<vector>
#include<iostream>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
struct record {
string name;
int hh, mm, ss;
int time; // 用second来存储总时间
bool status;//in为1 out为0
}rec[10005];
map<string, int> park_time;
bool cmp(record a,record b) {
if (a.name != b.name)return a.name < b.name;
else if (a.time != b.time)return a.time < b.time;
}
//
bool cmp2(record a, record b) {
return a.time < b.time;
}
vector<record> valid;
int n,que_num;
int main() {
cin >> n >> que_num;
string temp_status;
for (int i = 0; i < n; i++) {
cin >> rec[i].name;
scanf("%d:%d:%d", &rec[i].hh, &rec[i].mm, &rec[i].ss);
rec[i].time = rec[i].hh * 3600 + rec[i].mm * 60 + rec[i].ss;
cin >> temp_status;
if (temp_status == "in")rec[i].status = true;
else rec[i].status = false;
}
sort(rec, rec + n, cmp);
int cnt = 0;
int maxtime = 0;
for (int i = 1; i < n; i++) {
if (rec[i].name == rec[i - 1].name) {
// 成对出现 说明两者有效,并计算两者之间的parktime
if (!rec[i].status&&rec[i - 1].status) {
valid.push_back(rec[i-1]);
valid.push_back(rec[i]);
park_time[rec[i].name]+= rec[i].time - rec[i - 1].time;
if (park_time[rec[i].name] > maxtime) {
maxtime = park_time[rec[i].name];
}
}
}
}
int hh, mm, ss;
int insecond;
sort(valid.begin(), valid.end(), cmp2);
int size = valid.size();
int j = 0;
int car_cnt = 0;
for (int i = 0; i < que_num; i++) {
scanf("%d:%d:%d", &hh, &mm, &ss);
insecond = hh * 3600 + mm * 60 + ss;
for (; j < size&&valid[j].time<=insecond; j++) {
if (valid[j].status == true)car_cnt++;
else car_cnt--;
}
printf("%d\n", car_cnt);
}
//处理最长停车时间
map<string,int>::iterator it;
for (it = park_time.begin(); it != park_time.end();it++) {
if (it->second == maxtime)printf("%s ", it->first.c_str());
}
printf("%02d:%02d:%02d", maxtime / 3600, (maxtime % 3600) / 60, maxtime % 60);
system("pause");
}