#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  cin >> s;
  string s1, s2;
  cin >> s1 >> s2;
  string s3 = s1.substr(0, 1);
  string s4 = s1.substr(1, 1);
  string s5 = s2.substr(0, 1);
  string s6 = s2.substr(1, 1);
  if (s3 == "T") {
    s3 = "A";
  } else if (s3 == "J") {
    s3 = "B";
  } else if (s3 == "Q") {
    s3 = "C";
  } else if (s3 == "K") {
    s3 = "D";
  } else if (s3 == "A") {
    s3 = "E";
  }
  if (s5 == "T") {
    s5 = "A";
  } else if (s5 == "J") {
    s5 = "B";
  } else if (s5 == "Q") {
    s5 = "C";
  } else if (s5 == "K") {
    s5 = "D";
  } else if (s5 == "A") {
    s5 = "E";
  }
  if (s4 == s && s6 != s) {
    cout << "YES" << endl;
  } else {
    if (s4 == s && s6 == s && s3 > s5) {
      cout << "YES" << endl;
    } else if (s4 == s6 && s3 > s5) {
      cout << "YES" << endl;
    } else {
      cout << "NO" << endl;
    }
  }
}
