String Transformation

No attempts yetTime limit1sMemory limit256 MB

Problem

A good string is defined as follows.

  1. ab is a good string.
  2. If a string SS is good, then aSbaSb, obtained by putting a in front of SS and b after it, is also good.
  3. If strings SS and TT are good, then their concatenation STST is also good.

You are given two good strings AA and BB. Using only the operation that swaps two adjacent characters, you want to turn AA into BB. 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 AA be aabbabab and BB be aaaabbbb. Five operations turn AA into BB. 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

Input

The first line contains the number of test cases TT.

Each of the next lines holds one test case: the strings AA and BB, separated by a space. AA and BB are good strings, and the length of each is between 2 and 100,000.

Output

For each test case, print on its own line the smallest number of operations needed to turn AA into BB. Print -1 if the change is impossible.