#include <bits/stdc++.h>
using namespace std;
char T[3];
char C1[3], C2[3];
int getval(char suit) {
  if (suit == 'A') return 1000;
  if (suit == 'K') return 999;
  if (suit == 'Q') return 998;
  if (suit == 'J') return 997;
  if (suit == 'T') return 900;
  return suit;
}
int main() {
  cin >> T;
  cin >> C1 >> C2;
  if (C1[1] == C2[1]) {
    if (getval(C1[0]) > getval(C2[0]))
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  } else if (C1[1] == T[0]) {
    cout << "YES" << endl;
  } else
    cout << "NO" << endl;
  return 0;
}
