#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e6;
long long a[100000 + 5];
bool vis[100000 + 5];
char c[] = {'6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'};
int main() {
  long long n;
  string s, s1, s2;
  cin >> s >> s1 >> s2;
  if (s1[1] == s[0]) {
    if (s2[1] != s[0])
      cout << "YES" << endl;
    else {
      int i = 0;
      for (i = 0; i < 9; i++) {
        if (s1[0] == c[i]) break;
      }
      int j = 0;
      for (j = 0; j < 9; j++) {
        if (s2[0] == c[j]) break;
      }
      if (i > j)
        cout << "YES" << endl;
      else
        cout << "NO" << endl;
    }
  } else {
    if (s2[1] == s[0])
      cout << "NO" << endl;
    else if (s1[1] == s2[1]) {
      int i = 0;
      for (i = 0; i < 9; i++) {
        if (s1[0] == c[i]) break;
      }
      int j = 0;
      for (j = 0; j < 9; j++) {
        if (s2[0] == c[j]) break;
      }
      if (i > j)
        cout << "YES" << endl;
      else
        cout << "NO" << endl;
    } else
      cout << "NO" << endl;
  }
  return 0;
}
