#include <bits/stdc++.h>
using namespace std;
int main() {
  char ch;
  string s1, s2;
  int S = 1, H = 2, D = 3, C = 4;
  int T = 10, J = 11, Q = 12, K = 13, A = 14;
  cin >> ch >> s1 >> s2;
  if (s1[1] == ch && s2[1] != ch)
    cout << "YES\n";
  else if (s1[1] == s2[1]) {
    if (s1[0] == 'A' && s2[0] != '!A')
      cout << "YES\n";
    else if (s1[0] == 'K' &&
             ((s2[0] == 'Q') || (s2[0] == 'J') || (s2[0] == 'T') ||
              (s2[0] == '9') || (s2[0] == '8') || (s2[0] == '7') ||
              (s2[0] == '6')))
      cout << "YES\n";
    else if (s1[0] == 'Q' &&
             ((s2[0] == 'J') || (s2[0] == 'T') || (s2[0] == '9') ||
              (s2[0] == '8') || (s2[0] == '7') || (s2[0] == '6')))
      cout << "YES\n";
    else if (s1[0] == 'J' &&
             ((s2[0] == 'T') || (s2[0] == '9') || (s2[0] == '8') ||
              (s2[0] == '7') || (s2[0] == '6')))
      cout << "YES\n";
    else if (s1[0] == 'T' && ((s2[0] == '9') || (s2[0] == '8') ||
                              (s2[0] == '7') || (s2[0] == '6')))
      cout << "YES\n";
    else if (s1[0] == '9' &&
             ((s2[0] == '8') || (s2[0] == '7') || (s2[0] == '6')))
      cout << "YES\n";
    else if (s1[0] == '8' && ((s2[0] == '7') || (s2[0] == '6')))
      cout << "YES\n";
    else if (s1[0] == '7' && (s2[0] == '6'))
      cout << "YES\n";
    else
      cout << "NO\n";
  } else
    cout << "NO\n";
}
