#include <bits/stdc++.h>
using namespace std;
int main() {
  char tru;
  char a1[3], a2[3];
  cin >> tru >> a1 >> a2;
  int flag = 0;
  int n1, n2;
  if (a1[1] == a2[1]) {
    if (a1[0] == 'T')
      n1 = 10;
    else if (a1[0] == 'J')
      n1 = 11;
    else if (a1[0] == 'Q')
      n1 = 12;
    else if (a1[0] == 'K')
      n1 = 13;
    else if (a1[0] == 'A')
      n1 = 14;
    else
      n1 = a1[0] - '0';
    if (a2[0] == 'T')
      n2 = 10;
    else if (a2[0] == 'J')
      n2 = 11;
    else if (a2[0] == 'Q')
      n2 = 12;
    else if (a2[0] == 'K')
      n2 = 13;
    else if (a2[0] == 'A')
      n2 = 14;
    else
      n2 = a2[0] - '0';
    if (n1 > n2) flag = 1;
  } else if (a1[1] == tru)
    flag = 1;
  if (flag == 1)
    printf("YES\n");
  else
    printf("NO\n");
}
