#include <bits/stdc++.h>
using namespace std;
int x[250];
int main() {
  x['6'] = 1;
  x['7'] = 2;
  x['8'] = 3;
  x['9'] = 4;
  x['T'] = 58;
  x['J'] = 59;
  x['Q'] = 60;
  x['K'] = 61;
  x['A'] = 62;
  string suit;
  char trump, one[2], two[2];
  cin >> trump;
  cin >> one >> two;
  if (one[1] == two[1]) {
    if (x[one[0]] > x[two[0]]) {
      cout << "YES";
      return 0;
    }
  } else {
    if (one[1] == trump) {
      cout << "YES";
      return 0;
    }
  }
  cout << "NO";
  return 0;
}
