Chemical Analysis

Interview

Time limit1sMemory limit128 MB

Summary
Given up to 12 element bitmasks and a target bitmask, find the fewest elements whose bitwise OR equals the target, or report that none does.
Level

Medium6 of 10

Topics
Bit manipulation, Brute force, Dynamic programming, Implementation
Solved
No attempts yet

Problem

The Curiosity rover carries a lot of high-tech hardware so it can analyze the Martian surface on the spot. (Sending rocks back from Mars is hard to imagine.) One way scientists infer which chemical elements are present in a sample is to heat or burn it and observe which frequencies of light are emitted. Each element has a characteristic spectrum, so from the observed spectrum one can work out which combination of elements could have produced it.

We model this as follows. There are nn possible frequencies (1≤n≤1001 \le n \le 100) and mm candidate elements (1≤m≤121 \le m \le 12). Each candidate element is described by an nn-bit vector of zeros and ones telling at which frequencies it emits light. The sample is described by the same kind of nn-bit vector.

We assume that if an element occurs in the sample in any quantity, then every frequency it emits is observed. When several elements emit the same frequency, that frequency is still observed just once — there is no cancellation. Therefore a set of elements explains the sample exactly when the union of their emitted frequencies equals the sample's spectrum: every frequency an element emits must appear in the sample, and every observed frequency must be emitted by at least one chosen element.

For each sample, determine the smallest number of elements whose combined spectrum equals the observed spectrum, or report that no combination does.

Input

The first line contains the number KK of data sets. Then follow KK data sets, each of this form:

The first line holds two integers nn and mm: the number of frequencies and the number of elements.

The next mm lines each contain a string of nn ones and zeros describing one element (line ii describes element ii). The following line contains a string of nn ones and zeros describing the sample.

Output

For each data set, first print Data Set x: on its own line, where xx is the data set's number (starting from 1). On the next line print the minimum number of elements whose combined spectrum equals the observed spectrum. If no combination of elements produces exactly the observed spectrum, print Impossible instead. Separate consecutive data sets with a blank line.

Examples1

  1. Example 1

    Input
    2
    12 5
    000111000000
    001100000001
    101000000010
    111111111111
    000000011000
    101100000011
    4 3
    1111
    0101
    1000
    1001
    
    Expected output
    Data Set 1:
    2
    
    Data Set 2:
    Impossible