#include <bits/stdc++.h>
using namespace std;
void yes() {
  cout << "YES";
  exit(0);
}
void no() {
  cout << "NO";
  exit(0);
}
int main() {
  char t;
  cin >> t;
  string s1, s2;
  cin >> s1 >> s2;
  map<char, int> m;
  string temp = "6789TJQKA";
  for (int i = 0; i < temp.length(); i++) m[temp[i]] = i + 1;
  if (s1[1] == s2[1])
    if (m[s1[0]] > m[s2[0]])
      yes();
    else {
    }
  else if (s1[1] == t && s2[1] != t)
    yes();
  cout << "NO";
}
