#include <bits/stdc++.h>
using namespace std;
string card = "6789TJQKA";
int ca[] = {6, 7, 8, 9, 10, 11, 12, 13, 14};
int main() {
  char trump;
  int fir, sec;
  string one, two;
  cin >> trump;
  cin >> one >> two;
  for (int i = 0; i < card.size(); i++)
    if (one[0] == card[i]) fir = ca[i];
  for (int i = 0; i < card.size(); i++)
    if (two[0] == card[i]) sec = ca[i];
  if (one[1] == two[1]) {
    if (fir > sec)
      cout << "YES";
    else
      cout << "NO";
  } else if (one[1] == trump)
    cout << "YES";
  else
    cout << "NO";
  return 0;
}
