#include <bits/stdc++.h>
using namespace std;
char m[100], n[100];
char k;
int main() {
  int flag1, flag2;
  char rank[] = {'6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'};
  cin >> k >> m >> n;
  if (m[1] == n[1]) {
    for (int i = 0; i < 9; i++) {
      if (rank[i] == m[0]) flag1 = i;
      if (rank[i] == n[0]) flag2 = i;
    }
    if (flag1 > flag2) {
      printf("YES\n");
    } else {
      printf("NO\n");
    }
  } else {
    if (m[1] == k) {
      printf("YES\n");
    } else {
      printf("NO\n");
    }
  }
  return 0;
}
