#include <bits/stdc++.h>
using namespace std;
int main() {
  string s, s1, s2;
  cin >> s >> s1 >> s2;
  if (s[0] == s1[1] && s[0] != s2[1]) {
    cout << "YES";
    return 0;
  }
  if (s1[0] == 'T') {
    s1[0] = 10 + 48;
  }
  if (s1[0] == 'J') {
    s1[0] = 11 + 48;
  }
  if (s1[0] == 'Q') {
    s1[0] = 12 + 48;
  }
  if (s1[0] == 'K') {
    s1[0] = 13 + 48;
  }
  if (s1[0] == 'A') {
    s1[0] = 14 + 48;
  }
  if (s2[0] == 'T') {
    s2[0] = 10 + 48;
  }
  if (s2[0] == 'J') {
    s2[0] = 11 + 48;
  }
  if (s2[0] == 'Q') {
    s2[0] = 12 + 48;
  }
  if (s2[0] == 'K') {
    s2[0] = 13 + 48;
  }
  if (s2[0] == 'A') {
    s2[0] = 14 + 48;
  }
  if (s1[1] == s2[1] && s1[0] > s2[0]) {
    cout << "YES";
    return 0;
  }
  cout << "NO";
  return 0;
}
