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

int main(){
    int N, O, A, B, AB, oTotal, ATotal, BTotal, ABTotal, OzaA, OzaB, OzaAB, AzaAB, BzaAB, ABzaAB, OzaO, AzaA, BzaB = 0;
    
    scanf("%d", &N);
    scanf("%d %d %d %d", &O, &A, &B, &AB);

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

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

        //printf("%s", bloodType[i]);

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

    if((O - oTotal) < 0){
        printf("nemoguce"); 
        return 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
        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
        BzaB = BTotal;

    if((AB - ABTotal) < 0){
        ABzaAB = ABTotal;
        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{
        ABzaAB = ABTotal;
    }

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

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

    return 0;
}