#include <bits/stdc++.h>
using namespace std;
char s[11], s1[11], s2[11];
void change(char c[]) {
  if (c[0] == '6')
    c[0] = 1;
  else if (c[0] == '7')
    c[0] = 2;
  else if (c[0] == '8')
    c[0] = 3;
  else if (c[0] == '9')
    c[0] = 4;
  else if (c[0] == 'T')
    c[0] = 5;
  else if (c[0] == 'J')
    c[0] = 6;
  else if (c[0] == 'Q')
    c[0] = 7;
  else if (c[0] == 'K')
    c[0] = 8;
  else if (c[0] == 'A')
    c[0] = 9;
}
int main() {
  int flag = 1;
  cin >> s >> s1 >> s2;
  change(s1);
  change(s2);
  if (s1[1] == s2[1]) {
    if (s1[0] > s2[0])
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  } else if (s1[1] == s[0])
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}
