#include <bits/stdc++.h>
using namespace std;
int fu(char k) {
  switch (k) {
    case '6':
    case '7':
    case '8':
    case '9':
      return (k - 48);
    case 'T':
      return 10;
    case 'J':
      return 11;
    case 'Q':
      return 12;
    case 'K':
      return 13;
    case 'A':
      return 20;
  }
}
int main() {
  char t;
  cin >> t;
  string s1, s2;
  cin >> s1 >> s2;
  if (s1[1] == t && s2[1] != t)
    cout << "YES";
  else if (s1[1] != t && s2[1] == t)
    cout << "NO";
  else if (s1[1] == s2[1]) {
    int ans1 = fu(s1[0]), ans2 = fu(s2[0]);
    if (ans1 > ans2)
      cout << "YES";
    else
      cout << "NO";
  } else
    cout << "NO";
}
