#include <bits/stdc++.h>
using namespace std;
bool cmp(char a, char b) {
  if (isdigit(a) && isdigit(b) && a > b) return true;
  if (!isdigit(a) && isdigit(b)) return true;
  if (a == 'A' && b != 'A') return true;
  if (a == 'K' && b != 'A' && b != 'K') return true;
  if (a == 'Q' && (b == 'J' || b == 'T')) return true;
  if (a == 'J' && b == 'T') return true;
  return false;
}
int main(int argc, char* argv[]) {
  char c;
  string a, b;
  cin >> c >> a >> b;
  if (a[1] == c && b[1] != c)
    cout << "YES" << endl;
  else if (a[1] == c && b[1] == c && cmp(a[0], b[0]))
    cout << "YES" << endl;
  else if (a[1] != c && b[1] != c && a[1] == b[1] && cmp(a[0], b[0]))
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}
