James found a love letter that his friend Harry has written to his girlfriend. James is a prankster, so he decides to meddle with the letter. He changes all the words in the letter into palindromes.   

To do this, he follows two rules:  

He can only reduce the value of a letter by $1$, i.e. he can change d to c, but he cannot change c to d or d to b.  
The letter $a$ may not be reduced any further.  

Each reduction in the value of any letter is counted as a single operation. Find the minimum number of operations required to convert a given string into a palindrome.

Example 

$s=ade$   

The following two operations are performed:  cde → cdd → cdc.  Return $2$.

Function Description  

Complete the theLoveLetterMystery function in the editor below.  

theLoveLetterMystery has the following parameter(s):  

string s: the text of the letter   

Returns   

int: the minimum number of operations  

Input Format

The first line contains an integer $q$, the number of queries. 

The next $q$ lines will each contain a string $s$.

Constraints

$1\leq q\leq10$ 

$1\leq$ | s | $\leq10^{4}$ 

All strings are composed of lower case English letters, ascii[a-z], with no spaces.

Sample Input
STDIN   Function
-----   --------
4       q = 4
abc     query 1 = 'abc'
abcba
abcd
cba

Sample Output
2
0
4
2

Explanation

For the first query, abc → abb → aba.
For the second query, abcba is already a palindromic string.
For the third query, abcd → abcc → abcb → abca → abba.
For the fourth query, cba → bba → aba.

Source: hackerrank | Difficulty: EASY
Tags: String algorithms


