#include <bits/stdc++.h>
using namespace std;
int main() {
  int rank[128];
  rank['6'] = 0;
  rank['7'] = 1;
  rank['8'] = 2;
  rank['9'] = 3;
  rank['T'] = 4;
  rank['J'] = 5;
  rank['Q'] = 6;
  rank['K'] = 7;
  rank['A'] = 8;
  char c, a[3], b[3];
  scanf("%c%s%s", &c, a, b);
  puts(a[1] == c && b[1] != c || a[1] == b[1] && rank[a[0]] > rank[b[0]]
           ? "YES"
           : "NO");
  return 0;
}
