#include <bits/stdc++.h>
using namespace std;
int main() {
  char ch;
  string p1, p2, c = "6789TJQKA";
  cin >> ch;
  cin >> p1 >> p2;
  int s1 = c.find(p1[0]);
  int s2 = c.find(p2[0]);
  if ((s1 > s2 && p1[1] == p2[1]) || p1[1] == ch && p2[1] != ch)
    cout << "YES";
  else
    cout << "NO";
}
