#include <bits/stdc++.h>
using namespace std;
int main() {
  char a;
  cin >> a;
  char arr[] = {'6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'};
  map<char, int> m;
  for (int i = 0; i < 9; i++) m.insert(make_pair(arr[i], i));
  string s1, s2;
  cin >> s1 >> s2;
  if (s2[1] == a) {
    if (s1[1] == a) {
      if (m[s1[0]] > m[s2[0]])
        cout << "YES";
      else
        cout << "NO";
    } else {
      cout << "NO";
    }
  } else {
    if (s1[1] == a) {
      cout << "YES";
    } else {
      if (m[s1[0]] > m[s2[0]] && s1[1] == s2[1])
        cout << "YES";
      else
        cout << "NO";
    }
  }
  return 0;
}
