#include <bits/stdc++.h>
using namespace std;
int main() {
  string ans = "NO";
  map<char, int> pl;
  pl['6'] = 1;
  pl['7'] = 2;
  pl['8'] = 3;
  pl['9'] = 4;
  pl['T'] = 5;
  pl['J'] = 6;
  pl['Q'] = 7;
  pl['K'] = 8;
  pl['A'] = 9;
  char trp, a, h, _a, _h;
  cin >> trp;
  cin >> a >> h >> _a >> _h;
  if (h == trp && _h != trp) ans = "YES";
  if (h == _h && pl[a] > pl[_a]) ans = "YES";
  cout << ans << endl;
}
