using System;

namespace Petlja2
{
    class Program
    {
        static void Main(string[] args)
        {
            int N = Convert.ToInt32(Console.ReadLine());
            String ab = Console.ReadLine();
            char[] a = ab.ToCharArray();

            String[] pacGr =new string[N];
            for(int i = 0; i < N; i++)
            {
                pacGr[i]  = Console.ReadLine();
            }

            int[,] Matr = new int[N,4];
            for(int i =0; i<N;i++)
            {
                for(int j= 0; j<4;j++)
                {
                    Matr[i,j] = 0;
                }
            }

            for(int i = 0;i<pacGr.Length; i++)
            {
                if(pacGr[i][0] == 'O')
                {
                    if (a[0] < pacGr[i][2])
                    {
                        Console.WriteLine("nemoguce");
                        return;
                    }
                    else
                    {
                        a[0] = (char)((int)Char.GetNumericValue(a[0]) - (int)Char.GetNumericValue(pacGr[i][2]));
                        Matr[i,0] += a[0];
                    }
                }
                if (pacGr[i][0] == 'A')
                {
                    if (a[2] < 0 || a[0] < 0 || pacGr[i][2]!=0 || (a[0] + a[2] < pacGr[i][2]))
                    {
                        Console.WriteLine("nemoguce");
                        return;
                    }
                    else if(a[2] > pacGr[i][2])
                    {
                        a[2] = (char)((int)Char.GetNumericValue(a[2]) - (int)Char.GetNumericValue(pacGr[i][2]));
                        Matr[i, 2] += a[2];
                    }
                    else if(a[2] <= pacGr[i][2])
                    {     
                        int pom = (Math.Abs(a[2] - pacGr[i][2]));
                        a[2] = (char)0;
                        a[0] = (char)(a[0] - pom);
                        Matr[i, 2] += pom;
                        Matr[i, 0] += a[0];
                    }

                }
                if (pacGr[i][0] == 'B')
                {
                    if ((a[2] < 0 && a[0] < 0 && pacGr[i][2] != 0) || (a[0] + a[2] < pacGr[i][2]))
                    {
                        Console.WriteLine("nemoguce");
                        return;
                    }
                    else if (a[4] > pacGr[i][2])
                    {
                        a[4] = (char)((int)Char.GetNumericValue(a[4]) - (int)Char.GetNumericValue(pacGr[i][2]));
                        Matr[i, 4] += a[4];
                    }
                    else if (a[4] <= pacGr[i][2])
                    {      
                        int pom = (Math.Abs(a[4] - pacGr[i][2]));
                        a[4] = (char)0;
                        a[0] = (char)(a[0] - pom);
                        Matr[i, 4] += pom;
                        Matr[i, 0] += a[0];
                    }
                }
                if (pacGr[i][0] == 'A' && pacGr[i][1] == 'B')
                {
                    if ((a[2] < 0 && a[0] < 0 && a[4] < 0 && a[6] < 0 && pacGr[i][2] != 0) || (a[0] + a[2] + a[4] + a[6] < pacGr[i][2]))
                    {
                        Console.WriteLine("nemoguce");
                        return;
                    }
                    if(pacGr[i][1] < a[6])
                    {
                        a[6] = (char)((int)Char.GetNumericValue(a[6]) - (int)Char.GetNumericValue(pacGr[i][2]));
                        Matr[i, 6] += a[6];
                    }
                    else if(pacGr[i][1] >= a[6])
                    {
                        //pacGr[i][1] = pacGr[i][1] - a[6];
                    }
                }

            }

            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    Console.Write(Matr[i, j]);
                }
                Console.Write("\n");
            }

        }
    }
}