#include <bits/stdc++.h>

using namespace std;

int n, o, a, b, ab, o1, a1, b1, ab1;
vector<pair <int, int>> par(10005);

/*bool Sortiranje(pair<int, int>& p1, pair<int, int>& p2)
{
    if(p2.second == 0 && p1.second == 0) return true;
    else if(p1.second == 0) return false;
    else if(p2.second == 0) return true;
    if(p1.first == p2.first) return p1.second < p2.second;
    else return p1.first < p2.first;
}
*/
void Izlaz(int s1, int s2, int s3, int s4)
{
    cout << s1 << " " << s2 << " " << s3 << " " << s4 << endl;
}

int main()
{
    cin >> n >> o >> a >> b >> ab;
    o1 = 0; a1 = 0; b1 = 0; ab1 = 0;
    pair <int, int> gas[n];
    for(int i = 0; i < n; i++)
    {
        string s;
        cin >> s >> par[i].second;
        gas[i].second = par[i].second;
        if(s == "O")
        {
            par[i].first = 0;
            gas[i].first = 0;
            o1 +=par[i].second;
        }
        if(s == "A")
        {
            par[i].first = 1;
            gas[i].first = 1;
            a1 +=par[i].second;
        }
        if(s == "B")
        {
            par[i].first = 2;
            gas[i].first = 2;
            b1 +=par[i].second;
        }
        if(s == "AB")
        {
            par[i].first = 3;
            gas[i].first = 3;
            ab1 +=par[i].second;
        }
    }
    //sort(par.begin(), par.end(), Sortiranje);
    if(o >= o1 && o + a + b + ab >= o1 + a1 + b1 + ab1)
    {
        if(a - a1 + b - b1 <= o - o1)
        {
            for(int i = 0; i < n; i++)
            {
                if(gas[i].first == 0)
                {
                    Izlaz(gas[i].second, 0, 0, 0);
                    o1 -= gas[i].second;
                }
                if(gas[i].first == 1)
                {
                    if(a < gas[i].second)
                    {
                        Izlaz(gas[i].second - a, a, 0, 0);
                        a = 0;
                        o -= (gas[i].second - a);
                    }
                    else
                    {
                        Izlaz(0, gas[i].second, 0, 0);
                        a -= gas[i].second;
                    }
                }
                if(gas[i].first == 2)
                {
                    if(b < gas[i].second)
                    {
                        Izlaz(gas[i].second - b, 0, b, 0);
                        b = 0;
                        o -= (gas[i].second - b);
                    }
                    else
                    {
                        Izlaz(0, 0, gas[i].second, 0);
                        b -= gas[i].second;
                    }
                }
                if(gas[i].first == 3)
                {
                    if(ab < gas[i].second)
                    {
                        if(a >= gas[i].second - ab)
                        {
                            Izlaz(0, gas[i].second - ab, 0, ab);
                            ab = 0;
                            a -= gas[i].second - ab;
                        }
                        else if(b >= (gas[i].second - ab) - a)
                        {
                            Izlaz(0, a, (gas[i].second - ab) - a, ab);
                            a = 0;
                            ab = 0;
                        }
                        else
                        {
                            Izlaz(((gas[i].second - ab) - a) - b, a, ((gas[i].second - ab) - a), ab);
                            a = 0;
                            ab = 0;
                            b = 0;
                            o -= (((gas[i].second - ab) - a) - b);
                        }
                    }
                    else
                    {
                        Izlaz(0, 0, 0, gas[i].second);
                        ab -= gas[i].second;
                    }
                }
            }
        }
        else cout << "nemoguce";
    }
    else cout << "nemoguce";
    return 0;
}