Digits4All
Peter has some digits , each of them is available in infinite amount. Using the digits and addition, multiplication and concatenation he wants to represent positive numbers. For example if his digits are 1, 2, 5
and the number to be represented is 16
, then some of its legal representations are:
16 = 5+5+5+1 (by addition)
16 = 4*4 = (2+2)*(2+1+1) (by addition, by multiplication)
16 = (1)(1+5) (by addition, concatenation)
If the number to be represented is 101
, then we have the following possibilities:
101 = (10)(1) = (2*5)(1) (by multiplication, by concatenation)
101 = (10)(1) = (5+5)(1) (by addition, by concatenation)
He does not have 0 digits, and he forbids numbers with leading zeroes, that is the representation 101=(1)(01)
is not legal.
For the representation of a number
he can use the addition, multiplication and concatenation in any combinations, but he wants to use the
minimal number of digits (
) for the representation. Help him by computing these
’s!
Input specification
In the first line the is the number of different digits Peter has. In the second line comes the space separated list of his digits . In the third comes the : the number of queries that follows. Each of the next lines has a single positive number , the representable number.
Output specification
lines with the number or with if it is impossible to represent with the given digits.
Constraints
Sample input 1
3
1 2 5
2
16
101
download as text file
Sample output 1
3
3
download as text file
Sample input 2
1
1
3
2
12
1111
download as text file
Sample output 2
2
3
4
download as text file
Sample input 3
3
2 4 6
5
10
20
30
40
50
download as text file
Sample output 3
2
3
3
3
3
download as text file
Sample input 4
4
2 5 7 8
7
9190
3644
1111
4846
4544
404
4333
download as text file
Sample output 4
6
5
5
6
4
4
6
download as text file
Sample input 5
2
2 3
1
1
download as text file
Sample output 5
-1
download as text file