#include <bits/stdc++.h>
using namespace std;
int main(void) {
  char t;
  string fi, se;
  map<char, int> c;
  map<char, bool> trump;
  cin >> t;
  trump[t] = true;
  cin >> fi >> se;
  c['6'] = 1;
  c['7'] = 2;
  c['8'] = 3;
  c['9'] = 4;
  c['T'] = 5;
  c['J'] = 6;
  c['Q'] = 7;
  c['K'] = 8;
  c['A'] = 9;
  if ((trump[fi[1]] && (fi[1] != se[1])) ||
      ((fi[1] == se[1]) && (c[fi[0]] > c[se[0]])))
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return (0);
}
