#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, rank1, rank2, i;
  char array[9] = {'6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'};
  char trump[2];
  char line[10], first[10], second[10];
  while (gets(trump)) {
    rank1 = rank2 = 0;
    gets(line);
    n = strlen(line);
    int s = 0, a = 0, b = 0;
    for (i = 0; i < n; i++) {
      if (line[i] != ' ') {
        if (s < 2)
          first[a++] = line[i];
        else if (s < 4)
          second[b++] = line[i];
        s++;
      }
    }
    if (first[1] == second[1]) {
      for (i = 0; i < 9; i++)
        if (first[0] == array[i]) {
          rank1 = i;
          break;
        }
      for (i = 0; i < 9; i++)
        if (second[0] == array[i]) {
          rank2 = i;
          break;
        }
      if (rank1 > rank2)
        cout << "YES" << endl;
      else
        cout << "NO" << endl;
    } else if (first[1] == trump[0])
      cout << "YES" << endl;
    else if (second[1] == trump[0])
      cout << "NO" << endl;
    else
      cout << "NO" << endl;
  }
  return 0;
}
