#include <bits/stdc++.h>
using namespace std;
int main() {
  string rank;
  rank = "6789TJQKA";
  string t, s;
  cin >> t;
  getline(cin, s);
  getline(cin, s);
  string str1 = s.substr(1, 1), str2 = s.substr(4, 1);
  size_t found = t.find(str1), found2 = t.find(str2);
  if (s.find(t) == 1 && found2 != 0) {
    cout << "YES";
  } else if (found != 0 && found2 == 0) {
    cout << "NO";
  } else if (str1.compare(str2) == 0) {
    if (rank.find(s.substr(0, 1)) > rank.find(s.substr(3, 1)))
      cout << "YES";
    else
      cout << "NO";
  } else
    cout << "NO";
  return 0;
}
