Mr. Bee is going to found a new WEB 2.0 company. He decides to use “Ab3bd” as his new company name. But when he asks his artist to design some logo, the artist tells him that the name in palindrome would be easier to design. The easiest way to get a palindrome is just reverse the string and append after the original string, “Ab3bddb3bA” for example. But this name is too long and hence difficult to remember. Mr. Bee want to add fewer characters to form a palindrome. Because he is such a stupid boss, he can’t do it himself, so he ask you to help him and promise if you accomplish this job you can get an Accepted as reward.
A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inserted into the string in order to obtain a palindrome.
As an example, by inserting 2 characters, the string "Ab3bd" can be transformed intoa palindrome ("dAb3bAd" or "Adb3bdA"). However, inserting fewer than 2 characters does not produce a palindrome.
The first line contains one integer: the number of test cases k, the following k lines contains the length of the input string N, 3≦N≦5000, and one string with length N. The string is formed from uppercase letters from ‘A’ to ‘Z’, lowercase letters from ‘a’ to ‘z’ and digits from ‘0’ to ‘9’. Uppercase and lowercase letters are to be considered distinct.
For each test case output one integer in a line itself, this is the desired minimal number.
2 5 Ab3bd 5 Ab3bd
2 2
Migrated from old NTUJ.
No. | Testdata Range | Score |
---|