import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static int Sort(String s1, String s2) {
        if (s1.length() > 2) {
            if (s2.length() > 1) {
                if (s2.equals("JOKER")) {
                    return -1;
                }
                return 1;
            }
        }
        if (s2.length() > 2) {
            return -1;
        }
        String[] tmp = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        int a1 = 0;
        int a2 = 0;
        for (int i = 0; i < 13; i++) {
            if (a1 != 0 && a2 != 0) {
                break;
            }
            if (s1.equals(tmp[i])) {
                a1 = i;
            }
            if (s2.equals(tmp[i])) {
                a2 = i;
            }
        }
        return a1 - a2;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String str = in.nextLine();
            List<String> list1 = new ArrayList<>();
            List<String> list2 = new ArrayList<>();
            int i = 0;
            StringBuilder stringBuilder = new StringBuilder();
            while (i < str.length()) {
                char a = str.charAt(i);
                if (a == '-') {
                    list1.add(stringBuilder.toString());
                    stringBuilder = new StringBuilder();
                    i++;
                    break;
                } else if (a == ' ') {
                    list1.add(stringBuilder.toString());
                    stringBuilder = new StringBuilder();
                    i++;
                } else {
                    stringBuilder.append(a);
                    i++;
                }
            }
            while (i < str.length()) {
                char a = str.charAt(i);
                if (a == ' ') {
                    list2.add(stringBuilder.toString());
                    stringBuilder = new StringBuilder();
                    i++;
                } else {
                    stringBuilder.append(a);
                    i++;
                }
            }
            list2.add(stringBuilder.toString());
            int d1 = list1.size();
            int d2 = list2.size();
            if (d1 == d2) {
                if (d1 == 1) {
                    if (Sort(list1.get(0), list2.get(0)) > 0) {
                        System.out.print(list1.get(0));
                        continue;
                    } else if (Sort(list1.get(0), list2.get(0)) < 0) {
                        
                        System.out.print(list2.get(0));
                        continue;
                    }
                } else if (d1 == 2 || d1 == 4 || d1 == 5) {
                    if (Sort(list1.get(0), list2.get(0)) > 0) {
                        for (String a : list1) {
                            System.out.print(a + " ");
                        }
                        continue;
                    } else if (Sort(list1.get(0), list2.get(0)) < 0) {
                        for (String a : list2) {
                            System.out.print(a + " ");
                        }
                        continue;
                    }
                } else if (d1 == 3) {
                    for (int k = 0; k < 3; k++) {
                        if (Sort(list1.get(k), list2.get(k)) > 0) {
                            for (String a : list1) {
                                System.out.print(a + " ");
                            }
                            break;
                        } else if (Sort(list1.get(k), list2.get(k)) < 0) {
                            for (String a : list2) {
                                System.out.print(a + " ");
                            }
                            break;
                        }
                    }
                }
            } else {
                if (wang(list1)) {
                    for (String a : list1) {
                        System.out.print(a + " ");
                    }
                    continue;
                }
                if (wang(list2)) {
                    for (String a : list2) {
                        System.out.print(a + " ");
                    }
                    continue;
                }
                if (d1 == 4) {
                    int m = 0;
                    for (; m < 3; m++) {
                        if (!list1.get(m).equals(list1.get(m + 1))) {
                            System.out.println("ERROR");
                            break;
                        }
                    }
                    if (m == 3) {
                        for (String a : list1) {
                            System.out.print(a + " ");
                        }
                    
                    }
                    continue;
                    
                }
                if (d2 == 4) {
                    int m = 0;
                    for (; m < 3; m++) {
                        if (!list2.get(m).equals(list2.get(m + 1))) {
                            System.out.println("ERROR");
                            break;
                        }
                    }
                    if (m == 3) {
                        for (String a : list2) {
                            System.out.print(a + " ");
                        }
                    
                    }
                    continue;
                }
                System.out.println("ERROR");
            }
        }
    }

    public static boolean wang(List<String> list1) {
        if (list1.get(0).equals("JOKER") || list1.get(0).equals("joker")) {
            if (list1.get(1).equals("JOKER") || list1.get(1).equals("joker")) {
                return true;
            }
        }
        return false;
    }
}