Codeforces Round 750 (Div. 2)
C
Grandma Capa Knits a Scarf
Codeforces
1200
原题
← BD →
翻译中…

Grandma Capa has decided to knit a scarf and asked Grandpa Sher to make a pattern for it, a pattern is a string consisting of lowercase English letters. Grandpa Sher wrote a string $s$ of length $n$.

翻译中…

Grandma Capa wants to knit a beautiful scarf, and in her opinion, a beautiful scarf can only be knit from a string that is a palindrome. She wants to change the pattern written by Grandpa Sher, but to avoid offending him, she will choose one lowercase English letter and erase some (at her choice, possibly none or all) occurrences of that letter in string $s$.

翻译中…

She also wants to minimize the number of erased symbols from the pattern. Please help her and find the minimum number of symbols she can erase to make string $s$ a palindrome, or tell her that it's impossible. Notice that she can only erase symbols equal to the one letter she chose.

翻译中…

A string is a palindrome if it is the same from the left to the right and from the right to the left. For example, the strings 'kek', 'abacaba', 'r' and 'papicipap' are palindromes, while the strings 'abb' and 'iq' are not.

翻译中…

Input

翻译中…

The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The next $2 \cdot t$ lines contain the description of test cases. The description of each test case consists of two lines.

翻译中…

The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the length of the string.

翻译中…

The second line of each test case contains the string $s$ consisting of $n$ lowercase English letters.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

翻译中…

Output

翻译中…

For each test case print the minimum number of erased symbols required to make the string a palindrome, if it is possible, and $-1$, if it is impossible.

翻译中…

Note

翻译中…

In the first test case, you can choose a letter 'a' and erase its first and last occurrences, you will get a string 'bcaacb', which is a palindrome. You can also choose a letter 'b' and erase all its occurrences, you will get a string 'acaaca', which is a palindrome as well.

翻译中…

In the second test case, it can be shown that it is impossible to choose a letter and erase some of its occurrences to get a palindrome.

翻译中…

In the third test case, you don't have to erase any symbols because the string is already a palindrome.

翻译中…

样例

Input #1
5
8
abcaacab
6
xyzxyz
4
abba
8
rprarlap
10
khyyhhyhky
Output #1
2
-1
0
3
2
InputCopy OutputCopy
题目信息
题号
1582C
时间限制
1 s
内存限制
256 MB
标签
暴力
数据结构
贪心
字符串
双指针

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。