This page is still under construction.

Parts of this page are still being built. What you see may change.

Magical Thinking v2

Time limit20sMemory limit1024 MB

Summary
Given friends' true-false answer lists and their scores, find the highest score you could have gotten under some answer key consistent with all of them.
Level

Medium6 of 10

Topics
Math, Brute force
Solved
No attempts yet

Problem

You and NN of your friends just took the B.A.T. (Binary Answer Test) to try to get into wizard school. The B.A.T. has QQ true-false questions, and each one is worth 1 point. You have no wizard powers, so you just picked arbitrary answers and hoped for the best.

The results of the test have already been sent out by quail mail, but the quail with your results has not arrived yet. However, each of your friends has told you their list of answers and their total score. You also remember your own list of answers. You are an optimist and you think that you probably did well!

Given that there is one correct list of answers (but you do not know what those answers are), and given your friends' answers and scores, what is the highest score that you possibly could have achieved?

Input

The first line of the input gives the number of test cases, TT. TT test cases follow. Each begins with one line with two integers NN and QQ. Then, N+1N+1 lines follow. The ii-th of these lines represents the ii-th examinee's list of answers AiA_i, which has QQ characters, each either T (True) or F (False). AN+1A_{N+1} is your own list of answers. Finally, one line with NN integers follows. The ii-th of these integers, SiS_i, is the ii-th examinee's score. Your own score is not in this list, because it is unknown.

Output

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy is the highest score that you could have achieved that is consistent with the given information.

Constraints

  • 1≤T≤1001 \le T \le 100.
  • The length of AiA_i is QQ, for all ii.
  • Each character of AiA_i is either T or F, for all ii.
  • 0≤Si≤Q0 \le S_i \le Q.
  • It is guaranteed that at least one list of correct answers is consistent with all of the friends' answers and scores.

Hint

Note that the last sample case would not appear in the Small dataset.

In sample case #1, your friend answered TF and you answered FF, and exactly one of your friend's answers was right. If your friend was wrong on question 1 and right on question 2, then the real set of answers is FF and you got both questions right. It is impossible to do better than this!

In sample case #2, your friend answered all Ts and got all of the questions wrong, so the real set of answers must be all Fs, which means that you got only question 3 right.

In sample case #3, the only possible real lists of answers that are consistent with the given information are FTT and FFF. For example, the real answer list cannot be TFT, because the first friend's answers and score would be consistent with that, but the second friend would have scored 0 instead of 2. Of these two possibilities, FTT is more favorable to you and would give you a score of 2.

Examples1

  1. Example 1

    Input
    3
    1 2
    TF
    FF
    1
    1 3
    TTT
    TTF
    0
    2 3
    TTF
    FTF
    TTT
    1 2
    
    Expected output
    Case #1: 2
    Case #2: 1
    Case #3: 2