#include <bits/stdc++.h>
using namespace std;
bool mayor(char a[], char b[]) {
  if (a[0] == 'A' &&
      (b[0] == 'T' || b[0] == 'J' || b[0] == 'Q' || b[0] == 'K' ||
       b[0] == '9' || b[0] == '8' || b[0] == '7' || b[0] == '6'))
    return true;
  else if (a[0] == 'K' &&
           (b[0] == 'T' || b[0] == 'J' || b[0] == 'Q' || b[0] == '9' ||
            b[0] == '8' || b[0] == '7' || b[0] == '6'))
    return true;
  else if (a[0] == 'Q' && (b[0] == 'T' || b[0] == 'J' || b[0] == '9' ||
                           b[0] == '8' || b[0] == '7' || b[0] == '6'))
    return true;
  else if (a[0] == 'J' && (b[0] == 'T' || b[0] == '9' || b[0] == '8' ||
                           b[0] == '7' || b[0] == '6'))
    return true;
  else if (a[0] == 'T' &&
           (b[0] == '9' || b[0] == '8' || b[0] == '7' || b[0] == '6'))
    return true;
  else if (a[0] == '9' && (b[0] == '8' || b[0] == '7' || b[0] == '6'))
    return true;
  else if (a[0] == '8' && (b[0] == '7' || b[0] == '6'))
    return true;
  else if (a[0] == '7' && b[0] == '6')
    return true;
  else
    return false;
}
int main() {
  char c[2], a[3], b[3];
  while (scanf("%s%s%s", c, a, b) != EOF) {
    if (a[1] == c[0] && b[1] != c[0])
      printf("YES\n");
    else if (a[1] != c[0] && b[1] == c[0])
      printf("NO\n");
    else if (a[1] == b[1] && mayor(a, b))
      printf("YES\n");
    else
      printf("NO\n");
  }
  return 0;
}
