#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2 * 1000 * 100 + 17;
int n;
char c;
string s, x, t[] = {"6", "7", "8", "9", "T", "J", "Q", "K", "A"};
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> c >> s >> x;
  if (s[1] == c and x[1] != c) {
    cout << "YES";
  } else if (s[1] != x[1]) {
    cout << "NO";
  } else {
    int t1, t2;
    for (int i = 0; i < 9; i++) {
      if (t[i][0] == s[0]) t1 = i;
      if (t[i][0] == x[0]) t2 = i;
    }
    if (t1 > t2)
      cout << "YES";
    else
      cout << "NO";
  }
  return 0;
}
