#include <bits/stdc++.h>
using namespace std;
int main() {
  char c;
  string a, b;
  int q = 0, p = 0;
  cin >> c >> a >> b;
  if (a[0] == 'T') a[0] = 'a';
  if (a[0] == 'J') a[0] = 'b';
  if (a[0] == 'Q') a[0] = 'c';
  if (a[0] == 'K') a[0] = 'd';
  if (a[0] == 'A') a[0] = 'e';
  if (b[0] == 'T') b[0] = 'a';
  if (b[0] == 'J') b[0] = 'b';
  if (b[0] == 'Q') b[0] = 'c';
  if (b[0] == 'K') b[0] = 'd';
  if (b[0] == 'A') b[0] = 'e';
  if (a[1] == c && b[1] != c) {
    p++;
    cout << "YES";
  }
  if (a[1] == b[1]) {
    if (a[0] > b[0]) {
      q++;
      cout << "YES";
    } else {
      cout << "NO";
      q++;
    }
  }
  if (q == 0 && p == 0) cout << "NO";
}
