#include <bits/stdc++.h>
using namespace std;
int main() {
  char rank[9] = {'6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'};
  int i, j;
  char trump;
  string p1, p2;
  scanf("%c", &trump);
  cin >> p1 >> p2;
  if (p1[1] == p2[1]) {
    for (i = 0; i < 9; i++)
      if (p1[0] == rank[i]) break;
    for (j = 0; j < 9; j++)
      if (p2[0] == rank[j]) break;
    if (i > j)
      printf("YES\n");
    else
      printf("NO\n");
  } else if (p1[1] == trump && p2[1] != trump) {
    printf("YES\n");
  } else {
    printf("NO\n");
  }
  return 0;
}
