#!/usr/bin/python3
from functools import cmp_to_key
hands = []
try:
while True:
hands.append(input().split(" "))
except EOFError:
pass
alphabet = ["A", "K", "Q", "J", "T", "9", "8", "7", "6", "5", "4", "3", "2"];
def level(a):
if len(set(a)) == 1:
return 6
if len(set(a)) == 2:
if a.count(a[0]) == 1 or a.count(a[0]) == 4:
return 5
return 4
if len(set(a)) == 3:
if a.count(list(set(a))[0]) == 3 or a.count(list(set(a))[1]) == 3 or a.count(list(set(a))[2]) == 3:
return 3
return 2
if len(set(a)) == 5:
return 0
return 1
def compar(a, b):
a = a[0]
b = b[0]
if level(a) > level(b):
return 1
if level(b) > level(a):
return -1
for i in range(len(a)):
if alphabet.index(a[i]) < alphabet.index(b[i]):
return 1
if alphabet.index(b[i]) < alphabet.index(a[i]):
return -1
return 0
hands.sort(key=cmp_to_key(compar))
i = 1
s = 0
for h in hands:
print(h, level(h[0]))
s += int(h[1])*i
i += 1
print(s)
alphabet = ["A", "K", "Q", "T", "9", "8", "7", "6", "5", "4", "3", "2", "J"];
def level(a):
r = set(a)-set(["J"])
if len(r) == 0 or len(r) == 1:
return 6
if len(r) == 5:
return 0
if len(r) == 2:
c2 = a.count("J")+a.count(list(r)[0])
c1 = a.count("J")+a.count(list(r)[1])
if c1 == 4 or c2 == 4:
return 5
return 4
if len(r) == 3:
l = a.count("J")
if l+a.count(list(set(r))[0]) == 3 or l+a.count(list(set(r))[1]) == 3 or l+a.count(list(set(r))[2]) == 3:
return 3
return 2
return 1
def compar(a, b):
a = a[0]
b = b[0]
if level(a) > level(b):
return 1
if level(b) > level(a):
return -1
for i in range(len(a)):
if alphabet.index(a[i]) < alphabet.index(b[i]):
return 1
if alphabet.index(b[i]) < alphabet.index(a[i]):
return -1
return 0
hands.sort(key=cmp_to_key(compar))
i = 1
s = 0
for h in hands:
print(h, level(h[0]))
s += int(h[1])*i
i += 1
print(s)