The Combination Lock

easy graphs bfs shortest-paths 1000 ms · 256 MB

The Combination Lock

Mira's bike lock has four wheels side by side. Each wheel shows one digit from 0 to 9, and the code the lock displays is the four digits read from left to right, for example 0391.

In one move Mira turns exactly one wheel by one notch, either up or down. Turning up changes a digit d to d + 1, and turning down changes it to d - 1. The wheels wrap around: turning 9 up gives 0, and turning 0 down gives 9. All other wheels keep their digits.

The lock is old, and some codes jam it: if the lock ever displays a jammed code, it gets stuck for good. Every code the lock displays counts, including the code it shows before the first move and the code it shows after the last one.

The lock currently displays 0000, and Mira wants it to display a target code. Find the minimum number of moves she needs without the lock ever displaying a jammed code, or report that it is impossible. In particular, the answer is -1 if 0000 itself or the target is jammed, and 0 if the target is 0000 and 0000 is not jammed.

Mira has several locks to open, so answer T independent test cases.

Input

The first line contains an integer T (1 ≤ T ≤ 100), the number of test cases.

Each test case starts with a line containing a string s and an integer m (0 ≤ m ≤ 10^4): the target code and the number of jammed codes. The next line contains m jammed codes separated by spaces (the line is empty when m = 0).

Every code, including s, consists of exactly four digits 0–9, leading zeros included. The jammed codes of a test case may repeat.

Output

For each test case print one line: the minimum number of moves to make the lock display s, or -1 if it cannot be done.

Notes

In the first test case of the first example nothing is jammed, so each wheel is turned on its own along the shorter direction: 1 notch up for the first wheel, 1 down for the second (0 to 9), and 2 up for the fourth, for a total of 4. 0 jams shows up as an empty line in the input.

In the second test case, 0000 can only change into 1000, 9000, 0100, 0900, 0010, 0090, 0001 and 0009, and all eight of them are jammed, so the lock can never leave 0000.

In the third test case the target 0002 is jammed, so the answer is -1 even though it is close.

In the fourth test case the direct route 0000 → 0001 → 0002 passes through the jammed 0001, and it is the only route with 2 moves. No route has exactly 3 moves. One shortest valid route is 0000 → 0100 → 0101 → 0102 → 0002, using 4 moves.

In the second example, the first lock already shows its target 0000, so the answer is 0. In the second test case 0000 is jammed (it is listed twice), so the lock is stuck from the start. In the third test case nothing is jammed and every wheel needs 5 notches in either direction, 20 moves in total.

Example 1

Input

4
1902 0

0005 8
1000 9000 0100 0900 0010 0090 0001 0009
0002 1
0002
0002 4
0001 0009 0010 0090

Output

4
-1
-1
4

Example 2

Input

3
0000 0

1234 2
0000 0000
5555 0

Output

0
-1
20

Hints · reveal one at a time if you're stuck

Hint 1

Treat every four-digit code as a vertex, with an edge between two codes that differ by one notch on exactly one wheel. Since every move costs the same, what search finds the fewest moves, and how do the jammed codes change the graph?

Hint 2

Breadth-first search from 0000 over only the 10000 codes works, skipping jammed codes. Check the start and the target for jams first, and remember the wheels wrap from 9 to 0, so each wheel has two neighbours. Reset the jammed marks between test cases.

report an issue Prefer your own editor? sooks prep 54 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.