
type
  TSuit = (sH, sS, sD, sC);
  TRank = (r6, r7, r8, r9, r0, rJ, rQ, rK, rA);
  THand = array [TSuit, TRank] of Boolean;

const
  sSuit: String = 'hsdc';
  sRank: String = '67890JQKA';

var
  f: Text;
  ch: Char;
  trump: TSuit;
  s: String;
  first, second: THand;
  desk: array [TRank] of Integer;

procedure Parse(var s: String; var h: THand);
var
  i: Integer;
begin
  FillChar(h, SizeOf(h), false);
  i := 1;
  while i < Length(s) do begin
    h[TSuit(Pos(s[i + 1], sSuit) - 1), TRank(Pos(s[i], sRank) - 1)] := true;
    Inc(i, 2);
  end;
end;

function RecFirst: Boolean; forward;

function RecSecond(vs: Tsuit; vr: TRank): Boolean;
var
  mr: TRank;
  b: Boolean;
label
  RS_OUT;
begin
  first[vs, vr] := false; Inc(desk[vr]);
  for mr := vr to High(TRank) do begin
    if not second[vs, mr] then continue;
    second[vs, mr] := false; Inc(desk[mr]);
    b := RecFirst;
    second[vs, mr] := true; Dec(desk[mr]);
    if not b then begin
      RecSecond := true;
      goto RS_OUT;
    end;
  end;
  if vs = trump then goto RS_OUT;
  for mr := Low(TRank) to High(TRank) do begin
    if not second[trump, mr] then continue;
    second[trump, mr] := false; Inc(desk[mr]);
    b := RecFirst;
    second[trump, mr] := true; Dec(desk[mr]);
    if not b then begin
      RecSecond := true;
      goto RS_OUT;
    end;
  end;
  RecSecond := false;
RS_OUT:
  first[vs, vr] := true; Dec(desk[vr]);
end;

function RecFirst: Boolean;
var
  vs: TSuit;
  vr: TRank;
begin
  for vr := Low(TRank) to High(TRank) do begin
    if desk[vr] = 0 then continue;
    for vs := Low(TSuit) to High(TSuit) do begin
      if not first[vs, vr] then continue;
      if not RecSecond(vs, vr) then begin
        RecFirst := true;
        exit;
      end;
    end;
  end;
  RecFirst := false;
end;

var
  rs: TSuit;
  rr: TRank;

function Rec0: Boolean;
var
  vs: TSuit;
  vr: TRank;
begin
  for vs := Low(TSuit) to High(TSuit) do begin
    for vr := Low(TRank) to High(TRank) do begin
      if not first[vs, vr] then continue;
      if not RecSecond(vs, vr) then begin
        rs := vs; rr := vr;
        Rec0 := true;
        exit;
      end;
    end;
  end;
  Rec0 := false;
end;

begin
  Readln(ch);
  trump := TSuit(Pos(ch, sSuit) - 1);
  Readln(s);
  Parse(s, first);
  Readln(s);
  Parse(s, second);
  FillChar(desk, SizeOf(desk), 0);
  if Rec0 then
    Writeln(sRank[ord(rr) + 1], sSuit[ord(rs) + 1])
  else
    Writeln('NO');
end.
