#include <bits/stdc++.h>
using namespace std;
int main() {
  string str = "6789TJQKA";
  char fir[5], sec[5], trump;
  scanf("%c %s %s", &trump, fir, sec);
  if (fir[1] == trump && fir[1] != sec[1])
    printf("YES\n");
  else if (fir[1] != sec[1])
    printf("NO\n");
  else {
    int one = 0, two = 0;
    while (1) {
      if (str[one] == fir[0]) break;
      one++;
    }
    while (1) {
      if (str[two] == sec[0]) break;
      two++;
    }
    one > two ? printf("YES\n") : printf("NO\n");
  }
  return 0;
}
