#include <bits/stdc++.h>
int main() {
  char a[4], b[4], trump, fir, sec, rank_1, rank_2;
  int rr, kk;
  while (scanf("%c", &trump) != EOF) {
    scanf("%s%s", a, b);
    rank_1 = a[0];
    fir = a[1];
    rank_2 = b[0];
    sec = b[1];
    {
      if (isdigit(rank_1))
        rr = rank_1 - 48;
      else if (rank_1 == 'T')
        rr = 10;
      else if (rank_1 == 'J')
        rr = 11;
      else if (rank_1 == 'Q')
        rr = 12;
      else if (rank_1 == 'K')
        rr = 13;
      else if (rank_1 == 'A')
        rr = 14;
    }
    {
      if (isdigit(rank_2))
        kk = rank_2 - 48;
      else if (rank_2 == 'T')
        kk = 10;
      else if (rank_2 == 'J')
        kk = 11;
      else if (rank_2 == 'Q')
        kk = 12;
      else if (rank_2 == 'K')
        kk = 13;
      else if (rank_2 == 'A')
        kk = 14;
    }
    if (fir == sec && rr > kk)
      printf("YES\n");
    else if (fir == trump && sec != trump)
      printf("YES\n");
    else
      printf("NO\n");
    getchar();
  }
  return 0;
}
