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



const int MAX_N = 1e4+5;
int G[MAX_N];
int BR[MAX_N];

void racunaj(int n, int max_o, int max_a, int max_b, int max_ab, int visak_o, int visak_a, int visak_b )
{
    cout<<"moguce"<<'\n';
    int o, a, b, ab;
    for(int i = 0; i < n; i++)
    {

        o = 0; a = 0; b = 0, ab = 0;

        if(G[i] == 0) // O grupa
        {
            o = BR[i];
            max_o-=BR[i];
        }
        else if (G[i] == 1) // A grupa
        {
            if(BR[i] <= max_a)
            {
                a = BR[i];
                max_a -= BR[i];
            }
            else
            {
                a = max_a;
                o = BR[i] - max_a;
                max_a = 0;
                max_o -= o;
            }
        }
        else if (G[i] == 2) // B grupa
        {
            if(BR[i] <= max_b)
            {
                b = BR[i];
                max_b -= BR[i];
            }
            else
            {
                b = max_b;
                o = BR[i] - max_b;
                max_b = 0;
                max_o -= o;
            }
        }
        else
        {
            //cout<<max_o<<" "<<max_a<<" "<<max_b<<" "<<max_ab<<'\n';
            //cout<<visak_o<<" "<<visak_a<<" "<<visak_b<<" "<<'\n';
            if(BR[i] <= max_ab)
            {
                ab = BR[i];
                max_ab -=BR[i];
            }
            else{
                ab = max_ab;
                int ostatak = BR[i] - max_ab;
                max_ab = 0;

                if(ostatak <= visak_o)
                {
                    o = ostatak;
                    visak_o -= o;
                    max_o -= o;
                }
                else{
                    o = visak_o;
                    max_o -=o;
                    ostatak -=o;
                    visak_o = 0;

                    if(ostatak <= visak_a)
                    {

                        a = ostatak;
                        visak_a -= ostatak;
                        max_a -=ostatak;
                    }
                    else{
                        a = visak_a;
                        max_a -= a;
                        ostatak -= a;
                        visak_a = 0;

                        b = ostatak;
                        max_b -=b;
                        visak_b -=ostatak;
                    }
                }
            }
        }
        cout<<o<<" "<<a<<" "<<b<<" "<<ab<<'\n';
    }
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    int max_o, max_a, max_b, max_ab, n;
    cin>>n;
    cin>>max_o>>max_a>>max_b>>max_ab;

    int o=0,a=0,b=0,ab=0;
    for(int i = 0; i < n; i++)
    {
        string g; int br;
        cin>>g>>br;


        BR[i] = br;

        if(g.compare("O") == 0) {G[i] = 0; o += br;}
        else if(g.compare("A") == 0) {G[i] = 1; a += br;}
        else if(g.compare("B") == 0) {G[i] = 2; b += br;}
        else if(g.compare("AB") == 0) {G[i] = 3; ab += br;}

    }

    //bool moguce = true;
   // cout<<"O: "<<o<<endl;
    //cout<<"A: "<<a<<endl;
    //cout<<"B: "<<b<<endl;
    //cout<<"AB: "<<ab<<endl;
   // int visak_o = max_o - o, visak_a = max_a - a, visak_b = max_b - b, visak_ab = max_ab - ab;
    int temp_o = max_o, temp_a = max_a, temp_b = max_b, temp_ab = max_ab; //VISAK
    if(o <= temp_o)  // 1 uslov
    {
        //cout<<"prvi";
        temp_o -= o;
//----------------------------------------------
        //cout<<"temp-a "<<temp_a<<endl;
        //cout<<"temp_o "<<temp_o<<endl;
        if(a <= temp_a + temp_o) //2 uslov
        {
            //cout<<"drugi";
            if(a <= temp_a)
                temp_a -= a;
            else{
                temp_o -= (a - temp_a);
                temp_a = 0;
            }
//-------------------------------------------------------------
            if(b <= temp_b + temp_o) //3 uslov
            {
                //cout<<"treci";
                if(b <= temp_b)
                    temp_b -= b;
                else{
                    temp_o -= (b - temp_b);
                    temp_b = 0;
                }
//------------------------------------------------
                if(ab <= temp_a+temp_b+temp_ab+temp_o) //4 uslov
                {
                    racunaj(n, max_o, max_a, max_b, max_ab, temp_o, temp_a, temp_b);
                    return 0;
                }
            }

        }
    }
    cout<<("nemoguce");

    return 0;
}