#include <bits/stdc++.h>
using namespace std;
const int N = 1e6;
int v[N];
string t = "6789TJQKA";
int main() {
  char c;
  string s1, s2;
  cin >> c >> s1 >> s2;
  if (s1[1] == c && s2[1] != c)
    puts("YES");
  else if (s1[1] == s2[1]) {
    int ii, jj;
    for (int i = 0; i < int(int(t.size())); i++) {
      if (t[i] == s1[0]) ii = i;
      if (t[i] == s2[0]) jj = i;
    }
    if (ii > jj)
      puts("YES");
    else
      puts("NO");
  } else {
    puts("NO");
  }
  return 0;
}
