Charging Chaos (Large)

Time limit5sMemory limit512 MB

Summary
Find a bit mask applied to every outlet string that makes the outlet set match the device set with the fewest flipped bits, or report that it is impossible.
Level

Medium6 of 10

Topics
Bit manipulation, Hash map, Brute force
Solved
No attempts yet

Problem

Shota the farmer just moved into his newly built farmhouse, and the outlets there are not set up for his devices. He owns a pile of smartphones and laptops, and a tablet for his favorite cow Wagyu to use. In total he owns NN devices.

The devices have different specifications and come from different companies, so each one needs its own electric flow to charge. Each outlet also puts out one specific electric flow. An electric flow is a string of 0s and 1s of length LL.

Shota wants to charge all NN devices at the same time, and his new house has exactly NN outlets. The flow from the outlets is configured on a master control panel with LL switches. Flipping switch ii flips the ii-th bit of the flow from every outlet in the house.

For example, suppose three outlets put out 10, 01, and 11. Flipping the second switch turns those flows into 11, 00, and 10. If Shota has a smartphone that needs 11, a tablet that needs 10, and a laptop that needs 00, flipping the second switch alone charges all three.

Misaki measured the flow of every outlet in the house and found that they are all different. Decide whether Shota can charge all of his devices at the same time, and if he can, find the smallest number of switches that have to be flipped. The switches are big and heavy, so Misaki does not want to flip more of them than she has to.

Input

The first line contains the number of test cases TT. TT test cases follow, each on three lines.

The first line of a test case contains two integers NN and LL separated by a space. The second line contains NN strings of length LL separated by spaces, the flow that each outlet puts out at the start. The third line contains NN strings of length LL separated by spaces, the flow that each of Shota's devices needs.

Limits

  • 1≤T≤1001 \le T \le 100
  • 1≤N≤1501 \le N \le 150
  • 2≤L≤402 \le L \le 40
  • No two outlets put out the same flow at the start.
  • No two devices need the same flow.

Output

For each test case, print one line in the form Case #x: y, where xx is the test case number starting from 1 and yy is the smallest number of switches that have to be flipped so that Shota can charge all of his devices. If there is no way to charge them all, print NOT POSSIBLE in place of yy, without the quotes.

Hint

In the first test case of the first example, flipping the second switch once turns the outlet flows into 00, 10, and 11. Shota then charges device 1 from outlet 0, device 2 from outlet 1, and device 0 from outlet 2. Flipping nothing charges nothing, so 1 is the smallest answer.

Examples1

  1. Example 1

    Input
    3
    3 2
    01 11 10
    11 00 10
    2 3
    101 111
    010 001
    2 2
    01 10
    10 01
    
    Expected output
    Case #1: 1
    Case #2: NOT POSSIBLE
    Case #3: 0