#include <bits/stdc++.h>
using namespace std;
int m[300];
int main() {
  string s, x, y;
  while (cin >> s >> x >> y) {
    m['6'] = 1;
    m['7'] = 2;
    m['8'] = 3;
    m['9'] = 4;
    m['T'] = 5;
    m['J'] = 6;
    m['Q'] = 7;
    m['K'] = 8;
    m['A'] = 9;
    if (x[1] == s[0] && y[1] != s[0])
      cout << "YES" << endl;
    else if (x[1] != s[0] && y[1] == s[0])
      cout << "NO" << endl;
    else if (x[1] == y[1]) {
      if (m[x[0]] > m[y[0]])
        cout << "YES" << endl;
      else
        cout << "NO" << endl;
    } else
      cout << "NO" << endl;
  }
}
