using System;

namespace prviKrugDrugi
{
    class Program
    {
        struct s
        {
            public int k;
            public string grupa;
        }
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            bool l = true;

            #region unos
            string[] unos = Console.ReadLine().Split();

            s[] g = new s[4];

            g[0].grupa = "O";
            g[1].grupa = "A";
            g[2].grupa = "B";
            g[3].grupa = "AB";

            for (int i = 0; i < 4; i++)
            {
                g[i].k = Convert.ToInt32(unos[i]);
            }

            s[] p = new s[n];

            for (int i = 0; i < n; i++)
            {
                unos = Console.ReadLine().Split();
                p[i].grupa = unos[0];
                p[i].k = Convert.ToInt32(unos[1]);
            } 
            #endregion

            int[,] mat = new int[n, 4];

            for (int i = 0; i < n; i++)
            {
                string t = p[i].grupa;

                if (t == g[0].grupa)
                {
                    if (g[0].k >= p[i].k)
                    {
                        g[0].k -= p[i].k;
                        mat[i, 0] = p[i].k;
                        mat[i, 1] = 0;
                        mat[i, 2] = 0;
                        mat[i, 3] = 0;
                    }
                    else {
                        Console.WriteLine("nemoguce");
                        l = false;
                        break;
                    }
                }
                else if (t == g[1].grupa)
                {
                    if (g[1].k >= p[i].k)
                    {
                        g[1].k -= p[i].k;
                        mat[i, 0] = 0;
                        mat[i, 1] = p[i].k;
                        mat[i, 2] = 0;
                        mat[i, 3] = 0;
                    }
                    else if ((g[1].k + g[0].k) >= p[i].k)
                    {
                        mat[i, 0] = p[i].k - g[1].k;
                        mat[i, 1] = g[1].k;
                        mat[i, 2] = 0;
                        mat[i, 3] = 0;
                        g[0].k += g[1].k - p[i].k;
                        g[1].k = 0;
                    }
                    else
                    {
                        Console.WriteLine("nemoguce");
                        l = false;
                        break;
                    }
                }
                else if (t == g[2].grupa)
                {
                    if (g[2].k >= p[i].k)
                    {
                        g[2].k -= p[i].k;
                        mat[i, 0] = 0;
                        mat[i, 1] = 0;
                        mat[i, 2] = p[i].k;
                        mat[i, 3] = 0;
                    }
                    else if ((g[2].k + g[0].k) >= p[i].k)
                    {
                        mat[i, 0] = p[i].k - g[2].k;
                        mat[i, 1] = 0;
                        mat[i, 2] = p[2].k;
                        mat[i, 3] = 0;
                        g[0].k += g[2].k - p[i].k;
                        g[2].k = 0;
                    }
                    else
                    {
                        Console.WriteLine("nemoguce");
                        l = false;
                        break;
                    }
                }
                else {
                    if (g[3].k >= p[i].k)
                    {
                        g[3].k -= p[i].k;
                        Console.WriteLine("{0} {1} {2} {3}", 0, 0, 0, p[i].k);
                    }
                    else if ((g[3].k + g[2].k) >= p[i].k)
                    {
                        mat[i, 0] = 0;
                        mat[i, 1] = 0;
                        mat[i, 2] = p[i].k - g[3].k;
                        mat[i, 3] = p[3].k;
                        g[2].k += g[3].k - p[i].k;
                        g[3].k = 0;
                    }
                    else if ((g[3].k + g[1].k) >= p[i].k)
                    {
                        mat[i, 0] = 0;
                        mat[i, 1] = p[i].k - g[3].k;
                        mat[i, 2] = 0;
                        mat[i, 3] = p[3].k;
                        g[1].k += g[3].k - p[i].k;
                        g[3].k = 0;
                    }
                    else if ((g[3].k + g[0].k) >= p[i].k)
                    {
                        mat[i, 0] = p[i].k - g[3].k;
                        mat[i, 1] = 0;
                        mat[i, 2] = 0;
                        mat[i, 3] = p[3].k;
                        g[0].k += g[3].k - p[i].k;
                        g[3].k = 0;
                    }
                    else {
                        Console.WriteLine("nemoguce");
                        l = false;
                        break;
                    }
                }
            }
            #region ispis
            if (l)
            {
                Console.WriteLine("moguce je");
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < 4; j++)
                    {
                        Console.Write(mat[i, j] + " ");
                    }
                    Console.WriteLine();
                }
            } 
            #endregion
        }
    }
}