Broken Calculator (Small)

Split X into factors typed with working digits only, minimizing the total of digit, multiply, and equals presses.

Medium5Dynamic programmingRecursionNumber theoryInterviewNo attempts yetTime limit5sMemory limit512 MB

Problem

Alice is a student who is very good at math. She is in a math class where the teacher is showing everyone how to use a calculator. The teacher calls out an integer, and each student has to type that exact number into their calculator. A student who fails to type it is punished for missing such an easy task.

At the start of the class Alice notices that her calculator is broken. Some of the digit buttons do not respond at all, and among the operator buttons only multiply and equals still work. Alice has to produce the number the teacher called out using only the buttons that still work.

The expression Alice types has this shape. She types a number, then optionally presses multiply and types another number, repeating that as often as she likes, and finally presses equals. Every number she types must consist only of working digit buttons, and its first digit must not be 0. The product of all the numbers she types must be exactly XX, the number the teacher called out. The total click count is the sum of the digit counts of the numbers she typed, plus the number of multiply presses, plus one for the final equals.

Suppose the teacher calls out 60 and only the digits 1, 2 and 5 work. Typing 15*2*2= costs 4 digit clicks, 2 multiply clicks and 1 equals click, so 7 clicks in total. Typing 12*5= costs only 5 clicks.

Alice wants the click count to be as small as possible. Find that minimum.

Input

The first line contains TT, the number of integers the teacher calls out. TT test cases follow.

Each test case has two lines. The first line contains ten numbers, each 0 or 1. Counting from 0, the ii-th number is 1 if the digit ii button can be pressed and 0 if it is broken. The second line contains XX, the integer the teacher called out.

Limits

  • 1T1001 \le T \le 100
  • 1X1061 \le X \le 10^6

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 minimum number of button clicks. If XX cannot be produced, print Impossible in place of yy.

Notes

Alice presses equals even when she types a single number and no multiplication happens. With every digit button working and XX equal to 128, she presses 1, 2, 8 and equals, which is 4 clicks.

Sometimes no product of typeable numbers equals XX. If only the odd digit buttons work, every product is odd, so 128 cannot be produced.