#include <bits/stdc++.h>
#define LOCAL
using namespace std;
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#ifdef LOCAL
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
#define vec vector
#define ll long long
#define ld long double
#define sza(x) ((int)x.size())
#define all(a) (a).begin(), (a).end()
const int MAX_N = 1e5 + 5;
const ll MOD = 1e9 + 7;
const ll INF = 1e9;
const ld EPS = 1e-9;
struct ttime{
int h, m, s;
ttime(){
h = 8, m = 0, s = 0;
// h = m = s = 0;
}
bool operator<=(const ttime &t){
if(h != t.h) return h <= t.h;
if(m != t.m) return m <= t.m;
return s <= t.s;
}
bool operator<(const ttime &t){
if(h != t.h) return h < t.h;
if(m != t.m) return m < t.m;
return s < t.s;
}
int operator-(const ttime &t){
int span1 = h * 3600 + m * 60 + s;
int span2 = t.h * 3600 + t.m * 60 + t.s;
int d = span1 - span2;
return (d + 30) / 60; //+ (d % 60 != 0);
}
void add(int t){
int span = h * 3600 + m * 60 + s + t * 60;
h = span / 3600;
span %= 3600;
m = span / 60;
span %= 60;
s = span;
}
void out(){
printf("%02d:%02d:%02d", h, m, s);
}
};
struct player{
ttime arr;
int t;
int tag;
ttime serv;
player(){
}
player(int h, int m, int s, int t, int tag){
arr.h = h;
arr.m = m;
arr.s = s;
this->t = t;
this->tag = tag;
serv.h = serv.m = serv.s = 0;
}
bool operator<(const player &p){
return arr < p.arr;
}
};
struct table{
ttime empty_time;
bool isvip;
int cnt;
};
vec<player> p;
vec<table> tb(110);
ttime end_time;
int n; // 人数
int k, m; // 桌子数,vip桌子数
deque<player*> dq1, dq2; // 普通队列,vip队列
vec<player*> ans;
ttime Max(ttime &a, ttime &b){
if(a < b) return b;
return a;
}
int get_idx(ttime &a, int st){
int res = -1;
for(int i = 0; i < k; i ++){
if(tb[i].empty_time <= a && tb[i].isvip){
res = i;
break;
}
}
if(res != -1 && st) return res;
res = -1;
for(int i = 0; i < k; i ++){
if(tb[i].empty_time <= a){
res = i;
break;
}
}
if(res != -1) return res;
res = 0;
for(int i = 0; i < k; i ++){
if(tb[i].empty_time < tb[res].empty_time)
res = i;
}
return res;
}
void deal(){
while(dq1.size() || dq2.size()){
player *ans_ptr;
int ans_idx;
if(dq1.empty()){
player *p = dq2.front();
dq2.pop_front();
int idx = get_idx(p->arr, 1);
p->serv = Max(tb[idx].empty_time, p->arr);
tb[idx].empty_time = p->serv;
tb[idx].empty_time.add(p->t);
ans_idx = idx;
ans_ptr = p;
}else if(dq2.empty()){
player *p = dq1.front();
dq1.pop_front();
int idx = get_idx(p->arr, 0);
p->serv = Max(tb[idx].empty_time, p->arr);
tb[idx].empty_time = p->serv;
tb[idx].empty_time.add(p->t);
ans_idx = idx;
ans_ptr = p;
}else{
player *p2 = dq2.front();
player *p1 = dq1.front();
int idx1 = get_idx(p1->arr, 0);
int idx2 = get_idx(p2->arr, 1);
if(p1->arr < p2 -> arr){
if(tb[idx1].isvip && !(tb[idx1].empty_time < p2->arr)){
dq2.pop_front();
p2->serv = Max(tb[idx1].empty_time, p2->arr);
tb[idx1].empty_time = p2->serv;
tb[idx1].empty_time.add(p2->t);
ans_idx = idx1;
ans_ptr = p2;
}else{
dq1.pop_front();
p1->serv = Max(tb[idx1].empty_time, p1->arr);
tb[idx1].empty_time = p1->serv;
tb[idx1].empty_time.add(p1->t);
ans_idx = idx1;
ans_ptr = p1;
}
}else{
dq2.pop_front();
p2->serv = Max(tb[idx2].empty_time, p2->arr);
tb[idx2].empty_time = p2->serv;
tb[idx2].empty_time.add(p2->t);
ans_idx = idx2;
ans_ptr = p2;
}
}
if(end_time <= ans_ptr->serv) continue;
ans.push_back(ans_ptr);
tb[ans_idx].cnt ++;
}
}
void solve() {
cin >> n;
for(int i = 0; i < n; i ++){
int h, m, s, t, tag;
scanf("%d:%d:%d%d%d", &h, &m, &s, &t, &tag);
t = min(t, 120);
p.push_back(player(h, m, s, t, tag));
}
cin >> k >> m;
for(int i = 0; i < m; i ++){
int t;
scanf("%d", &t);
t --;
tb[t].isvip = 1;
}
sort(all(p));
end_time.h = 21;
for(int i = 0; i < p.size(); i ++){
if(p[i].tag) dq2.push_back(&p[i]);
else dq1.push_back(&p[i]);
}
deal();
for(int i = 0; i < ans.size(); i ++){
ans[i]->arr.out();
printf(" ");
ans[i]->serv.out();
printf(" %d\n", ans[i]->serv - ans[i]->arr);
}
for(int i = 0; i < k - 1; i ++){
printf("%d ", tb[i].cnt);
}
cout << tb[k - 1].cnt;
cout << endl;
}
int main() {
// ios_base::sync_with_stdio(0);
// cin.tie(0); cout.tie(0);
int tc = 1;
// cin >> tc;
for (int t = 1; t <= tc; t++) {
// cout << "Case #" << t << ": ";
solve();
}
}