#include <bits/stdc++.h>
using namespace std;
int main() {
  char te[3], a[5], b[5];
  map<string, int> m1;
  map<string, int> m2;
  m1["S"] = 1, m1["H"] = 2, m1["D"] = 3, m1["C"] = 4;
  m2["6"] = 1, m2["7"] = 2, m2["8"] = 3, m2["9"] = 4, m2["T"] = 5, m2["J"] = 6,
  m2["Q"] = 7, m2["K"] = 8, m2["A"] = 9;
  while (~scanf("%s%s%s", te, a, b)) {
    char a1[5], a2[5], b1[5], b2[5];
    if (a[1] == te[0] && b[1] != te[0])
      printf("YES\n");
    else {
      a1[0] = a[1], a1[1] = '\0';
      a2[0] = a[0], a2[1] = '\0';
      b1[0] = b[1], b1[1] = '\0';
      b2[0] = b[0], b2[1] = '\0';
      if (m1[a1] == m1[b1] && m2[a2] > m2[b2])
        printf("YES\n");
      else
        printf("NO\n");
    }
  }
}
