Sardar Singh has many men fighting for him, and he would like to calculate the power of each of them to better plan for his fight against Ramadhir.

The power of a string S of lowercase English alphabets is defined to be 

\sum_{i = 1}^{|S|} i\cdot ord(S_{i})

where ord(S_{i}) is the position of S_{i} in the alphabet, i.e, ord('a') = 1, ord('b') = 2, \dots, ord('z') = 26.

Each of Sardar Singh's men has a name consisting of lowercase English alphabets. The power of a man is defined to be the maximum power over all possible rearrangements of this string.

Find the power of each of Sardar Singh's men.

------ Input Format ------ 

- The first line of input contains an integer T, denoting the total number of Sardar Singh's men.
- Each of the next T lines contains a single string S_{i}, the name of Sardar Singh's i-th man.

------ Output Format ------ 

- Output T lines, each containing a single integer. The i-th of these lines should have the power of the i-th of Sardar Singh's men.

------ Constraints ------ 

$1 ≤ T ≤ 60$
$1 ≤ |S_{i}| ≤ 100$ 
$S_{i}$ consists of lowercase english alphabets only.

----- Sample Input 1 ------ 
1
faizal
----- Sample Output 1 ------ 
273
----- explanation 1 ------ 
The rearrangement with maximum power is $aafilz$. Its power can be calculated as
$$
1\cdot ord('a') + 2\cdot ord('a') + 3\cdot ord('f') + 4\cdot ord('i') + 5\cdot ord('l') + 6\cdot ord('z')
$$
which equals $273$.
It can be verified that no rearrangement gives a larger power.

Source: codechef | Difficulty: EASY
Tags: String algorithms

Data structures


Solutions:
for _ in range(int(input())):
	s = sorted(input())
	result = 0
	for i in range(len(s)):
		result += (i + 1) * (ord(s[i]) - 96)
	print(result)


n = int(input())
for i in range(n):
	x = input()
	fc = [0] * 26
	for i in x:
		p = ord(i) - 97
		fc[p] += 1
	p = 1
	s = 0
	for i in range(len(fc)):
		if fc[i] != 0:
			if fc[i] > 1:
				k = fc[i]
				while k != 0:
					s += p * (i + 1)
					k -= 1
					p += 1
			else:
				s += p * (i + 1)
				p += 1
	print(s)


for i in range(int(input())):
	x = list(input())
	x.sort()
	c = 1
	s = 0
	for j in x:
		s = s + c * (ord(j) - 96)
		c = c + 1
	print(s)


t = int(input())
for _ in range(t):
	s = input()
	n = len(s)
	L = list(s)
	L.sort()
	total = 0
	for i in range(0, n):
		total += (i + 1) * (ord(L[i]) - ord('a') + 1)
	print(total)


t = int(input())
for i in range(t):
	l = list(input())
	l.sort()
	n = 'abcdefghijklmnopqrstuvwxyz'
	n = list(n)
	s = 0
	for j in range(1, len(l) + 1):
		ind = n.index(l[j - 1])
		s = s + j * (ind + 1)
	print(s)


t = int(input())
for i in range(t):
	l = []
	x = 0
	s = input()
	l = list(s.strip(''))
	l.sort()
	for j in range(len(l)):
		x += (j + 1) * (ord(l[j]) - 96)
	print(x)


t = int(input())
for i in range(t):
	a = input()
	sort_char = sorted(a)
	a = ''.join(sort_char)
	b = ['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']
	m = 0
	for j in range(len(a)):
		for k in range(len(b)):
			if a[j] == b[k]:
				m = m + (j + 1) * (k + 1)
	print(m)


t = int(input())
for i in range(t):
	l = list(input())
	p = l
	sum = 0
	c = 0
	l.sort()
	for i in range(len(l)):
		c = c + 1
		sum = sum + c * (ord(l[i]) - 96)
	print(sum)


t = int(input())
for _ in range(t):
	s = list(input())
	s.sort()
	l = []
	for i in s:
		l.append(ord(i) - 96)
	su = 0
	for j in range(len(l)):
		su = su + (j + 1) * l[j]
	print(su)


