#include <bits/stdc++.h>
using namespace std;
int main() {
  string rank = "6789TJQKA";
  char trump;
  cin >> trump;
  string s1, s2;
  cin >> s1 >> s2;
  if (s1[1] == trump && s2[1] != trump ||
      (s1[1] == trump && s2[1] == trump &&
       rank.find(s1[0]) > rank.find(s2[0]))) {
    cout << "YES";
    return 0;
  } else if (s2[1] == trump) {
    cout << "NO";
    return 0;
  } else {
    if (s1[1] == s2[1]) {
      int i = rank.find(s1[0]);
      int j = rank.find(s2[0]);
      if (i > j) {
        cout << "YES";
        return 0;
      } else {
        cout << "NO";
        return 0;
      }
    }
  }
  cout << "NO";
  return 0;
}
