#include <bits/stdc++.h>
using namespace std;
int main() {
  map<char, int> mapp;
  const char a[] = "6789TJQKA";
  for (int i = 0; i < 9; i++) {
    mapp[a[i]] = i;
  }
  char suit;
  cin >> suit;
  char fc[2], sc[2];
  cin >> fc >> sc;
  if (fc[1] == suit && sc[1] != suit)
    cout << "YES" << endl;
  else if (fc[1] == sc[1] && mapp[fc[0]] > mapp[sc[0]])
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}
