#include <bits/stdc++.h>
int a[200005], k, b[15];
int fact(int a, int b, int c) {
  int i, s = 1, t = 1, j = 1;
  for (i = a; i >= 1; i--) {
    s *= i;
    if (j <= b) {
      t *= j++;
      if (j - 1 == c) k = t;
    }
  }
  s = s / (t * k);
  return s;
}
long long prime(int n) {
  long long s = sqrt(n);
  for (int i = 2; i <= s; i++) {
    if (n % i == 0) return 0;
  }
  return 1;
}
using namespace std;
int main() {
  int n, i, m, su = 0, l = 1, q = 0, qq = 0, r = 1;
  double aa, ab, ac, ad, ar, br, ans;
  string s, ss, sss;
  cin >> s >> ss >> sss;
  map<char, int> mp;
  mp['6'] = 1;
  mp['7'] = 2;
  mp['8'] = 3;
  mp['9'] = 4;
  mp['T'] = 5;
  mp['J'] = 6;
  mp['Q'] = 7;
  mp['K'] = 8;
  mp['A'] = 9;
  if (ss[1] == sss[1]) {
    if (mp[ss[0]] > mp[sss[0]])
      puts("YES");
    else
      puts("NO");
    return 0;
  }
  if (s[0] == ss[1]) {
    puts("YES");
    return 0;
  }
  if (s[0] == sss[1] || ss[1] != sss[1]) {
    puts("NO");
    return 0;
  }
}
