#include <bits/stdc++.h>

using namespace std;

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

    int n;
    cin>>n;

    int zalihe[4];

    for (int i=0;i<4;i++)
    {
        cin>>zalihe[i];
    }

    int matrica[n][4];

    int niz_0[n],niz_a[n],niz_b[n],niz_ab[n];
    int broj_0=0,broj_a=0,broj_b=0,broj_ab=0;
    int brojac0[n],brojaca[n],brojacb[n],brojacab[n];

    for (int i=0;i<n;i++)
    {
        string grupa;
        cin>>grupa;

        int jedinica;
        cin>>jedinica;

        if (grupa[0]=='O')
        {
            niz_0[broj_0]=jedinica;
            brojac0[broj_0]=i;
            broj_0++;
        }

        if (grupa[0]=='A' and grupa[1]!='B')
        {
            niz_a[broj_a]=jedinica;
            brojaca[broj_a]=i;
            broj_a++;
        }

        if (grupa[0]=='B')
        {
            niz_b[broj_b]=jedinica;
            brojacb[broj_b]=i;
            broj_b++;
        }

        if (grupa[0]=='A' and grupa[1]=='B')
        {
            niz_ab[broj_ab]=jedinica;
            brojacab[broj_ab]=i;
            broj_ab++;
        }
    }

    bool provera=true;

    for (int i=0;i<broj_0;i++)
    {
        if (niz_0[i]<=zalihe[0])
        {
            matrica[brojac0[i]][0]=niz_0[i];

            matrica[brojac0[i]][1]=0,matrica[brojac0[i]][2]=0,matrica[brojac0[i]][3]=0;

            zalihe[0]-=niz_0[i];
        }

        else
        {
            provera=false;

            break;
        }
    }

    if (provera==false)
    {
        cout<<"nemoguce";
    }

    else
    {
        for (int i=0;i<broj_a;i++)
        {
            if (niz_a[i]<=zalihe[1])
            {
                matrica[brojaca[i]][1]=niz_a[i];
                matrica[brojaca[i]][0]=0, matrica[brojaca[i]][2]=0, matrica[brojaca[i]][3]=0;

                zalihe[1]-=niz_a[i];
            }

            else
            {
                if (niz_a[i]<=zalihe[0]+zalihe[1])
                {
                    matrica[brojaca[i]][1]=zalihe[1];

                    niz_a[i]-=zalihe[1];

                    zalihe[1]=0;

                    matrica[brojaca[i]][0]=niz_a[i];

                    matrica[brojaca[i]][2]=0, matrica[brojaca[i]][3]=0;

                    zalihe[0]-=niz_a[i];
                }

                else
                {
                    provera=false;

                    break;
                }
            }
        }

        if (provera==false)
        {
            cout<<"nemoguce";
        }

        else
        {
            for (int i=0;i<broj_b;i++)
            {
                if (niz_b[i]<=zalihe[2])
                {
                    matrica[brojacb[i]][2]=niz_b[i];
                    matrica[brojacb[i]][0]=0, matrica[brojacb[i]][1]=0, matrica[brojacb[i]][3]=0;

                    zalihe[2]-=niz_b[i];
                }

                else
                {
                    if (niz_b[i]<=zalihe[2]+zalihe[0])
                    {
                        matrica[brojacb[i]][2]=zalihe[2];

                        niz_b[i]-=zalihe[2];

                        zalihe[2]=0;

                        matrica[brojacb[i]][0]=niz_b[i];

                        zalihe[0]-=niz_b[i];

                        matrica[brojacb[i]][1]=0, matrica[brojacb[i]][3]=0;
                    }

                    else
                    {
                        provera=false;

                        break;
                    }
                }
            }

            if (provera==false)
            {
                cout<<"nemoguce";
            }

            else
            {
                for (int i=0;i<broj_ab;i++)
                {
                    if (niz_ab[i]<=zalihe[0]+zalihe[1]+zalihe[2]+zalihe[3])
                    {
                        if (niz_ab[i]<=zalihe[3])
                        {
                            matrica[brojacab[i]][3]=niz_ab[i];

                            matrica[brojacab[i]][0]=0, matrica[brojacab[i]][1]=0, matrica[brojacab[i]][2]=0;

                            zalihe[3]-=niz_ab[i];
                        }

                        else
                        {
                            if (niz_ab[i]<=zalihe[3]+zalihe[2])
                            {
                                matrica[brojacab[i]][3]=zalihe[3];

                                niz_ab[i]-=zalihe[3];

                                zalihe[3]=0;

                                matrica[brojacab[i]][2]=niz_ab[i];

                                zalihe[2]-=niz_ab[i];

                                matrica[brojacab[i]][1]=0, matrica[brojacab[i]][0]=0;
                            }

                            else
                            {
                                if (niz_ab[i]<=zalihe[1]+zalihe[2]+zalihe[3])
                                {
                                    matrica[brojacab[i]][3]=zalihe[3];

                                    niz_ab[i]-=zalihe[3];

                                    zalihe[3]=0;

                                    matrica[brojacab[i]][2]=zalihe[2];

                                    niz_ab[i]-=zalihe[2];

                                    zalihe[2]=0;

                                    matrica[brojacab[i]][1]=niz_ab[i];

                                    zalihe[1]-=niz_ab[i];

                                    matrica[brojacab[i]][0]=0;
                                }

                                else
                                {
                                    if (niz_ab[i]<=zalihe[0]+zalihe[1]+zalihe[2]+zalihe[3])
                                    {
                                        matrica[brojacab[i]][3]=zalihe[3];

                                        niz_ab[i]-=zalihe[3];

                                        zalihe[3]=0;

                                        matrica[brojacab[i]][2]=zalihe[2];

                                        niz_ab[i]-=zalihe[2];

                                        zalihe[2]=0;

                                        matrica[brojacab[i]][1]=zalihe[1];

                                        niz_ab[i]-=zalihe[1];

                                        zalihe[1]=0;

                                        matrica[brojacab[i]][0]=niz_ab[i];

                                        zalihe[0]-=niz_ab[i];
                                    }

                                    else
                                    {
                                        provera=false;

                                        break;
                                    }
                                }
                            }
                        }
                    }
                }

                if (provera==true)
                {
                    cout<<"moguce"<<endl;

                    for (int i1=0;i1<n;i1++)
                    {
                        for (int i2=0;i2<4;i2++)
                        {
                            cout<<matrica[i1][i2]<<" ";
                        }

                        cout<<endl;
                    }
                }

                else
                {
                    cout<<"nemoguce";
                }
            }
        }
    }
}