#ACSL1617S1. AGRAM

AGRAM

PROBLEM: Agram is a card game for 2 players, using the cards from a standard 52-card pack. The dealer deals five cards to each player. The opponent player leads any card, playing it face up in the middle of the playing area.

For this program the following strategy will be used to determine which card the dealer will play:

The dealer must play a card of the same suit if he can.

He plays the lowest card in that suit that is of a higher rank than the card the opponent played.

If he does not have such a card, he plays his lowest card in that suit.

If he does not have a card in that suit, he plays the lowest ranking card regardless of suit.

If there is a tie, he plays the card in the following order: clubs, diamonds, hearts and spades.

INPUT: There will be 5 lines of input. Each line will contain the opponent’s lead card and the 5 cards held by the dealer. All cards will be represented by 2-character strings in value-suit order. AH represents the ace of hearts. K, Q and J and T will be used for king, queen, jack and 10 respectively. Note that the ace in this game has the lowest rank.

OUTPUT: For each input line print the card the dealer must play according to the strategy listed above.

SAMPLE INPUT

5D, 2D, 6H, 9D, TD, 6H
TC, AC, KC, QH, JS, TD
3D, 4H, 5C, 6S, 2D, 7H
KS, TH, QC, 3D, 9H, 3H
AC, AD, KH, AS, KS, QS

SAMPLE OUTPUT

9D
KC
2D
3D
AD