Multiplication Game

Given a set of allowed digits, find the fewest factors (minus one) whose digits all come from the set and whose product is K.

Medium6Dynamic programmingNumber theoryNo attempts yetTime limit1sMemory limit128 MB

Problem

There is a game where you combine number cards, arithmetic operator cards, and bracket cards to build a target value. Jin likes this game and packed the cards for the IT engineering college retreat, but did not bring the whole set. Of the operator cards Jin brought only multiplication cards, and of the number cards only NN of the digit types from 0 through 9. So Jin changed the rules to a multiplication game.

In the multiplication game you build a fixed number out of number cards and multiplication cards. The team that uses the fewest multiplication cards wins. Jin has plenty of every digit type that was packed, so no type ever runs short and the same type can be used again as many times as needed.

Number cards placed side by side form a multi-digit number. Every digit in the decimal writing of that number must belong to the packed digit types, and a writing with a leading zero is not used. So building KK means writing KK as a product of numbers whose every digit belongs to the packed types. A product of tt numbers uses t1t-1 multiplication cards.

For example, if the packed digit types are 2, 3, 6, then 64 can be built as 32×232 \times 2, which uses one multiplication card. No method uses fewer.

Given the packed digit types and a target number, find the minimum number of multiplication cards.

Input

The first line contains the number of test cases TT (1T501 \le T \le 50).

The first line of each test case contains the number of digit types NN (1N101 \le N \le 10) and the NN digit types, separated by spaces. The types are distinct and each lies between 0 and 9.

The second line of each test case contains the number of queries MM (1M201 \le M \le 20).

Each of the next MM lines contains one target natural number KK (1K1061 \le K \le 10^6).

Output

For each query print the minimum number of multiplication cards on its own line. If KK cannot be built, print -1.