Game Simulator

Statement

"Tractor" is a very popular poker game in China. There are four players in the game. Suppose their names are Alice, Bob, Charles and David, in clockwise order. A judge is needed for this game. The players are divided into two teams, Alice and Charles are in team 1, and the other two are in team 2. The prop they use to play the game are two decks of pokers, including 108 cards in total. A simplified rule of the game is described below.

The whole game contains a number of rounds. In each round, one team is called "Declarers" (CT); the other team is called "Defenders" (FT). Each team has a current rank (CR). The goal of the player is to increase his own team's CR as much as possible.

A certain round has a Main Suit (Heart - H, Spade - S, Club - C, Diamond - D, or None - no main suit in this round) and its CR. The CR in this round is the CR of the CT, and Main Suit will be given. The Main Suit and CR will be used to determine the order of the cards.

Cards ranked 5, 10, King value 5, 10, 10 pts (points) respectively, all other cards value 0 pts. In one round, we only consider the FT's pts. The rules of getting pts for FT will be discussed later.

If the FT gets less than 80 pts in one round, they will hold be FT in the next round. This situation is called "make". Otherwise, they become CT in the next round and the original CT become FT instead. This situation is called "down".

If the FT gets 0 pts, the CR of the current CT will be increased by 3, for example, if the CR of the CT is 9, it will become Queen (12). Otherwise, if the FT gets less than 40 pts, the CR of the CT will be increased by 2. Otherwise, if the FT gets less than 80 pts, the CR of the CT will be increased by 1. Otherwise, if the FT gets not less than 80 + k * 40 pts and less than 120 + k * 40 pts, the CR of the current FT will be increased by k. For example, if the FT gets 255 pts in a round, the CR of the current FT will be increased by 4; and if the FT gets 80 pts, both teams' CR remain unchanged. If a team's CR becomes beyond Ace, this team is considered the WINNER of the whole game.

During a round, one of the players in CT is called the dealer. If "make", the pard (teammate) of the dealer becomes the next round’s dealer. Otherwise ("down"), the player on dealer's right-hand side becomes the dealer of the next round. For example, if the dealer of the current round is Alice and her team (CT) is "down", the dealer of the next round should be Bob (on Alice's right-hand side).

At the start of a round, each of the players except the dealer gets exactly 25 cards; the dealer gets all the remaining 33 cards. After that, the dealer chooses 8 of his cards and gives them to the judge, and these cards are called "hidden cards".

Now each player has exactly 25 cards. A round consists of several tricks. In the first trick, the dealer plays one or more cards (called "lead"), then, in clockwise order, players play the same number of cards as the first player one by one (called "follow"). The winner of the current trick leads cards during the next trick, and so on. If the winner of the current trick is a member of FT, then the FT gets the sum of the cards' pts played in this trick.

Now we start to describe how to determine the winner of a trick. After the main suit and the CR of the current round are fixed, we can determine the "trumps" which are cards with main suit or CR (Current Rank), and the jokers. All other cards are "not-trumps".

We can have an order among all the cards according the following rules:

  1. "Trumps" are ordered higher than "not-trumps".
  2. For the trumps, the order are listed below:
    • Red Joker
    • Black Joker
    • card with main suit and CR (if exists)
    • other card with CR
    • other trumps ordered by their ranks(i.e., A, K, Q, J, T, 9, 8, 7, …, 3, 2)
  3. For the "not-trumps", they are ordered by their ranks.

Assume in all the description below, in the current round, the CR of the CT is 7.

Suppose the main suit is H, the cards can be arranged in this order (as an example):

  S2 , C2 , D2
< S3 , C3 , D3
< S4 , C4 , D4
< S5 , C5 , D5
< S6 , C6 , D6
< S8 , C8 , D8
< S9 , C9 , D9
< ST , FT , CT (T - 10)
< SJ , CJ , DJ (J - Jack)
< SQ , CQ , DQ (Q - Queen)
< SK , CK , DK (K - King)
< SA , CA , DA (A - Ace)
< H2
< H3
< H4
< H5
< H6
< H8
< H9
< HT
< HJ
< HQ
< HK
< HA
< S7 = C7 = D7
< H7
< BJ (the Black Joker)
< RJ (the Red Joker)

