#include <bits/stdc++.h>
using namespace std;
int conv(char a) {
  if (a >= '6' and a <= '9') return a - '0';
  switch (a) {
    case 'T':
      return 10;
    case 'J':
      return 11;
    case 'Q':
      return 12;
    case 'K':
      return 13;
    default:
      return 14;
  }
}
int main() {
  char tramp, fSuit, sSuit;
  int fNum, sNum;
  string curr;
  cin >> tramp >> curr;
  fSuit = curr[1];
  fNum = conv(curr[0]);
  cin >> curr;
  sSuit = curr[1];
  sNum = conv(curr[0]);
  if (fSuit == sSuit)
    return printf(fNum > sNum ? "YES" : "NO"), 0;
  else
    return printf(fSuit == tramp ? "YES" : "NO"), 0;
  return 0;
}
