#include <bits/stdc++.h>
using namespace std;
int main() {
  char tramp_suit;
  string card1, card2, s = "6789TJQKA";
  int rank1 = 0, rank2 = 0;
  cin >> tramp_suit >> card1 >> card2;
  for (int i = 0; s[i]; i++) {
    if (s[i] == card1[0]) rank1 = i;
    if (s[i] == card2[0]) rank2 = i;
  }
  if ((card1[1] == tramp_suit && card2[1] != tramp_suit) ||
      (card1[1] == card2[1] && rank1 > rank2))
    cout << "YES\n";
  else
    cout << "NO\n";
  return 0;
}
