#include <iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;
    int zaliheO,zaliheA,zaliheB,zaliheAB;
    cin >> zaliheO>>zaliheA>>zaliheB>>zaliheAB;
    string grupa[n];
    int jedinica[n];
    int dodO=0,dodA=0,dodB=0,dodAB=0;
    for(int i=0;i<n;i++)
    cin >> grupa[i] >> jedinica[i];
   for(int i=0;i<n;i++){
    if(grupa[i]=="0"){
            if(zaliheO>0){
        zaliheO =zaliheO - 1;
        dodO+=1;
        cout<< dodO;
        cout<< dodA;
        cout<< dodB;
        cout<< dodAB<<endl;
        }
        }
    if(grupa[i]=="A"){
        if(zaliheA>0)
        if(zaliheA>zaliheO){
            zaliheA-=1;
            dodA +=1;
            cout<< dodO;
        cout<< dodA;
        cout<< dodB;
        cout<< dodAB<<endl;
            }
        else
        if(zaliheO>0){
            zaliheO-=1;
            dodO +=1;
            cout<< dodO;
        cout<< dodA;
        cout<< dodB;
        cout<< dodAB<<endl;
            }
            }
    if(grupa[i]=="B")
    if(zaliheB>0)
            if(zaliheB>zaliheO){
            zaliheB-=1;
            dodB+=1;
            cout<< dodO;
        cout<< dodA;
        cout<< dodB;
        cout<< dodAB<<endl;

            }
        else
        if(zaliheO>0){
            zaliheO-=1;
            dodO+=1;
            cout<< dodO;
        cout<< dodA;
        cout<< dodB;
        cout<< dodAB<<endl;
            }

    if(grupa[i]=="AB")
        {
        if(zaliheAB>0)
        if(zaliheAB==max(zaliheAB,max(zaliheB,max(zaliheA,zaliheO))))
            {
            zaliheAB-=1;
            dodAB+=1;
            cout<< dodO;
        cout<< dodA;
        cout<< dodB;
            cout<< dodAB<<endl;
            }
            else
                if(zaliheB>0)
            if(zaliheB == max(zaliheB,max(zaliheA,zaliheO)) )
                {
            zaliheB-=1;
            dodB+=1;
           cout<< dodO;
        cout<< dodA;
        cout<< dodB;
        cout<< dodAB<<endl;

                }
            else
            if(zaliheA>0)
                if(zaliheA == max(zaliheA,zaliheO)){
                zaliheA -=1;
                dodA +=1;
                cout<< dodO;
        cout<< dodA;
        cout<< dodB;
        cout<< dodAB<<endl;
                }
                else
                if(zaliheO>0){
                zaliheO -=1;
                dodO+=1;
                cout<< dodO;
        cout<< dodA;
        cout<< dodB;
        cout<< dodAB<<endl;
                }
            else

                cout<<"nemoguce"<< endl;

        }
}

return 0;

}