#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define pll pair<ll, ll>
#define mp make_pair
#define faster_cin_cout ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(0)
template <typename T>
inline void read(T &x) {char ch=getchar(); int f=1; x=0; while(!isdigit(ch)){if(ch=='-')f *= -1; ch=getchar();} while(isdigit(ch)) {x = (x<<1) + (x<<3) + (ch^48); ch=getchar();} x*=f;}
ll qpow(ll x, ll y) { ll a=1, b=x; while(y){if(y&0x1) a*=b; b*=b; y>>=1;} return a;}
// 3 4 5 6 7 8 9 10 J Q K A 2 joker JOKER
void print(const vector<string>& p) {
for(int i = 0; i < p.size(); i++) {
if(i < p.size()-1) cout << p[i] << " ";
else cout << p[i] << endl;
}
}
bool cal12345(vector<string>& p1, vector<string>& p2, map<string, int>& t) {
if(t[p1[0]] < t[p2[0]]) print(p2);
else print(p1);
return true;
}
bool cal2(vector<string>& p1, vector<string>& p2, map<string, int>& t) {
if(p1.size() == 2 && p1[0] == "joker" && p1[1] == "JOKER") {
print(p1);
return true;
}
if(p2.size() == 2 && p2[0] == "joker" && p2[1] == "JOKER") {
print(p2);
return true;
}
return false;
}
bool cal4(vector<string>& p1, vector<string>& p2, map<string, int>& t) {
if(p1.size() == 4 && p2.size() == 4) {
if(t[p1[0]] < t[p2[0]]) {print(p2); return true;}
else {print(p1); return true;}
}
if(p1.size() == 4) {print(p1); return true;}
if(p2.size() == 4) {print(p2); return true;}
return false;
}
bool cal(vector<string>& p1, vector<string>& p2, map<string, int>& t) {
int n1 = p1.size(), n2 = p2.size();
if(n1 > 5 || n2 > 5 || n1 < 1 || n2 < 1) return false;
// 判断双方牌数量相同的情况
if(n1 == n2) return cal12345(p1, p2, t);
// 判断存在 joker 和 JOKER 的情况
if(n1 == 2 || n2 == 2) {
if(cal2(p1, p2, t)) return true;
}
// 没有通过上一个case说明没有双王
// 判断存在炸弹的情况
if(n1 == 4 || n2 == 4) return cal4(p1, p2, t);
// 其余情况直接返回false
return false;
}
int main(void)
{
#ifndef ONLINE_JUDGE
ifstream cin("in.txt");
#endif
map<string, int> t;
t["3"] = 3;
t["4"] = 4;
t["5"] = 5;
t["6"] = 6;
t["7"] = 7;
t["8"] = 8;
t["9"] = 9;
t["10"]= 10;
t["J"] = 11;
t["Q"] = 12;
t["K"] = 13;
t["A"] = 14;
t["2"] = 20;
t["joker"] = 100;
t["JOKER"] = 101;
string str, player1, player2;
while(getline(cin, str)) {
stringstream ss(str);
vector<string> p1, p2;
getline(ss, player1, '-');
getline(ss, player2);
stringstream ss1(player1);
while(getline(ss1, str, ' ')) p1.push_back(str);
stringstream ss2(player2);
while(getline(ss2, str, ' ')) p2.push_back(str);
if(!cal(p1, p2, t)) cout << "ERROR" << endl;
}
return 0;
}