for i in range(int(input())):
	s = input()
	a = 1
	f = [0 for j in range(27)]
	for j in s:
		f[ord(j) - ord('a') + 1] += 1
	ans = 0
	for j in range(27):
		while f[j] > 0:
			ans += a * j
			a += 1
			f[j] -= 1
	print(ans)


for i in range(int(input())):
	x = sorted(input())
	a = 0
	c = 1
	for i in x:
		a += c * (ord(i) - 96)
		c += 1
	print(a)


n = int(input())
for i in range(n):
	x = input()
	x = x.lower()
	z = sorted(x)
	c = 0
	for i in range(1, len(x) + 1):
		c += i * (ord(z[i - 1]) - 96)
	print(c)


for _ in range(int(input())):
	s = list(input())
	s.sort()
	l = {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6, 'g': 7, 'h': 8, 'i': 9, 'j': 10, 'k': 11, 'l': 12, 'm': 13, 'n': 14, 'o': 15, 'p': 16, 'q': 17, 'r': 18, 's': 19, 't': 20, 'u': 21, 'v': 22, 'w': 23, 'x': 24, 'y': 25, 'z': 26}
	ans = 0
	for i in range(len(s)):
		ans += (i + 1) * l.get(s[i])
	print(ans)


for _ in range(int(input())):
	s = input()
	sum = 0
	s = list(s)
	s.sort()
	for i in range(len(s)):
		sum += (ord(s[i]) - 96) * (i + 1)
	print(sum)


for t in range(int(input())):
	s = str(input())
	S = sorted(s)
	res = 0
	for i in range(len(S)):
		res += (i + 1) * ((ord(S[i]) + 4) % 100)
	print(res)


for i in range(int(input())):
	s = sorted(input().strip())
	a = 0
	for i in range(1, len(s) + 1):
		a += i * (ord(s[i - 1]) - 96)
	print(a)


import string
alpha = []
for letter in string.ascii_lowercase:
	alpha.append(letter)
t = int(input())

def result(st):
	arr = list(st)
	arr.sort()
	cool = 0
	for i in range(0, len(st)):
		ind = alpha.index(arr[i]) + 1
		cool += (i + 1) * ind
	return cool
for i in range(0, t):
	st = input()
	print(result(st))


for _ in range(int(input())):
	n = input()
	sum = 0
	n = ''.join(sorted(n))
	for i in range(len(n)):
		sum += (i + 1) * (ord(n[i]) - 96)
	print(sum)


for i in range(int(input())):
	a = input()
	a = sorted(a)
	r = 1
	sum = 0
	for i in a:
		sum += r * (ord(i) - 96)
		r += 1
	print(sum)


for _ in range(int(input())):
	n = sorted(input())
	c = 0
	for i in range(len(n)):
		c += (i + 1) * (ord(n[i]) - 96)
	print(c)


for _ in range(int(input())):
	name = input()
	s = ''.join(sorted(name))
	f = 0
	for i in range(len(s)):
		p = ord(s[i]) - 96
		f = f + (i + 1) * p
	print(f)


for _ in range(int(input())):
	a = ''.join(sorted(input()))
	(k, m) = (0, 1)
	for i in a:
		k += m * (ord(i) - 96)
		m += 1
	print(k)


for i in range(int(input())):
	l = list(input())
	l.sort()
	t = 'abcdefghijklmnopqrstuvwxyz'
	x = 0
	for i in range(len(l)):
		x += (i + 1) * (t.index(l[i]) + 1)
	print(x)


def ordi(s: str):
	return ord(s) - 96
n = int(input())
for i in range(n):
	a = []
	s = input()
	for i in s:
		a.append(i)
	a.sort()
	p = 0
	j = 1
	for i in range(len(a)):
		p += j * ordi(a[i])
		j += 1
	print(p)


