Medium String > Basic

A **combination lock** is a type of locking device in which a sequence of symbols, usually numbers, alphabets, is used to open the lock. A **four** digit combination lock is a device that contains **four** wheels with **26** slots in each. Each slot contains a single character from (**A – Z** ) .You can rotate each of the wheel independently ,towards upward and downward, and can make any words that contains only of **four** alphabets. A **notation pointer** denoted the starting position i.e. the starting alphabet combination from which the rotation should start. ![Combination Scroll][1] **Genjamin Bates** is a famous treasure hunter. The last clue to solve the mystery now depends on unlocking the scroll as fast as possible. To unlock the device faster he asked you to design a system that will calculate the minimum seconds to unlock the scroll. A rotation to the next state takes **1** second. Input: ------ Input starts with an integer which denotes the number of test cases you need to perform **TC**.<br> Each test case contains **4** lines where each line contains 26 alphabets which will be uppercase English alphabet.<br> The first line ( **A<sub>1</sub>, A<sub>2</sub>, A<sub>3</sub>, … , A<sub>26</sub>**) denotes the alphabet of the first wheel. Similarly, for the second (**B<sub>1</sub>, B<sub>2</sub>, B<sub>3</sub>, … , B<sub>26</sub>**), third ( **C<sub>1</sub>, C<sub>2</sub>, C<sub>3</sub>, … , C<sub>26</sub>**) and fourth wheel ( **D<sub>1</sub>, D<sub>2</sub>, D<sub>3</sub>, … , D<sub>26</sub>**). <br> As all of them are wheel, it is pretty clear that the last alphabets of each list connected to the first alphabet of that list. <br> Now you have to perform **Q** queries. Each query contains two strings ( **S** and **T** ). The first one denotes the string where the **notation pointer** is pointing at for all of the four wheels. The second one denotes the secret combination lock. Constraints ------ 1 <= **TC** <= 15<br> ‘A’ <= ( **A<sub>1</sub>, A<sub>2</sub>, A<sub>3</sub>, … , A<sub>26</sub>**) <= ‘Z’<br> ‘A’ <= (**B<sub>1</sub>, B<sub>2</sub>, B<sub>3</sub>, … , B<sub>26</sub>**) <= ‘Z’<br> ‘A’ <= ( **C<sub>1</sub>, C<sub>2</sub>, C<sub>3</sub>, … , C<sub>26</sub>**) <= ‘Z’<br> ‘A’ <= ( **D<sub>1</sub>, D<sub>2</sub>, D<sub>3</sub>, … , D<sub>26</sub>**) <= ‘Z’<br> 1 <= **Q** <= 10<sup>5</sup><br> **|S| , |T|** = 4 Output: ------- For each test case, you need to print the number of the test case as “**Case X**”, where **X** denotes case number.<br> Next **Q** lines contain the result of queries where you need to find minimum number of seconds to bring alphabets of combination **T** at the notation pointer from the combination S. That means after some (may be 0) movement of wheels the notation pointer will point the string **T**. Have a look to the samples for better understandings. **Please use fast I/O methods.** Sample Input ------------ 1 ABCDEFGHIJKLMNOPQRSTUVWXYZ BCDEFGHIJKLMNOPQRSTUVWXYZA CDEFGHIJKLMNOPQRSTUVWXYZAB DEFGHIJKLMNOPQRSTUVWXYZABC 1 CEGI DFHJ Sample Output ------------- Case 1: 4 [1]: https://s3-ap-southeast-1.amazonaws.com/devskillimagestorage/questionimages/3e20f539-f9f4-c378-6867-08d584e60a4f_c8c7a4dfd35c4dbcb4c1315d57bf564b_W702xH242.png

Avik Sarkar