#include <bits/stdc++.h>
using namespace std;
int main() {
  char S;
  cin >> ws >> S;
  char card[2][2];
  for (int i = 0; i < 2; ++i) {
    cin >> ws >> card[i][0] >> card[i][1];
    card[i][0] = strchr("6789TJQKA", card[i][0]) - "6789TJQKA";
  }
  if ((card[0][1] == card[1][1] && card[0][0] > card[1][0]) ||
      (card[0][1] == S && card[1][1] != S))
    printf("YES\n");
  else
    printf("NO\n");
  return 0;
}
