There is a card game called "Durak", which means "Fool" in Russian. The game is quite popular in the countries that used to form USSR. The problem does not state all the game's rules explicitly — you can find them later yourselves if you want.

To play durak you need a pack of 36 cards. Each card has a suit ("S", "H", "D" and "C") and a rank (in the increasing order "6", "7", "8", "9", "T", "J", "Q", "K" and "A"). At the beginning of the game one suit is arbitrarily chosen as trump. 

The players move like that: one player puts one or several of his cards on the table and the other one should beat each of them with his cards.

A card beats another one if both cards have similar suits and the first card has a higher rank then the second one. Besides, a trump card can beat any non-trump card whatever the cards’ ranks are. In all other cases you can not beat the second card with the first one.

You are given the trump suit and two different cards. Determine whether the first one beats the second one or not.
The first line contains the tramp suit. It is "S", "H", "D" or "C".

The second line contains the description of the two different cards. Each card is described by one word consisting of two symbols. The first symbol stands for the rank ("6", "7", "8", "9", "T", "J", "Q", "K" and "A"), and the second one stands for the suit ("S", "H", "D" and "C").
H
QH 9S
YES
S
8D 6D
YES
C
7H AS
NO
C
KC 9C
YES
D
7D KD
NO
H
7H KD
YES
D
AS AH
NO
H
KH KS
YES
C
9H 6C
NO
C
9H JC
NO
D
TD JD
NO
H
6S 7S
NO
D
7S 8S
NO
S
8H 9H
NO
C
9D TD
NO
H
TC JC
NO
C
JH QH
NO
H
QD KD
NO
D
KS AS
NO
S
AH 6H
YES
H
7D 6D
YES
S
8H 7H
YES
D
9S 8S
YES
S
TC 9C
YES
H
JS TS
YES
S
QD JD
YES
D
KH QH
YES
H
AD KD
YES
H
QS QD
NO
C
TS TH
NO
C
6C 6D
YES
H
8H 8D
YES
S
7D 7S
NO
H
JC JH
NO
H
8H 9C
YES
D
9D 6S
YES
C
JC AH
YES
S
AS KD
YES
S
7S JS
NO
H
TH 8H
YES
S
7S QS
NO
C
KC QC
YES
S
AD 9S
NO
D
7H 8D
NO
H
JC 9H
NO
C
7S AC
NO
C
8C 7C
YES
H
9D 8S
NO
D
AC KS
NO
H
8C QH
NO
S
7S TS
NO
C
AH 6S
NO
S
KS QS
YES
H
AC QC
YES
S
9H 8D
NO
S
TS JS
NO
S
8H 7C
NO
C
AH 6S
NO
S
7S QS
NO
C
AH 6S
NO
S
TS KS
NO
C
TH KH
NO
H
9C 6D
NO
H
9C 8D
NO
H
TH AH
NO
H
TH JH
NO
H
QS 9C
NO
H
KC AC
NO
H
AH KH
YES
H
KS QS
YES
C
AD KS
NO
H
QS 9C
NO
H
9D 7S
NO
D
6D 9S
YES
H
AH KH
YES
H
KC AC
NO
D
8S 6C
NO
S
AC KC
YES
import sys
import math
from sys import stdin, stdout
 
# Most Frequently Used Number Theory Concepts
# SOE(helper function of get gcd)
def sieve(N):
    primeNumbers = [True]*(N+1)
    primeNumbers[0] = False
    primeNumbers[1] = False
    i = 2
    while i*i <= N:
        j = i
        if primeNumbers[j]:
            while j*i <= N:
                primeNumbers[j*i] = False
                j += 1
        i += 1
    return primeNumbers
 
# get prime number form 1 to N
def getPrime(N):
    primes = sieve(N)
    result = []
    for i in range(len(primes)):
        if primes[i]:
            result.append(i)
    return result
 
# factors of n
def factor(N):
    factors = []
    i = 1
    while i*i <= N:
        if N % i == 0:
            factors.append(i)
            if i != N//i:
                factors.append(N//i)
        i += 1
    return sorted(factors)
 
# reduce a string in number under range(1000000007)
def reduceB(b) : 
    # Initialize result 
    mod = 0
    # Calculating mod of b with a 
    # to make b like 0 <= b < a 
    for i in range(0, len(b)) : 
        mod = (mod * 10 + ord(b[i])) % 1000000007 
        # print(b[i], ord(b[i]), mod)
    return mod	 # return modulo 
 
# gcd of two numbers
def gcd(a, b):
    if a < b:
        return gcd(b, a)
    if b == 0:
        return a
    return gcd(b, a % b)
 
# TAKE INPUT
def get_ints_in_variables():
    return map(int, sys.stdin.readline().strip().split())
def get_int(): return int(input())
def get_ints_in_list(): return list(
    map(int, sys.stdin.readline().strip().split()))
def get_list_of_list(n): return [list(
    map(int, sys.stdin.readline().strip().split())) for _ in range(n)]
def get_string(): return sys.stdin.readline().strip()
 
def main():
    # Write Your Code Here
    trump = input()
    rc1 , rc2 =  sys.stdin.readline().strip().split()
    # temp = '6789TJQKA'
    # if rc1[1] == rc2[1]:
    #     print("YES" if temp.find(rc1[0]) > temp.find(rc2[0]) else "NO")
    # else:
    #     print("YES" if rc1[1] == trump else "NO")
    mp = {"6":1, "7":2, "8":3, "9":4, "T":5, "J":6, "Q":7, "K":8, "A":9}
    if rc1[1] == trump and rc2[1] == trump:
        if mp[rc1[0]] > mp[rc2[0]]:
            print("YES")
        else:
            print("NO")
    elif rc1[1] == trump:
        print("YES")
    else:
        print("NO")
    
    
# for printing format
# print("Case #{}: {}".format(t+1, ans))
#  calling main Function
if __name__ == "__main__":
    main()