#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:20000000")
using namespace std;
int ri() {
  int x;
  scanf("%d", &x);
  return x;
}
long long rll() {
  long long x;
  scanf("%lld", &x);
  return x;
}
string mas[] = {"6", "7", "8", "9", "T", "J", "Q", "K", "A"};
void solve() {
  string koz;
  cin >> koz;
  string a, b;
  cin >> a >> b;
  int ind1, ind2;
  for (int i = (int)(0); i <= (int)(8); i++) {
    if (a[0] == mas[i][0]) ind1 = i;
    if (b[0] == mas[i][0]) ind2 = i;
  }
  if (a[1] == koz[0] && b[1] == koz[0]) {
    if (ind2 < ind1)
      cout << "YES";
    else
      cout << "NO";
    return;
  }
  if (a[1] == koz[0]) {
    cout << "YES";
    return;
  }
  if (b[1] == koz[0]) {
    cout << "NO";
    return;
  }
  if (a[1] == b[1]) {
    if (ind2 < ind1)
      cout << "YES";
    else
      cout << "NO";
    return;
  }
  cout << "NO";
}
int main() {
  solve();
  return 0;
}
