#include <iostream>
#include <string>
#include <vector>

struct kk {
    int e, index;
    kk(int _e, int _index) {
        e = _e;
        index = _index;
    }
};

int main()
{
    int n;
    std::cin >> n;
    int o, a, b, ab;
    std::cin >> o >> a >> b >> ab;
    std::vector<kk> _o;
    std::vector<kk> _a;
    std::vector<kk> _b;
    std::vector<kk> _ab;

    std::vector<int> temp (4, 0);
    std::vector<std::vector<int>> res(n, temp);

    for (int i = 0; i < n; i++) {
        std::string t;
        std::cin >> t;
        int tt;
        std::cin >> tt;
        kk kkk(tt, i);
        switch (t[0]) {
        case 'O':
            _o.push_back(kkk);
            break;
        case 'B':
            _b.push_back(kkk);
            break;
        case 'A':
            if (t.length() == 1) {
                _a.push_back(kkk);
            }
            else {
                _ab.push_back(kkk);
            }
            break;
        }
    }

    for (int i = 0; i < _o.size(); i++) {
        if (o >= _o[i].e) {
            o -= _o[i].e;
            res[_o[i].index][0] += _o[i].e;
        }
        else {
            std::cout << "nemoguce" << std::endl;
            return 0;
        }
    }

    for (int i = 0; i < _a.size(); i++) {
        a -= _a[i].e;
        if (a >= 0) {
            res[_a[i].index][1] += _a[i].e;
        }
        else {
            int m = abs(a);
            a = 0;

            o -= m;

            if (o < 0) {
                std::cout << "nemoguce" << std::endl;
                return 0;
            }
            else {
                res[_a[i].index][0] += m;
            }
        }
    }

    for (int i = 0; i < _b.size(); i++) {
        b -= _b[i].e;
        if (b >= 0) {
            res[_b[i].index][2] += _b[i].e;
        }
        else {
            int m = abs(b);
            b = 0;

            o -= m;

            if (o < 0) {
                std::cout << "nemoguce" << std::endl;
                return 0;
            }
            else {
                res[_b[i].index][0] += m;
            }
        }
    }

    for (int i = 0; i < _ab.size(); i++) {
        ab -= _ab[i].e;
        if (ab >= 0) {
            res[_ab[i].index][3] += _ab[i].e;
        }
        else {
            res[_ab[i].index][3] += ab + _ab[i].e;
            int m = abs(ab);
            ab = 0;
            a -= m;
            
            if (a < 0) {
                res[_ab[i].index][1] += a+m;
                m = abs(a);
                a = 0;
                
                b -= m;

                if (b < 0) {
                    res[_ab[i].index][2] += b + m;
                    m = abs(b);
                    b = 0;
                    o -= m;

                    if (o < 0) {
                        std::cout << "nemoguce" << std::endl;
                        return 0;
                    }
                    else {
                        res[_ab[i].index][0] += m;
                    }
                }
                else {
                    res[_ab[i].index][2] += m;
                }
            }
            else {
                res[_ab[i].index][1] += m;
            }
        }
    }
    std::cout << "moguce" << std::endl;
    for (int i = 0; i < res.size(); i++) {
        for (int j = 0; j < res[i].size(); j++) {
            std::cout << res[i][j] << " ";
        }
        std::cout << std::endl;
    }
}