#include <bits/stdc++.h>
using namespace std;
int main() {
  string now = "6789TJQKA";
  int len = now.size();
  char t;
  string a, b;
  cin >> t >> a >> b;
  if (a[1] == t && b[1] != t)
    return cout << "YES", 0;
  else if (a[1] != t && b[1] == t)
    return cout << "NO", 0;
  else {
    if (a[1] == b[1]) {
      for (int i = 0; i < len; i++) {
        if (a[0] == now[i])
          return cout << "NO", 0;
        else if (b[0] == now[i]) {
          if (a[0] != now[i])
            return cout << "YES", 0;
          else
            return cout << "NO", 0;
        }
      }
    } else
      return cout << "NO", 0;
  }
}