for t in range(int(input())):
	s = input()
	l = list(s)
	l.sort()
	s = ''.join(l)
	sum = 0
	for i in range(len(s)):
		sum += (i + 1) * (ord(s[i]) - 96)
	print(sum)


for t in range(int(input())):
	s = input()
	s = sorted(s)
	l = 'abcdefghijklmnopqrstuvwxyz'
	r = 0
	for i in range(len(s)):
		r += (i + 1) * (l.index(s[i]) + 1)
	print(r)


for _ in range(int(input())):
	S = sorted(input())
	power = 0
	index = 1
	for s in S:
		power += index * (ord(s) - 96)
		index += 1
	print(power)


for i in range(int(input())):
	S = list(sorted(input()))
	a = 0
	z = 1
	for i in range(0, len(S)):
		a = a + z * (ord(S[i]) - 96)
		z += 1
	print(a)


T = int(input())
for i in range(T):
	S = sorted(input())
	p = 0
	c = 1
	for j in S:
		p = p + c * (ord(j) - 96)
		c = c + 1
	print(p)


t = int(input())
for i in range(t):
	s = input()
	lst = []
	for c in s:
		lst.append(int(ord(c) - 96))
	lst = sorted(lst)
	total = 0
	for (i, j) in enumerate(lst, 1):
		total += i * j
	print(total)


for _ in range(int(input())):
	s = sorted(list(input()))
	total = 0
	for i in range(1, len(s) + 1):
		total += i * (ord(s[i - 1]) - 96)
	print(total)


t = int(input())
while t > 0:
	string = input()
	size = len(string)
	add = 0
	a = 1
	i = 0
	new_string = sorted(string)
	for j in range(size):
		b = 123 - ord(new_string[i])
		p = 27 - b
		add = add + a * p
		a = a + 1
		i = i + 1
	print(add)
	t = t - 1


for i in range(int(input())):
	s = input()
	l = []
	for i in range(len(s)):
		l.append(s[i])
	l.sort()
	s = 0
	for j in range(len(l)):
		s = s + (j + 1) * (ord(l[j]) - 96)
	print(s)


cases = int(input())
for _ in range(cases):
	string = sorted(input())
	ans = 0
	start = 1
	for i in string:
		ans = ans + start * (ord(i) - 96)
		start = start + 1
	print(ans)


t = int(input())
while t > 0:
	s = input()
	s = sorted(s)
	summ = 0
	for i in range(len(s)):
		summ += (i + 1) * (ord(s[i]) - 96)
	print(summ)
	t -= 1


for t in range(int(input())):
	s = input()
	s.lower()
	s = list(s)
	s.sort()
	k = 0
	for i in range(len(s)):
		k += (i + 1) * (ord(s[i]) - 96)
	print(k)


for _ in range(int(input())):
	s = sorted(input())
	p = 0
	c = 1
	for x in s:
		p = p + c * (ord(x) - 96)
		c = c + 1
	print(p)


T = int(input())
for i in range(T):
	str = input()
	li = list(str)
	li.sort()
	sum = 0
	for i in range(0, len(li)):
		sum = sum + (i + 1) * (ord(li[i]) - 96)
	print(sum)


for t in range(int(input())):
	name = input()
	name = ''.join(sorted(name))
	power = 0
	for i in range(len(name)):
		power += (i + 1) * (ord(name[i]) - 96)
	print(power)


t = int(input())
s = 'abcdefghijklmnopqrstuvwxyz'
for i in range(t):
	man = list(input())
	power = 0
	man.sort()
	for j in range(len(man)):
		power += (s.index(man[j]) + 1) * (j + 1)
	print(power)


import string
for _ in range(int(input())):
	s = input()
	alphaIdx = {}
	c = 1
	for ch in string.ascii_lowercase:
		alphaIdx[ch] = c
		c += 1
	s = ''.join(sorted(list(s)))
	maxPow = 0
	for i in range(1, len(s) + 1):
		maxPow += i * alphaIdx[s[i - 1]]
	print(maxPow)


