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