#include <bits/stdc++.h>
using namespace std;
int main() {
  map<char, int> mp;
  mp['S'] = 1;
  mp['H'] = 2;
  mp['D'] = 3;
  mp['C'] = 4;
  mp['6'] = 6;
  mp['7'] = 7;
  mp['8'] = 8;
  mp['9'] = 9;
  mp['T'] = 10;
  mp['J'] = 11;
  mp['Q'] = 12;
  mp['K'] = 13;
  mp['A'] = 14;
  char c;
  string a, b;
  cin >> c;
  cin >> a >> b;
  if ((a[1] == c && b[1] == c) || (a[1] == b[1])) {
    if (mp[a[0]] >= mp[b[0]])
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  } else if (a[1] == c && b[1] != c) {
    cout << "YES" << endl;
  } else
    cout << "NO" << endl;
  return 0;
}