t = int(input())
for tc in range(t):
	s = sorted(input())
	d = 0
	c = 1
	for j in s:
		d += c * (ord(j) - 96)
		c += 1
	print(d)


for i in range(int(input())):
	s = input()
	s1 = sorted(s)
	f = 1
	t = 0
	for ele in s1:
		t += f * (ord(ele) - 96)
		f = f + 1
	print(t)


a = 96
d = {chr(a + x): x for x in range(1, 27)}
for _ in range(int(input())):
	s = input()
	l = list(map(str, s))
	l.sort()
	res = 0
	for i in range(len(l)):
		res += (i + 1) * d[l[i]]
	print(res)


for i in range(int(input())):
	s = input()
	for i in s:
		s = ''.join(sorted(s))
	sum = 0
	c = 1
	for j in s:
		sum += c * (ord(j) - 96)
		c += 1
	print(sum)


for _ in range(int(input())):
	string = input()
	l = sorted(list(string))
	sum2 = 0
	for x in range(len(l)):
		sum2 += (x + 1) * (ord(l[x]) - 96)
	print(sum2)


for _ in range(int(input())):
	s = input()
	l = [i for i in s]
	l.sort()
	x = 0
	for i in range(len(l)):
		x += (i + 1) * (ord(l[i]) - 96)
	print(x)


t = int(input())
while t > 0:
	k = 0
	su = 0
	s = input()
	m = sorted(s)
	for i in m:
		k += 1
		su += k * (ord(i) - 96)
	print(su)
	t -= 1


for i in range(int(input())):
	T = input()
	S = []
	sum = 0
	for j in range(len(T)):
		S.append(ord(T[j]))
	S.sort()
	for k in range(len(T)):
		sum += (k + 1) * (S[k] - 96)
	print(sum)


for k in range(int(input())):
	s = input()
	s = sorted(s)
	v = 1
	c = 0
	for i in s:
		c = c + v * (ord(i) - 96)
		v += 1
	print(c)


for _ in range(int(input())):
	lst = ['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']
	s = list(input())
	s.sort()
	m = 0
	for i in range(len(s)):
		m = m + (i + 1) * (lst.index(s[i]) + 1)
	print(m)


t = int(input())
res = []
for i in range(t):
	li = sorted(list(input()))
	ans = 0
	for k in range(len(li)):
		calc = (k + 1) * (ord(li[k]) - 96)
		ans += calc
	res.append(str(ans))
print('\n'.join(res))


banyak_kasus = int(input())

def Operasi(huruf_nama):
	alfabet = 'abcdefghijklmnopqrstuvwxyz'
	hasil = 0
	for x in range(len(huruf_nama)):
		hasil += (x + 1) * (alfabet.index(huruf_nama[x]) + 1)
	return hasil
while banyak_kasus > 0:
	nama = input()
	nama_pisah = list(nama)
	nama_pisah.sort()
	print(Operasi(nama_pisah))
	banyak_kasus -= 1


for _ in range(int(input())):
	(s, m, sm) = (sorted(input()), 0, 0)
	li = {chr(i): i - 96 for i in range(97, 97 + 26)}
	for i in s:
		m += 1
		sm += m * li[i]
	print(sm)


for i in range(int(input())):
	s = input()
	l = sorted(s)
	total = 0
	index = 1
	for alpha in l:
		total += (ord(alpha) - 96) * index
		index += 1
	print(total)


tc = int(input())
for _ in range(tc):
	s = sorted(input())
	P = 0
	n = len(s)
	for i in range(n):
		P += (i + 1) * (ord(s[i]) - 96)
	print(P)


tc = int(input())
for _ in range(tc):
	s = sorted(input())
	A = ['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']
	P = 0
	n = len(s)
	for i in range(n):
		P += (i + 1) * (A.index(s[i]) + 1)
	print(P)


T = int(input())
for i in range(T):
	S = str(input())
	S1 = sorted(S)
	i = 0
	sum = 0
	while i <= len(S1) - 1:
		S = (i + 1) * (ord(S1[i]) - 96)
		sum += S
		i += 1
	print(sum)