// Arup Guha
// 2/19/09
// Solution to 2007 World Finals Problem A+: Consanguine Calculations

// Note: I am sure there's a much shorter way to do this. I just did it methodically.

import java.util.*;
import java.io.*;

public class blood {
	
	public String alleles;
	public boolean Rh;
	
	public blood(String a, boolean r) {
		alleles = a;
		Rh = r;
	}
	
	public blood(String type) {
		alleles = type.substring(0,type.length()-1);
		Rh = (type.charAt(type.length()-1) == '+');
	}
	
	public boolean equals(blood other) {
		return alleles.equals(other.alleles) && Rh == other.Rh;
	}
	
	// Returns true if something equal to this is in list.
	public boolean withIn(ArrayList<blood> list) {
		for (blood b: list)
			if (this.equals(b))
				return true;
		return false;
	}
	
	public String toString() {
		if (Rh)
			return alleles+"+";
		return alleles+"-";
	}
	
	// Returns a string representation useful for the problem for a list of blood types.
	public static String toString(ArrayList<blood> list) {
		
		// What we print for no types.
		if (list.size() == 0)
			return "IMPOSSIBLE";
			
		// For one type no parens!
		else if (list.size() == 1)
			return list.get(0).toString();
			
		// More than one type.
		else {
			
			String ans = "{";
			
			for (int i=0; i<list.size(); i++) {
				ans = ans + list.get(i).toString();
			
				// Don't add a comma at the end of the list.
				if (i != list.size()-1)
					ans = ans + ", ";
			}
			
			ans = ans + "}";
			return ans;
		}
	}
	
	public ArrayList<blood> getPossibleChildren(blood otherParent) {
		
		// If this is +, both signs are possible...otherwise just - is.
		boolean sign = this.Rh || otherParent.Rh;
		
		// Stores all possible blood types of the child.
		ArrayList<blood> answer = new ArrayList<blood>();
		
		// O can always happen if no parent is AB.
		if (!this.alleles.equals("AB") && !otherParent.alleles.equals("AB")) {
			answer.add(new blood("O", sign));
			if (sign) answer.add(new blood("O", false));
		}
				
		// If either parent is A or AB, A is a possible blood type.
		if (this.alleles.contains("A") || otherParent.alleles.contains("A")) {
			answer.add(new blood("A", sign));
			if (sign) answer.add(new blood("A", false));
		}
			
		// If either parent is A or AB, A is a possible blood type.
		if (this.alleles.contains("B") || otherParent.alleles.contains("B")) {
			answer.add(new blood("B", sign));
			if (sign) answer.add(new blood("B", false));
		}
			
		// First one's AB, the other one's A, B or AB.
		if (this.alleles.equals("AB") && !otherParent.alleles.equals("O")) {
			answer.add(new blood("AB", sign));
			if (sign) answer.add(new blood("AB", false));
		}
		
		// Second one's AB, the one's A or B - Making sure not to add AB twice.
		if (otherParent.alleles.equals("AB") && (this.alleles.equals("A") || this.alleles.equals("B"))) {
			answer.add(new blood("AB", sign));
			if (sign) answer.add(new blood("AB", false));
		}
		
		// First parent is A, second is B.
		if (this.alleles.equals("A") && otherParent.alleles.equals("B")) {
			answer.add(new blood("AB", sign));
			if (sign) answer.add(new blood("AB", false));
		}
		
		// Or the other way around.
		if (this.alleles.equals("B") && otherParent.alleles.equals("A")) {
			answer.add(new blood("AB", sign));
			if (sign) answer.add(new blood("AB", false));
		}
		return answer;
		
	}
	
	public ArrayList<blood> getPossibleParent(blood child) {
		
		String[] allTypes = {"O+","O-","A+","A-","B+","B-","AB+","AB-"};
		
		ArrayList<blood> answer = new ArrayList<blood>();
		
		// Try all types for the mother.
		for (String s: allTypes) {
			blood tryMom = new blood(s);
			
			// List out the possible kids for this mother's blood type with the given dad(this).
			ArrayList<blood> possibleChild = this.getPossibleChildren(tryMom);
					
			// If this works, add it.
			if (child.withIn(possibleChild))
				answer.add(tryMom);		
		}
		
		return answer;
	}
	
	public static void main(String[] args) throws Exception {
		
		Scanner fin = new Scanner(System.in);
		
		String mom, dad, child;
		int caseNum = 1;
		
		mom = fin.next();
		dad = fin.next();
		child = fin.next();
		
		// Loop till end of the data.
		while (!mom.equals("E")) {
			
			// Do the case where the child's blood type is unknown.
			if (child.equals("?")) {
				
				// Store our blood types here.
				blood p1;
				blood p2;
				ArrayList<blood> childList; 
				p1 = new blood(mom.substring(0,mom.length()-1),(mom.charAt(mom.length()-1)=='+'));
				p2 = new blood(dad.substring(0,dad.length()-1),(dad.charAt(dad.length()-1)=='+'));
				childList = p1.getPossibleChildren(p2);
				
				System.out.println("Case "+caseNum+": "+p1+" "+p2+" "+toString(childList));
				
			}
			else if (mom.equals("?")) {
				
				// Store our blood types here.
				blood p2;
				blood c;
				ArrayList<blood> momList; 
				p2 = new blood(dad.substring(0,dad.length()-1),(dad.charAt(dad.length()-1)=='+'));
				c = new blood(child.substring(0,child.length()-1),(child.charAt(child.length()-1)=='+'));
				momList = p2.getPossibleParent(c);
				
				System.out.println("Case "+caseNum+": "+toString(momList)+" "+p2+" "+c);
				
			}
			else {
				
				// Store our blood types here.
				blood p1;
				blood c;
				ArrayList<blood> dadList; 
				p1 = new blood(mom.substring(0,mom.length()-1),(mom.charAt(mom.length()-1)=='+'));
				c = new blood(child.substring(0,child.length()-1),(child.charAt(child.length()-1)=='+'));
				dadList = p1.getPossibleParent(c);
				
				System.out.println("Case "+caseNum+": "+p1+" "+toString(dadList)+" "+c);
			}
			
			// Get next input case.
			mom = fin.next();
			dad = fin.next();
			child = fin.next();
			caseNum++;
		}
	}
}
