#include <bits/stdc++.h>
using namespace std;
int main() {
  string str;
  cin >> str;
  string a, b;
  cin >> a >> b;
  if (a[1] == str[0] && b[1] != str[0]) {
    cout << "YES" << endl;
  } else if (a[1] != str[0] && b[1] == str[0]) {
    cout << "NO" << endl;
  } else {
    if (a[1] == b[1]) {
      int ww, qq;
      if (a[0] >= '0' && a[0] <= '9')
        ww = a[0] - '0';
      else {
        switch (a[0]) {
          case 'T':
            ww = 10;
            break;
          case 'J':
            ww = 11;
            break;
          case 'Q':
            ww = 12;
            break;
          case 'K':
            ww = 13;
            break;
          case 'A':
            ww = 14;
            break;
        }
      }
      if (b[0] >= '0' && b[0] <= '9')
        qq = b[0] - '0';
      else {
        switch (b[0]) {
          case 'T':
            qq = 10;
            break;
          case 'J':
            qq = 11;
            break;
          case 'Q':
            qq = 12;
            break;
          case 'K':
            qq = 13;
            break;
          case 'A':
            qq = 14;
            break;
        }
      }
      if (ww > qq)
        cout << "YES" << endl;
      else
        cout << "NO" << endl;
    } else
      cout << "NO" << endl;
  }
  return 0;
}
