#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  char tr;
  cin >> tr;
  string s1, s2;
  cin >> s1 >> s2;
  vector<char> v1{'6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'};
  if (tr != s2[1] && s1[1] == tr) {
    cout << "YES";
    return 0;
  }
  if (tr != s1[1] && s2[1] == tr) {
    cout << "NO";
    return 0;
  }
  if (s2[1] == s1[1]) {
    find(v1.begin(), v1.end(), s1[0]) - find(v1.begin(), v1.end(), s2[0]) > 0
        ? cout << "YES"
        : cout << "NO";
    return 0;
  }
  cout << "NO";
}
