#include <bits/stdc++.h>
using namespace std;
int a[1000];
string b, c;
char d[10000];
int main() {
  char c1;
  cin >> c1;
  a[(int)c1] = 1;
  d[(int)'6'] = 1;
  d[(int)'7'] = 2;
  d[(int)'8'] = 3;
  d[(int)'9'] = 4;
  d[(int)'T'] = 5;
  d[(int)'J'] = 6;
  d[(int)'Q'] = 7;
  d[(int)'K'] = 8;
  d[(int)'A'] = 9;
  cin >> b >> c;
  if (a[(int)(b[1])] && a[(int)c[1]] == 0)
    cout << "YES";
  else if (a[(int)b[1]] && a[(int)c[1]]) {
    if (d[(int)b[0]] > d[(int)c[0]])
      cout << "YES";
    else
      cout << "NO";
  } else if (a[(int)c[1]])
    cout << "NO";
  else {
    if (b[1] == c[1] && d[(int)b[0]] > d[(int)c[0]])
      cout << "YES";
    else
      cout << "NO";
  }
  return 0;
}
