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