#include <bits/stdc++.h>
using namespace std;
long long toInt(char x) { return x - '0'; }
long long dao(long long a) { return (a % 10) * 10 + a / 10; }
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  map<char, long long> m;
  m['6'] = 6;
  m['7'] = 7;
  m['8'] = 8;
  m['9'] = 9;
  m['T'] = 10;
  m['J'] = 11;
  m['Q'] = 12;
  m['K'] = 13;
  m['A'] = 14;
  char a;
  string b, c;
  cin >> a >> b >> c;
  if (b[1] == c[1]) {
    if (m[b[0]] > m[c[0]]) {
      cout << "YES";
    } else {
      cout << "NO";
    }
  } else if (a == b[1]) {
    cout << "YES";
  } else {
    cout << "NO";
  }
  return 0;
}
