#include<iostream>
#include<sstream>
using namespace std;
int M, X, Y, F;
int minute;
struct Family {
int ID;
int num;
int price;
int enter_minute;
};
struct Place {
int total_money;
int total_entered;
int total_quit;
int ID_counter;
int register_timer;
int special_family;
Family queue[20000];
int queue_size;
Family families_entered[20000];
int families_entered_size;
Place() {
total_money = 0;
total_entered = 0;
total_quit = 0;
ID_counter = 1;
register_timer = 0;
special_family = -1;
queue_size = 0;
families_entered_size = 0;
for (int i = 0; i < 20000; i++) {
queue[i].ID = -1;
families_entered[i].ID = -1;
}
}
void helper_add_family_entered(Family family) {
families_entered[families_entered_size] = family;
families_entered_size++;
}
void add_family_from_line(string line) {
stringstream ss(line);
string token;
while (ss >> token) {
int A, P;
if (sscanf(token.c_str(), "%d,%d", &A, &P) == 2) {
add_family(A, P);
} else {
continue;
}
}
}
void add_family(int num, int price) {
if (queue_size == 0) {
register_timer = 0;
}
queue[queue_size].ID = ID_counter++;
queue[queue_size].num = num;
queue[queue_size].price = price;
queue[queue_size].enter_minute = minute;
queue_size++;
}
void helper_shift_left_from(int idx) {
for (int i = idx; i < queue_size - 1; i++) {
queue[i] = queue[i + 1];
}
queue[queue_size - 1].ID = -1;
queue_size--;
if (queue_size == 0) {
register_timer = -1;
}
}
void progress_registration() {
if (queue_size > 0) {
if (register_timer == 2) {
register_timer = 0;
int price = queue[0].price;
if (special_family == queue[0].ID) {
price = 0;
special_family = -1;
}
total_money += price;
total_entered += queue[0].num;
helper_add_family_entered(queue[0]);
helper_shift_left_from(0);
}
register_timer++;
}
if (special_family == -1) {
find_special();
}
}
void find_special() {
for (int i = 0; i < queue_size; i++) {
int sum = 0;
for (int j = i; j < queue_size - 1; j++) {
sum += queue[j].price;
if (sum == Y) {
special_family = queue[j + 1].ID;
return;
}
}
}
}
void quit_family() {
for (int i = queue_size - 1; i >= 0; i--) {
if (i <= X)
return;
if (queue[i].ID == special_family)
return;
if (queue[i].num >= 100)
continue;
if (minute - queue[i].enter_minute >= 60) {
total_quit += queue[i].num;
helper_shift_left_from(i);
}
}
}
void summary(int index) {
cout << "Place " << index << " Summary\n";
cout << "Total Money: " << total_money << "\n";
cout << "Total People Entered: " << total_entered << "\n";
cout << "Total People Quit: " << total_quit << "\n";
int current_num = 0;
for (int i = 0; i < queue_size; i++) {
current_num += queue[i].num;
}
cout << "Current People Queuing: " << current_num << "\n";
cout << "\n";
cout << "Entered Families :\n";
for (int i = families_entered_size - 1; i >= 0; i--) {
cout << "Family " << families_entered[i].ID << " with " << families_entered[i].num << " people\n";
}
}
void helper_print_queue() {
cout << "Queue: ";
for (int i = 0; i < queue_size; i++) {
cout << queue[i].ID << " " << queue[i].num << " " << queue[i].price << " " << queue[i].enter_minute << "\n";
}
cout << "\n";
}
};
int main() {
cin >> F >> X >> Y >> M;
cin.ignore();
Place *places = new Place[F];
for (int i = 0; i < F; i++) {
// Input format is A,P A,P ...
string line;
getline(cin, line);
places[i].add_family_from_line(line);
}
for (minute=0; minute <= M; minute++) {
cin.ignore();
for (int place_idx = 0; place_idx < F; place_idx++) {
string line;
getline(cin, line);
places[place_idx].progress_registration();
places[place_idx].quit_family();
places[place_idx].add_family_from_line(line);
if (place_idx == 0) {
cout << "Minute " << minute << "\n";
places[place_idx].helper_print_queue();
}
}
}
for (int i = 0; i < F; i++) {
places[i].summary(i);
cout << "\n";
}
return 0;
}