#include <bits/stdc++.h>
void rep(std::string& x) {
  switch (x[0]) {
    case 'T':
      x[0] = 'a';
      break;
    case 'J':
      x[0] = 'b';
      break;
    case 'Q':
      x[0] = 'c';
      break;
    case 'K':
      x[0] = 'd';
      break;
    case 'A':
      x[0] = 'e';
      break;
  }
}
int main() {
  std::string s, a, b;
  std::cin >> s >> a >> b;
  if (a[1] == s[0] && b[1] != s[0]) {
    std::cout << "YES" << std::endl;
  } else if (a[1] != b[1]) {
    std::cout << "NO" << std::endl;
  } else {
    rep(a);
    rep(b);
    if (a > b) {
      std::cout << "YES" << std::endl;
    } else {
      std::cout << "NO" << std::endl;
    }
  }
}
