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