#include <cstdio>
#include <cstring>

#include <vector>
#include <map>

using namespace std;

#define forn(i, n) for (int i = 0; i < (int)(n); i++)

struct card
{
  int val, suit;

  card() {}
  card( int a, int b ) : val(a), suit(b) { }
};

bool operator < ( card a, card b )
{
  if (a.val != b.val)
    return a.val < b.val;
  return a.suit < b.suit;
}

typedef vector <card> hand;

int n, suit;
hand a, b;

static const char *suits = "hsdc";
static const char *values = "67890JQKA";

int Suit( char x ) { return strchr(suits, x) - suits; }
int Value( char x ) { return strchr(values, x) - values; }

void ReadHand( hand &a )
{
  char s[99];
  scanf("%s", s);

  for (int i = 0; s[i]; i += 2)
    a.push_back(card(Value(s[i]), Suit(s[i + 1])));
}

const int maxn = 18;

int bn[1 << maxn];
int is[10];

#define IS(a, i) (((a) >> (i)) & 1)

inline bool Beat( const card &a, const card &b )
{
  return (a.suit == b.suit && a.val > b.val) || (a.suit == suit && b.suit != suit);
}

#define mp make_pair
typedef pair <int, pair<int,int> > state;
map <state, int> m;

int go( int pr1, int pr2, int pr3 )
{
  memset(is, 0, sizeof(is));
  forn(i, n)
  {
    if (IS(pr1, i)) is[a[i].val] = 1;
    if (IS(pr2, i)) is[b[i].val] = 1;
  }
  forn(i, n)
    if (!IS(pr1, i) && is[a[i].val])
      pr1 |= 1 << i, pr3 |= 1 << i;

  if (bn[pr3] == 0)
    return 0;

  state st = mp(pr1, mp(pr2, pr3));
  if (m.count(st))
    return m[st];
  int &res = m[st];

  forn(i, n)
    if (IS(pr3, i))
    {
      forn(j, n)
        if (!IS(pr2, j) && Beat(b[j], a[i]))
          if (go(pr1, pr2 ^ (1 << j), pr3 ^ (1 << i)) == 0)
            return res = 0;
      return res = 1;
    }
}

int main()
{
  freopen("fool.in", "r", stdin);
  freopen("fool.out", "w", stdout);

  char ch;
  scanf(" %c", &ch);
  suit = Suit(ch);
  ReadHand(a);
  ReadHand(b);

  n = a.size();
  forn(i, 1 << maxn)
    bn[i] = (i & 1) + bn[i >> 1];

  int good = 0;
  card res;
  res.val = res.suit = -1;
  forn(i, n)
    if (go(1 << i, 0, 1 << i))
      if (!good || a[i] < res)
        res = a[i], good = 1;
  if (!good)
    puts("NO");
  else
    printf("%c%c\n", values[res.val], suits[res.suit]);
  return 0;
}