This page is still under construction.

Parts of this page are still being built. What you see may change.

Multiplication Game

Time limit1sMemory limit128 MB

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

Medium6 of 10

Topics
Dynamic programming, Number theory
Solved
No attempts yet

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 t−1t-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 (1≤T≤501 \le T \le 50).

The first line of each test case contains the number of digit types NN (1≤N≤101 \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 (1≤M≤201 \le M \le 20).

Each of the next MM lines contains one target natural number KK (1≤K≤1061 \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.

Examples2

  1. Example 1

    Input
    2
    3 2 3 6
    3
    64
    5
    7
    4 1 2 3 4
    2
    31
    16
    
    Expected output
    1
    -1
    -1
    0
    1
    
  2. Example 2

    Input
    1
    1 2
    6
    2
    4
    8
    1024
    44
    1
    
    Expected output
    0
    1
    2
    9
    1
    -1