cswork / OAOpen practice workspace

IxlOA #3

Anagram Difference

Estimated EasyJudge-ready

Topics (AI-labelled)HashingStrings

For each pair of lowercase strings, find the minimum number of characters to replace on either side to make them anagrams; return −1 if their lengths differ.

Input

The first line contains q (1≤q≤100); each test case gives two strings: first |a| and |b|, then each string on its own line. An empty string is written as a line containing only -; non-empty strings contain only a-z, with length≤10000.

Output

For each test case, output one line with the minimum number of replacements; output −1 if the lengths differ.

Example 1

Input:

3
3 3
tea
ate
3 3
tea
toe
3 4
act
acts

Output:

0
1
-1

Example notes

Derive a linear-scan/dynamic-programming solution directly from the problem rules; the examples have been checked case by case against an independently built oracle.

Editorial

Loading solution…