#AGC048A. [AGC048A] atcoder < S

[AGC048A] atcoder < S

Score : 300300 points

Problem Statement

Given is a string SS consisting of lowercase English letters. Snuke can do the operation of swapping two adjacent characters in SS. For example, if S=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 $ holds lexicographically.

The definition of the lexicographic relation $x< y$

For strings xx and yy, it is said that x<yx< y holds lexicographically if and only if one of the following conditions is satisfied:

  • There exists an integer ii (1imin(x,y)1 \leq i \leq \mathrm{min}(|x|,|y|)) such that the first i1i-1 characters of xx and those of yy are equal and the ii-th character of xx is strictly smaller than that of yy in alphabetical order.
  • x<y|x|< |y| holds, and the first x|x| characters of yy are equal to xx.
Determine whether the objective is achievable. If it is achievable, find the minimum number of operations needed.

Solve TT test cases in an input file.

Constraints

  • 1T1001 \leq T \leq 100
  • 1S10001 \leq |S| \leq 1000
  • SS 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:

TT

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

SS

Output

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

3
atcodeer
codeforces
aaa
1
0
-1
  • The first test case: for example, swapping the last two characters makes S=S=atcodere >> atcoder hold.
  • The second test case: before doing any operation, we already have S=S=codeforces >> atcoder.
  • The third test case: no sequence of operations makes S>S> atcoder hold.