


1. Each wheel has 10 slots: ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ’9’.
2. The wheels can rotate freely and wrap around, ‘9’ can be left rotated to ‘8’ and right rotated to ‘0’, similarly ‘0’ can be right rotated to ‘1’ and left rotated to ‘9’. Rotation of one wheel counts to one rotation.
3. Initially, the lock starts at “0000”, a string representing the state of four wheels.
The first line of input contains an integer ‘T’ denoting the number of test cases.
The first line of each test case contains an integer ‘N’ denoting the number of dead-end codes.
The second line of each test case contains the ‘N’ single-space separated dead-end codes.
The third line of each test case contains a string ‘target’ representing the unlocking code.
For each test case, print a single line containing a single integer denoting the minimum number of rotations will be printed.
The output of each test case is printed in a separate line.
You don’t have to print anything, it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= N <= 5 * 10 ^ 4
|TARGET| = 4
Where ‘T’ is the total number of test cases, ‘N’ represents the number of dead-end codes, and ‘|TARGET|’ represents the length of the target.
Time limit: 1 sec.
The idea is to use breadth first search. Find all possible codes that can be formed after one rotation in current code and for all those codes, find the next codes that can be formed after one rotation. These steps will lead to all possible codes that can be formed from the initial string. After, say ‘r’ number of rotations, the codes formed contain a code that is equal to the given target, you have reached to the target in minimum number of rotations that is ‘r’.