## GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

## Most Similar Words Codeforces Solution | Codeforces Problem Solution 2022

C. Most Similar Words
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given $n$ words of equal length $m$, consisting of lowercase Latin alphabet letters. The $i$-th word is denoted ${s}_{i}$.

In one move you can choose any position in any single word and change the letter at that position to the previous or next letter in alphabetical order. For example:

• you can change 'e' to 'd' or to 'f';
• 'a' can only be changed to 'b';
• 'z' can only be changed to 'y'.

The difference between two words is the minimum number of moves required to make them equal. For example, the difference between "best" and "cost" is $1+10+0+0=11$.

Find the minimum difference of ${s}_{i}$ and ${s}_{j}$ such that $\left(i. In other words, find the minimum difference over all possible pairs of the $n$ words.

Input

The first line of the input contains a single integer $t$ ($1\le t\le 100$) — the number of test cases. The description of test cases follows.

Most Similar Words Codeforces Solution

The first line of each test case contains $2$ integers $n$ and $m$ ($2\le n\le 50$$1\le m\le 8$) — the number of strings and their length respectively.

Then follows $n$ lines, the $i$-th of which containing a single string ${s}_{i}$ of length $m$, consisting of lowercase Latin letters.

Output

For each test case, print a single integer — the minimum difference over all possible pairs of the given strings.