#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  char suit;
  cin >> suit;
  string p1, p2;
  cin >> p1 >> p2;
  long long a = 0;
  long long b = 0;
  if (p1[0] == 'T')
    a = 10;
  else if (p1[0] == 'J')
    a = 11;
  else if (p1[0] == 'Q')
    a = 12;
  else if (p1[0] == 'K')
    a = 13;
  else if (p1[0] == 'A')
    a = 14;
  else
    a = p1[0] - '0';
  if (p2[0] == 'T')
    b = 10;
  else if (p2[0] == 'J')
    b = 11;
  else if (p2[0] == 'Q')
    b = 12;
  else if (p2[0] == 'K')
    b = 13;
  else if (p2[0] == 'A')
    b = 14;
  else
    b = p2[0] - '0';
  if (p1[1] == suit && p2[1] != suit)
    cout << "YES";
  else if (p2[1] == suit && p1[1] != suit)
    cout << "NO";
  else if (p2[1] != suit && p1[1] != suit && p1[1] == p2[1]) {
    if (a > b)
      cout << "YES";
    else
      cout << "NO";
  } else if (p2[1] == suit && p1[1] == suit) {
    if (a > b)
      cout << "YES";
    else
      cout << "NO";
  } else
    cout << "NO";
  return 0;
}
