#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k, i, a, counter = 0, ar[100000];
  map<char, int> mp;
  mp['6'] = 1, mp['7'] = 2, mp['8'] = 3, mp['9'] = 4, mp['T'] = 5;
  mp['J'] = 6, mp['Q'] = 7, mp['K'] = 8, mp['A'] = 9;
  string str1, str2;
  char trump;
  cin >> trump;
  cin >> str1 >> str2;
  if (str1[1] == trump && str1[1] != str2[1])
    cout << "YES" << endl;
  else if (str1[1] == str2[1] && mp[str1[0]] > mp[str2[0]])
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}
