#include <bits/stdc++.h>
int main() {
  char ch[1], s1[5], s2[5];
  int n1, n2;
  while (~scanf("%s%s%s", ch, s1, s2)) {
    if (s1[1] == s2[1]) {
      if (s1[0] == 'T')
        n1 = 10;
      else if (s1[0] == 'J')
        n1 = 11;
      else if (s1[0] == 'Q')
        n1 = 12;
      else if (s1[0] == 'K')
        n1 = 13;
      else
        n1 = s1[0] - 48;
      if (s2[0] == 'T')
        n2 = 10;
      else if (s2[0] == 'J')
        n2 = 11;
      else if (s2[0] == 'Q')
        n2 = 12;
      else if (s2[0] == 'K')
        n2 = 13;
      else
        n2 = s2[0] - 48;
      if (n1 > n2)
        printf("YES\n");
      else
        printf("NO\n");
    } else {
      if (s1[1] == ch[0])
        printf("YES\n");
      else
        printf("NO\n");
    }
  }
  return 0;
}
