#include <bits/stdc++.h>
using namespace std;
void solve() {
  char x;
  cin >> x;
  string a, b;
  cin >> a >> b;
  string arr = "6789TJQKA";
  if (a[1] == x && b[1] != x)
    cout << "YES\n";
  else {
    long long c, d;
    for (int i = 0; i < 9; i++) {
      if (arr[i] == a[0]) c = i;
      if (arr[i] == b[0]) d = i;
    }
    if (c > d && a[1] == b[1])
      cout << "YES\n";
    else
      cout << "NO\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long T;
  T = 1;
  for (int t = 0; t < T; t++) {
    solve();
  }
  return 0;
}
