Number card trick

Choose the sorted N-number multiset bounded by M whose subset product counts give the largest posterior weight.

Medium4Brute forceCombinatoricsNo attempts yetTime limit5sMemory limit512 MB

Problem

Maryam and Peiling practice a number trick. It works like this.

Maryam picks NN integers between 22 and MM, inclusive. The numbers are independent and every allowed value is equally likely. She writes them on NN cards, one number per card, so the same number can appear several times. She then repeats the following KK times. She takes a random subset of the cards, putting each card in with probability 0.50.5 independently of the others, and writes down the product of the numbers on those cards. The empty subset gives the product 11.

Maryam shows the KK products to Peiling. Knowing only NN, MM, KK and the products, Peiling has to name the numbers on the cards.

The products often fail to pin the numbers down. When every product is 11, they say nothing at all about the cards. This problem therefore asks for the candidate with the largest posterior probability.

Call a sequence BB with 2B1B2BNM2 \le B_1 \le B_2 \le \dots \le B_N \le M a candidate. Let P1,P2,,PKP_1, P_2, \dots, P_K be the observed products, and let cB(p)c_B(p) be the number of subsets of {1,2,,N}\{1, 2, \dots, N\} whose product is pp. The empty subset has product 11. Let o(B)o(B) be the number of distinct orderings of BB: if the value vv appears mvm_v times in BB, then o(B)=N!/vmv!o(B) = N! / \prod_v m_v!. Define the weight of a candidate as

W(B)=o(B)×j=1KcB(Pj)W(B) = o(B) \times \prod_{j=1}^{K} c_B(P_j)

W(B)W(B) is proportional to the probability that Maryam's numbers form the multiset BB, given the observed products.

For each set, print the candidate with the largest W(B)W(B). If several candidates tie for the largest weight, print the lexicographically smallest one.

Input

The first line contains the number of test cases TT, which is always 11. The second line contains four space separated integers RR, NN, MM and KK. Each of the next RR lines contains KK space separated products and describes one independent set produced by the procedure above.

Output

Print "Case #1:" on the first line. Then print RR lines, one answer per set, in order. Each answer is NN digits in non-decreasing order with no spaces between them. Because M<10M < 10, every number is a single digit.

Limits

  • T=1T = 1
  • 1R1001 \le R \le 100
  • 1N61 \le N \le 6
  • 2M92 \le M \le 9
  • 1K101 \le K \le 10
  • Every input was produced by the procedure in the statement, so each PjP_j is the product of some subset of that set's hidden numbers.

Notes

The first set of the first example has N=3N = 3, M=4M = 4 and lists the product 3636. The only way to write 3636 as a product of three numbers between 22 and 44 is 3×3×43 \times 3 \times 4, so the answer is 334. In the second set every product is 11, so cB(1)=1c_B(1) = 1 for every candidate and the largest o(B)o(B) wins. A candidate with three distinct values has o(B)=6o(B) = 6, the largest possible, and 234 is the lexicographically smallest of those.