#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

int main() {

    int N, O, A, B, AB = 0;
    int OzaA = 0;
    int OzaAB = 0;
    int OzaB = 0;
    int AzaAB = 0;
    int BzaAB = 0;
    int ABzaAB = 0;
    int OzaO = 0;
    int AzaA = 0;
    int BzaB = 0;
    int oTotal = 0;
    int ATotal = 0;
    int BTotal = 0;
    int ABTotal = 0;

    cin >> N;
    cin >> O >> A >> B >> AB;

    int* required = new int[N];
    string* bloodType = new string[N];

    for (int i = 0; i < N; i++) {
        cin >> bloodType[i] >> required[i];

        if (bloodType[i] == "O")
            oTotal += required[i];
        else if (bloodType[i] == "A")
            ATotal += required[i];
        else if (bloodType[i] == "B")
            BTotal += required[i];
        else if (bloodType[i] == "AB")
            ABTotal += required[i];
    }

    if ((O - oTotal) < 0) {
        printf("nemoguce");
        return 0;
    }
    else{
        if(oTotal == 0){ // Proveravamo da li nam je O uopste potreban, da bi znali da li cemo ga uzeti
            OzaO = 0;
        }
        else{
            OzaO = oTotal;
        }
    }

    if ((A - ATotal) < 0) {
        if (((O - oTotal) - abs(A - ATotal)) < 0) {
            printf("nemoguce");
            return 0;
        }
        else {
            OzaA = abs(A - ATotal);
            AzaA = A;
        }
    }
    else{
        if(ATotal == 0){
            AzaA = 0;
        }
        else{
            AzaA = ATotal;
        }
    }
        

    if ((B - BTotal) < 0) {
        if ((A - ATotal) >= 0) { // Ostalo nam je jos A, nije dirano O
            if ((O - oTotal) - abs(B - BTotal) < 0) {
                printf("nemoguce");
                return 0;
            }
            else {
                OzaB = abs(B - BTotal);
                BzaB = B;
            }
        }
        else { // Nije ostalo A, dirano je O
            if ((O - oTotal) - abs(A - ATotal) > 0) {
                if ((O - oTotal) - abs(A - ATotal) - abs(B - BTotal) < 0) {
                    printf("nemoguce");
                    return 0;
                }
                else {
                    OzaB = abs(B - BTotal);
                    BzaB = B;
                }
            }
            else {
                printf("nemoguce");
                return 0;
            }
        }
    }
    else{
        if(BTotal == 0){
            BzaB = 0;
        }
        else{
            BzaB = BTotal;
        }
    }

    if ((AB - ABTotal) < 0) { // Fali nam AB
        ABzaAB = AB; // Ukoliko ga ima dodelicemo ga njemu
        if (A - ATotal > 0) { // Ostalo je jos A, u prevodu -> nije dirano
            if ((A - ATotal) - abs(AB - ABTotal) < 0) { // Znaci moracemo da capamo iz B
                AzaAB = A - ATotal;
                if ((B - BTotal) > 0) { // Ostalo nam je jos B, u prevodu -> nije dirano
                    if ((B - BTotal) - (abs(AB - ABTotal) - AzaAB) < 0) { // Znaci moramo da vadimo iz O (AKO GA UOPSTE IMA)
                        BzaAB = B - BTotal;
                        if ((O - oTotal) > 0) { // Ostalo nam je jos O
                            if ((O - oTotal) - (abs(AB - ABTotal) - AzaAB - BzaAB)) { // Skupili smo dovoljno O, tako da imamo i AB
                                OzaAB = abs(AB - ABTotal) - AzaAB - BzaAB;
                            }
                            else { // Nema dovoljno O da bi se skupilo
                                printf("nemoguce");
                                return 0;
                            }
                        }
                        else { // Nemamo vise O, znaci nije moguce skupiti AB
                            printf("nemoguce");
                            return 0;
                        }
                    }
                    else { // Bice dovoljno A i B -> ne treba nam O
                        BzaAB = abs(AB - ABTotal) - AzaAB;
                    }
                }
                else { // Nemamo vise B, mozda je dirano O koje jedino jos mozemo probati
                    if (O - oTotal > 0) { // Ostalo nam je jos O
                        if ((O - oTotal) - (abs(AB - ABTotal) - AzaAB) < 0) { // Nemamo dovoljno O, znaci ne mozemo skupiti AB
                            printf("nemoguce");
                            return 0;
                        }
                        else { // Imamo dovoljno O
                            OzaAB = abs(AB - ABTotal) - AzaAB;
                        }
                    }
                    else { // Nije vise ostalo ni O, ne mozemo skupiti AB
                        printf("nemoguce");
                        return 0;
                    }
                }
            }
            else { // A nam je dovoljan, ubaci AzaAB i to je to
                AzaAB = abs(AB - ABTotal);
                ABzaAB = AB;
            }
        }
        else { // Nemamo vise A, mozda je dirano O
            if (B - BTotal >= 0) { // Imamo jos B -> B nije diralo O, treba proveriti O ako nema dovoljno B
                if ((B - BTotal) - abs(AB - ABTotal) >= 0) { // Imali smo dovoljno B, O ne moramo ni proveriti
                    BzaAB = abs(AB - ABTotal);
                }
                else { // Nismo imali dovoljno B, znaci moramo pokusati povuci iz O
                    BzaAB = B - BTotal;
                    if (((O - oTotal) - OzaA) - (abs(AB - ABTotal) - BzaAB) > 0) { // Imamo dovoljno O
                        OzaAB = abs(AB - ABTotal) - BzaAB;
                    }
                    else { // Nismo imali dovoljno O, nije moguce skupiti AB
                        printf("nemoguce");
                        return 0;
                    }
                }
            }
            else { // Nemamo vise ni B, O je mozda korisceno za oba
                if ((O - oTotal) - OzaB - (A - ATotal)) { // Ostalo je jos O
                    if (((O - oTotal) - OzaB - (A - ATotal)) - abs(AB - ABTotal) >= 0) { // Imali smo dovoljno O
                        OzaAB = abs(AB - ABTotal);
                    }
                    else { // Nismo imali dovoljno O, nije moguce skupiti AB
                        printf("nemoguce");
                        return 0;
                    }
                }
                else { // Nemamo vise O, nije bilo moguce skupiti dovoljno
                    printf("nemoguce");
                    return 0;
                }
            }
        }
    }
    else {
        if(ABTotal == 0){
            ABzaAB = 0;
        }
        else{
            ABzaAB = ABTotal;
        }
    }

    printf("moguce\n");
    for (int i = 0; i < N; i++) {
        if (bloodType[i] == "O") {
            printf("%d 0 0 0\n", required[i]);
        }
        else if (bloodType[i] == "A") {
            if (OzaA > 0) {
                if (OzaA - required[i] < 0) {
                    printf("%d %d 0 0\n", OzaA, (OzaA - required[i]));
                    AzaA = AzaA - abs(OzaA - required[i]);
                    OzaA = 0;
                }
                else {
                    printf("%d 0 0 0\n", OzaA);
                    OzaA = OzaA - required[i];
                }
            }
            else {
                printf("0 %d 0 0\n", required[i]);
                AzaA = AzaA - required[i];
            }
        }
        else if (bloodType[i] == "B") {
            if (OzaB > 0) {
                if (OzaB - required[i] < 0) {
                    printf("%d 0 %d 0\n", OzaB, (OzaB - required[i]));
                    BzaB = BzaB - abs(OzaB - required[i]);
                    OzaB = 0;
                }
                else {
                    printf("%d 0 0 0\n", OzaB);
                    OzaB = OzaB - required[i];
                }
            }
            else {
                printf("0 0 %d 0\n", required[i]);
                BzaB = BzaB - required[i];
            }
        }
        else if (bloodType[i] == "AB") {
            if (ABzaAB > 0) {
                if (ABzaAB - required[i] < 0) { // Ide ceo ABzaAB
                    int remaining = abs(ABzaAB - required[i]);
                    if (OzaAB > 0) {
                        if (OzaAB - remaining < 0) { // Ide ceo OzaAB
                            int remaining1 = abs(OzaAB - remaining);
                            if (AzaAB > 0) {
                                if (AzaAB - remaining1 < 0) {
                                    int remaining2 = abs(AzaAB - remaining1);
                                    printf("%d %d %d %d\n", OzaAB, AzaAB, remaining2, ABzaAB);
                                    ABzaAB = 0;
                                    OzaAB = 0;
                                    AzaAB = 0;
                                    BzaAB = BzaAB - remaining2;
                                }
                                else { // Znaci ubacujemo AzaAB
                                    printf("%d %d 0 %d\n", OzaAB, remaining1, ABzaAB);
                                    ABzaAB = 0;
                                    OzaAB = 0;
                                    AzaAB = AzaAB - remaining1;
                                }
                            }
                            else {
                                printf("%d 0 %d %d\n", OzaAB, remaining1, ABzaAB);
                                ABzaAB = 0;
                                OzaAB = 0;
                                BzaAB = BzaAB - remaining1;
                            }
                        }
                        else { // OzaAB je dovoljan
                            printf("%d 0 0 %d\n", remaining, ABzaAB);
                            ABzaAB = 0;
                            OzaAB = OzaAB - remaining;
                        }
                    }
                    else {
                        if (AzaAB > 0) {
                            if (AzaAB - remaining < 0) { // Ide ceo AzaAB i ostatak BzaAB
                                printf("0 %d %d %d\n", AzaAB, remaining, ABzaAB);
                                ABzaAB = 0;
                                AzaAB = 0;
                                BzaAB = BzaAB - remaining;
                            }
                            else {// Ide samo BzaAB
                                printf("0 0 %d %d\n", remaining, ABzaAB);
                                ABzaAB = 0;
                                BzaAB = BzaAB - remaining;
                            }
                        }
                        else {
                            printf("0 0 %d %d\n", remaining, ABzaAB);
                            ABzaAB = 0;
                            BzaAB = BzaAB - remaining;
                        }
                    }
                }
                else {
                    printf("0 0 0 %d\n", required[i]);
                    ABzaAB = ABzaAB - required[i];
                }
            }
            else {
                if (OzaAB > 0) {
                    if (OzaAB - required[i] < 0) {
                        int remaining = abs(OzaAB - required[i]);

                        if (AzaAB - remaining < 0) {
                            int remaining1 = abs(AzaAB - remaining);
                            printf("%d %d %d 0\n", OzaAB, AzaAB, required[i]);
                            OzaAB = 0;
                            AzaAB = 0;
                            BzaAB = BzaAB - required[i];
                        }
                        else {
                            printf("%d %d 0 0\n", OzaAB, required[i]);
                            OzaAB = 0;
                            AzaAB = AzaAB - required[i];
                        }
                    }
                    else {
                        printf("0 %d 0 0\n", required[i]);
                        OzaAB = OzaAB - required[i];
                    }
                }
                else {
                    if (AzaAB > 0) {
                        if (AzaAB - required[i] < 0) {
                            int remaining = abs(AzaAB - required[i]);
                            printf("0 %d %d 0\n", AzaAB, remaining);
                            AzaAB = 0;
                            BzaAB = BzaAB - remaining;
                        }
                        else {
                            printf("0 %d 0 0\n", required[i]);
                            AzaAB = AzaAB - required[i];
                        }
                    }
                    else {
                        printf("0 0 %d 0\n", required[i]);
                    }
                }
            }
        }
    }

    return 0;
}