406560: GYM102441 B Redistribution of Digits

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

B. Redistribution of Digitstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

You are required to form numbers based on a given set of digits. Each number is also provided with an upper bound. Each digit from the given set must be used exactly once. If the set does not contain a digit, you are not allowed to use it.

Input

First line contains an integer $$$t$$$ — the number of tests.

Each of the next $$$t$$$ lines contains a string $$$s$$$ — a given set of digits, integer $$$n$$$ — quantity of numbers which should be formed and $$$n$$$ integers $$$a_i$$$ — upper bounds for numbers. The digits for all numbers $$$a_i$$$ are given in a non-increasing order starting from the most significant digit. The set of digits and the numbers $$$a_i$$$ do not contain a digit $$$0$$$.

$$$$$$ 1 \le t \le 1000 $$$$$$ $$$$$$ 1 \le |s| \le 500 $$$$$$ $$$$$$ 1 \le n \le 50 $$$$$$ $$$$$$ 1 \le a_i \le 10^9 $$$$$$

Output

You are required to output $$$t$$$ lines containing the given quantity of numbers corresponding the conditions above. If a solution does not exist you should print "-1". If there are multiple solutions, you may output any of them.

ExampleInput
3
1234 2 21 43
12534 2 21 43
42 1 42
Output
14 23
-1
42

加入题单

算法标签: