#include <stdio.h>
#include <stdlib.h>
//BLOOD SUPPLIES + BLOOD ARRAY -> O,A,B,AB

void main(){
	int n,i,j;
	scanf("%i", &n);
	int bloodsupplies[4];
	int bloodsums[] = {0,0,0,0};
	
	struct patients{
		int bloodneeded;
		int bloodarray[4];
		char bloodtype[2];
	} patients[n];
	
	for(i=0;i<n;i++){
		for(j=0;j<4;j++){
			patients[i].bloodarray[j] = 0;
		}
	}
	
	scanf("%i %i %i %i", &bloodsupplies[0], &bloodsupplies[1], &bloodsupplies[2], &bloodsupplies[3]);  
	
	for(i=0;i<n;i++){
		scanf("%s", &patients[i].bloodtype);
		scanf("%i", &patients[i].bloodneeded);
	}
	
	//load up sum of O, A, B, AB bloods
	for(i=0,j=0;i<n;i++){
		if(strcmp(patients[i].bloodtype, "O") == 0){
			bloodsums[0] += patients[i].bloodneeded;
			continue;
		}
		
		if(strcmp(patients[i].bloodtype, "A") == 0){
			bloodsums[1] += patients[i].bloodneeded;
			continue;
		}
		
		if(strcmp(patients[i].bloodtype, "B") == 0){
			bloodsums[2] += patients[i].bloodneeded;
			continue;
		}
		
		if(strcmp(patients[i].bloodtype, "AB") == 0){
			bloodsums[3] += patients[i].bloodneeded;
			continue;
		}
	}
	
	if(bloodsums[0] > bloodsupplies[0]){
		printf("nemoguce");
		exit(0);
	}
	
	for(i=0; i<n; i++){//set blood array for all O patients
		if(strcmp(patients[i].bloodtype, "O") == 0){
			patients[i].bloodarray[0] = patients[i].bloodneeded; //set the O blood
			bloodsupplies[0] -= patients[i].bloodneeded;
		}	
	}
	
	if(bloodsums[1] > bloodsupplies[1]){ //no A blood, dip into O pool
		bloodsums[1] -= bloodsupplies[1]; //reduce all the A blood available from the full sum then check if theres enough O
		if(bloodsums[1] > bloodsupplies[0]){
			printf("nemoguce");
			exit(0);
		}
	}
	
	for(i=0; i<n; i++){//set blood array for all A patients
		if(strcmp(patients[i].bloodtype, "A") == 0){
			if(bloodsupplies[1] != 0 ){ //if A blood exists
			
				if(patients[i].bloodneeded < bloodsupplies[1]){ //if they can take only A
					patients[i].bloodarray[1] = patients[i].bloodneeded; //set the A blood
					bloodsupplies[1] -= patients[i].bloodneeded; //reduce supplies
					continue;
				}
				
				if(patients[i].bloodneeded > bloodsupplies[1]){ //if they need more than A has
					j = patients[i].bloodneeded - bloodsupplies[1];// j = needed O blood
					patients[i].bloodarray[1] = bloodsupplies[1];
					patients[i].bloodarray[0] = j;
					bloodsupplies[1] = 0; //reduce supplies
					bloodsupplies[0] -= j;
					continue;
				}
				
			} else{ // no A blood?
				patients[i].bloodarray[0] = patients[i].bloodneeded;
				bloodsupplies[0] -= patients[i].bloodneeded;	
			}
		}	
	}
	
	for(i=0; i<n; i++){//set blood array for all B patients
		if(strcmp(patients[i].bloodtype, "B") == 0){
			if(bloodsupplies[2] != 0 ){ //if B blood exists
			
				if(patients[i].bloodneeded < bloodsupplies[2]){ //if they can take only B
					patients[i].bloodarray[2] = patients[i].bloodneeded; //set the B blood
					bloodsupplies[2] -= patients[i].bloodneeded; //reduce supplies
					continue;
				}
				
				if(patients[i].bloodneeded > bloodsupplies[2]){ //if they need more than B has
					j = patients[i].bloodneeded - bloodsupplies[2];// j = needed O blood
					patients[i].bloodarray[2] = bloodsupplies[2];
					patients[i].bloodarray[0] = j;
					bloodsupplies[2] = 0; //reduce supplies
					bloodsupplies[0] -= j;
					continue;
				}
				
			} else{ // no B blood?
				patients[i].bloodarray[0] = patients[i].bloodneeded;
				bloodsupplies[0] -= patients[i].bloodneeded;	
			}
		}	
	}

	//time for AB 
	if(bloodsums[3] > (bloodsupplies[0] + bloodsupplies[1] + bloodsupplies[2] + bloodsupplies[3])){ //is there enough blood for AB?
			printf("nemoguce");
			exit(0);
	}
	
	for(i=0; i<n; i++){//set blood array for all AB patients
		if(strcmp(patients[i].bloodtype, "AB") == 0){
			while(patients[i].bloodneeded > 0){
				
				if(bloodsupplies[0] > 0){
					bloodsupplies[0]--;
					patients[i].bloodneeded--;
					patients[i].bloodarray[0]++;
				}else{
					if(bloodsupplies[1] > 0){
						bloodsupplies[1]--;
						patients[i].bloodneeded--;
						patients[i].bloodarray[1]++;
					}else{
						if(bloodsupplies[2] > 0){
							bloodsupplies[2]--;
							patients[i].bloodneeded--;
							patients[i].bloodarray[2]++;
						}else{
							bloodsupplies[3]--;
							patients[i].bloodneeded--;
							patients[i].bloodarray[3]++;
						}
					}
				}

			}
		}
	}
	
	//print em
	printf("moguce\n");
	for(i=0;i<n;i++){
		for(j=0;j<4;j++){
			printf("%i ", patients[i].bloodarray[j]);
		}
		printf("\n");
	}
}