#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
const int N = 2800;
map<string, int>GotTime;
map<string, int> give;
map<string, string>id_name;
struct node {
string name;
string id;
int good;
string TIME;
int time;
int rank;
}a[N], NotGood[N];
int top = 0;
bool cmp(node a, node b) {
if (a.time == b.time) return a.rank < b.rank;
return a.time < b.time;
}
int GetTime(string s) {
int h = 0;
h += s[0] - '0';
h *= 10;
h += s[1] - '0';
int m = 0;
m += s[3] - '0';
m *= 10;
m += s[4] - '0';
return h * 60 + m;
}
bool check(string s, string name) {
if (s.length() != 18)return 0;
for (auto c:s){if (!(c >= '0' && c <= '9'))return 0;}
return 1;
}
int main() {
int DAY, gap_day;cin >> DAY >> gap_day;
for (int i = 1; i <= DAY; i ++) {
int info;int mask;cin >> info >> mask;
int cnt = 0;
for (int j = 1; j <= info; j ++) {cnt++;
cin >> a[cnt].name >> a[cnt].id >> a[cnt].good >> a[cnt].TIME;
a[cnt].rank = j;
a[cnt].time = GetTime(a[cnt].TIME);
if (!check(a[cnt].id, a[cnt].name))cnt--;
if (a[cnt].good == 1) NotGood[++top] = a[cnt];
}
info = cnt;
sort(a + 1, a + 1 + info, cmp);
give.clear();
for (int j = 1; j <= cnt; j ++) {
if (!GotTime.count(a[j].id) || GotTime[a[j].id] + gap_day + 1<= i) {
if (!give.count(a[j].id) && mask > 0) {
GotTime[a[j].id] = i;
cout << a[j].name << " " << a[j].id << endl, mask--;
give[a[j].id] = 1;
}
}
}
}
give.clear();
for (int i = 1; i <= top; i ++) {
if (!give.count(NotGood[i].id)) {
give[NotGood[i].id] = 1;
cout << NotGood[i].name << " " << NotGood[i].id << endl;
}
}
}