#include<stdio.h>
#include<iostream>
#include<string>
#include<math.h>
using namespace std;
int main() {
	int n, o, a, b, ab, kol[10001];
	int uo = 0, ua = 0, ub = 0, uab = 0;
	string pac[10001];
	int dat[10001][4];
	
	cin >> n;
	cin >> o >> a >> b >> ab;

	int i, j;
	for (i = 0; i < n; i++) {
		for (j = 0; j < 4; j++) {
			dat[i][j] = 0;
		}
	}
	for (i = 0; i < n; i++) {
		cin >> pac[i] >> kol[i];
		if (pac[i] == "O") {
			uo += kol[i];
		}
		else if (pac[i] == "A") {
			ua += kol[i];
		}
		else if (pac[i] == "B") {
			ub += kol[i];
		}
		else {
			uab += kol[i];
		}
	}
	
	if (uo > o) {
		printf("nemoguce");
		return 0;
	}
	o -= uo;
	for (i = 0; i < n; i++) {
		if (pac[i] == "O") {
			dat[i][0] += kol[i];
		}
	}
	
	if (a + b + o < ua + ub) {
		printf("nemoguce");
		return 0;
	}

	int oc = 0, ac = 0, bc = 0;
	if (a + o < ua || b + o < uo) {
		printf("nemoguce");
		return 0;
	}
	if (ua - a < ub - b) {
		if (ua > a) {
			if (b + o - ua + a < ub) {
				printf("nemoguce");
				return 0;
			}
		}
		for (i = 0; i < n; i++) {
			if (pac[i] == "A") {
				if (ac < a) {
					if (ac + kol[i] <= a) {
						dat[i][1] += kol[i];
						ac += kol[i];
					}
					else {
						dat[i][1] = a - ac;
						dat[i][0] = kol[i] - a + ac;
						oc += kol[i] - a + ac;
						ac = a; a = 0;
					}
				}
				else {
					dat[i][0] = kol[i];
					oc += kol[i];
				}
			}
		}
		o -= oc; oc = 0;
		for (i = 0; i < n; i++) {
			if (pac[i] == "B") {
				if (bc < b) {
					if (bc + kol[i] <= b) {
						dat[i][2] += kol[i];
						bc += kol[i];
					}
					else {
						dat[i][2] = b - bc;
						dat[i][0] = kol[i] - a + bc;
						oc += kol[i] - b + bc;
						bc = b; b = 0;
					}
				}
				else {
					dat[i][0] = kol[i];
					oc += kol[i];
				}
			}
		}
		o -= oc; oc = 0;
	}
	else {
		if (ub > b) {
			if (a + o - ub + b < ua) {
				printf("nemoguce");
				return 0;
			}
		}
		for (i = 0; i < n; i++) {
			if (pac[i] == "B") {
				if (bc < b) {
					if (bc + kol[i] <= b) {
						dat[i][2] += kol[i];
						bc += kol[i];
					}
					else {
						dat[i][2] = b - bc;
						dat[i][0] = kol[i] - a + bc;
						oc += kol[i] - b + bc;
						bc = b; b = 0;
					}
				}
				else {
					dat[i][0] = kol[i];
					oc += kol[i];
				}
			}
		}
		o -= oc; oc = 0;
		for (i = 0; i < n; i++) {
			if (pac[i] == "A") {
				if (ac < a) {
					if (ac + kol[i] <= a) {
						dat[i][1] += kol[i];
						ac += kol[i];
					}
					else {
						dat[i][1] = a - ac;
						dat[i][0] = kol[i] - a + ac;
						oc += kol[i] - a + ac;
						ac = a; a = 0;
					}
				}
				else {
					dat[i][0] = kol[i];
					oc += kol[i];
				}
			}
		}
		o -= oc; oc = 0;
	}
	if (ab + a + o + b < uab) {
		printf("nemoguce");
		return 0;
	}
	if (a != 0) {
		if (b != 0) {
			for (i = 0; i < n; i++) {
				if (pac[i] == "AB") {
					if (ac < a) {
						if (ac + kol[i] <= a) {
							dat[i][1] += kol[i];
							ac += kol[i];
						}
						else {
							dat[i][1] = a - ac;
							dat[i][0] = kol[i] - a + ac;
							oc += kol[i] - a + ac;
							ac = a; a = 0;
						}
					}
					else if (oc < o) {
						if (oc + kol[i] <= o) {
							dat[i][0] += kol[i];
							oc += kol[i];
						}
						else {
							dat[i][0] = o - oc;
							dat[i][2] = kol[i] - o + oc;
							bc += kol[i] - o + oc;
							oc = o; o = 0;
						}
					}
					else if (bc < b){
						if (bc + kol[i] <= b) {
							dat[i][2] += kol[i];
							bc += kol[i];
						}
						else {
							dat[i][2] = b - bc;
							dat[i][3] = kol[i] - b + bc;
							bc = o; b = 0;
						}
					}
					else {
						dat[i][3] += kol[i];
					}
				}
			}
		}
		else {
			for (i = 0; i < n; i++) {
				if (pac[i] == "AB") {
					if (ac < a) {
						if (ac + kol[i] <= a) {
							dat[i][1] += kol[i];
							ac += kol[i];
						}
						else {
							dat[i][1] = a - ac;
							dat[i][0] = kol[i] - a + ac;
							oc += kol[i] - a + ac;
							ac = a; a = 0;
						}
					}
					else if (oc < o) {
						if (oc + kol[i] <= o) {
							dat[i][0] += kol[i];
							oc += kol[i];
						}
						else {
							dat[i][0] = o - oc;
							dat[i][2] = kol[i] - o + oc;
						}
					}
					else {
						dat[i][3] += kol[i];
					}
				}
			}
		}
	}
	else {
		if (b != 0) {
			for (i = 0; i < n; i++) {
				if (pac[i] == "AB") {
					if (oc < o) {
						if (oc + kol[i] <= o) {
							dat[i][0] += kol[i];
							oc += kol[i];
						}
						else {
							dat[i][0] = o - oc;
							dat[i][2] = kol[i] - o + oc;
							bc += kol[i] - o + oc;
							oc = o; o = 0;
						}
					}
					else if (bc < b) {
						if (bc + kol[i] <= b) {
							dat[i][2] += kol[i];
							bc += kol[i];
						}
						else {
							dat[i][2] = b - bc;
							dat[i][3] = kol[i] - b + bc;
							bc = o; b = 0;
						}
					}
					else {
						dat[i][3] += kol[i];
					}
				}
			}
		}
		else {
			for (i = 0; i < n; i++) {
				if (pac[i] == "AB") {
					if (oc < o) {
						if (oc + kol[i] <= o) {
							dat[i][0] += kol[i];
							oc += kol[i];
						}
						else {
							dat[i][0] = o - oc;
							dat[i][2] = kol[i] - o + oc;
						}
					}
					else {
						dat[i][3] += kol[i];
					}
				}
			}
		}
	}
	for (i = 0; i < n; i++) {
		printf("%d %d %d %d\n", dat[i][0], dat[i][1], dat[i][2], dat[i][3]);
	}
}