#include <bits/stdc++.h>
int valid(char ch) {
  return ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') ||
          (ch >= 'A' && ch <= 'Z'));
}
char next() {
  char ch;
  scanf("%c", &ch);
  while (!valid(ch)) scanf("%c", &ch);
  return ch;
}
char converte(char ch) {
  if (ch == 'T')
    ch = '9' + 1;
  else if (ch == 'J')
    ch = '9' + 2;
  else if (ch == 'Q')
    ch = '9' + 3;
  else if (ch == 'K')
    ch = '9' + 4;
  else if (ch == 'A')
    ch = '9' + 5;
  return ch;
}
int main() {
  char trump, r1, s1, r2, s2;
  trump = next();
  r1 = next();
  s1 = next();
  r2 = next();
  s2 = next();
  r1 = converte(r1);
  r2 = converte(r2);
  if (s1 == trump && s2 != trump)
    printf("YES\n");
  else if (s1 != trump && s2 == trump)
    printf("NO\n");
  else if (s1 == s2 && r1 > r2)
    printf("YES\n");
  else
    printf("NO\n");
  return 0;
}
