#include <bits/stdc++.h>
using namespace std;
int check(char c) {
  if (c == '6') return 1;
  if ((c == '7')) return 2;
  if (c == '8') return 3;
  if (c == '9') return 4;
  if (c == 'T') return 5;
  if (c == 'J') return 6;
  if (c == 'Q') return 7;
  if (c == 'K') return 8;
  if (c == 'A')
    return 9;
  else
    return 0;
}
int main() {
  string s;
  cin >> s;
  string s1;
  cin >> s1;
  string s2;
  cin >> s2;
  if (s[0] == s1[1]) {
    if (s1[1] != s2[1]) {
      cout << "YES";
    } else {
      int a = check(s1[0]);
      int b = check(s2[0]);
      if (a > b)
        cout << "YES";
      else {
        cout << "NO";
      }
    }
  } else if (s2[1] == s[0]) {
    if (s2[1] != s1[1]) {
      cout << "NO";
    } else {
      int a = check(s1[0]);
      int b = check(s2[0]);
      if (a <= b)
        cout << "NO";
      else {
        cout << "YES";
      }
    }
  } else {
    if (s1[1] != s2[1]) {
      cout << "NO";
    } else {
      int a = check(s1[0]);
      int b = check(s2[0]);
      if (a > b) {
        cout << "YES";
      } else
        cout << "NO";
    }
  }
}
