P16865 [GKS 2021 #H] Transform the String

Description

You are given a string $S$ which denotes a padlock consisting of lower case English letters. You are also given a string $F$ consisting of set of favorite lower case English letters. You are allowed to perform several operations on the padlock. In each operation, you can change one letter of the string to the one following it or preceding it in the alphabetical order. For example: for the letter $c$, you are allowed to change it to either $b$ or $d$ in an operation. The letters can be considered in a cyclic order, i.e., the preceding letter for letter $a$ would be letter $z$. Similarly, the following letter for letter $z$ would be letter $a$. Your aim is to find the minimum number of operations that are required such that each letter in string $S$ after applying the operations, is present in string $F$.

Input Format

The first line of the input gives the number of test cases, $T$. $T$ test cases follow. Each test case consists of two lines. The first line of each test case contains the string $S$. The second line of each test case contains the string $F$.

Output Format

For each test case, output one line containing Case #$x$: $y$, where $x$ is the test case number (starting from $1$) and $y$ is the minimum number of operations that are required such that each letter in string $S$ after applying the operations, is one of the characters in string $F$.

Explanation/Hint

In Sample Case #$1$, all the letters in string $S$ should be converted to letter $a$. We can keep on changing the letters to its preceding letter till we reach the letter $a$. We do not need to change the first letter as it is already $a$. The second letter needs $1$ operation to change it to $a$. The third letter needs $2$ operations to change it to $a$. The fourth letter needs $3$ operation to change it to $a$. Hence, we need a total of $6$ operations to change string $S$ such that all letters are changed to $a$. In Sample Case #$2$, string $S$ already contains only the favorite letter from string $F$. Hence, we do not require any more operations. ### Limits $1 \le T \le 100$. $1 \le |S| \le 10^5$. $S$ only consists of lower case English letters. $F$ only consists of distinct lower case English letters. The letters in string $F$ are lexicographically sorted. **Test Set $1$** The length of $F$ is $1$. **Test Set $2$** $1 \le |F| \le 26$.