If "None" during this round, then the pokers can be arranged in this order:

  H2 , S2 , C2 , D2
< H3 , S3 , C3 , D3
< H4 , S4 , C4 , D4
< H5 , S5 , C5 , D5
< H6 , S6 , C6 , D6
< H8 , S8 , C8 , D8
< H9 , S9 , C9 , D9
< HT , ST , FT , CT
< HJ , SJ , CJ , DJ
< HQ , SQ , CQ , DQ
< HK , SK , CK , DK
< HA , SA , CA , DA
< H7 = S7 = C7 = D7
< BJ
< RJ

In these two tables, cards written in italic are "trumps", and cards written in boldface are "not-trumps".

In each trick, the lead cards (played by the player leading this trick) must be either all "trumps", or all "not-trumps" with same suit.

The possible structures of the cards are listed below (assume the main suit is H and main rank is 7 for the example):

  • Single. A single card, such as D9.
  • Pair. Two same cards, such as D9D9. But D7S7 is not a pair although their orders are the same.
  • Tractor. Two or more consecutive-ordered pairs, satisfying the condition that they are all "trumps", or all "not-trumps" with same suit, such as SJSJSQSQSKSKSASA, H7H7S7S7HAHA or RJRJBJBJ. But, these are not tractors: S7S7C7C7 (their orders are the same), C7C7C6C6 (they are not consecutive-ordered), DADAD2D2 (Ace is not "one", so they are not consecutive-ordered), H2H2H4H4, or D2D2D3. Be careful: if "None" in this round, H7H7S7S7HAHA is not a tractor (H7 and S7 are same-ordered because of "None").
  • Throw. The combination of the structures above, satisfying the condition that they are all "trumps", or all "not-trumps" with same suit. Each of the Single, Pair or Tractor in a Throw is one of the Throw’s component. In the original tractor game, in some situation, the throw will be rejected. But, to keep the rule simple, we assume in this problem all the throws are accepted. For example, RJRJBJBJH7H7HQHQHJHJH9H9H6H6HAH2 contains six components: two tractors, two pairs and two single cards (RJRJBJBJH7H7-HQHQHJHJ-H9H9-H6H6-HA-H2); CACAC8C8CK contains three components: two pairs and one single card(CACA-C8C8-CK).

A throw can be treated as different list of components, for example, H2H2H3H3H4H4H5H5H6H6 can also be treated as H2H2H3H3-H4H4H5H5H6H6, or H2H2-H3H3H4H4H4H5H5-H6H6, and so on. For the lead cards, each time we choose the longest component (choose the one with the highest order to break the tie) to construct a list of components, this list is the structure of the lead cards, also the structure of the trick. So that the structure of the trick is unique.

After the first player lead his or her cards, other players follow cards one by one in clockwise order, as mentioned above.

An important part of the game is to determine the winner of a trick:

If one’s follow cards contain both "trumps" and "not-trumps", or all "not-trumps" but with different suits, this player can't be winner of this trick.

Otherwise, if the lead cards are all "not-trumps" and one’s follow cards contain "not-trumps" with different suit from the lead cards, this follow player can’t be the winner of this trick.

Else, if one’s follow cards can't be constructed as the same structure of lead cards, this player can't be the winner of this trick either.

Otherwise, if the structure of this trick is not "throw", the one who played the highest-ordered card wins this trick. If more than one player played the same highest-ordered card, the winner of this trick will be the one who plays the highest-ordered card first.

Now let's consider the "throw" situation. We construct the follow cards into the structure of the lead cards, so that the order of the highest-ordered card in all the longest components of the "throw" is as high as possible (this card is called "honor card"). Note that tractor can be treated as several pairs or shorter tractors, and pair can be treated as two single cards. The winner of this trick is the one who plays the highest-ordered "honor card". Similarly, if more than one player played the same highest-ordered "honor card" the winner of this trick will be the one who plays the highest-ordered "honor card" first.

