#include <bits/stdc++.h>
using namespace std;
int main() {
  char trump, frank, fsuit, srank, ssuit;
  cin >> trump;
  scanf("\n%c%c %c%c", &frank, &fsuit, &srank, &ssuit);
  map<char, int> ranks;
  ranks['6'] = 6;
  ranks['7'] = 7;
  ranks['8'] = 8;
  ranks['9'] = 9;
  ranks['T'] = 10;
  ranks['J'] = 11;
  ranks['Q'] = 12;
  ranks['K'] = 13;
  ranks['A'] = 14;
  if (fsuit == trump && ssuit != trump) {
    cout << "YES" << endl;
  } else if (fsuit == ssuit) {
    if (ranks[frank] > ranks[srank])
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  } else
    cout << "NO" << endl;
  return 0;
}
