#include <bits/stdc++.h>
using namespace std;
int min(int a, int b);
int max(int a, int b);
int main() {
  char a;
  char b[5], c[5];
  int f[200];
  f['6'] = 1;
  f['7'] = 2;
  f['8'] = 3;
  f['9'] = 4;
  f['T'] = 5;
  f['J'] = 6;
  f['Q'] = 7;
  f['K'] = 8;
  f['A'] = 9;
  while (cin >> a) {
    cin >> b >> c;
    bool flag;
    if (b[1] == c[1]) {
      if (f[b[0]] > f[c[0]])
        flag = true;
      else
        flag = false;
    } else {
      if (b[1] == a)
        flag = true;
      else
        flag = false;
    }
    if (flag) {
      printf("YES\n");
    } else {
      printf("NO\n");
    }
  }
  return 0;
}
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
