The Number Trick

Given K observed subset products, find the most likely hidden multiset of N numbers from 2 to M under the given posterior score.

Medium7Brute forceCombinatoricsProbabilityNo attempts yetTime limit5sMemory limit1536 MB

Problem

Maryam and Peiling are practicing a number trick.

Maryam first picks NN integers independently and uniformly at random from 2 to MM, and writes one of them on each of NN cards. The same number may appear several times. She then repeats the following KK times: she takes each of the NN cards independently with probability 1/21/2 to form a subset, and writes down the product of the numbers on the cards she took. If she takes no card, the product is 1.

Maryam tells Peiling the KK products together with NN and MM. Peiling has to name the NN hidden numbers.

Peiling cannot always be right. When every product is 1, the products say nothing about the hidden numbers. So Peiling names the collection of NN numbers that is most likely given the products she saw. Your program computes exactly that answer for each of RR independent groups of products.

Here is the same rule as a formula. Let AA be a multiset of NN integers from 2 to MM.

  • W(A)W(A) is the number of ordered NN-tuples whose multiset is AA. If vv occurs fvf_v times in AA, then W(A)=N!/v=2Mfv!W(A) = N! / \prod_{v=2}^{M} f_v!.
  • cA(p)c_A(p) is the number of subsets whose product is pp when the cards carry AA. There are 2N2^N subsets in total, and two cards are distinct even when they carry the same number.

If one group of products is P1,,PKP_1, \dots, P_K, the score of AA is

S(A)=W(A)×j=1KcA(Pj)S(A) = W(A) \times \prod_{j=1}^{K} c_A(P_j)

S(A)S(A) is proportional to the posterior probability that the hidden multiset is AA once those products are observed. The answer for that group is therefore an AA that maximizes S(A)S(A). Several multisets may reach the maximum, so take the one whose elements, listed in non-decreasing order, form the lexicographically smallest string.

Input

The first line contains the number of test cases TT. TT is always 1.

The second line contains the integers RR, NN, MM, KK separated by spaces.

Each of the next RR lines contains the KK products of one group, separated by spaces.

Limits

  • T=1T = 1
  • 1R1001 \le R \le 100
  • 1N121 \le N \le 12
  • 2M82 \le M \le 8
  • 1K121 \le K \le 12
  • Every given product is at least 1 and at most MNM^N.
  • For every group there is at least one multiset AA with cA(Pj)1c_A(P_j) \ge 1 for all jj, so a candidate with a positive score always exists.

Output

Print "Case #1:" on the first line.

Then print RR lines. Line ii holds the answer for group ii, that is the lexicographically smallest multiset among those maximizing S(A)S(A), printed as NN digits. Write the digits in non-decreasing order with no spaces between them. Each digit is between 2 and MM. Since M<10M < 10, every hidden number has one digit.