Solve the mystery.
Input:
First line contains a single integer denoting number of test cases(T).
Next T lines have one test case per line.
Each test case is a string of alphabets [a-z].

Output:
Print answer to each test case in an individual line.

Constraints:
1 ≤ T ≤ 100
1 ≤ |S| ≤ 100
a ≤ S[i] ≤ z

Problem Setter : Siddharth Seth

SAMPLE INPUT
5
malayalam
ariyama
alla
brook
kurruk

SAMPLE OUTPUT
121
-1
108
-1
114
t=input()
if t>=1 and t<=100:
    for i in range(0,t):
        s=raw_input()
        l=len(s)
        if l>=1 and l<=100:
            k=0   
            for j in range(0,len(s)):
                if ord(s[j])<97 and ord(s[j])>122:
                    k=1
                    break;
            if k==0:
                wd=""
                wd2=""
                for j in range(0,len(s)):
                    wd=wd+s[j]
                    
                for j in range(len(s)-1,-1,-1):
                    wd2=wd2+s[j]

                if wd==wd2:
                    print(ord(s[len(s)/2]))
                else:
                    print(-1)