#include<iostream>
using namespace std;

int main()
{
    int n,i,j;
    cin>>n;
    int a[n][4];
    for(i=0;i<n;i++)
        for(j=0;j<4;j++)
            a[i][j]=0;
    int zaliha[4],kolicina[n];
    for(i=0;i<4;i++)
        cin>>zaliha[i];
    char *krv;
    for(i=0;i<n;i++)
    {
        cin>>krv>>kolicina[i];
        if(krv[0]=='O')
            a[i][0]=kolicina[i];
        else if(krv[1]=='B')
            a[i][3]=kolicina[i];
        else if(krv[0]=='B')
            a[i][2]=kolicina[i];
        else
            a[i][1]=kolicina[i];

    }
    int k=0,zam;
        for(i=0;i<n&&;i++)
        {
            if(k>3)
            {
                cout<<"nemoguce";
                return 0;
            }
            while(a[i][3]==0)
                i++;
            if(zaliha[3-k]>a[i][3])
                zaliha[3-k]-=a[i][3];
            else if(zaliha[3-k]!=0)  
            {
                zam=a[i][3];
                a[i][3]=zaliha[3-k];
                zaliha[3-k]=0;
                k++;
                while(zam!=0&&k=3)
                {
                    if(zaliha[3-k]>zam)
                    {
                        a[i][3-k]=zam;
                        zaliha[3-k]-=zam;
                        zam=0;
                    }
                    else
                    {
                        
                    }
                }
            }
        }
}