Solutions:
for t in range(int(input())):
	s = input()
	count = 0
	for i in range(len(s) // 2):
		count += abs(ord(s[i]) - ord(s[len(s) - i - 1]))
	print(count)


def case(t):
	alphabet = 'abcdefghijklmnopqrstuvwxyz'
	rev = t[::-1]
	ans = 0
	for i in range(len(t) // 2):
		op1 = t[i]
		op2 = rev[i]
		pos1 = alphabet.find(op1)
		pos2 = alphabet.find(op2)
		ans += abs(pos1 - pos2)
	return ans
t = int(input())
for i in range(t):
	s = input()
	print(case(s))


T = int(input())
for _ in range(T):
	s = input().strip()
	ans = 0
	for i in range(0, len(s) // 2):
		ans += abs(ord(s[i]) - ord(s[len(s) - i - 1]))
	print(ans)


t = int(input())
for j in range(t):
	s = input().strip()
	n = len(s)
	ans = 0
	for i in range(n // 2):
		ans += abs(ord(s[i]) - ord(s[n - i - 1]))
	print(ans)


for _ in range(int(input())):
	a = input()
	print(sum([abs(ord(a[i]) - ord(a[len(a) - i - 1])) for i in range(len(a))]) // 2)


T = int(input())
for _ in range(T):
	s = input()
	(i, j) = (0, len(s) - 1)
	count = 0
	while True:
		if i >= j:
			print(count)
			break
		count += abs(ord(s[i]) - ord(s[j]))
		i += 1
		j -= 1


T = int(input())
for Ti in range(T):
	words = input().split()
	r = 0
	for w in words:
		mi = len(w) // 2
		for li in range(mi):
			ri = len(w) - 1 - li
			r += abs(ord(w[li]) - ord(w[ri]))
	print(r)


def palindistance(s):
	distance = 0
	for i in range(int(len(s) / 2)):
		distance += abs(ord(s[i]) - ord(s[0 - (i + 1)]))
	return distance
T = int(input())
for i in range(T):
	print(palindistance(list(input())))


T = int(input())
for t in range(T):
	s = input()
	nb = 0
	for i in range(len(s) // 2):
		nb += abs(ord(s[i]) - ord(s[-i - 1]))
	print(nb)


t = int(input())
for _ in range(t):
	s = input()
	half = len(s) // 2
	a = s[:half]
	b = ''.join(reversed(s[-half:]))
	print(sum((abs(ord(a[i]) - ord(b[i])) for i in range(half))))


x = int(input())
for i in range(x):
	s = input()
	c = 0
	if len(s) % 2 == 0:
		for i in range(int(len(s) / 2)):
			c += abs(ord(s[i]) - ord(s[len(s) - 1 - i]))
	else:
		for i in range(int((len(s) - 1) / 2)):
			c += abs(ord(s[i]) - ord(s[len(s) - 1 - i]))
	print(c)


import sys
input()

def midsplit(line):
	length = len(line)
	half = int(length / 2)
	return (line[:half], line[-half:])

def distance(front, back):
	dis = 0
	for i in range(len(front)):
		fchar = front[i]
		bchar = back[-i - 1]
		dis += abs(ord(fchar) - ord(bchar))
	return dis
for line in sys.stdin:
	(front, back) = midsplit(line.strip())
	print(distance(front, back))


t = int(input())
for k in range(0, t):
	letter = input()
	rletter = letter[::-1]
	length = len(letter)
	if length % 2 == 1:
		count = 0
		start = int((length + 1) / 2)
		for i in range(0, length):
			if ord(letter[i]) > ord(rletter[i]):
				count = count + (ord(letter[i]) - ord(rletter[i]))
		print(count)
	elif length % 2 == 0:
		count = 0
		start = int(length / 2)
		for i in range(0, length):
			if ord(letter[i]) > ord(rletter[i]):
				count = count + (ord(letter[i]) - ord(rletter[i]))
		print(count)


def solve(string):
	l = len(string)
	res = 0
	for i in range(l // 2):
		res += abs(ord(string[i]) - ord(string[l - 1 - i]))
	return res
T = int(input())
for _ in range(T):
	print(solve(input()))


import sys
T = int(sys.stdin.readline())
for _ in range(T):
	s = list(sys.stdin.readline().strip())
	print(sum((abs(ord(s[i]) - ord(s[-i - 1])) for i in range(len(s) // 2))))


import sys
T = int(sys.stdin.readline())
for _ in range(T):
	N = [c for c in sys.stdin.readline().rstrip('\n')]
	Nr = N[::-1]
	if Nr == N:
		print(0)
	else:
		list = [abs(ord(x) - ord(y)) for (x, y) in zip(N, Nr)]
		print(sum(list[0:len(list) // 2]))


import math
t = int(input())
for i in range(t):
	x = input()
	count = 0
	for j in range(0, len(x) // 2):
		if ord(x[j]) != ord(x[len(x) - (1 + j)]):
			count = count + abs(ord(x[j]) - ord(x[len(x) - (1 + j)]))
	print(count)


test = int(input())
for testCase in range(test):
	curr = input()
	l = len(curr)
	diff = 0
	for i in range(int(l / 2)):
		diff += abs(ord(curr[i]) - ord(curr[l - i - 1]))
	print(diff)


import math
for i in range(int(input())):
	string = input()
	part1 = string[:math.floor(len(string) / 2)]
	part2 = string[:math.floor(len(string) / 2 - 0.5):-1]
	dist = 0
	for (i, char) in enumerate(part1):
		dist += abs(ord(char) - ord(part2[i]))
	print(dist)


import string

def make_alpha_dict():
	alpha_dict = {}
	i = 0
	for char in string.ascii_lowercase:
		alpha_dict[char] = i
		i += 1
	return alpha_dict

def palindrome(input_str, alpha_dict):
	ops = 0
	for i in range(len(input_str) // 2):
		ops += abs(alpha_dict[input_str[i]] - alpha_dict[input_str[-i - 1]])
	return ops
alpha_dict = make_alpha_dict()
test_count = int(input())
for i in range(test_count):
	input_str = input()
	print(palindrome(input_str, alpha_dict))


T = int(input())
alphabet = list('abcdefghijklmnopqrstuvwxyz')
for _ in range(T):
	in_lst = list(input())
	num_elem = len(in_lst)
	if num_elem % 2 == 1:
		val_range = int((num_elem - 1) / 2)
	else:
		val_range = int(num_elem / 2)
	if num_elem == 1:
		print(0)
	else:
		cnt_oper = 0
		for i in range(val_range):
			ltr1idx = alphabet.index(in_lst[i])
			ltr2idx = alphabet.index(in_lst[-i - 1])
			oper = abs(ltr1idx - ltr2idx)
			cnt_oper += oper
		print(cnt_oper)


x = int(input())
for i in range(x):
	s = input()
	c = 0
	if len(s) % 2 == 0:
		for i in range(int(len(s) / 2)):
			c += abs(ord(s[i]) - ord(s[len(s) - 1 - i]))
	else:
		for i in range(int((len(s) - 1) / 2)):
			c += abs(ord(s[i]) - ord(s[len(s) - 1 - i]))
	print(c)


T = int(input())
for Ti in range(T):
	s = input().split()
	r = 0
	for w in s:
		for i in range(len(w) // 2):
			r += abs(ord(w[i]) - ord(w[-i - 1]))
	print(r)


def palinder(S):
	z = tuple('abcdefghijklmnopqrstuvwxyz')
	ans = 0
	for i in range(int(len(S) / 2)):
		ans += abs(z.index(S[i]) - z.index(S[-(i + 1)]))
	return ans
for i in range(int(input())):
	print(palinder(input()))


T = int(input())
for t in range(T):
	s = input()
	v = 0
	for i in range(len(s) // 2):
		v += abs(ord(s[i]) - ord(s[-i - 1]))
	print(v)


def prog():
	for i in range(int(input())):
		text = str(input())
		count = 0
		for i in range(len(text) // 2 + (1 if len(text) % 2 == 1 else 0)):
			count += abs(ord(text[i]) - ord(text[len(text) - i - 1]))
		print(count)
prog()


import sys
import math

def main():
	f = sys.stdin
	f.__next__()
	for line in f:
		l = line.strip()
		l = list(l)
		c = 0
		ln = len(l)
		for i in range(ln // 2):
			c += abs(ord(l[i]) - ord(l[ln - i - 1]))
		print(c)
main()


import math
import fileinput

def min_ops_until_palindrome(string):
	result = 0
	for i in range(math.ceil(len(string) / 2)):
		(l, r) = (string[i], string[len(string) - 1 - i])
		result += abs(ord(l) - ord(r))
	return result
input = [l.rstrip() for l in fileinput.input()]
for string in input[1:]:
	print(min_ops_until_palindrome(string))


N = int(input())
for _ in range(N):
	s = input()
	l = len(s)
	sum = 0
	for i in range(l // 2):
		sum += abs(ord(s[i]) - ord(s[-(i + 1)]))
	print(sum)


def converter(string):
	mutations = 0
	half = int(len(string) / 2 + 0.5)
	for n in range(0, half):
		if string[n] != string[len(string) - n - 1]:
			if ord(string[n]) < ord(string[len(string) - n - 1]):
				mutations += ord(string[len(string) - n - 1]) - ord(string[n])
			else:
				mutations += ord(string[n]) - ord(string[len(string) - n - 1])
	return mutations
t = int(input())
for x in range(0, t):
	print(converter(input()))


import string
import math
T = int(input())
ALPHA = {}
for i in range(len(string.ascii_lowercase)):
	ALPHA[string.ascii_lowercase[i]] = i

def makePalindrome(string):
	moves = 0
	startIndex = 0
	endIndex = len(string) - 1
	while startIndex < endIndex:
		(a, b) = (string[startIndex], string[endIndex])
		if a == b:
			pass
		else:
			moves += abs(ALPHA[b] - ALPHA[a])
		startIndex = startIndex + 1
		endIndex = endIndex - 1
	return moves
for _ in range(T):
	print(makePalindrome(input()))


import sys
import math

def isPalindrome(word):
	last_pos = len(word) - 1
	i = 0
	while i <= last_pos:
		if word[i] != word[last_pos]:
			return i
		else:
			i = i + 1
			last_pos = last_pos - 1
	return -1

def replace(word, n, char):
	return word[:n] + char + word[n + 1:]

def changeLetter(data):
	results = []
	for word in data:
		discrepancy_index = isPalindrome(word)
		changes = 0
		for letter_index in range(math.ceil(len(word) / 2)):
			diff = abs(ord(word[letter_index]) - ord(word[len(word) - 1 - letter_index]))
			changes = changes + diff
		print(changes)
	return results
data = [x.strip() for x in sys.stdin.readlines()]
changeLetter(data[1:])


n = int(input())
for i in range(n):
	str = input()
	lan = len(str)
	cnt = 0
	for j in range(int(lan / 2)):
		cnt += abs(ord(str[j]) - ord(str[lan - j - 1]))
	print(cnt)


from itertools import islice

def min_ops_to_palindrome(s):
	rs = reversed(s)
	return sum((calc_dist(si, rsi) for (si, rsi) in islice(zip(s, rs), len(s) // 2)))

def calc_dist(c1, c2):
	return abs(ord(c1) - ord(c2))
t = int(input())
for i in range(t):
	s = input()
	print(min_ops_to_palindrome(s))


def getDiff(RHSc, LHSc):
	maximum = max(RHSc, LHSc)
	minimum = min(RHSc, LHSc)
	return maximum - minimum
nTestCases = int(input())
strings = []
for i in range(0, nTestCases):
	strings.append(input())
for string in strings:
	count = 0
	stringLength = len(string)
	if stringLength % 2 != 0:
		subset = range(int(stringLength / 2) + 1, stringLength)
	else:
		subset = range(int(stringLength / 2), stringLength)
	for i in subset:
		RHS = ord(string[i])
		LHS = ord(string[stringLength - i - 1])
		count += getDiff(RHS, LHS)
	print(count)


def turn_palindrome(s):
	(middle, rem) = divmod(len(s), 2)
	left = s[:middle if rem == 0 else middle + 1]
	right = s[:middle - 1:-1]
	count = 0
	for i in range(middle):
		if left[i] != right[i]:
			count += abs(ord(right[i]) - ord(left[i]))
	return count
T = int(input())
cases = [input() for i in range(T)]
for case in cases:
	print(turn_palindrome(case))


import sys
T = int(sys.stdin.readline())
for _ in range(T):
	s = list(sys.stdin.readline().strip())
	print(sum((abs(ord(s[i]) - ord(s[-i - 1])) for i in range(len(s) // 2))))


import fileinput
alphabet = 'abcdefghijklmnopqrstuvwxyz'

def clicks(n, line):
	answer = 0
	midpoint = int(n / 2)
	for x in range(0, midpoint):
		firstHalfLetter = line[x]
		secondHalfLetter = line[len(line) - 1 - x]
		answer += abs(alphabet.index(secondHalfLetter) - alphabet.index(firstHalfLetter))
	return answer

def main():
	file = fileinput.input()
	testinput = []
	T = int(file.readline())
	for x in range(0, T):
		line = file.readline().rstrip('\n')
		linelen = len(list(line))
		answer = clicks(linelen, line)
		print(answer)
main()


def make_palindrome(word):
	count_ops = 0
	for i in range(len(word) // 2):
		if word[i] != word[-(i + 1)]:
			count_ops += abs(ord(word[i]) - ord(word[-(i + 1)]))
	print(count_ops)
for word in range(int(input())):
	make_palindrome(input())


def palindromize(word):
	s = ''
	e = ''
	i = 0
	k = -1
	r = 0
	c = ''
	while i < len(word) / 2:
		a = word[i]
		b = word[k]
		if a == b:
			p = 0
			c = a
		elif a < b:
			p = ord(b) - ord(a)
			c = a
		elif b < a:
			p = ord(a) - ord(b)
			c = b
		i += 1
		k -= 1
		s = s + c
		e = c + e
		r += p
	return r
T = int(input())
while T > 0:
	case = str(input())
	print(palindromize(case))
	T -= 1


numTestCases = int(input())
for i in range(numTestCases):
	origStr = input()
	counter = 0
	for i in range(len(origStr) // 2):
		counter += abs(ord(origStr[i]) - 97 - (ord(origStr[i * -1 - 1]) - 97))
	print(counter)


from math import floor

def computeCount(string):
	count = 0
	for i in range(floor(len(string) / 2)):
		intValueBegin = ord(string[i])
		intValueEnd = ord(string[len(string) - 1 - i])
		while intValueBegin != intValueEnd:
			if intValueBegin > intValueEnd:
				intValueBegin -= 1
			else:
				intValueEnd -= 1
			count += 1
	return count
N = int(input())
for i in range(N):
	string = input()
	print(computeCount(string))


import math

def abso(inp):
	return int(math.fabs(inp))
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
diff = []
words = []
testcases = int(input())
for what in range(testcases):
	words.append(input())
for number in range(testcases):
	word = words[number]
	diff.append(0)
	if len(word) % 2 == 0:
		for no in range(0, int(len(word) / 2)):
			index1 = letters.index(word[no])
			index2 = letters.index(word[len(word) - 1 - no])
			diff[number] += abso(index1 - index2)
	else:
		for no in range(0, int((len(word) - 1) / 2)):
			index1 = letters.index(word[no])
			index2 = letters.index(word[len(word) - 1 - no])
			diff[number] += abso(index1 - index2)
for number in diff:
	print(number)


n = int(input())
for i in range(n):
	word = list(input())
	if len(word) == 1:
		print(0)
		continue
	left = word[:len(word) // 2]
	right = list(word[len(word) // 2:])
	if len(word) % 2 == 1:
		right = right[1:]
	s = 0
	for (l, r) in zip(left, right[::-1]):
		s += abs(ord(l) - ord(r))
	print(s)


import sys
from operator import abs

def main():
	t = sys.stdin.readline().strip()
	for i in range(int(t)):
		testcase = sys.stdin.readline().strip()
		cnt = 0
		for j in range(int(len(testcase) / 2)):
			cnt = cnt + abs(ord(testcase[j]) - ord(testcase[len(testcase) - 1 - j]))
		print(cnt)
main()


def palendrome_it(ords):
	positions = len(ords) // 2
	steps = 0
	for i in range(positions):
		steps += abs(ords[i] - ords[-(i + 1)])
	return steps
t = int(input())
for i in range(t):
	test_case = [ord(ch) for ch in input()]
	print(palendrome_it(test_case))


t = int(input())
while t > 0:
	in_ = list(map(ord, input()))
	n = len(in_) // 2
	res = sum(list(map(lambda c1, c2: abs(c1 - c2), in_[:n], reversed(in_[-n:]))))
	print(res)
	t = t - 1


def operations(left, right, length):
	size = length
	operations = 0
	for i in range(0, size):
		if ord(left[i]) < ord(right[size - 1 - i]):
			operations = operations + ord(right[size - 1 - i]) - ord(left[i])
		elif ord(left[i]) > ord(right[size - 1 - i]):
			operations = operations + ord(left[i]) - ord(right[size - 1 - i])
	return operations
T = int(input())
for i in range(0, T):
	palindrome = str(input())
	length = len(palindrome)
	if length % 2 == 0:
		left_pal = list(palindrome[:int(length / 2)])
		right_pal = list(palindrome[int(length / 2):])
		print(operations(left_pal, right_pal, int(length / 2)))
	else:
		left_pal = list(palindrome[:int(length / 2)])
		right_pal = list(palindrome[int(length / 2) + 1:])
		print(operations(left_pal, right_pal, int(length / 2)))


alphabet = 'abcdefghijklmnopqrstuvwxyz'

def comp_chars(c1, c2):
	c1_val = alphabet.index(c1)
	c2_val = alphabet.index(c2)
	return c1_val - c2_val
test_cases = int(input())
for i in range(test_cases):
	word = input()
	changes = 0
	for j in range(len(word) // 2):
		comparison = comp_chars(word[j], word[-(j + 1)])
		if comparison > 0:
			changes += comparison
		elif comparison < 0:
			changes -= comparison
	print(changes)


nstrings = int(input())
for _ in range(nstrings):
	q = 0
	s = input()
	l = len(s)
	for i in range(l // 2):
		q += abs(ord(s[i]) - ord(s[l - 1 - i]))
	print(q)


def count_edits(index, string):
	first = ord(string[index])
	second = ord(string[len(string) - index - 1])
	if first < second:
		return second - first
	return first - second
count = int(input())
for i in range(count):
	string = input()
	edits = 0
	for i in range(int(len(string) / 2)):
		edits += count_edits(i, string)
	print(edits)


T = int(input())
count = 0
words = []
wordascii = []
for x in range(0, T):
	words.append(input())
for word in words:
	for letter in word:
		wordascii.append(ord(letter))
	size = len(wordascii)
	for index in range(0, int(size / 2)):
		count = count + abs(wordascii[index] - wordascii[size - 1 - index])
	print(count)
	count = 0
	del wordascii[:]


size = int(input())
for _ in range(size):
	word = str(input())
	current = 0
	for i in range(int(len(word) / 2)):
		current += abs(ord(word[i]) - ord(word[len(word) - i - 1]))
	print(current)


t = int(input())
for i in range(t):
	s = input()
	(b, e) = (0, len(s) - 1)
	cnt = 0
	while b < e:
		if s[b] != s[e]:
			cnt += abs(ord(s[e]) - ord(s[b]))
		b += 1
		e -= 1
	print(cnt)


import string
alphabet = list(string.ascii_lowercase)
for _ in range(int(input())):
	word = input()
	wordLen = len(word)
	operations = 0
	for i in range(int(wordLen / 2)):
		if word[i] != word[wordLen - 1 - i]:
			operations += abs(alphabet.index(word[i]) - alphabet.index(word[wordLen - 1 - i]))
	print(operations)


def change_number(string):
	total = 0
	half_point = len(string) // 2
	first_half = string[:half_point]
	second_half = string[-half_point:]
	for (c1, c2) in zip(first_half, reversed(second_half)):
		difference = abs(ord(c1) - ord(c2))
		total += difference
	return total
cases = int(input())
for _ in range(cases):
	string = input()
	print(change_number(string))


import sys
from math import floor
num_test_cases = int(sys.stdin.readline())
for i in range(num_test_cases):
	test_case = sys.stdin.readline().strip()
	test_len = len(test_case)
	str_to_copy = test_case[:floor((test_len - 1) / 2) + (test_len + 1) % 2]
	str_to_modify = test_case[floor(test_len / 2) + (test_len + 0) % 2:]
	copy_list = list(str_to_copy)
	mod_list = list(str_to_modify)
	mod_list.reverse()
	count = 0
	iter = 0
	for x in mod_list:
		target_num = ord(copy_list[iter])
		mod_num = ord(x)
		count = count + abs(mod_num - target_num)
		iter = iter + 1
	print(str(count))


def calculate(s):
	answer = 0
	for i in range(len(s) // 2):
		answer += abs(ord(s[i]) - ord(s[len(s) - i - 1]))
	return answer
n = int(input())
for i in range(n):
	s = input()
	print(calculate(s))


T = int(input())
for x in range(T):
	String = input()
	z = 0
	Steps = 0
	for x in range(int(len(String) / 2)):
		if String[-1 - z] != String[0 + z]:
			Steps = Steps + abs(ord(String[-1 - z]) - ord(String[0 + z]))
		z += 1
	print(Steps)


import sys
letters = list('abcdefghijklmnopqrstuvwxyz')

def readinput(input):
	testcases = []
	for (i, l) in enumerate(input):
		if i == 0:
			pass
		else:
			testcases.append(l.strip())
	return testcases

def palindrome(s):
	steps = 0
	word = list(s)
	i = 0
	j = len(word) - 1
	while True:
		if j <= i:
			break
		steps += abs(letters.index(word[j]) - letters.index(word[i]))
		j -= 1
		i += 1
	return steps
for word in readinput(sys.stdin):
	print(palindrome(word))


T = int(input())
for t in range(T):
	word = input()
	count = 0
	j = 0
	for w in word:
		temp = word[len(word) - j - 1]
		if j == int(len(word) / 2):
			j += 1
			break
		if ord(w) != ord(temp):
			count += abs(ord(temp) - ord(w))
		j += 1
	print(count)


import math
for n in range(int(input())):
	word = input()
	length = len(word)
	moves = 0
	for i in range(math.floor(length / 2)):
		moves += abs(ord(word[length - 1 - i]) - ord(word[i]))
	print(moves)


def is_palindrome(case, str_len):
	N = 0
	if case == case[::-1]:
		return N
	else:
		for i in range(0, int(str_len / 2)):
			if case[i] == case[str_len - i - 1]:
				continue
			else:
				N = N + abs(ord(case[str_len - i - 1]) - ord(case[i]))
	return N
T = int(input())
for i in range(T):
	case = input()
	str_len = len(case)
	print(is_palindrome(case, str_len))


N = int(input())
assert N <= 10 and N >= 1
for _ in range(N):
	word = list(input())
	size = len(word)
	assert size >= 1 and size <= 10000
	operation = 0
	for i in range(0, int(size / 2)):
		operation += abs(ord(word[i]) - ord(word[-1 - i]))
	print(operation)


def run(s):
	i = 0
	j = len(s) - 1
	n = 0
	while i < j:
		(a, b) = (ord(s[i]), ord(s[j]))
		n += abs(a - b)
		i += 1
		j -= 1
	return n
cases = int(input())
for i in range(cases):
	print(run(input()))


def main():
	N = int(input())
	for i in range(N):
		string = str(input())
		sum = 0
		for j in range(len(string) // 2):
			(fv, lv) = (string[j], string[len(string) - j - 1])
			sum += abs(ord(lv) - ord(fv))
		print(sum)
main()


def love_letter_mystery(word):
	operations = 0
	tot_operations = 0
	j = -1
	i = 0
	if is_palindrome(word) == True:
		return 0
	else:
		for i in range(0, len(word)):
			word_first = word[i]
			word_last = word[j]
			j = j - 1
			if word_first == word_last:
				continue
			else:
				operations = reduction(word_last, word_first)
				tot_operations = operations + tot_operations
	return tot_operations

def reduction(word_last, word_first):
	operations = 0
	if ord(word_last) > ord(word_first):
		operations = ord(word_last) - ord(word_first)
	else:
		return operations
	return operations

def is_palindrome(word):
	i = 0
	j = -1
	flag = 0
	for k in range(0, len(word)):
		if word[i] == word[j]:
			flag = 0
			j = j - 1
			i = i + 1
		else:
			flag = 1
	if flag == 1:
		return False
	else:
		return True
T = int(input())
for i in range(0, T):
	word = input()
	result = love_letter_mystery(word)
	print(result)


T = int(input())
for _ in range(T):
	s = input()
	nops = 0
	for i in range(len(s) // 2):
		nops += abs(ord(s[i]) - ord(s[-i - 1]))
	print(nops)


def isPalindrome(word):
	return word == word[::-1]

def numOperationsToPalindrome(word):
	if isPalindrome(word):
		return 0
	opsCount = 0
	left = 0
	right = len(word) - 1
	while not isPalindrome(word):
		if word[left] != word[right]:
			opsCount += abs(ord(word[left]) - ord(word[right]))
			if ord(word[left]) > ord(word[right]):
				word[left] = word[right]
			else:
				word[right] = word[left]
		else:
			left += 1
			right -= 1
	return opsCount
for _ in range(int(input())):
	print(numOperationsToPalindrome(list(input())))


tc = int(input())
for _ in range(tc):
	pal = input()
	sum = 0
	for (a, b) in zip(pal, pal[::-1]):
		if a != b:
			sum += abs(ord(a) - ord(b))
	print(int(sum / 2))


tests = int(input())
letters = []
for t in range(0, tests):
	letters.append(str(input()))

def makePalindrome(letter):
	letter = list(letter)
	steps = 0
	i = 0
	while i < int(len(letter) / 2):
		if ord(letter[i]) < ord(letter[-(i + 1)]):
			steps += ord(letter[-(i + 1)]) - ord(letter[i])
			letter[-(i + 1)] = chr(ord(letter[-(i + 1)]) - (ord(letter[-(i + 1)]) - ord(letter[i])))
		elif ord(letter[i]) > ord(letter[-(i + 1)]):
			steps += ord(letter[i]) - ord(letter[-(i + 1)])
			letter[i] = chr(ord(letter[i]) - (ord(letter[i]) - ord(letter[-(i + 1)])))
		i += 1
	return steps

def isPalindrome(letter):
	for i in range(0, int(len(letter) / 2)):
		if letter[i] != letter[-i]:
			return False
	return True
for l in letters:
	print(makePalindrome(l))


import fileinput
src = fileinput.input()
T = int(src.readline())
for _ in range(T):
	input = src.readline().lower().strip()
	cnt = 0
	fst = list(input[0:int(len(input) / 2)])
	snd = list(input[len(input) - int(len(input) / 2):len(input)][::-1])
	for i in range(len(fst)):
		while fst[i] != snd[i]:
			if ord(fst[i]) < ord(snd[i]):
				snd[i] = chr(ord(snd[i]) - 1)
			else:
				fst[i] = chr(ord(fst[i]) - 1)
			cnt += 1
	print(cnt)


def count_steps(str):
	reductions = 0
	for i in range(len(str) // 2):
		num1 = ord(str[i])
		num2 = ord(str[-1 - i])
		reductions = reductions + abs(num1 - num2)
	return reductions
test_cases = int(input())
i = 0
while i < test_cases:
	i = i + 1
	str = input()
	print(count_steps(str))


import sys

def countOperations(stringList):
	count = 0
	while len(stringList) > 1:
		if stringList[0] == stringList[-1]:
			stringList = stringList[1:-1]
		elif stringList[0] > stringList[-1]:
			stringList[0] = chr(ord(stringList[0]) - 1)
			count += 1
		elif stringList[0] < stringList[-1]:
			stringList[-1] = chr(ord(stringList[-1]) - 1)
			count += 1
	return count
t = int(sys.stdin.readline())
assert 1 <= t <= 10
for i in range(0, t, 1):
	line = str(sys.stdin.readline()).strip()
	print(countOperations(list(line)))


import fileinput
import math
lines = []
for line in fileinput.input():
	lines.append(line)
lines = lines[1:]
for i in range(0, len(lines)):
	if '\n' in lines[i]:
		lines[i] = lines[i][:-1]
for line in lines:
	half_length = int(math.floor(len(line) / 2))
	steps = 0
	for i in range(0, half_length):
		steps += int(math.fabs(ord(line[i]) - ord(line[-(i + 1)])))
	print(steps)


testcases = int(input())
for testcase in range(testcases):
	word = input()
	total = 0
	for letter in range(len(word) // 2):
		total += abs(ord(word[letter]) - ord(word[-letter - 1]))
	print(total)


def palindrome_actions(s):
	res = 0
	for i in range(len(s) // 2):
		res += abs(ord(s[i]) - ord(s[-i - 1]))
	return res
test_cases = int(input())
while test_cases > 0:
	test_cases -= 1
	print(palindrome_actions(input()))


cases = int(input())
for i in range(cases):
	string = input().strip()
	length = len(string) // 2
	changes = 0
	for x in range(length):
		changes += abs(ord(string[x]) - ord(string[-x - 1]))
	print(changes)


def is_palindrome(str):
	return str == str[::-1]
n = int(input())
for i in range(n):
	str = input()
	steps = 0
	(first, last) = (0, len(str) - 1)
	while not is_palindrome(str):
		str = list(str)
		while str[first] > str[last]:
			str[first] = chr(ord(str[first]) - 1)
			steps += 1
		while str[last] > str[first]:
			str[last] = chr(ord(str[last]) - 1)
			steps += 1
		first += 1
		last -= 1
		str = ''.join(str)
	print(steps)


n = int(input())
alpha = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
ls = []
for x in range(0, n):
	num = 0
	ls = list(input())
	length = int(len(ls) / 2)
	a = []
	b = []
	for y in range(0, length):
		a.append(ls[y])
		b.append(ls[len(ls) - y - 1])
		A = 0
		B = 0
		for z in range(0, 26):
			if a[y] == alpha[z]:
				A = z
			if b[y] == alpha[z]:
				B = z
		num += abs(A - B)
	print(num)


for t in range(int(input())):
	ops = 0
	word = input()
	length = len(word)
	for i in range(length // 2):
		ops += abs(ord(word[i]) - ord(word[length - 1 - i]))
	print(ops)


def count_steps(word):
	word = list(word)
	if len(word) % 2 != 0:
		word.pop(len(word) // 2)
	m = len(word) // 2
	front = word[:m]
	back = word[m:]

	def count(char_pair):
		(ca, cb) = char_pair
		return abs(ord(ca) - ord(cb))
	return sum(map(count, zip(front, reversed(back))))
t = int(input())
for i in range(t):
	print(count_steps(input()))


def loveLetter(word):
	steps = 0
	for i in range(len(word) // 2):
		leftChar = word[i]
		rightChar = word[-i - 1]
		diff = abs(ord(leftChar) - ord(rightChar))
		steps += diff
	return steps
testCases = int(input())
for i in range(testCases):
	print(loveLetter(input()))


from math import ceil
num_of_cases = int(input())
for case in range(num_of_cases):
	word = input()
	operations = 0
	for index in range(ceil(len(word) / 2)):
		operations += abs(ord(word[index]) - ord(word[-index - 1]))
	print(operations)


t = int(input())
for i in range(t):
	s = input()
	half = len(s) // 2
	first = ''
	second = ''
	rev = ''
	op = 0
	first = s[:half]
	second = s[half:]
	for i in second:
		rev = i + rev
	for i in range(len(first)):
		if ord(rev[i]) > ord(first[i]):
			op = op + (ord(rev[i]) - ord(first[i]))
		else:
			op = op + (ord(first[i]) - ord(rev[i]))
	print(op)


T = int(input())
for i in range(T):
	line = input()
	c = sum([abs(ord(line[j]) - ord(line[-j - 1])) for j in range(len(line) // 2)])
	print(c)


def isPalindrome(word_list):
	start = 0
	end = len(word_list) - 1
	while start < end:
		if word_list[start] != word_list[end]:
			return False
		else:
			start += 1
			end -= 1
	return True

def findNumberOfOperations(word_list):
	numOperations = 0
	start = 0
	end = len(word_list) - 1
	while start < end:
		maxChar = max(ord(word_list[start]), ord(word_list[end]))
		minChar = min(ord(word_list[start]), ord(word_list[end]))
		diff = abs(maxChar - minChar)
		numOperations += diff
		start += 1
		end -= 1
	return numOperations
tests = int(input())
for i in range(tests):
	word = str(input())
	word_list = list(word)
	print(findNumberOfOperations(word_list))


num_cases = int(input())
for _ in range(num_cases):
	line = input()
	total_diff = 0
	for i in range(len(line) // 2):
		diff = abs(ord(line[i]) - ord(line[-i - 1]))
		total_diff += diff
	print(total_diff)


cases = int(input())
for i in range(cases):
	operations = 0
	string = input()
	(start, end) = (0, len(string) - 1)
	while start < end:
		operations += abs(ord(string[start]) - ord(string[end]))
		start += 1
		end -= 1
	print(operations)


import sys
import math

def main():
	list = []
	testcases = input()
	for i in range(0, int(testcases)):
		list.append(input())
	for s in list:
		print(minOp(s))
	return

def minOp(s):
	letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
	count = 0
	i = 0
	while i < len(s) // 2:
		if s[i] != s[-1 - i]:
			count += math.fabs(letters.index(s[i]) - letters.index(s[-1 - i]))
			i += 1
		else:
			i += 1
	return int(count)
main()


for t in range(int(input())):
	a = input()
	s = 0
	for i in range(len(a) // 2):
		s += abs(ord(a[i]) - ord(a[len(a) - 1 - i]))
	print(s)


import string
import math
l = string.ascii_letters
t = int(input())
while t > 0:
	message = input()
	pal = ['', '']
	if len(message) % 2 == 0:
		pal[0] = message[0:int(len(message) / 2)]
		pal[1] = message[int(len(message) / 2):len(message)]
	else:
		pal[0] = message[0:int(len(message) / 2) + 1]
		pal[1] = message[int(len(message) / 2):len(message)]
	pal[0] = pal[0][::-1]
	t -= 1
	sum = 0
	for i in range(0, len(pal[0])):
		left = pal[0][i]
		right = pal[1][i]
		sum += math.fabs(l.index(left) - l.index(right))
	print(int(sum))


T = int(input())
for x in range(0, T):
	string = input()
	left = 0
	right = 0
	count = 0
	left = int(len(string) / 2) - 1
	if len(string) % 2 == 1:
		right = left + 2
	else:
		right = left + 1
	while left >= 0:
		count = abs(ord(string[left]) - ord(string[right])) + count
		left = left - 1
		right = right + 1
	print(count)


for t in range(int(input())):
	s = list(map(ord, input()))
	print(sum((abs(a - b) for (a, b) in zip(s, s[::-1]))) // 2)


def chardist(x, y):
	if x > y:
		return ord(x) - ord(y)
	else:
		return ord(y) - ord(x)
t = int(input())
for i in range(t):
	word = input()
	ops = 0
	for j in range(len(word) // 2):
		ops = ops + chardist(word[j], word[-(j + 1)])
	print(ops)


T = int(input())
i = 0
for x in range(0, T):
	testString = str(input())
	testStringLength = len(testString)
	numberOfOperations = 0
	for y in range(0, testStringLength // 2):
		numberOfOperations += abs(ord(testString[y]) - ord(testString[testStringLength - (y + 1)]))
	print(numberOfOperations)


T = int(input())
for _ in range(T):
	the_string = input()
	ops = 0
	for front_index in range(len(the_string) // 2):
		back_index = (front_index + 1) * -1
		ops += abs(ord(the_string[front_index]) - ord(the_string[back_index]))
	print(ops)


def foo(string):
	if string == string[::-1] or len(string) <= 1:
		return 0
	else:
		return abs(ord(string[0]) - ord(string[len(string) - 1])) + foo(string[1:len(string) - 1])
import sys
sys.setrecursionlimit(10000)
n = int(input())
for i in range(n):
	a = foo(input())
	print(a)


t = int(input())
for i in range(t):
	s = input()
	v = 0
	for j in range(len(s) // 2):
		v += abs(ord(s[j]) - ord(s[len(s) - j - 1]))
	print(v)


n = int(input())
i = 0
answer = 0
while i < n:
	buff = input()
	j = 0
	answer = 0
	limit = len(buff) / 2
	count = len(buff) - 1
	while j < limit:
		if buff[j] > buff[count - j]:
			answer = answer + ord(buff[j]) - ord(buff[count - j])
		else:
			answer = answer + ord(buff[count - j]) - ord(buff[j])
		j = j + 1
	i = i + 1
	print(answer)