#include <bits/stdc++.h>
using namespace std;
char trump;
string first;
string second;
int main() {
  std::cin >> trump;
  cin >> first >> second;
  char no1 = first[0];
  char no2 = second[0];
  char suit1 = first[1];
  char suit2 = second[1];
  unordered_map<char, int> suitss;
  suitss['6'] = 6;
  suitss['7'] = 7;
  suitss['8'] = 8;
  suitss['9'] = 9;
  suitss['T'] = 101;
  suitss['J'] = 102;
  suitss['Q'] = 103;
  suitss['K'] = 104;
  suitss['A'] = 105;
  if (suit1 == trump) {
    if (suit2 == trump) {
      if (suitss[no1] > suitss[no2]) {
        cout << "YES";
        return 0;
      } else {
        cout << "NO";
        return 0;
      }
    } else {
      cout << "YES";
      return 0;
    }
  } else {
    if (suit1 == suit2) {
      if (suitss[no1] > suitss[no2]) {
        cout << "YES";
        return 0;
      } else {
        cout << "NO";
        return 0;
      }
    }
  }
  cout << "NO";
  return 0;
}
