#include <bits/stdc++.h>
using namespace std;
int main() {
  char c;
  string a, b;
  long long n = 0, i, j;
  cin >> c;
  cin >> a >> b;
  if (a[1] == c && b[1] != c)
    n = 5;
  else if (a[1] == b[1]) {
    if (a[0] == 'T')
      i = 10;
    else if (a[0] == 'J')
      i = 11;
    else if (a[0] == 'Q')
      i = 12;
    else if (a[0] == 'K')
      i = 13;
    else if (a[0] == 'A')
      i = 14;
    else
      i = a[0] - '0';
    if (b[0] == 'T')
      j = 10;
    else if (b[0] == 'J')
      j = 11;
    else if (b[0] == 'Q')
      j = 12;
    else if (b[0] == 'K')
      j = 13;
    else if (b[0] == 'A')
      j = 14;
    else
      j = b[0] - '0';
    if (i > j) n = 5;
  }
  if (n == 5)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}
