#include <bits/stdc++.h>
using namespace std;
long long int min(long long int a, long long int b) { return (a < b) ? a : b; }
long long int max(long long int a, long long int b) { return (a > b) ? a : b; }
long long int fp(long long int a, long long int b) {
  if (b == 0) return 1;
  long long int x = fp(a, b / 2);
  x = (x * x) % 1000000007;
  if (b & 1) x = (x * a) % 1000000007;
  return x;
}
void c_p_c() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
}
signed main() {
  c_p_c();
  char c;
  cin >> c;
  long long int f = 0, g = 0;
  char c1[3], c2[3];
  cin >> c1 >> c2;
  unordered_map<char, long long int> rank;
  rank['6'] = 1;
  rank['7'] = 2;
  rank['8'] = 3;
  rank['9'] = 4;
  rank['T'] = 5;
  rank['J'] = 6;
  rank['Q'] = 7;
  rank['K'] = 8;
  rank['A'] = 9;
  if (c1[1] == c) f = 1;
  if (c2[1] == c) g = 2;
  if (f > g)
    cout << "YES"
         << "\n";
  else {
    if (c1[1] == c2[1]) {
      if (rank[c1[0]] > rank[c2[0]])
        cout << "YES"
             << "\n";
      else {
        cout << "NO"
             << "\n";
      }
    } else {
      cout << "NO"
           << "\n";
    }
  }
}
