#include<iostream>
#include<string>
using namespace std;

int main(){
    int N, O, A, B, AB, i, ABx[1000], Ox[1000], Ax[1000], Bx[1000], ZA,ZB,ZO, ANX, BNX, ONX, ABNX;
    string strN;
    int Nx[1000];

    cout<<"Unesi broj pacijenata: \n";
    cin>>N;

    cout<<"Unesi zalihe krvnih grupa redom O, A, B, AB\n";
    cin>>O>>A>>B>>AB;


    for(i = 0; i<N; i++){

        strN.erase();
       cout<<"Unesi krvnu grupu "<<i+1<<". pacijenta: \n";
       cin >> strN;
       cout<<" a zatim unesi kolicinu krvi te grupe koji su potrebni pacijentu: \n";
       cin>>Nx[i];
    ANX = A; BNX = B; ONX = O;ABNX = AB;
       if(strN == "O"){
            if(O < Nx[i]){
                cout<<"nemoguce";break;
            }else{
                O = O - Nx[i];
                Ox[i] = Nx[i];
                Ax[i] = 0;
                Bx[i] = 0;
                ABx[i] = 0;
            }


       }else if(strN == "A"){

        if(A>=Nx[i]){
        A = A - Nx[i];
        Ox[i] = 0;
        Ax[i] = Nx[i];
        Bx[i] = 0;
        ABx[i] = 0;
        }else{
            if(A + B + O + AB < Nx[i]){
                cout<<"nemoguce";break;
            }else{
            Nx[i] = Nx[i] - A;
            O = O - Nx[i];
            Ox[i] = Nx[i];
            Ax[i] = A;
            Bx[i] = 0;
            ABx[i] = 0;
            }
        }

       }else if(strN == "B"){
            if(B>=Nx[i]){
            B = B - Nx[i];
            Ox[i] = 0;
            Ax[i] = 0;
            Bx[i] = Nx[i];
            ABx[i] = 0;
        }else{
            if(A + B + O + AB < Nx[i]){
                cout<<"nemoguce";break;
            }else{
            Nx[i] = Nx[i] - B;
            O = O - Nx[i];
            Ox[i] = Nx[i];
            Ax[i] = 0;
            Bx[i] = B;
            ABx[i] = 0;
            }
       }


    }else if(strN == "AB"){

        if(AB>=Nx[i]){
                ABNX = Nx[i];
            AB = AB - Nx[i];
            Ox[i] = 0;
            Ax[i] = 0;
            Bx[i] = 0;
            ABx[i] = Nx[i];
        }else{
            ZA = A;
            ZB = B;
            ZO = O;
        if(A + B + O + AB < Nx[i]){
                cout<<"nemoguce";break;
        }else{
           Nx[i] = Nx[i] - AB;
           AB = 0;
        if(O<=Nx[i]){
          Nx[i] = Nx[i] - O;

          O = 0;

        }else{
            O = O - Nx[i];
            ONX = ONX - Nx[i] ;
            Nx[i] = 0;

        }
        if(A<=Nx[i]){
          Nx[i] = Nx[i] - A;

          A = 0;

        }else{
            A = A - Nx[i];
            ANX = ANX - (ANX - Nx[i]);
            Nx[i] = 0;
        }
        if(B<=Nx[i]){
          Nx[i] = Nx[i] - B;

          B = 0;
        }else{
            B = B - Nx[i];

            BNX = BNX - (BNX - Nx[i]);
            Nx[i] = 0;
        }
        Ox[i] = ONX;
        Ax[i] = ANX;
        Bx[i] = BNX;
        ABx[i] = ABNX;

        }

    }

    }
    }
if(N == i){
for(i = 0; i < N; i++){
    cout<<"moguce \n";
    cout<<Ox[i]<<" "<<Ax[i]<<" "<<Bx[i]<<" "<<ABx[i]<<"\n";
}
}

return 0;

}