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=int(raw_input())
while t:
	t-=1
	try:
		s=raw_input()
	except:
		pass
	flag=0
	if len(s)==1:
		print ord(s[0])
	else:
		for i in xrange(len(s)/2):
			if s[i]!=s[len(s)-i-1]:
				flag=1
				break
		if flag==0:
			print ord(s[i+1])
		else:
			print -1