using System;

namespace Transfuzija
{
    class Program
    {
        static void Main(string[] args)
        {
            int N = Convert.ToInt32(Console.ReadLine());
            string[] ss = Console.ReadLine().Split();
            int O1 = Convert.ToInt32(ss[0]);
            int A1 = Convert.ToInt32(ss[1]);
            int B1 = Convert.ToInt32(ss[2]);
            int AB1 = Convert.ToInt32(ss[3]);
            int A=0;
            int B=0;
            int O=0;
            int AB=0;
            for (int i = 0; i < N; i++)
            {
                string[] s = Console.ReadLine().Split();
                if (s[0] == "O")
                {
                    O = Convert.ToInt32(s[1]);
                }
                if (s[0] == "A")
                {
                    A = Convert.ToInt32(s[1]);
                }
                if (s[0] == "B")
                {
                    B = Convert.ToInt32(s[1]);
                }
                if (s[0] == "AB")
                {
                    AB = Convert.ToInt32(s[1]);
                }
            }
            bool ok = true;
            while (O1 > 0)
            {
                O1--;
                O--;
                if (O1<=0 && O>0)
                {
                    Console.WriteLine("nemoguce");
                    ok = false;
                }
            }
            while ((A1 > 0 || O1 > 0) && ok == true)
            {
                if (A1 <= 0 && O1 > 0)
                {
                    O1--;
                    A--;
                    continue;
                }
                A1--;
                A--;
                if (A>0 && A1<=0 && O1<=0)
                {
                    Console.WriteLine("nemoguce");
                    ok = false;
                }
            }
            while ((B1 > 0 || O1 > 0) && ok == true)
            {
                if (B1 <= 0 && O1 > 0)
                {
                    O1--;
                    B--;
                    continue;
                }
                B1--;
                B--;
                if (B > 0 && B1 <= 0 && O1 <= 0)
                {
                    Console.WriteLine("nemoguce");
                    ok = false;
                }
            }
            while ((AB1 > 0 || A1 > 0 || B1 > 0 || O1 > 0) && ok == true)
            {
                if (AB1 <= 0 && O1 > 0)
                {
                    O1--;
                    AB--;
                    continue;
                }
                if (AB1 <= 0 && A1 > 0)
                {
                    A1--;
                    AB--;
                    continue;
                }
                if (AB1 <= 0 && B1 > 0)
                {
                    B1--;
                    AB--;
                    continue;
                }
                AB1--;
                AB--;
                if (AB > 0 && A1 <= 0 && O1 <= 0 && B1<=0 && AB1<=0)
                {
                    Console.WriteLine("nemoguce");
                    ok = false;
                }
            }
            
        }
    }
}