#include <iostream>

using namespace std;
struct Ver{
string cs;
int sz;
};
struct Valami{
string o1,a1,b1,ab1;
};
int main()
{
    int o,a,b,ab,i,n;
    cin>>n;
    Ver ver[n];
    cin>>o;
    cin>>a;
    cin>>b;
    cin>>ab;
    cin.get();
    Valami valami;
    valami.o1= "O";
    valami.a1= "A";
    valami.b1= "B";
    valami.ab1= "AB";
    int x=0,y=0,z=0,v=0;
    for(i=0;i<n;i++)
    {
        getline(cin, ver[i].cs);
        cin>>ver[i].sz;
        cin.get();

        if(ver[i].cs==valami.o1)
        {
            x=x+ver[i].sz;
        }
        else if(ver[i].cs==valami.a1)
        {
            y=y+ver[i].sz;
        }
        if(ver[i].cs==valami.b1)
        {
            z=z+ver[i].sz;
        }
        if(ver[i].cs==valami.ab1)
        {
            v=v+ver[i].sz;
        }

    }
    int db=0;
       if(x>o)
        db++;
       else if ((x<=o)&&(x!=0)&&(x!=y)&&(x!=z)&&(x!=v))
       {
           o=o-x;
           cout<<abs(x)<<0<<0<<0<<endl;
       }

       if((y<=a)&&(y!=0))
       {
           a=a-y;
           cout<<0<<y<<0<<0<<endl;
       }
       else if((y>a)&&(y!=0))
       {
           y=a-y;
           if(o>abs(y))
            {
                o=o-y;
                cout<<abs(y)<<a<<0<<0<<endl;
            }
           if(o<abs(y))
            db++;

       }

       if((z<=b)&&(z!=0))
       {
           b=b-z;
           cout<<0<<0<<z<<0<<endl;
       }
       else if((z>b)&&(z!=0))
       {
           z=b-z;
           if(o>abs(z))
            {
                o=o-z;
                cout<<abs(z)<<0<<b<<0<<endl;
            }
           if(o<abs(z))
            db++;
       }

       if(v<=ab)
       {
           ab=ab-v;
           cout<<0<<0<<0<<v<<endl;
       }
       else if(v>ab)
       {
           v=ab-v;
           if(o>abs(v))
            {
                o=o-v;
                cout<<abs(v)<<0<<0<<ab<<endl;
            }
           if(o<abs(v))
            db++;
       }
       if(db!=0)
        cout<<"nemoguce";
    return 0;
}