Fool Game

No attempts yetTime limit1sMemory limit128 MB

Problem

The game of "fool" is played with a small deck that has nine ranks — 6, 7, 8, 9, 0 (ten), J (Jack), Q (Queen), K (King), A (Ace) — in each of four suits: h (Hearts), s (Spades), d (Diamonds), and c (Clubs). For example, the queen of spades is written Qs and the ten of diamonds is 0d.

One suit is declared the trump. A card X beats a card Y when either:

  • X and Y have the same suit and X has the higher rank, or
  • X is a trump and Y is not.

A move proceeds as follows. First, the first player puts one of his cards on the table. The second player must either beat it with one of his cards, laying that card on top, or — if he has no card that can beat it — take all the cards on the table. Whenever a card is beaten, the first player may flip in another of his remaining cards whose rank equals that of some card already on the table. The flipped-in card must in turn be beaten; otherwise the second player takes it together with all other cards on the table. This repeats.

For example, suppose hearts are trump, the first player holds 6s 6d Qh Kd, and the second player holds 6h 7h 0s Qd. The first player can move with Kd, which is beaten by 6h; then flip in 6s, beaten by 0s; then 6d, beaten by Qd; and finally Qh, which cannot be beaten by the remaining 7h, so the second player must take.

Your task is to write a program that, given the trump suit and both players' hands, finds a first move for the first player that eventually forces the second player to take. Assume the second player defends optimally to avoid taking.

If more than one such move exists, output the one with the smallest rank. If several moves share the smallest rank, choose the card whose suit comes first in the order listed above (that is, h < s < d < c). In the example, the second player could instead beat Kd with 7h, preventing any further flips, so Kd is not a valid answer; on the other hand, moving Qh would be taken immediately.

Input

The first line contains a single character — h, s, d, or c — giving the trump suit. The second line is a string with the first player's cards, and the third line a string with the second player's cards. Each string concatenates the cards with no separators (each card is a rank character followed by a suit character). All cards are distinct, and both players hold the same number of cards.

Output

Print a single line containing the card to move that forces the second player to take, or the string NO if no such move exists.