#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

string findMax(const string& line) {     if (line.find("joker JOKER") != string::npos) return "joker JOKER";     int dash = line.find('-');     // 分开两手牌!     string card1 = line.substr(0, dash);     string card2 = line.substr(dash + 1);     // 获得两手牌中的张数!     int card1_cnt = count(card1.begin(), card1.end(), ' ') + 1;     int card2_cnt = count(card2.begin(), card2.end(), ' ') + 1;     // 获取两手牌的各自第一张牌!     string card1_first = card1.substr(0, card1.find(' '));     string card2_first = card2.substr(0, card2.find(' '));     if (card1_first == card2_first) {         // == C++ 的运算符重载!          string str = "345678910JQLA2jokerJOKER";         if (str.find(card1_first) > str.find(card2_first)) {             return card1;         }         return card2;     }     if (card1_cnt == 4) return card1;     else if (card2_cnt == 4) return card2;     return "ERROR";
}

int main() {     string line, res;     while (getline(cin, line)) {         res = findMax(line);         cout << res << endl;     }     return 0;
}