#include <bits/stdc++.h>
using namespace std;
int main() {
  char k1[4], k2[4], k[4];
  cin >> k;
  cin >> k1;
  cin >> k2;
  switch (k1[0]) {
    case '6': {
    };
    case '7': {
    };
    case '8': {
    };
    case '9': {
      k1[0] = k1[0] - 5;
      break;
    }
    case 'T': {
      k1[0] = '5';
      break;
    }
    case 'J': {
      k1[0] = '6';
      break;
    }
    case 'Q': {
      k1[0] = '7';
      break;
    }
    case 'K': {
      k1[0] = '8';
      break;
    }
  }
  switch (k2[0]) {
    case '6': {
    };
    case '7': {
    };
    case '8': {
    };
    case '9': {
      k2[0] = k2[0] - 5;
      break;
    }
    case 'T': {
      k2[0] = '5';
      break;
    }
    case 'J': {
      k2[0] = '6';
      break;
    }
    case 'Q': {
      k2[0] = '7';
      break;
    }
    case 'K': {
      k2[0] = '8';
      break;
    }
  }
  if (k1[1] == k[0]) {
    if (k1[1] != k2[1]) {
      cout << "YES";
      return 0;
    } else {
      if (k1[0] > k2[0]) {
        cout << "YES";
        return 0;
      } else {
        cout << "NO";
        return 0;
      }
    }
  }
  if (k1[1] != k2[1]) {
    cout << "NO";
    return 0;
  } else {
    if (k1[0] > k2[0]) {
      cout << "YES";
      return 0;
    } else {
      cout << "NO";
      return 0;
    }
  }
  return 0;
}
