#include <bits/stdc++.h>
using namespace std;
int p[256];
int main() {
  for (int i = 0; i <= 60; i++) {
    p[i] = i;
  }
  p[int('T')] = 66;
  p[int('J')] = 67;
  p[int('Q')] = 68;
  p[int('K')] = 69;
  p[int('A')] = 70;
  char a[2], b[2];
  char q;
  cin >> q;
  cin >> a >> b;
  swap(a[0], a[1]);
  swap(b[0], b[1]);
  if (a[0] == b[0]) {
    if (p[(int)a[1]] > p[(int)b[1]])
      cout << "YES";
    else
      cout << "NO";
    return 0;
  }
  if (a[0] == q)
    cout << "YES";
  else
    cout << "NO";
}
