#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=(int)1e4+10;
ll a[maxn][5];
int n;
map<string,int>m;
map<string,int>ind;
pair<string,int>pp[maxn];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ind["O"]=0;
    ind["A"]=1;
    ind["B"]=2;
    ind["AB"]=3;
    cin >> n;
    cin >> m["O"] >> m["A"] >> m["B"] >> m["AB"];
    for (int i=0; i<n; i++)
    {
        string s;
        cin >> s;
        int p;
        cin >> p;
        pp[i]=make_pair(s,p);
    }
    for (int i=0; i<n; i++)
    {
        if (pp[i].first!="O") continue;
        if (m["O"]-pp[i].second<0)
        {
            cout << "nemoguce";
            return 0;
        }
        m["O"]-=pp[i].second;
        a[i][ind["O"]]+=pp[i].second;
    }
    for (int i=0; i<n; i++)
    {
        if (pp[i].first!="A") continue;
        if (m["A"]-pp[i].second<0)
        {
            a[i][ind["A"]]+=m["A"];
            pp[i].second-=m["A"];
            m["A"]=0;
            if (m["O"]-pp[i].second<0)
            {
                cout << "nemoguce";
                return 0;
            }
            m["O"]-=pp[i].second;
            a[i][ind["O"]]+=pp[i].second;
            continue;
        }
        m["A"]-=pp[i].second;
        a[i][ind["A"]]+=pp[i].second;
    }
    for (int i=0; i<n; i++)
    {
        if (pp[i].first!="B") continue;
        if (m["B"]-pp[i].second<0)
        {
            a[i][ind["B"]]+=m["B"];
            pp[i].second-=m["B"];
            m["B"]=0;
            if (m["O"]-pp[i].second<0)
            {
                cout << "nemoguce";
                return 0;
            }
            m["O"]-=pp[i].second;
            a[i][ind["O"]]+=pp[i].second;
            continue;
        }
        m["B"]-=pp[i].second;
        a[i][ind["B"]]+=pp[i].second;
    }
    for (int i=0; i<n; i++)
    {
        if (pp[i].first!="AB") continue;
        if (m["AB"]-pp[i].second<0)
        {
            a[i][ind["AB"]]+=m["AB"];
            pp[i].second-=m["AB"];
            m["AB"]=0;
            if (m["B"]-pp[i].second<0)
            {
                a[i][ind["B"]]+=m["B"];
                pp[i].second-=m["B"];
                m["B"]=0;
                if (m["A"]-pp[i].second<0)
                {
                    a[i][ind["A"]]+=m["A"];
                    pp[i].second-=m["A"];
                    m["A"]=0;
                    if (m["O"]-pp[i].second<0)
                    {
                        cout << "nemoguce";
                        return 0;
                    }
                    m["O"]-=pp[i].second;
                    a[i][ind["O"]]+=pp[i].second;
                    continue;
                }
                m["A"]-=pp[i].second;
                a[i][ind["A"]]+=pp[i].second;
                continue;
            }
            m["B"]-=pp[i].second;
            a[i][ind["B"]]+=pp[i].second;
            continue;
        }
        m["AB"]-=pp[i].second;
        a[i][ind["AB"]]+=pp[i].second;
        continue;
    }
    cout << "moguce\n";
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<4; j++)
            cout << a[i][j] << " ";
        cout << "\n";
    }
    return 0;
}