#include <bits/stdc++.h>
using namespace std;
int main(void) {
  char tramp;
  cin >> tramp;
  string rank = "6789TJQKA";
  char c1_s, c1_r;
  cin >> c1_r >> c1_s;
  char c2_s, c2_r;
  cin >> c2_r >> c2_s;
  if (c1_s == tramp && c2_s != tramp) {
    cout << "YES" << endl;
    return 0;
  }
  if (c1_s == c2_s && rank.find(c1_r) > rank.find(c2_r)) {
    cout << "YES" << endl;
    return 0;
  }
  cout << "NO" << endl;
}
