#include <bits/stdc++.h>
using namespace std;
int compare(char *p);
int main() {
  char trump;
  cin >> trump;
  char c0[2], c1[2];
  cin >> c0[0] >> c1[0];
  cin >> c0[1] >> c1[1];
  if (c1[0] != trump) {
    if (c1[0] != c1[1])
      cout << "NO" << endl;
    else if (compare(c0))
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  } else {
    if (c1[1] == trump) {
      if (compare(c0))
        cout << "YES" << endl;
      else
        cout << "NO" << endl;
    } else
      cout << "YES" << endl;
  }
  return 0;
}
int compare(char *p) {
  int c[2];
  int i = 0;
  while (i < 2) {
    switch (p[i]) {
      case '6':
        c[i] = 1;
        break;
      case '7':
        c[i] = 2;
        break;
      case '8':
        c[i] = 3;
        break;
      case '9':
        c[i] = 4;
        break;
      case 'T':
        c[i] = 5;
        break;
      case 'J':
        c[i] = 6;
        break;
      case 'Q':
        c[i] = 7;
        break;
      case 'K':
        c[i] = 8;
        break;
      case 'A':
        c[i] = 9;
        break;
    }
    i++;
  }
  if (c[0] > c[1])
    return 1;
  else
    return 0;
}