There are many hair-raising rules about lead and follow cards; fortunately, they're not related to this problem, the only thing we care about is: when someone leads a "not-trump" "throw" the only possible way to beat it is to "throw" the same structure of "trumps". And it’s impossible to beat a leading "trump" "throw".

Special attention on the examples below. In these examples, Alice always leads cards. And assume in all the following examples, the CR is 7, and the main suit is H.

Alice Bob Charles David Winner Comments
SA S2 ST S5 Alice Alice plays the highest-ranked card SA.
SA S2 ST SA Alice Alice plays the first SA.
SA S2 ST H2 David David plays the first only "trump".
SA H2 C7 D7 Charles Charles plays "trump" C7, while David plays D7 with the same order of C7.
C2C2 C3C4 C7D7 RJBJ Alice The structure of this trick is "pair", while all players except Alice play two single cards.
D3D3 DTDT SKSK H2H3 Bob Bob plays a pair with order higher than Alice's, while Charles discard a pair with wrong suit.
D3D3 DTDT SKSK H2H2 David David plays the only "trump" pair.
D6D6D8D8 DJDJDKDK DTDTD2D3 HTHTBJBJ Alice Alice plays the only tractor (because the CR is 7).
H6H6H8H8 H7H7BJBJ C2C2C3C4 HKHKRJRJ Bob Bob's tractor is higher-ordered than Alices'.
H6H6H8H8 H7H7D7D7 C2C2C3C4 HKHKRJRJ Bob Bob also plays a tractor!
SASK STST C2H3 S7SK Alice Alice makes a throw.
SASK HKH3 HAH2 S7SK Charles Both Bob and Charles can beat Alice.
SASK HAH2 HAH3 S7SK Bob Both Bob and Charles can beat Alice, but Bob's HA comes first.
S2S2S3S3SA H3H3H4H4RJ D7D7H7H7H2 S7S7SQSJS6 Charles Both Bob and Charles can beat Alice.

There is a special rule about "hidden cards": if the winner of the last trick of a certain round is a member of FT, then, in addition, the FT gets the sum of the hidden cards' pts, multiplied by 2w (2 to the power of w). When the structure of the final trick is not "throw", then w is the number of lead cards of the last trick of this round. If the structure is "throw" instead, w is the length of the longest components, in the example RJRJBJBJH7H7HQHQHJHJH6H6HA, the w is 6 because the length of RJRJBJBJH7H7 (the longest components of the "throw") is 6.

To make the problem easier, you are only to write a single-round tractor game simulator.

Input:

Multiple test cases, the number of them T is given in the very first line. For each test case:

The first line contains the main suit of this round (H, S, C, D, O; O denotes "None" in this round), the dealer of this round, the CR of team 1, the CR of team 2, separated by single spaces. Each of the rest lines contains 4 strings: the lead cards and the cards played by the second, third and last player. In one string, the cards can be given in any order. Each player will play exactly 25 cards in one round.

You may assume the input is always valid.

There is a blank line between consecutive test cases, and a blank line also appears between T and the first test case.

Output:

For each test case:

The first line contains the case number.

The second line contains the pts get by the FT in this round.

If a team wins the whole game after this round, output "Winner: Team X"(without quotes, X should be either 1 or 2) in the second line. If no team wins, output the new CR of team 1, the new CR of team 2 after this round, followed by the name of the dealer of the next round, separated by single spaces.

See the example for further details.

Example input:

1

O Charles 2 2
S6S6S7S7 SASKSJST STS8S4S4 S3S5SJSQ
S9S9 H3D3 C3DT SAD3
DA DQ DK D4
SKS8S5S3 RJC2D2H2 C6C8CJD9 H3CKDTD5
H7H7 H6H4 HJHQ H9H9
DJDJ DKH5 D5D4 D6D6
D8D8 C4C3 HTH5 D9D7
C5C5 C6CT H8HQ C7C4
H8 C7 HA HA
H2 RJ BJ CK
DA BJ C8 HK
S2S2C2 CQCAD2 HTHJHK C9CQCA

Example output:

Case #1:
50
3 2 Alice

Time and memory limit:

  • 1 second
  • 64MB

Problem source: SPOJ

s