#include <bits/stdc++.h>
using namespace std;
int main() {
  string s, ss;
  s = "6789TJQKA";
  ss = "SHDC";
  char t;
  cin >> t;
  string s1, s2;
  cin >> s1 >> s2;
  if (s1[1] == t) {
    if (s2[1] == t) {
      string x, y;
      x += s1[0];
      y += s2[0];
      if (s.find(x) > s.find(y)) {
        cout << "YES";
      } else {
        cout << "NO";
      }
    } else {
      cout << "YES";
    }
  } else {
    if (s2[1] == t) {
      cout << "NO";
    } else {
      string x, y;
      x += s1[0];
      y += s2[0];
      if (s.find(x) > s.find(y) && s1[1] == s2[1]) {
        cout << "YES";
      } else {
        cout << "NO";
      }
    }
  }
  return 0;
}
