#include <bits/stdc++.h>
using namespace std;
int main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  char c;
  cin >> c;
  string s1, s2;
  map<char, int> m1;
  m1['6'] = 0;
  m1['7'] = 1;
  m1['8'] = 2;
  m1['9'] = 3;
  m1['T'] = 4;
  m1['J'] = 5;
  m1['Q'] = 6;
  m1['K'] = 7;
  m1['A'] = 8;
  cin >> s1 >> s2;
  if (m1[s1[0]] > m1[s2[0]] && s1[1] == s2[1])
    cout << "YES";
  else if (s1[1] == c && s2[1] != c)
    cout << "YES";
  else
    cout << "NO";
  return 0;
}
