You are given some special Tarot cards from the Major Arcana, including The Lovers, Death, The Star, The Moon, The Sun, and The World, along with some other playing cards in hand. You have at most one of each kind of Tarot card, and each Tarot card has a unique ability that can alter the suits of other playing cards in your hand. Your task is to determine the maximum number of flushes that can be played by the playing cards in hand after using the given Tarot cards each at most once.
The playing cards are standard playing cards, shown as below.
Here is a list of Tarot cards abilities.
![]() | ![]() | ![]() | ![]() |
![]() | ![]() |
You can use the Tarot cards in any order. Each given Tarot card can be used at most once and can never be used at all. There is a special rule about The Lovers:
A flush is a set of $$$5$$$ playing cards, which could be considered a same suit: there is a suit (among Diamond, Club, Heart, and Spade) such that each of the $$$5$$$ playing cards is either of this suit or is a Wild Card.
Playing a flush means that the $$$5$$$ cards forming the flush are discarded from hand, all of which cannot be used in another flush. To the contrary of many card games including Balatro, you draw no new cards from the deck after playing cards.
The input consists of multiple test cases. The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 13$$$) — the number of test cases. The description of the test cases follows.
The first line contains an integer $$$n$$$ ($$$1 \leq n \leq 52$$$), the number of playing cards in hand.
The second line contains $$$n$$$ space-separated strings, each representing a playing card in hand. Each playing card is represented by two characters: one for rank and one for suit, where the suit is one of D (Diamonds), C (Clubs), H (Hearts), or S (Spades), and the rank is one of 2-9, T (10, Ten), J (Jack), Q (Queen), K (King), or A (Ace).
The third line contains six space-separated integers $$$t_1, t_2, t_3, t_4, t_5, t_6 ~ (0 \leq t_i \leq 1)$$$, where:
It is guaranteed that the sum of $$$n$$$ among $$$T$$$ test cases does not exceed $$$104 = 52 \times 2$$$, and the playing cards are pairwise distinct in each test case.
For each test case, print the maximum number of flushes that can be played in a single line.
452H 3H 4H 5H 6D1 1 1 1 0 052S 3S 4D 5C 6D0 0 1 0 1 152S 3S 4D 5C 6D0 0 1 0 1 013AS 2S 3S 4S 5H 6H 7H 8H 9H TH JH QH KH0 0 0 0 0 1
1 1 0 2
In the first case, we can convert $$$6 \diamondsuit$$$ into $$$6 \heartsuit$$$ using The Sun, and play $$$2 \heartsuit 3 \heartsuit 4 \heartsuit 5 \heartsuit 6 \heartsuit $$$ as a flush. This is not the only possible way; another possible way to play the same set of cards is:
In the second test case, one possible way to play a flush is: