AtCoder Grand Contest 048
A
atcoder < S
AtCoder
⌀ 1187
原题
←B →
翻译中…

Problem Statement

翻译中…

Given is a string $S$ consisting of lowercase English letters. Snuke can do the operation of swapping two adjacent characters in $S$. For example, if $S=$agc, one operation can change it to gac (by swapping a and g) or acg (by swapping g and c).

翻译中…

Snuke wants to do this operation zero or more times so that atcoder $<S$ holds lexicographically.

翻译中…

The definition of the lexicographic relation $x< y$ For strings $x$ and $y$, it is said that $x< y$ holds lexicographically if and only if one of the following conditions is satisfied: There exists an integer $i$ ($1 \leq i \leq \mathrm{min}(|x|,|y|)$) such that the first $i-1$ characters of $x$ and those of $y$ are equal and the $i$-th character of $x$ is strictly smaller than that of $y$ in alphabetical order. $|x|< |y|$ holds, and the first $|x|$ characters of $y$ are equal to $x$.

翻译中…

Determine whether the objective is achievable. If it is achievable, find the minimum number of operations needed.

翻译中…

Solve $T$ test cases in an input file.

翻译中…

Constraints

翻译中…
  • $1 \leq T \leq 100$
  • $1 \leq |S| \leq 1000$
  • $S$ is a string consisting of lowercase English letters.
翻译中…

Input

翻译中…

Input is given from Standard Input in the following format. The first line of input is as follows:

翻译中…

T

翻译中…

Then, $T$ test cases follow, each of which is in the following format:

翻译中…

S

翻译中…

Output

翻译中…

For each test case, print $-1$ if the objective is unachievable, and print the minimum number of operations needed if it is achievable. Use one line for each case.

翻译中…

样例

Input #1
3
atcodeer
codeforces
aaa
Output #1
1
0
-1
- The first test case: for example, swapping the last two characters makes $S=$`atcodere` $>$ `atcoder` hold. - The second test case: before doing any operation, we already have $S=$`codeforces` $>$ `atcoder`. - The third test case: no sequence of operations makes $S>$ `atcoder` hold.
题目信息
题号
agc048_a
时间限制
2 s
内存限制
1024 MB

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