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