A good string is defined as follows.
ab is a good string.a in front of S and b after it, is also good.You are given two good strings A and B. Using only the operation that swaps two adjacent characters, you want to turn A into B. Every string that appears while you do this must also be a good string. Write a program that finds the smallest number of operations needed.
For example, let A be aabbabab and B be aaaabbbb. Five operations turn A into B. The brackets mark the two characters swapped at that step.
aabba[ba]b → aab[ba]abb → aaba[ba]bb → aa[ba]abbb → aaa[ba]bbb → aaaabbbb
The first line contains the number of test cases T.
Each of the next lines holds one test case: the strings A and B, separated by a space. A and B are good strings, and the length of each is between 2 and 100,000.
For each test case, print on its own line the smallest number of operations needed to turn A into B. Print -1 if the change is impossible.