import java.util.HashMap;
import java.util.Scanner;

public class Main2 {
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        HashMap<String,String> rezultati = new HashMap<>();
        HashMap<Integer,String> redosled = new HashMap<>();
        int n = scanner.nextInt();
        int o=0,a=0,b=0,ab=0;
        int O=0,A=0,B=0,AB=0;
        String result = "nemoguce";
        Scanner scanner1 = new Scanner(System.in);
        Scanner scanner2 = new Scanner(System.in);
        String[] inputBag = scanner1.nextLine().split("");
        o = Integer.parseInt(inputBag[0]);a = Integer.parseInt(inputBag[2]);b = Integer.parseInt(inputBag[4]);ab = Integer.parseInt(inputBag[6]);
        for(int i =0; i < n;i++)
        {
            String[] inputLine = scanner2.nextLine().split(" ");
            String tip = inputLine[0];
            int size = Integer.parseInt(inputLine[1]);
            if(tip.equals("A"))
            {
                A += size;
                redosled.put(i,"A");
            }
            else if(tip.equals("O"))
            {
                O += size;
                redosled.put(i,"O");
            }
            else if(tip.equals("B"))
            {
                B += size;
                redosled.put(i,"B");
            }
            else if(tip.equals("AB"))
            {
                AB += size;
                redosled.put(i,"AB");
            }
        }
        String tempResult = "";
        if(O < o)
        {
            o -= O;
            rezultati.put("O",O + " 0 0 0");
        }
        else
        {
            System.out.println(result);
            return;
        }
        if(a +o > A)
        {
            if(a > A)
            {
                a -= A;
                rezultati.put("A","0 " + A + " 0 0");
            }
            else
            {
                int tempa = a;
                A -= a;
                a = 0;
                o -= A;
                String res = A+ " " + tempa + " 0 0";
                rezultati.put("A",res);
            }
        }
        else
        {
            System.out.println(result);
            return;
        }
        if(b +o > B)
        {
            if(b > B)
            {
                b -= B;
                rezultati.put("B","0 0 " + B + " 0");
            }
            else
            {
                int tempb = b;
                B -= b;
                b = 0;
                o -= B;
                rezultati.put("B",B+ " 0 " + tempb + " 0");
            }
        }
        else
        {
            System.out.println(result);
            return;
        }
        if(b +o + ab + a> AB) {
            if (ab > AB) {
                ab -= AB;
                rezultati.put("AB", "0 0 0 " + AB);
            }
            else if (ab + a > AB)
            {
                int tempab = ab;
                AB -= ab;
                ab = 0;
                a -= AB;
                rezultati.put("AB","0 " +AB+ " 0 " + tempab);
            }
            else if (ab + b > AB)
            {
                int tempab = ab;
                AB -= ab;
                ab = 0;
                b -= AB;
                rezultati.put("AB","0 " +AB+ " 0 " + tempab);
            }
            else if (ab + a + b > AB)
            {
                int tempab = ab;
                AB -= ab;
                ab = 0;
                AB -= a;
                a = 0;
                int tempAB = AB;
                b -= AB;
                rezultati.put("AB","0 " +tempAB+ " " + AB+" " + tempab);
            }
            else if (ab + a + b + o > AB)
            {
                int tempab = ab;
                AB -= ab;
                ab = 0;
                AB -= a;
                a = 0;
                int tempAB = AB;
                AB -= b;
                int tempAB2 = AB;
                o -= AB;
                rezultati.put("AB",tempAB + " " +ab+ " " + tempAB2+" " + tempab);
            }
        }

        else
        {
            System.out.println(result);
            return;
        }
        result = "";
        for(int i = 0; i < redosled.size();i++)
        {
            result += rezultati.get(redosled.get(i)) + "\n";
        }
        System.out.println(result);
    }
}
import java.util.HashMap;
import java.util.Scanner;

public class Main2 {
    public static void main(String[] args)
    {
        Scanner scanner = new Scanner(System.in);
        HashMap<String,String> rezultati = new HashMap<>();
        HashMap<Integer,String> redosled = new HashMap<>();
        int n = scanner.nextInt();
        int o=0,a=0,b=0,ab=0;
        int O=0,A=0,B=0,AB=0;
        String result = "nemoguce";
        Scanner scanner1 = new Scanner(System.in);
        Scanner scanner2 = new Scanner(System.in);
        String[] inputBag = scanner1.nextLine().split("");
        o = Integer.parseInt(inputBag[0]);a = Integer.parseInt(inputBag[2]);b = Integer.parseInt(inputBag[4]);ab = Integer.parseInt(inputBag[6]);
        for(int i =0; i < n;i++)
        {
            String[] inputLine = scanner2.nextLine().split(" ");
            String tip = inputLine[0];
            int size = Integer.parseInt(inputLine[1]);
            if(tip.equals("A"))
            {
                A += size;
                redosled.put(i,"A");
            }
            else if(tip.equals("O"))
            {
                O += size;
                redosled.put(i,"O");
            }
            else if(tip.equals("B"))
            {
                B += size;
                redosled.put(i,"B");
            }
            else if(tip.equals("AB"))
            {
                AB += size;
                redosled.put(i,"AB");
            }
        }
        String tempResult = "";
        if(O < o)
        {
            o -= O;
            rezultati.put("O",O + " 0 0 0");
        }
        else
        {
            System.out.println(result);
            return;
        }
        if(a +o > A)
        {
            if(a > A)
            {
                a -= A;
                rezultati.put("A","0 " + A + " 0 0");
            }
            else
            {
                int tempa = a;
                A -= a;
                a = 0;
                o -= A;
                String res = A+ " " + tempa + " 0 0";
                rezultati.put("A",res);
            }
        }
        else
        {
            System.out.println(result);
            return;
        }
        if(b +o > B)
        {
            if(b > B)
            {
                b -= B;
                rezultati.put("B","0 0 " + B + " 0");
            }
            else
            {
                int tempb = b;
                B -= b;
                b = 0;
                o -= B;
                rezultati.put("B",B+ " 0 " + tempb + " 0");
            }
        }
        else
        {
            System.out.println(result);
            return;
        }
        if(b +o + ab + a> AB) {
            if (ab > AB) {
                ab -= AB;
                rezultati.put("AB", "0 0 0 " + AB);
            }
            else if (ab + a > AB)
            {
                int tempab = ab;
                AB -= ab;
                ab = 0;
                a -= AB;
                rezultati.put("AB","0 " +AB+ " 0 " + tempab);
            }
            else if (ab + b > AB)
            {
                int tempab = ab;
                AB -= ab;
                ab = 0;
                b -= AB;
                rezultati.put("AB","0 " +AB+ " 0 " + tempab);
            }
            else if (ab + a + b > AB)
            {
                int tempab = ab;
                AB -= ab;
                ab = 0;
                AB -= a;
                a = 0;
                int tempAB = AB;
                b -= AB;
                rezultati.put("AB","0 " +tempAB+ " " + AB+" " + tempab);
            }
            else if (ab + a + b + o > AB)
            {
                int tempab = ab;
                AB -= ab;
                ab = 0;
                AB -= a;
                a = 0;
                int tempAB = AB;
                AB -= b;
                int tempAB2 = AB;
                o -= AB;
                rezultati.put("AB",tempAB + " " +ab+ " " + tempAB2+" " + tempab);
            }
        }

        else
        {
            System.out.println(result);
            return;
        }
        result = "";
        for(int i = 0; i < redosled.size();i++)
        {
            result += rezultati.get(redosled.get(i)) + "\n";
        }
        System.out.println(result);
    }
}