You are given a string $s[1 \dots n]$ consisting of lowercase Latin letters. It is guaranteed that $n = 2^k$ for some integer $k \ge 0$.

The string $s[1 \dots n]$ is called $c$-good if at least one of the following three conditions is satisfied:  The length of $s$ is $1$, and it consists of the character $c$ (i.e. $s_1=c$); The length of $s$ is greater than $1$, the first half of the string consists of only the character $c$ (i.e. $s_1=s_2=\dots=s_{\frac{n}{2}}=c$) and the second half of the string (i.e. the string $s_{\frac{n}{2} + 1}s_{\frac{n}{2} + 2} \dots s_n$) is a $(c+1)$-good string;  The length of $s$ is greater than $1$, the second half of the string consists of only the character $c$ (i.e. $s_{\frac{n}{2} + 1}=s_{\frac{n}{2} + 2}=\dots=s_n=c$) and the first half of the string (i.e. the string $s_1s_2 \dots s_{\frac{n}{2}}$) is a $(c+1)$-good string. 

For example: "aabc" is 'a'-good, "ffgheeee" is 'e'-good.

In one move, you can choose one index $i$ from $1$ to $n$ and replace $s_i$ with any lowercase Latin letter (any character from 'a' to 'z').

Your task is to find the minimum number of moves required to obtain an 'a'-good string from $s$ (i.e. $c$-good string for $c=$ 'a'). It is guaranteed that the answer always exists.

You have to answer $t$ independent test cases.

Another example of an 'a'-good string is as follows. Consider the string $s = $"cdbbaaaa". It is an 'a'-good string, because:  the second half of the string ("aaaa") consists of only the character 'a';  the first half of the string ("cdbb") is 'b'-good string, because:   the second half of the string ("bb") consists of only the character 'b';  the first half of the string ("cd") is 'c'-good string, because:   the first half of the string ("c") consists of only the character 'c';  the second half of the string ("d") is 'd'-good string.   


-----Input-----

The first line of the input contains one integer $t$ ($1 \le t \le 2 \cdot 10^4$) — the number of test cases. Then $t$ test cases follow.

The first line of the test case contains one integer $n$ ($1 \le n \le 131~072$) — the length of $s$. It is guaranteed that $n = 2^k$ for some integer $k \ge 0$. The second line of the test case contains the string $s$ consisting of $n$ lowercase Latin letters.

It is guaranteed that the sum of $n$ does not exceed $2 \cdot 10^5$ ($\sum n \le 2 \cdot 10^5$).


-----Output-----

For each test case, print the answer — the minimum number of moves required to obtain an 'a'-good string from $s$ (i.e. $c$-good string with $c =$ 'a'). It is guaranteed that the answer exists.


-----Example-----
Input
6
8
bbdcaaaa
8
asdfghjk
8
ceaaaabb
8
bbaaddcc
1
z
2
ac

Output
0
7
4
5
1
1

Difficulty: MEDIUM
Tags: Bit manipulation

Implementation

Complete search

Dynamic programming

Divide and conquer
Skill types: Dynamic programming

Bit manipulation

Complete search

Starter code:

Solutions:
def solve(l, c):
	cc = chr(c)
	if len(l) == 1:
		return 1 if l[0] != cc else 0
	k = len(l) // 2
	l1 = k - l[:k].count(cc) + solve(l[k:], c + 1)
	l2 = k - l[k:].count(cc) + solve(l[:k], c + 1)
	return min(l1, l2)
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(solve(s, ord('a')))


from sys import stdin
from collections import deque
mod = 10 ** 9 + 7
import sys
import random
from queue import PriorityQueue
from collections import Counter as cc
from bisect import bisect_right
from bisect import bisect_left
from collections import defaultdict
from math import sqrt, factorial, gcd, log2, inf, ceil
import heapq
input = lambda : sys.stdin.readline().rstrip()
from sys import stdin, stdout
from heapq import heapify, heappush, heappop
from itertools import permutations
from math import factorial as f

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p
import sys
t = int(input())

def solve(start, end, c):
	ans = 0
	if start == end:
		if s[start - 1] != chr(c):
			return 1
		else:
			return 0
	mid = (start + end) // 2
	z1 = pref1[c - 97][mid] - pref1[c - 97][start - 1]
	z2 = pref1[c - 97][end] - pref1[c - 97][mid]
	ans += min(end - mid - z2 + solve(start, mid, c + 1), mid - start + 1 - z1 + solve(mid + 1, end, c + 1))
	return ans
for _ in range(t):
	n = int(input())
	s = input()
	pref1 = []
	ans = 0
	if len(s) == 1:
		if s[0] != 'a':
			print(1)
		else:
			print(0)
		continue
	for i in range(26):
		pref1.append([0])
	for i in range(26):
		for j in range(1, n + 1):
			if s[j - 1] == chr(i + 97):
				pref1[i].append(pref1[i][-1] + 1)
			else:
				pref1[i].append(pref1[i][-1])
	start = 1
	end = n
	c = 97
	ans = solve(start, end, c)
	print(ans)


import os

def counter(f, l, c):
	count = 0
	for i in range(f, l + 1):
		if s[i] != x[c]:
			count += 1
	return count

def calc(f, l, c):
	if f == l:
		if s[f] == x[c]:
			return 0
		else:
			return 1
	var = (f + l) // 2
	return min(counter(f, var, c) + calc(var + 1, l, c + 1), counter(var + 1, l, c) + calc(f, var, c + 1))
t = int(input())
while t:
	t -= 1
	n = int(input())
	s = input()
	x = ['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']
	f = 0
	l = n - 1
	c = 0
	print(calc(f, l, c))


def getcost(l, r, s, ch):
	count = 0
	for i in range(l, r + 1):
		if s[i] != ch:
			count += 1
	return count

def helper(l, r, s, num):
	mid = l + (r - l) // 2
	if l == r:
		if s[l] == chr(num):
			return 0
		else:
			return 1
	return min(getcost(l, mid, s, chr(num)) + helper(mid + 1, r, s, num + 1), getcost(mid + 1, r, s, chr(num)) + helper(l, mid, s, num + 1))

def answer(n, s):
	if n == 1:
		if s[0] == 'a':
			return 0
		else:
			return 1
	l = 0
	r = n - 1
	mid = (l + r) // 2
	return min(getcost(l, mid, s, 'a') + helper(mid + 1, r, s, ord('b')), getcost(mid + 1, r, s, 'a') + helper(l, mid, s, ord('b')))
t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	print(answer(n, s))


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

def solve(nums, ch, left, right):
	if left == right:
		return 0 if nums[left] == ch else 1
	(cnt1, cnt2) = (0, 0)
	mid = (left + right) // 2
	for x in range(left, mid + 1):
		if nums[x] != ch:
			cnt1 += 1
	for x in range(mid + 1, right + 1):
		if nums[x] != ch:
			cnt2 += 1
	a = cnt2 + solve(nums, ch + 1, left, mid)
	b = cnt1 + solve(nums, ch + 1, mid + 1, right)
	return min(a, b)
t = int(input())
for _ in range(t):
	n = int(input())
	arr = [x for x in input()]
	nums = [0] + [ord(item) - 97 for item in arr]
	print(solve(nums, 0, 1, n))


import string
x = 'abcdefghijklmnopqrstuvwxyz'

def func(s, n, i):
	if len(s) == 1:
		if s == x[i]:
			return 0
		else:
			return 1
	left_count = s[:n // 2].count(x[i])
	right_count = s[n // 2:].count(x[i])
	return min(func(s[:n // 2], n // 2, i + 1) + (n // 2 - right_count), func(s[n // 2:], n // 2, i + 1) + (n // 2 - left_count))
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	print(func(s, n, 0))


def cgs(x, y, c, s):
	if y - x <= 1:
		if s[x] != c:
			return 1
		else:
			return 0
	n = y - x
	(a, b, m) = (0, 0, x + (y - x) // 2)
	for j in range(x, m):
		if s[j] == c:
			a += 1
	for j in range(m, y):
		if s[j] == c:
			b += 1
	ch = chr(ord(c) + 1)
	return min(n // 2 - a + cgs(m, y, ch, s), n // 2 - b + cgs(x, m, ch, s))
for i in range(int(input())):
	n = int(input())
	s = input()
	print(cgs(0, n, 'a', s))


def two(s, r, n):
	if n == 1:
		t = 1 - s.count(chr(r))
		return t
	a = s[:n // 2]
	b = s[n // 2:]
	t = n // 2 - a.count(chr(r)) + two(b, r + 1, n // 2)
	m = n // 2 - b.count(chr(r)) + two(a, r + 1, n // 2)
	return min(t, m)
for _ in range(int(input())):
	n = int(input())
	s = input()
	r = 97
	print(two(s, r, n))


from sys import stdin

def calc(s, c):
	if len(s) == 1:
		return int(s[0] != c)
	m = len(s) // 2
	cntl = calc(s[m:], chr(ord(c) + 1))
	cntl += len(s) / 2 - s[0:m].count(c)
	cntr = calc(s[0:m], chr(ord(c) + 1))
	cntr += len(s) / 2 - s[m:].count(c)
	return int(min(cntl, cntr))
lines = stdin.readlines()
t = int(lines[0])
for i in range(2, len(lines), 2):
	s = lines[i].rstrip('\r\n')
	print(calc(s, 'a'))


from sys import stdin, stdout
import math

def main():
	t = int(stdin.readline())
	for _ in range(t):
		n = int(stdin.readline())
		st = stdin.readline().rstrip()
		p = int(math.log2(n))
		alp = ord('a')
		total_dp = [0] * n
		for char_int in range(alp + p, alp - 1, -1):
			pp = int(pow(2, alp + p - char_int))
			half_pp = int(pp / 2)
			char = chr(char_int)
			cnt = 0
			dp = [0] * n
			for (i, s) in enumerate(st):
				if s == char:
					cnt += 1
				if pp == 1:
					total_dp[i] = 1 - cnt
					cnt = 0
					continue
				if (i + 1) % half_pp == 0:
					dp[i] = half_pp - cnt
					cnt = 0
				if (i + 1) % pp == 0:
					total_dp[i] = min(total_dp[i] + dp[i - half_pp], total_dp[i - half_pp] + dp[i])
		stdout.write(str(total_dp[n - 1]) + '\n')
main()


from sys import stdin, stdout
import heapq
import cProfile
from collections import Counter, defaultdict, deque
from functools import reduce
import math

def get_int():
	return int(stdin.readline().strip())

def get_tuple():
	return map(int, stdin.readline().split())

def get_list():
	return list(map(int, stdin.readline().split()))

def solve():
	n = get_int()
	st = input()
	max_sc = chr(ord('a') + int(math.log(n, 2)))

	def dac(s, e, sc):
		nonlocal st, max_sc
		if s > e or sc > max_sc:
			return 0
		if s == e:
			return 0 if sc == st[s] else 1
		if e - s == 1:
			case1 = dac(s, s, chr(ord(sc))) + dac(e, e, chr(ord(sc) + 1))
			case2 = dac(s, s, chr(ord(sc) + 1)) + dac(e, e, chr(ord(sc)))
			return min(case1, case2)
		(first_part_sc, second_part_sc) = ((e - s + 1) // 2, (e - s + 1) // 2)
		for i in range(s, s + (e - s) // 2 + 1):
			if st[i] == sc:
				first_part_sc -= 1
		for i in range(s + (e - s) // 2 + 1, e + 1):
			if st[i] == sc:
				second_part_sc -= 1
		ans = min(second_part_sc + dac(s, s + (e - s) // 2, chr(ord(sc) + 1)), first_part_sc + dac(s + (e - s) // 2 + 1, e, chr(ord(sc) + 1)))
		return ans
	print(dac(0, n - 1, 'a'))

def main():
	solve()
TestCases = True
if TestCases:
	for i in range(get_int()):
		main()
else:
	main()


import sys
from math import sqrt, gcd, ceil, log
from bisect import bisect
from collections import defaultdict, Counter, deque
from heapq import heapify, heappush, heappop
inp = sys.stdin.readline
read = lambda : list(map(int, inp().strip().split()))

def solve():
	ans = ''
	for _ in range(int(input())):
		n = int(input())
		s = input()

		def getCost(l, r, ch):
			return r - l - s[l:r].count(chr(ch))

		def getMin(l, r, ch):
			if l + 1 == r:
				return [0, 1][chr(ch) != s[l]]
			return min(getCost(l, (l + r) // 2, ch) + getMin((l + r) // 2, r, ch + 1), getCost((l + r) // 2, r, ch) + getMin(l, (l + r) // 2, ch + 1))
		ans += str(getMin(0, n, 97)) + '\n'
	print(ans)
solve()


from sys import stdin as sin
d = 'abcdefghijklmnopqrstuvwxyz'

def rec(s, n, p):
	if n == 1:
		if s == d[p]:
			return 0
		else:
			return 1
	else:
		return min(n // 2 - s[0:n // 2].count(d[p]) + rec(s[n // 2:n], n // 2, p + 1), n // 2 - s[n // 2:n].count(d[p]) + rec(s[0:n // 2], n // 2, p + 1))
for _ in range(int(sin.readline())):
	n = int(sin.readline())
	s = input()
	c = 0
	p = 0
	print(rec(s, n, 0))


t = int(input())

def makegood(string, num):
	s = chr(num)
	m = len(string)
	if m == 1:
		if string[0] != s:
			return 1
		else:
			return 0
	st1 = string[:m // 2]
	st2 = string[m // 2:]
	check1 = m // 2 - st1.count(s) + makegood(st2, num + 1)
	check2 = m // 2 - st2.count(s) + makegood(st1, num + 1)
	return min(check1, check2)
for i in range(t):
	n = int(input())
	string = input()
	print(makegood(string, 97))


q = int(input())

def util(s, l, id, st):
	if l == 1:
		if st[s] == chr(id + ord('a')):
			return 0
		else:
			return 1
	lh = rh = 0
	for i in range(l):
		if st[s + i] != chr(id + ord('a')):
			if i < l // 2:
				lh += 1
			else:
				rh += 1
	return min(lh + util(s + int(l // 2), int(l // 2), id + 1, st), rh + util(s, int(l // 2), id + 1, st))
for i in range(q):
	n = int(input())
	st = input().strip()
	print(util(0, len(st), 0, st))


def test(n, string, index):
	if len(n) == 1:
		if string[index] == n[0]:
			return 0
		else:
			return 1
	else:
		tag = n[:len(n) // 2].count(string[index])
		tag1 = n[len(n) // 2:].count(string[index])
		sum1 = len(n) // 2 - tag + test(n[len(n) // 2:], string, index + 1)
		sum2 = len(n) // 2 - tag1 + test(n[:len(n) // 2], string, index + 1)
		return min(sum1, sum2)
a = input()
for x in range(int(a)):
	c = input()
	b = input()
	ans = 0
	string = 'abcdefghijklmnopqrstuvwxyz'
	print(test(b, string, 0))


def get_cost(l, r, c, s):
	cnt = 0
	for i in range(l, r + 1):
		if s[i] != c:
			cnt += 1
	return cnt

def get_min(l, r, c, s):
	if l == r:
		return 0 if s[l] == c else 1
	mid = int((l + r) / 2)
	nc = chr(ord(c) + 1)
	return min(get_cost(l, mid, c, s) + get_min(mid + 1, r, nc, s), get_cost(mid + 1, r, c, s) + get_min(l, mid, nc, s))

def main():
	t = int(input())
	s = 'a' * 140000
	c = 'a'
	for x in range(t):
		n = int(input())
		s = input()
		print(get_min(0, n - 1, c, s))
main()


import math
lookup = 'abcdefghijklmnopqrstuvwxyz'
t = int(input())

def helper(string, k):
	n = len(string)
	m = n // 2
	if n == 1 and string == lookup[k]:
		return 0
	elif n == 1:
		return 1
	else:
		front1 = helper(string[0:m], k + 1)
		back1 = helper(string[m:n], k + 1)
		for i in range(m):
			if string[i] != lookup[k]:
				back1 += 1
		for i in range(m):
			if string[m + i] != lookup[k]:
				front1 += 1
		return min(front1, back1)
for i in range(t):
	n = int(input())
	string = input()
	print(helper(string, 0))


g = ['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']

def solve(s, ch):
	if len(s) == 1:
		if s[0] == ch:
			return 0
		else:
			return 1
	countf = 0
	counts = 0
	for i in range(len(s) // 2):
		if s[i] == ch:
			countf += 1
	for i in range(len(s) // 2, len(s)):
		if s[i] == ch:
			counts += 1
	return min(len(s) // 2 - countf + solve(s[len(s) // 2:], g[g.index(ch) + 1]), len(s) // 2 - counts + solve(s[:len(s) // 2], g[g.index(ch) + 1]))
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(solve(s, 'a'))


import math, sys
from collections import defaultdict, Counter, deque
INF = float('inf')
MOD = 1000000007

def gcd(a, b):
	while b:
		(a, b) = (b, a % b)
	return a

def isPrime(n):
	if n <= 1:
		return False
	i = 2
	while i ** 2 <= n:
		if n % i == 0:
			return False
		i += 1
	return True

def primeFactors(n):
	factors = []
	i = 2
	while i ** 2 <= n:
		while n % i == 0:
			factors.append(i)
			n //= i
		i += 1
	if n > 1:
		factors.append(n)
	return factors

def vars():
	return map(int, input().split())

def array():
	return list(map(int, input().split()))

def getcost(l, r, ch, s):
	cnt = 0
	for i in range(l, r + 1):
		if s[i] != chr(ch):
			cnt += 1
	return cnt

def good(l, r, s, ch):
	if l >= r:
		if s[l] == chr(ch):
			return 0
		else:
			return 1
	m = (l + r) // 2
	left = getcost(l, m, ch, s) + good(m + 1, r, s, ch + 1)
	right = getcost(m + 1, r, ch, s) + good(l, m, s, ch + 1)
	return min(left, right)

def main():
	n = int(input())
	s = input()
	ch = 97
	print(good(0, n - 1, s, ch))
t = int(input())
for _ in range(t):
	main()


from math import log2

def g(s, pos, length):
	c = chr(int(log2(len(s) / length)) + ord('a') - 1)
	out = 0
	for i in range(pos, pos + length):
		if s[i] != c:
			out += 1
	return out

def f(s, pos, length):
	c = chr(int(log2(len(s) / length)) + ord('a') - 1)
	if length == 1:
		return 0 if s[0] == 'a' else 1
	elif length == 2:
		c = chr(ord(c) + 1)
		c2 = chr(ord(c) + 1)
		out = 2
		if s[pos] == c or s[pos + 1] == c:
			out -= 1
		if s[pos] == c2 or s[pos + 1] == c2:
			out -= 1
		return out
	else:
		out = min(g(s, pos, length // 2) + f(s, pos + length // 2, length // 2), f(s, pos, length // 2) + g(s, pos + length // 2, length // 2))
		return out
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	print(f(s, 0, n))


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator
from collections import deque, defaultdict, OrderedDict
import collections
z = 'abcdefghijklmnopqrst'

def moves(s, ls, ch):
	m = 0
	if ls == 1:
		if s[0] != ch:
			m += 1
		return m
	else:
		k = 0
		j = 0
		for i in range(0, ls // 2):
			if s[i] == ch:
				k += 1
		for i in range(ls // 2, ls):
			if s[i] == ch:
				j += 1
		makinggoodatL = ls // 2 - j + moves(s[:ls // 2], ls // 2, chr(ord(ch) + 1))
		makinggoodatR = ls // 2 - k + moves(s[ls // 2:], ls // 2, chr(ord(ch) + 1))
		m += min(makinggoodatR, makinggoodatL)
	return m

def main():
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	for _ in range(ri()):
		n = ri()
		a = rs()
		wi(moves(a, n, 'a'))
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

	def flush(self):
		os.write(self._fd, self.buffer.getvalue())
		(self.buffer.truncate(0), self.buffer.seek(0))

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


def rec(ar, al, k):
	if k == 1:
		if ar[0] == al:
			return 0
		else:
			return 1
	k = k // 2
	t = ar[:k].count(al)
	t = k - t
	m = ar[k:].count(al)
	m = k - m
	al = chr(ord(al) + 1)
	return min(t + rec(ar[k:], al, k), m + rec(ar[:k], al, k))
for _ in range(int(input())):
	n = int(input())
	arr = list(input())
	ch = 'a'
	print(rec(arr, ch, n))


T = int(input())

def f(x, z):
	if len(x) == 0:
		return 0
	if len(x) == 1:
		return int(x != z)
	m = len(x) // 2
	L = x[:m]
	R = x[m:]
	a = m - L.count(z) + f(R, chr(ord(z) + 1))
	b = m - R.count(z) + f(L, chr(ord(z) + 1))
	return min(a, b)
while T:
	n = int(input())
	S = input()
	print(f(S, 'a'))
	T = T - 1


def goodScore(s, c):
	if len(s) == 1:
		if s[0] == c:
			return 0
		return 1
	score1 = 0
	n = len(s)
	temp1 = ''
	for i in range(n // 2):
		if s[i] != c:
			score1 += 1
	temp1 = s[n // 2:]
	temp2 = s[:n // 2]
	score2 = 0
	for i in range(n // 2, n):
		if s[i] != c:
			score2 += 1
	return min(score1 + goodScore(temp1, chr(ord(c) + 1)), score2 + goodScore(temp2, chr(ord(c) + 1)))

def main():
	for t in range(int(input())):
		n = int(input())
		s = input()
		print(goodScore(s, 'a'))
main()


def dfs(l, r, char):
	if l == r:
		p = 0
		if s[l] != alp[char]:
			p += 1
		return p
	p = dfs((l + r + 1) // 2, r, (char + 1) % 26)
	for i in range(l, (l + r + 1) // 2):
		if s[i] != alp[char]:
			p += 1
	q = dfs(l, (l + r + 1) // 2 - 1, (char + 1) % 26)
	for i in range((l + r + 1) // 2, r + 1):
		if s[i] != alp[char]:
			q += 1
	return min(p, q)
alp = 'abcdefghijklmnopqrstuvwxyz'
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	print(dfs(0, n - 1, 0))


from sys import stdin, stdout

def fn(s, e, ch):
	if s == e:
		if ord(s1[s]) != ch:
			return 1
		return 0
	mid = s + e >> 1
	left = right = 0
	for i in range(s, mid + 1):
		if ord(s1[i]) != ch:
			left += 1
	for i in range(mid + 1, e + 1):
		if ord(s1[i]) != ch:
			right += 1
	return min(left + fn(mid + 1, e, ch + 1), right + fn(s, mid, ch + 1))
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	s1 = input()
	print(fn(0, n - 1, 97))


def min_moves(str, charac):
	if len(str) == 1:
		if str[0] == charac:
			return 0
		else:
			return 1
	else:
		n = len(str) // 2
		substr1 = str[0:n]
		step1 = 0
		substr2 = str[n:2 * n]
		step2 = 0
		for i in range(len(substr1)):
			if substr1[i] != charac:
				step1 = step1 + 1
		for i in range(len(substr2)):
			if substr2[i] != charac:
				step2 = step2 + 1
		newchar = chr(ord(charac) + 1)
		return min(step1 + min_moves(substr2, newchar), step2 + min_moves(substr1, newchar))
list_of_ans = []
t = int(input())
for i in range(t):
	v = int(input())
	str_inp = input()
	list_of_ans.append(min_moves(str_inp, 'a'))
for i in range(len(list_of_ans)):
	print(list_of_ans[i])


def solve(n, s, c):
	if n == 1:
		return int(s[0] != c)
	mid = int(n / 2)
	esq = s[:mid]
	dir = s[mid:]
	nxt = chr(ord(c) + 1)
	cnt_esq = mid - esq.count(c) + solve(mid, dir, nxt)
	cnt_dir = mid - dir.count(c) + solve(mid, esq, nxt)
	return min(cnt_esq, cnt_dir)

def main():
	t = int(input())
	for i in range(t):
		n = int(input())
		s = input()
		print(solve(n, s, 'a'))
main()


def calc(s, c):
	if len(s) == 1:
		if s[0] != c:
			return 1
		else:
			return 0
	l = 0
	r = len(s)
	mid = (l + r) // 2
	cntl = calc(s[mid:r], chr(ord(c) + 1))
	cntl += (r - l) // 2 - s[l:mid].count(c)
	cntr = calc(s[l:mid], chr(ord(c) + 1))
	cntr += (r - l) // 2 - s[mid:r].count(c)
	return min(cntl, cntr)
for _ in range(int(input())):
	n = int(input())
	string = input()
	print(calc(string, 'a'))


def getAns(c, lo, hi, prefixCnt):
	if lo + 1 == hi:
		return 1 - (prefixCnt[c][hi] - prefixCnt[c][lo])
	mid = (lo + hi) // 2
	ans = 1 << 30
	ans = min(ans, (hi - lo) // 2 - (prefixCnt[c][mid] - prefixCnt[c][lo]) + getAns(c + 1, mid, hi, prefixCnt))
	ans = min(ans, (hi - lo) // 2 - (prefixCnt[c][hi] - prefixCnt[c][mid]) + getAns(c + 1, lo, mid, prefixCnt))
	return ans
for _ in range(int(input())):
	n = int(input())
	s = input()
	prefixCnt = [[0 for _ in range(n + 1)] for _ in range(26)]
	for i in range(n):
		for j in range(26):
			prefixCnt[j][i + 1] = prefixCnt[j][i]
		index = ord(s[i]) - ord('a')
		prefixCnt[index][i + 1] += 1
	print(getAns(0, 0, n, prefixCnt))


import os, io
from sys import stdout
import collections
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
import sys
import sys
sys.setrecursionlimit(10 ** 6)

def primes(n):
	sieve = [True] * n
	for i in range(3, int(n ** 0.5) + 1, 2):
		if sieve[i]:
			sieve[i * i::2 * i] = [False] * ((n - i * i - 1) // (2 * i) + 1)
	return [2] + [i for i in range(3, n, 2) if sieve[i]]

def binomial_coefficient(n, k):
	if 0 <= k <= n:
		ntok = 1
		ktok = 1
		for t in range(1, min(k, n - k) + 1):
			ntok *= n
			ktok *= t
			n -= 1
		return ntok // ktok
	else:
		return 0

def powerOfK(k, max):
	if k == 1:
		return [1]
	if k == -1:
		return [-1, 1]
	result = []
	n = 1
	while n <= max:
		result.append(n)
		n *= k
	return result

def divisors(n):
	i = 1
	result = []
	while i * i <= n:
		if n % i == 0:
			if n / i == i:
				result.append(i)
			else:
				result.append(i)
				result.append(n / i)
		i += 1
	return result

def distance(xa, ya, xb, yb):
	return ((ya - yb) ** 2 + (xa - xb) ** 2) ** 0.5

def digitsSum(n):
	if n == 0:
		return 0
	r = 0
	while n > 0:
		r += n % 10
		n //= 10
	return r

def isValid(i, j, n, m):
	return i >= 0 and i < n and (j >= 0) and (j < m)

def print_grid(grid):
	for line in grid:
		print(' '.join(map(str, line)))

def kadane(a, size):
	max_so_far = 0
	max_ending_here = 0
	for i in range(0, size):
		max_ending_here = max_ending_here + a[i]
		if max_so_far < max_ending_here:
			max_so_far = max_ending_here
		if max_ending_here < 0:
			max_ending_here = 0
	return max_so_far

def prefixSum(arr):
	for i in range(1, len(arr)):
		arr[i] = arr[i] + arr[i - 1]
	return arr

def ceil(n, d):
	if n % d == 0:
		return n // d
	else:
		return n // d + 1

def _abs(a, b):
	return int(abs(a - b))

def evenodd(l):
	even = [e for e in l if e & 1 == 0]
	odd = [e for e in l if e & 1]
	return (even, odd)

def rec(s, l, letter, occ):
	if l == 1:
		if s[0] == chr(letter):
			return 0
		else:
			return 1
	fh = s[:l // 2]
	sh = s[l // 2:]
	a = occ - fh.count(chr(letter))
	b = occ - sh.count(chr(letter))
	ra = a + rec(sh, len(sh), letter + 1, d[chr(letter + 1)])
	rb = b + rec(fh, len(fh), letter + 1, d[chr(letter + 1)])
	return min(ra, rb)
t = int(input())
for _ in range(t):
	n = int(input())
	s = input().decode('utf-8').strip()
	count = n
	d = {}
	c = 97
	while count > 0:
		nb = count // 2
		if nb == 0:
			nb = 1
		d[chr(c)] = nb
		c += 1
		count = count // 2
	letter = 97
	result = rec(list(s), len(s), letter, d[chr(letter)])
	print(result)


def task_1(s, target):
	return len(s) - s.count(target)

def main_task(start, end, target):
	if start + 1 == end:
		if s[start] == target:
			return 0
		else:
			return 1
	m = (start + end) // 2
	a = task_1(s[start:m], target) + main_task(m, end, chr(ord(target) + 1))
	b = task_1(s[m:end], target) + main_task(start, m, chr(ord(target) + 1))
	return min(a, b)
for i in range(int(input())):
	n = int(input())
	s = input()
	print(main_task(0, n, 'a'))


def good(l, c):
	ch = chr(c)
	if len(l) == 1:
		return 1 if ch != l[0] else 0
	k = len(l) // 2
	lm = k - l[:k].count(ch) + good(l[k:], c + 1)
	rm = k - l[k:].count(ch) + good(l[:k], c + 1)
	return min(lm, rm)
for ty in range(int(input())):
	n = int(input())
	a = input()
	print(good(a, ord('a')))


def busca(palavra, aux):
	if len(palavra) == 1:
		if chr(aux) != palavra[0]:
			return 1
		else:
			return 0
	meio = len(palavra) // 2
	min_esquerda = meio - palavra[:meio].count(chr(aux)) + busca(palavra[meio:], aux + 1)
	min_direita = meio - palavra[meio:].count(chr(aux)) + busca(palavra[:meio], aux + 1)
	return min(min_esquerda, min_direita)
num_casos_teste = int(input())
for i in range(num_casos_teste):
	inutil = int(input())
	palavra = input()
	min_movimentos = busca(palavra, ord('a'))
	print(min_movimentos)


from sys import stdin

def main():
	for _ in range(int(input())):
		col = stdin.read().splitlines()[1::2]
		for s in col:
			le = len(s) + 1
			(cnt, col) = ([[0] * le for _ in range(26)], [0] * 26)
			for (i, c) in enumerate(map(ord, s), 1):
				col[c - 97] += 1
				for (x, row) in zip(col, cnt):
					row[i] = x

			def f(lo, hi, idx):
				row = cnt[idx]
				if lo + 1 == hi:
					return row[lo] == row[hi]
				mid = (lo + hi) // 2
				idx += 1
				return min(f(mid, hi, idx) - row[mid] + row[lo], f(lo, mid, idx) - row[hi] + row[mid])
			print(le - 2 + f(0, le - 1, 0))
main()


from collections import defaultdict as dd
from collections import deque
import bisect
import heapq

def ri():
	return int(input())

def rl():
	return list(map(int, input().split()))

def solve():
	n = ri()
	s = input()
	partials = {}
	for l in 'qwertyuiopasdfghjklzxcvbnm':
		partials[l] = [0] * n
	partials[s[0]][0] = 1
	for (i, c) in enumerate(s[1:]):
		for letter in 'qwertyuiopasdfghjklzxcvbnm':
			partials[letter][i + 1] = partials[letter][i]
			if letter == c:
				partials[c][i + 1] += 1

	def opt(lo, hi, letter):
		if lo == hi:
			if letter == s[lo]:
				return 0
			else:
				return 1
		else:
			mid = (lo + hi) // 2
			cost1 = mid - lo + 1 + opt(mid + 1, hi, chr(ord(letter) + 1))
			if lo == 0:
				cost1 -= partials[letter][mid]
			else:
				cost1 -= partials[letter][mid] - partials[letter][lo - 1]
			cost2 = hi - mid + opt(lo, mid, chr(ord(letter) + 1))
			cost2 -= partials[letter][hi] - partials[letter][mid]
			return min(cost1, cost2)
	print(opt(0, n - 1, 'a'))
mode = 'T'
if mode == 'T':
	t = ri()
	for i in range(t):
		solve()
else:
	solve()


import sys
input = sys.stdin.buffer.readline
Q = int(input())
for _ in range(Q):
	N = int(input())
	S = list(input().rstrip())
	dp = [[0] * 26 for _ in range(N + 1)]
	for (i, s) in enumerate(S):
		dp[i + 1] = dp[i][:]
		dp[i + 1][s - ord('a')] += 1
	K = N.bit_length() - 1
	ans = N
	s = 0
	for bit in range(N):
		score = N
		left = 0
		right = N
		for i in range(K):
			leng = 2 ** (K - i - 1)
			if bit & 1 << i:
				score -= dp[left + leng][s + i] - dp[left][s + i]
				left += leng
			else:
				score -= dp[right][s + i] - dp[right - leng][s + i]
				right -= leng
		if S[left] - ord('a') == s + K:
			score -= 1
		if score < ans:
			ans = score
	print(ans)


from sys import stdin, stdout
import math
from collections import Counter, deque
L = lambda : list(map(int, stdin.readline().strip().split()))
M = lambda : map(int, stdin.readline().strip().split())
I = lambda : int(stdin.readline().strip())
IN = lambda : stdin.readline().strip()
C = lambda : stdin.readline().strip().split()
mod = 1000000007

def s(a):
	print(' '.join(list(map(str, a))))

def good(s, c):
	n = len(s)
	if n == 1:
		if s[0] == c:
			return 0
		else:
			return 1
	l = n // 2 - s[:n // 2].count(c) + good(s[n // 2:], chr(ord(c) + 1))
	r = n // 2 - s[n // 2:].count(c) + good(s[:n // 2], chr(ord(c) + 1))
	return min(l, r)

def solve():
	n = I()
	s = IN()
	print(good(s, 'a'))
for _ in range(I()):
	solve()


import sys
inputlist = sys.stdin.readlines()
number_of_testcases = int(inputlist[0].strip())

def make_character(character, string, start_index, end_index):
	swaps = 0
	for i in range(start_index, end_index + 1):
		if string[i] != character:
			swaps += 1
	return swaps

def make_good(character, string, start_index, end_index):
	if start_index == end_index:
		return make_character(character, string, start_index, end_index)
	else:
		mid_index = (start_index + end_index) // 2
		result1 = make_good(chr(ord(character) + 1), string, start_index, mid_index) + make_character(character, string, mid_index + 1, end_index)
		result2 = make_good(chr(ord(character) + 1), string, mid_index + 1, end_index) + make_character(character, string, start_index, mid_index)
		return min(result1, result2)
for i in range(number_of_testcases):
	n = int(inputlist[2 * i + 1].strip())
	string = inputlist[2 * i + 2].strip()
	ans = make_good('a', string, 0, n - 1)
	print(ans)


t = int(input())

def busca(s, c):
	char = chr(97 + c)
	if len(s) == 1:
		if s[0] == char:
			return 0
		else:
			return 1
	else:
		meio = len(s) // 2
		esquerda = len(s) // 2 - s[:meio].count(char) + busca(s[meio:], c + 1)
		direita = len(s) // 2 - s[meio:].count(char) + busca(s[:meio], c + 1)
		return min(esquerda, direita)
for i in range(t):
	n = int(input())
	s = input()
	print(busca(s, 0))


import sys
input = sys.stdin.readline
alphabetlist = ['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']
for _ in range(int(input())):
	n = int(input())
	s = input()
	s = s[:n]
	data = [[0 for i in range(26)] for j in range(n + 1)]
	for i in range(1, n + 1):
		for j in range(26):
			data[i][j] = (s[i - 1] == alphabetlist[j]) + data[i - 1][j]
	memo = {}

	def dp(i, k, j):
		if (i, k, j) in memo:
			return memo[i, k, j]
		if k == 1:
			res = 1 - (s[i] == alphabetlist[j])
			memo[i, k, j] = 0
			memo[i, k, j] = res
			return res
		res = 0
		test1 = dp(i, k // 2, j + 1) + (k // 2 - (data[i + k][j] - data[i + k // 2][j]))
		test2 = dp(i + k // 2, k // 2, j + 1) + (k // 2 - (data[i + k // 2][j] - data[i][j]))
		res = min(test1, test2)
		memo[i, k, j] = 0
		memo[i, k, j] = res
		return res
	print(dp(0, n, 0))


import os
import sys
from io import BytesIO, IOBase
import math
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def chk(s, n, ch):
	if n == 0:
		return 0
	elif n == 1:
		if s[0] != ch:
			return 1
		else:
			return 0
	a = 0
	a1 = 0
	for i in range(0, n // 2):
		if s[i] != ch:
			a += 1
	a += chk(s[n // 2:], n // 2, chr(ord(ch) + 1))
	for i in range(n // 2, n):
		if s[i] != ch:
			a1 += 1
	a1 += chk(s[0:n // 2], n // 2, chr(ord(ch) + 1))
	ssss = set()
	return min(a, a1)
for _ in range(int(input())):
	n = int(input())
	s = input()
	l = list()
	l.append(4)
	ss = set(l)
	res = 1
	mm = 1
	if n == 1:
		if s == 'a':
			print(0)
		else:
			print(1)
		continue
	ans = chk(s, n, 'a')
	print(ans + res - mm)


cache = {}

def foo(c, s):
	if len(s) == 1:
		if s == c:
			return 0
		return 1
	if (c, s) in cache:
		return cache[c, s]
	n = len(s)
	mid = n >> 1
	first = s[:mid]
	second = s[mid:]
	val1 = mid - first.count(c) + foo(chr(ord(c) + 1), second)
	val2 = mid - second.count(c) + foo(chr(ord(c) + 1), first)
	val = min(val1, val2)
	cache[c, s] = val
	return val
t = int(input())
for _ in range(t):
	n = int(input())
	s = input().strip()
	print(foo('a', s))


import sys

def cost(l, r, c):
	res = 0
	for i in range(l, r + 1):
		if string[i] != chr(c):
			res += 1
	return res

def calc(l, r, c):
	if l == r:
		if string[l] == chr(c):
			return 0
		else:
			return 1
	mid = (l + r) // 2
	return min(cost(l, mid, c) + calc(mid + 1, r, c + 1), cost(mid + 1, r, c) + calc(l, mid, c + 1))
tc = int(sys.stdin.readline())
for _ in range(tc):
	n = int(sys.stdin.readline())
	string = sys.stdin.readline().rstrip()
	print(calc(0, n - 1, ord('a')))


import sys
input = sys.stdin.buffer.readline
T = int(input())
two_ch = dict()
tmp_two = 1
tmp_ch = 97
for i in range(26):
	two_ch[tmp_two] = tmp_ch
	tmp_two *= 2
	tmp_ch += 1
for testcase in range(T):
	n = int(input())
	s = list(input())
	if n == 1:
		if s[0] == 97:
			print(0)
		else:
			print(1)
		continue
	if n == 2:
		if s[0] == 97 and s[1] == 98 or (s[0] == 98 and s[1] == 97):
			print(0)
		elif s[0] == 97 or s[1] == 97 or s[0] == 98 or (s[1] == 98):
			print(1)
		else:
			print(2)
		continue
	dp = [1] * n
	p = two_ch[n]
	for i in range(n):
		if s[i] == p:
			dp[i] = 0
	dis = 1
	for c in range(p - 1, 96, -1):
		idx = 0
		while idx < n:
			(ctr1, ctr2) = (0, 0)
			for i in range(idx, idx + dis):
				if s[i] != c:
					ctr1 += 1
			for i in range(idx + dis, idx + dis + dis):
				if s[i] != c:
					ctr2 += 1
			dp[idx] = min(dp[idx] + ctr2, dp[idx + dis] + ctr1)
			idx += dis + dis
		dis *= 2
	print(dp[0])


import sys, math
reader = (line.rstrip() for line in sys.stdin)
input = reader.__next__

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input()

def listStr():
	return list(input())
import collections as col
import math

def solve():
	N = getInt()
	A = listStr()

	def solve2(S, targ):
		LL = len(S)
		if LL == 1:
			return 1 * (S[0] != targ)
		(count1, count2) = (S[:LL // 2].count(targ), S[LL // 2:].count(targ))
		new_targ = chr(ord(targ) + 1)
		return min(LL // 2 - count1 + solve2(S[LL // 2:], new_targ), LL // 2 - count2 + solve2(S[:LL // 2], new_targ))
	return solve2(A, 'a')
for t in range(getInt()):
	print(solve())


import math
import os
import random
import re
import sys
from collections import defaultdict

def isGood(ara):
	result = -1
	(frst, lst) = (0, len(ara) - 1)
	for i in range(len(ara)):
		if result > min(ara[frst], ara[lst]):
			return False
		if ara[frst] <= ara[lst]:
			result = ara[frst]
			frst += 1
		else:
			result = ara[lst]
			lst -= 1
	return True

def makeAraGood(araStr, isLeft, ch):
	cnt = 0
	if len(araStr) == 1:
		return 1 if araStr[0] != ch else 0
	nextChar = chr(ord(ch) + 1)
	leftStr = araStr[:len(araStr) // 2]
	rightStr = araStr[len(araStr) // 2:]
	if isLeft:
		for item in leftStr:
			if item != ch:
				cnt += 1
		cnt += min(makeAraGood(rightStr, False, nextChar), makeAraGood(rightStr, True, nextChar))
	else:
		for item in rightStr:
			if item != ch:
				cnt += 1
		cnt += min(makeAraGood(leftStr, False, nextChar), makeAraGood(leftStr, True, nextChar))
	return cnt
case = int(input())
for i in range(case):
	n = int(input())
	araStr = input()
	ans = min(makeAraGood(araStr, False, 'a'), makeAraGood(araStr, True, 'a'))
	print(ans)


global ans
ans = []

def printer_rec(s, cost, character):
	l = len(s)
	fh_cost = sh_cost = 0
	for i in range(l):
		if s[i] != character:
			if i < int(l / 2):
				fh_cost += 1
			else:
				sh_cost += 1
	character = chr(ord(character) + 1)
	temp = s[:int(l / 2)]
	if len(temp) >= 1:
		printer_rec(temp, cost + sh_cost, character)
		printer_rec(s[int(l / 2):], cost + fh_cost, character)
	else:
		ans.append(cost + sh_cost)
		ans.append(cost + sh_cost)
t = int(input())
for i in range(t):
	global inp_len
	inp_len = int(input())
	s = str(input())
	printer_rec(s, 0, 'a')
	print(min(ans))
	ans = []


def solve(n, s, c):
	ret = 0
	if n == 1:
		if s[0] == c:
			return 0
		else:
			return 1
	count1 = 0
	count2 = 0
	s1 = s[:n // 2]
	s2 = s[n // 2:]
	for i in range(n // 2):
		if s1[i] == c:
			count1 += 1
		if s2[i] == c:
			count2 += 1
	ret += min(n // 2 - count1 + solve(n // 2, s2, chr(ord(c) + 1)), n // 2 - count2 + solve(n // 2, s1, chr(ord(c) + 1)))
	return ret
T = int(input())
for t in range(T):
	n = int(input())
	s = input()
	print(solve(n, s, 'a'))


def f(s, c):
	n = len(s) // 2
	m = n or 1
	return s > '' and min((m - s.count(chr(c), i, i + m) + f(s[n - i:n - i + n], c + 1) for i in {0, n}))
for s in [*open(0)][2::2]:
	print(f(s[:-1], 97))


def dfs(s, q, n):
	o = chr(q)
	if n == 1:
		return 1 - (s == o)
	return min(n // 2 - s[:n // 2].count(o) + dfs(s[n // 2:], q + 1, n // 2), n // 2 - s[n // 2:].count(o) + dfs(s[:n // 2], q + 1, n // 2))
q = ord('a')
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(dfs(s, q, n))


from math import log2
for _ in range(int(input())):
	l = int(input())
	s = input()
	k = int(log2(l))
	news = [0] * l
	for i in range(l):
		news[i] = ord(s[i]) - 97
	dis = []
	for i in range(k + 1):
		dis.append(2 ** (k - i))

	def dpg(alph, st):
		if alph == k:
			if news[st] == alph:
				return 0
			else:
				return 1
		(cost1, cost2) = (0, 0)
		for i in range(st, st + dis[alph]):
			if i < st + dis[alph] / 2:
				if news[i] != alph:
					cost1 += 1
			elif news[i] != alph:
				cost2 += 1
		return min(dpg(alph + 1, st) + cost2, dpg(alph + 1, st + dis[alph + 1]) + cost1)
	print(dpg(0, 0))


import sys
input = sys.stdin.readline
import math

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def price(a, x):
	count = 0
	for i in a:
		if ord(i) != x:
			count = count + 1
	return count

def value(arr, x):
	if len(arr) > 2:
		return min(value(arr[:len(arr) // 2], x + 1) + price(arr[len(arr) // 2:], x), price(arr[:len(arr) // 2], x) + value(arr[len(arr) // 2:], x + 1))
	if len(arr) == 2:
		val = 2
		if ord(arr[0]) == x or ord(arr[1]) == x:
			val = val - 1
		if ord(arr[0]) == x + 1 or ord(arr[1]) == x + 1:
			val = val - 1
		return val
	if len(arr) == 1:
		if ord(arr[0]) == x:
			return 0
		else:
			return 1
	else:
		return 0
t = inlt()
for i in range(t[0]):
	n = inlt()[0]
	a = insr()
	print(value(a, 97))


def swaps(s, c):
	return sum([0 if c == d else 1 for d in s])

def solve(n, s, c):
	if n == 1:
		return 0 if s == c else 1
	n2 = n // 2
	s1 = s[:n2]
	s2 = s[n2:]
	d = chr(ord(c) + 1)
	return min(swaps(s1, c) + solve(n2, s2, d), swaps(s2, c) + solve(n2, s1, d))
t = int(input().strip())
for _ in range(t):
	n = int(input().strip())
	s = input().strip()
	print(solve(n, s, 'a'))


import heapq as hp
import collections
import bisect

def unpack(func=int):
	return map(func, input().split())

def l_unpack(func=int):
	return list(map(func, input().split()))

def s_unpack(func=int):
	return sorted(map(func, input().split()))

def range_n():
	return range(int(input()))

def getint():
	return int(input())

def foo(s, l, r, c, memo):
	t = (l, r, c)
	if t in memo:
		return memo[t]
	ans = 0
	if l == r:
		return 1 if s[l] != c else 0
	half = (r - l + 1) // 2
	a = abs(s[l:l + half].count(c) - half)
	b = abs(s[r - half + 1:r + 1].count(c) - half)
	ans += min(a + foo(s, l + half, r, chr(ord(c) + 1), memo), b + foo(s, l, l + half - 1, chr(ord(c) + 1), memo))
	memo[t] = ans
	return ans

def main():
	for _ in range_n():
		n = getint()
		s = list(input())
		memo = {}
		a = foo(s, 0, n - 1, 'a', memo)
		print(a)
main()


class Main:

	def main(self):
		t = int(input())
		for t1 in range(0, t):
			input()
			s1 = input()
			self.min = len(s1)
			self.check(s1, 'a', 0)
			print(self.min)

	def check(self, s, c, count):
		if len(s) == 1:
			count += 0 if s == c else 1
			self.min = min(self.min, count)
		else:
			s1 = s[0:len(s) // 2]
			s2 = s[len(s) // 2:]
			self.check(s1, chr(ord(c) + 1), count + (len(s2) - s2.count(c)))
			self.check(s2, chr(ord(c) + 1), count + (len(s1) - s1.count(c)))
Main().main()


def func(s, c):
	if len(s) == 1:
		if s[0] == c:
			return 0
		else:
			return 1
	t = s[0:len(s) // 2]
	m = s[len(s) // 2:]
	return min(len(t) - t.count(c) + func(m, chr(ord(c) + 1)), len(t) - m.count(c) + func(t, chr(ord(c) + 1)))
a = int(input())
for i in range(a):
	r = int(input())
	s = input()
	x = func(s, 'a')
	print(x)


import sys
sys.setrecursionlimit(10 ** 5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline()[:-1]

def cost(l, r, x):
	if r - l == 1:
		return (aa[l] != x) * 1
	if (l, r, x) in memo:
		return memo[l, r, x]
	m = (l + r) // 2
	ret1 = m - l - (cs[x][m] - cs[x][l]) + cost(m, r, x + 1)
	ret2 = r - m - (cs[x][r] - cs[x][m]) + cost(l, m, x + 1)
	memo[l, r, x] = res = min(ret1, ret2)
	return res
for _ in range(II()):
	memo = {}
	n = II()
	aa = [ord(c) - 97 for c in SI()]
	cs = [[0] * (n + 1) for _ in range(26)]
	for (i, a) in enumerate(aa):
		cs[a][i + 1] = 1
	for i in range(26):
		csi = cs[i]
		for j in range(n):
			csi[j + 1] += csi[j]
	print(cost(0, n, 0))


import math
import sys

def solve(s, l, r, letter):
	if l == r:
		if s[l] == chr(letter):
			return 0
		else:
			return 1
	mid = (r + l) // 2
	ans2 = solve(s, l, mid, letter + 1)
	ans1 = solve(s, mid + 1, r, letter + 1)
	for i in range(l, mid + 1):
		if s[i] != chr(letter):
			ans1 += 1
	for i in range(mid + 1, r + 1):
		if s[i] != chr(letter):
			ans2 += 1
	return min(ans1, ans2)
sys.setrecursionlimit(10000)
t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	print(solve(s, 0, n - 1, ord('a')))


def main():
	for case in range(int(input())):
		n = int(input())
		print(res(input(), 'a'))

def res(s, a):
	n = len(s)
	if n == 1:
		return int(s[0] != a)
	if n != 1:
		res1 = res(s[n // 2:], chr(ord(a) + 1)) - s[:n // 2].count(a)
		res2 = res(s[:n // 2], chr(ord(a) + 1)) - s[n // 2:].count(a)
		m = min(res1, res2)
		return [n // 2 + m, int(s[0] != a)][n == 1]
main()


def a_good(n, s, ch):
	if n == 1:
		if s == ch:
			return 0
		else:
			return 1
	mid = n // 2
	left = s[:mid].count(ch)
	right = s[mid:].count(ch)
	return min(mid - left + a_good(mid, s[mid:], chr(ord(ch) + 1)), mid - right + a_good(mid, s[:mid], chr(ord(ch) + 1)))
tt = int(input())
for _ in range(tt):
	n = int(input())
	s = input()
	print(a_good(n, s, 'a'))


def bs(l, r, ch):
	if l == r:
		if s[l] == ch:
			return 0
		else:
			return 1
	mid = (l + r) // 2
	return min(mid - l + 1 - s[l:mid + 1].count(ch) + bs(mid + 1, r, chr(ord(ch) + 1)), r - mid - s[mid + 1:r + 1].count(ch) + bs(l, mid, chr(ord(ch) + 1)))
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(bs(0, n - 1, 'a'))


def goodString(word, size, single):
	if size == 1:
		return 1 if word != single else 0
	single2 = chr(1 + ord(single))
	mid = size // 2
	firstWord = word[:mid]
	lastWord = word[mid:]
	single2 = chr(1 + ord(single))
	calculoOne = len(firstWord) - firstWord.count(single) + goodString(lastWord, len(lastWord), single2)
	calculoTwo = len(lastWord) - lastWord.count(single) + goodString(firstWord, len(firstWord), single2)
	if calculoOne < calculoTwo:
		return calculoOne
	else:
		return calculoTwo

def main():
	numberIn = int(input())
	for i in range(numberIn):
		size = int(input())
		word = input()
		print(goodString(word, size, 'a'))
main()


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']

def getcost(l, r, ch):
	global s
	cnt = 0
	for i in range(l, r + 1):
		if s[i] != ch:
			cnt += 1
	return cnt

def getmin(l, r, ch):
	global s
	if l == r:
		if s[l] == ch:
			return 0
		else:
			return 1
	mid = (l + r) // 2
	return min(getcost(l, mid, ch) + getmin(mid + 1, r, chr(ord(ch) + 1)), getcost(mid + 1, r, ch) + getmin(l, mid, chr(ord(ch) + 1)))
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']
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(getmin(0, n - 1, 'a'))


def solve(s, x, size, ans):
	if size == 1:
		if s[0] != x:
			ans += 1
		return ans
	l = s[:size // 2].count(x)
	r = s[size // 2:].count(x)
	return min(size // 2 - l + solve(s[size // 2:], chr(ord(x) + 1), size // 2, ans), size // 2 - r + solve(s[:size // 2], chr(ord(x) + 1), size // 2, ans))
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(solve(s, 'a', n, 0))


def find(s, c, k):
	if len(s) == 1:
		if s == c:
			return 0
		else:
			return 1
	first_half = s[:2 ** (k - 1)]
	second_half = s[2 ** (k - 1):]
	count_first = 0
	count_second = 0
	for x in first_half:
		if x != c:
			count_first += 1
	for x in second_half:
		if x != c:
			count_second += 1
	ans1 = count_first + find(second_half, chr(ord(c) + 1), k - 1)
	ans2 = count_second + find(first_half, chr(ord(c) + 1), k - 1)
	return min(ans1, ans2)
for _ in range(int(input())):
	input()
	s = input()
	for k in range(20):
		if 2 ** k == len(s):
			break
	print(find(s, 'a', k))


def makeGood(ch, s, i, j):
	if i == j:
		if s[i] != ch:
			return 1
	if i >= j:
		return 0
	mid = (i + j) // 2
	c1 = s[i:mid + 1].count(ch)
	c2 = s[mid + 1:j + 1].count(ch)
	ch2 = chr(ord(ch) + 1)
	cost1 = mid - i + 1 - c1 + makeGood(ch2, s, mid + 1, j)
	cost2 = j - mid - c2 + makeGood(ch2, s, i, mid)
	return min(cost1, cost2)
for t in range(int(input())):
	n = int(input())
	s = input()
	print(makeGood('a', s, 0, len(s) - 1))


import math

def calcula(left, rigth, c, s):
	if rigth == left:
		if s[left] == c:
			return 0
		else:
			return 1
	meio = (left + rigth) // 2
	a1 = (rigth - left + 1) // 2 - s[left:meio + 1].count(c) + calcula(meio + 1, rigth, chr(ord(c) + 1), s)
	a2 = (rigth - left + 1) // 2 - s[meio + 1:rigth + 1].count(c) + calcula(left, meio, chr(ord(c) + 1), s)
	return min(a1, a2)
t = int(input())
for w in range(t):
	n = int(input())
	s = input()
	if n == 1:
		if s == 'a':
			print(0)
		else:
			print(1)
	else:
		print(calcula(0, n - 1, 'a', s))


def good(l, c):
	ch = chr(c)
	if len(l) == 1:
		return 1 if ch != l[0] else 0
	x = len(l) // 2
	lm = x - l[:x].count(ch) + good(l[x:], c + 1)
	rm = x - l[x:].count(ch) + good(l[:x], c + 1)
	return min(lm, rm)
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	print(good(s, ord('a')))


def foo(st, l, ch):
	if len(st) == 1:
		if st == ch:
			return l
		else:
			return l + 1
	x = y = 0
	for i in range(0, len(st)):
		if st[i] != ch:
			if i < len(st) // 2:
				x += 1
			else:
				y += 1
	return l + min(foo(st[0:len(st) // 2], y, chr(ord(ch) + 1)), foo(st[len(st) // 2:len(s)], x, chr(ord(ch) + 1)))
for test in range(int(input())):
	n = int(input())
	s = input()
	print(foo(s, 0, 'a'))


def get_cnt(start, k, char, s):
	result = 0
	for index in range(start, start + 2 ** k):
		if s[index] != char:
			result += 1
	return result

def f(start, k, char, s):
	if k == 0:
		if s[start] == char:
			return 0
		return 1
	return min(get_cnt(start, k - 1, char, s) + f(start + 2 ** (k - 1), k - 1, char + 1, s), f(start, k - 1, char + 1, s) + get_cnt(start + 2 ** (k - 1), k - 1, char, s))

def solve():
	import sys
	import math
	input = sys.stdin.readline
	print = sys.stdout.write
	t = int(input())
	while t > 0:
		n = int(input())
		s = [ord(char) - 97 for char in input().strip()]
		max_k = int(math.log2(n))
		print(f'{f(0, max_k, 0, s)}')
		print('\n')
		t -= 1
solve()


def solve(n, s, c):
	if n == 1:
		return 1 if s[0] != c else 0
	half1 = s[:n // 2].count(c)
	half1Total = n // 2 - half1 + solve(n // 2, s[n // 2:], c + 1)
	half2 = s[n // 2:].count(c)
	half2Total = n // 2 - half2 + solve(n // 2, s[:n // 2], c + 1)
	return min(half1Total, half2Total)
for _ in range(int(input())):
	n = int(input())
	s = list(input())
	for i in range(n):
		s[i] = ord(s[i]) - 97
	print(solve(n, s, 0))


def call(n, ch, s):
	if n <= 0:
		return 0
	if n == 1:
		if s[0] != ch:
			return 1
		return 0
	(c1, c2) = (0, 0)
	for i in range(0, n // 2):
		if s[i] != ch:
			c1 = c1 + 1
	c1 += call(n // 2, chr(ord(ch) + 1), s[n // 2:])
	for j in range(n // 2, n):
		if s[j] != ch:
			c2 += 1
	c2 += call(n // 2, chr(ord(ch) + 1), s[:n // 2])
	if c1 == 0 and c2 == 0:
		return 0
	return min(c1, c2)
t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	s = input()
	print(call(n, 'a', s))


def solve(s, c):
	if len(s) == 1:
		return 0 if s[0] == c else 1
	s1 = s[:len(s) // 2]
	s2 = s[len(s) // 2:]
	cnt1 = s1.count(c)
	cnt2 = s2.count(c)
	ans1 = solve(s1, chr(ord(c) + 1))
	ans2 = solve(s2, chr(ord(c) + 1))
	n = len(s) // 2
	return min(n - cnt1 + ans2, n - cnt2 + ans1)
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(solve(s, 'a'))


def cnt(l, r, letter):
	if l + 1 == r:
		return int(s[l] != letter)
	m = (l + r) // 2
	cntl = cntr = 0
	for i in range(l, m):
		cntl += s[i] != letter
	for i in range(m, r):
		cntr += s[i] != letter
	new_letter = chr(ord(letter) + 1)
	return min(cntl + cnt(m, r, new_letter), cntr + cnt(l, m, new_letter))
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	print(cnt(0, n, 'a'))


import math
import collections
from sys import stdin, stdout, setrecursionlimit
from bisect import bisect_left as bsl
from bisect import bisect_right as bsr
import heapq as hq
setrecursionlimit(2 ** 20)
t = 1
t = int(stdin.readline())

def str_cal(s, d, n, c):
	if n == 1:
		if d[c] == s[0]:
			return 0
		else:
			return 1
	cnt1 = 0
	for i in s[:n // 2]:
		if i != d[c]:
			cnt1 += 1
	cnt2 = 0
	for i in s[n // 2:]:
		if i != d[c]:
			cnt2 += 1
	return min(cnt2 + str_cal(s[:n // 2], d, n // 2, c + 1), cnt1 + str_cal(s[n // 2:], d, n // 2, c + 1))
for _ in range(t):
	n = int(stdin.readline())
	s = str(stdin.readline().strip('\n'))
	d = {}
	cnt = 1
	for i in 'abcdefghijklmnopqrstuvwxyz':
		d[cnt] = i
		cnt += 1
	print(str_cal(s, d, n, 1))


t = int(input())

def equal(c, l, r):
	to_change = 0
	for i in range(l, r):
		if code[i] != c:
			to_change += 1
	return to_change

def changes(c, l, r):
	if r - l == 1:
		if code[l] == c:
			return 0
		else:
			return 1
	else:
		return min(equal(c, l, (l + r) // 2) + changes(c + 1, (l + r) // 2, r), changes(c + 1, l, (l + r) // 2) + equal(c, (l + r) // 2, r))
for _ in range(t):
	n = int(input())
	s = input()
	code = []
	for elem in s:
		code.append(ord(elem))
	print(changes(ord('a'), 0, n))


import math

def cost(s, key):
	res = 0
	for i in s:
		if ord(i) - 96 != key:
			res += 1
	return res
for _ in range(int(input())):
	n = int(input())
	s = input('')

	def check(s, key):
		if len(s) == 1:
			return cost(s, key)
		return min(check(s[:len(s) // 2], key + 1) + cost(s[len(s) // 2:], key), check(s[len(s) // 2:], key + 1) + cost(s[:len(s) // 2], key))
	print(check(s, 1))


import sys
input = sys.stdin.readline
(inp, ip) = (lambda : int(input()), lambda : [int(w) for w in input().split()])

def solve(s, no, ct):
	global ans
	n = len(s)
	if n == 1:
		if s != chr(no):
			ct += 1
		ans.append(ct)
		return
	(a, b) = (s[:n // 2].count(chr(no)), s[n // 2:].count(chr(no)))
	solve(s[n // 2:], no + 1, ct + n // 2 - a)
	solve(s[:n // 2], no + 1, ct + n // 2 - b)
for _ in range(inp()):
	n = inp()
	s = input().strip()
	ans = []
	solve(s, 97, 0)
	print(min(ans))


def chekk(s, x):
	if len(s) == 1:
		return int(s[0] != x)
	mid = len(s) // 2
	a = chekk(s[:mid], chr(ord(x) + 1))
	a += mid - s[mid:].count(x)
	b = chekk(s[mid:], chr(ord(x) + 1))
	b += mid - s[:mid].count(x)
	return min(a, b)
for n in range(int(input())):
	input()
	s = input()
	print(chekk(s, 'a'))


def hh1(x, m, c):
	return m - x.count(c)

def hh2(x, m, c):
	if m == 1:
		if x[0] == c:
			return 0
		else:
			return 1
	else:
		a = m // 2
		d = chr(1 + ord(c))
		return min(hh1(x[:a], a, c) + hh2(x[a:], a, d), hh1(x[a:], a, c) + hh2(x[:a], a, d))
for t in range(int(input())):
	n = int(input())
	s = list(input())
	print(hh2(s, n, 'a'))


import sys

def goback(st, c):
	if len(st) == 1:
		if st == c:
			return 0
		else:
			return 1
	left = int(len(st) / 2) - st[:int(len(st) / 2)].count(c) + goback(st[int(len(st) / 2):], chr(ord(c) + 1))
	right = int(len(st) / 2) - st[int(len(st) / 2):].count(c) + goback(st[:int(len(st) / 2)], chr(ord(c) + 1))
	return min(left, right)
for _ in range(int(sys.stdin.readline().rstrip())):
	n = int(sys.stdin.readline().rstrip())
	s = sys.stdin.readline().rstrip()
	print(goback(s, 'a'))


def c(li, l, h, good):
	if h - l == 1:
		if li[l] == good:
			return 0
		return 1
	m = l + (h - l) // 2
	return min((h - l) // 2 - li[l:m].count(good) + c(li, m, h, chr(ord(good) + 1)), (h - l) // 2 - li[m:h].count(good) + c(li, l, m, chr(ord(good) + 1)))

def main():
	for _ in range(int(input())):
		n = int(input())
		a = list(input())
		print(c(a, 0, n, 'a'))
main()


def count_move(s, target):
	count = 0
	for i in s:
		if i != target:
			count = count + 1
	return count

def find_target_string(s, target):
	if len(s) == 1:
		if s == target:
			return 0
		else:
			return 1
	n = int(len(s) / 2)
	l_min = find_target_string(s[0:n], chr(ord(target) + 1)) + count_move(s[n:len(s)], target)
	r_min = find_target_string(s[n:len(s)], chr(ord(target) + 1)) + count_move(s[0:n], target)
	return min(l_min, r_min)
n = int(input())
for i in range(0, n):
	input()
	s = input()
	print(find_target_string(s, 'a'))


t = int(input())

def find(s, ch):
	if len(s) == 1:
		return 0 if s[0] == ch else 1
	n = len(s)
	a = s[0:n // 2]
	b = s[n // 2:n]
	acount = a.count(ch)
	bcount = b.count(ch)
	ch = chr(ord(ch) + 1)
	return min(len(a) - acount + find(b, ch), len(b) - bcount + find(a, ch))
for _ in range(t):
	n = int(input())
	s = input()
	print(find(s, 'a'))


def sinistra(s, c):
	if len(s) == 1:
		if s == alph[c]:
			return 0
		else:
			return 1
	conta = 0
	for n in range(len(s) // 2):
		if s[n] != alph[c]:
			conta += 1
	s = s[len(s) // 2:]
	return conta + check(s, c + 1)

def destra(s, c):
	if len(s) == 1:
		if s == alph[c]:
			return 0
		else:
			return 1
	conta = 0
	for n in range(len(s) // 2):
		if s[n + len(s) // 2] != alph[c]:
			conta += 1
	s = s[:len(s) // 2]
	return conta + check(s, c + 1)

def check(s, c):
	return min(sinistra(s, c), destra(s, c))
a = ord('a')
alph = [chr(i) for i in range(a, a + 26)]
T = int(input())
for t in range(T):
	N = int(input())
	s = input()
	print(check(s, 0))


alph = 'abcdefghijklmnopqrstuvwqyz'

def solve(s, i, n):
	if n == 1:
		if s[0] == alph[i]:
			return 0
		else:
			return 1
	else:
		c1 = 0
		c2 = 0
		s1 = s[0:n // 2]
		s2 = s[n // 2:n]
		for x in s1:
			if x != alph[i]:
				c1 += 1
		for x in s2:
			if x != alph[i]:
				c2 += 1
		return min(c1 + solve(s2, i + 1, n // 2), c2 + solve(s1, i + 1, n // 2))
import sys
input = sys.stdin.readline
for f in range(int(input())):
	n = int(input())
	s = input()
	s = s[0:n]
	print(solve(s, 0, n))


import sys
sys.setrecursionlimit(10 ** 6)

def rec(i, j, k):
	if i == j:
		if s[i] == chr(k):
			return 0
		return 1
	else:
		mid = (i + j) // 2
		char = chr(k)
		l = (j - i + 1) // 2
		l1 = s[i:mid + 1].count(char)
		l2 = s[mid + 1:j + 1].count(char)
		return min(l - l1 + rec(mid + 1, j, k + 1), l - l2 + rec(i, mid, k + 1))
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(rec(0, n - 1, 97))


def solve(s, n, c):
	if n == 1:
		if s[0] != c:
			return 1
		return 0
	c1 = 0
	for i in range(0, n // 2):
		if s[i] != c:
			c1 += 1
	c2 = 0
	for i in range(n // 2, n):
		if s[i] != c:
			c2 += 1
	c = chr(ord(c) + 1)
	a = c1 + solve(s[n // 2:], n // 2, c)
	b = c2 + solve(s[:n // 2], n // 2, c)
	return min(a, b)
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(solve(s, n, 'a'))


def solve(c, left, s, cur):
	if len(s) == 0:
		return
	l = len(s) // 2
	if left:
		if len(s) == 1:
			if s != c:
				cur += 1
			global mn
			mn = min(cur, mn)
			return
		else:
			for i in range(l):
				if s[i] != c:
					cur += 1
		j = ord(c[0])
		j += 1
		c = chr(j)
		if len(s) == 2:
			solve(c, 0, s[l:len(s)], cur)
		else:
			solve(c, 0, s[l:len(s)], cur)
			solve(c, 1, s[l:len(s)], cur)
	else:
		if len(s) == 1:
			if s != c:
				cur += 1
			mn = min(cur, mn)
			return
		else:
			for i in range(l, len(s)):
				if s[i] != c:
					cur += 1
		j = ord(c[0])
		j += 1
		c = chr(j)
		if len(s) == 2:
			solve(c, 1, s[0:l], cur)
		else:
			solve(c, 0, s[0:l], cur)
			solve(c, 1, s[0:l], cur)
t = int(input())
for j in range(t):
	n = int(input())
	s = input()
	mn = 100000000
	solve('a', 1, s, 0)
	solve('a', 0, s, 0)
	print(mn)


def calc(s, low, high, ch):
	if low == high:
		if ch == s[low]:
			return 0
		else:
			return 1
	else:
		l = (high - low + 1) // 2
		(left_high, right_low) = (low + (l - 1), low + l)
		(s1, s2) = (s[low:left_high + 1], s[right_low:high + 1])
		v11 = list(filter(lambda x: x != ch, s1))
		v21 = list(filter(lambda x: x != ch, s2))
		v12 = calc(s, right_low, high, chr(ord(ch) + 1))
		v22 = calc(s, low, left_high, chr(ord(ch) + 1))
		return min(len(v11) + v12, len(v21) + v22)
for t in range(int(input())):
	n = int(input())
	s = input()
	ans = calc(s, 0, n - 1, 'a')
	print(ans)


def goodString(word, size, unique):
	if size == 1:
		if word != unique:
			return 1
		else:
			return 0
	uniqueTwo = chr(1 + ord(unique))
	mid = size // 2
	firstWord = word[:mid]
	lastWord = word[mid:]
	uniqueTwo = chr(1 + ord(unique))
	calculationOne = len(firstWord) - firstWord.count(unique) + goodString(lastWord, len(lastWord), uniqueTwo)
	calculationTwo = len(lastWord) - lastWord.count(unique) + goodString(firstWord, len(firstWord), uniqueTwo)
	if calculationOne < calculationTwo:
		return calculationOne
	else:
		return calculationTwo
n = int(input())
for i in range(n):
	size = int(input())
	word = input()
	print(goodString(word, size, 'a'))


def count(s, l, r, char):
	return sum([1 for i in range(l, r + 1) if ord(s[i]) != char])

def devide(s, l, r, ascii_char):
	if r == l:
		return 1 if ord(s[l]) != ascii_char else 0
	mid = (l + r) // 2
	left = devide(s, l, mid, ascii_char + 1) + count(s, mid + 1, r, ascii_char)
	rigth = devide(s, mid + 1, r, ascii_char + 1) + count(s, l, mid, ascii_char)
	return min(left, rigth)
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	print(devide(s, 0, len(s) - 1, 97))


def f(s, c):
	al = 0
	ar = 0
	sl = []
	sr = []
	if len(s) == 1:
		return int(s[0] != c)
	for i in range(round(len(s) / 2)):
		sl.append(s[i])
		ar += int(s[i] != c)
	for i in range(round(len(s) / 2), len(s)):
		sr.append(s[i])
		al += int(s[i] != c)
	return min(al + f(sl, chr(ord(c) + 1)), ar + f(sr, chr(ord(c) + 1)))
t = int(input())
for tt in range(t):
	n = int(input())
	s = input()
	a = []
	for i in range(n):
		a.append(s[i])
	print(f(s, 'a'))


def check(list, c):
	ch = chr(c)
	if len(list) == 1:
		return 1 if ch != list[0] else 0
	x = len(list) // 2
	leftm = x - list[:x].count(ch) + check(list[x:], c + 1)
	rigthm = x - list[x:].count(ch) + check(list[:x], c + 1)
	return min(leftm, rigthm)
for _ in range(int(input())):
	n = int(input())
	string = input()
	print(check(string, ord('a')))


import math
letters = {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9: 'i', 10: 'j', 11: 'k', 12: 'l', 13: 'm', 14: 'n', 15: 'o', 16: 'p', 17: 'q', 18: 'r', 19: 's', 20: 't', 21: 'u', 22: 'v', 23: 'w', 24: 'x', 25: 'y', 26: 'z'}

def allc(ss, c):
	count = 0
	for letter in ss:
		if letter != c:
			count += 1
	return count

def func(ss):
	length = len(ss)
	c = letters[int(math.log(n // length, 2) + 1)]
	if length == 1:
		return allc(ss, c)
	else:
		return min(func(ss[:length // 2]) + allc(ss[length // 2:], c), func(ss[length // 2:]) + allc(ss[:length // 2], c))
t = int(input())
for _ in range(t):
	n = int(input())
	string = input()
	print(func(string))


def sol(s, n, c):
	if n == 1:
		if s[0] == c:
			return 0
		else:
			return 1
	s1 = s[0:n // 2]
	s2 = s[n // 2:]
	x = s1.count(c)
	sx1 = n // 2 - x + sol(s2, n // 2, chr(ord(c) + 1))
	x = s2.count(c)
	sx2 = n // 2 - x + sol(s1, n // 2, chr(ord(c) + 1))
	return min(sx1, sx2)
t = int(input())
while t > 0:
	n = int(input())
	l = [i for i in input()]
	print(sol(l, n, 'a'))
	t -= 1


from sys import stdin
input = lambda : stdin.readline().strip()
from math import ceil, sqrt, factorial, gcd, log
from collections import deque
from bisect import bisect_left, bisect_right

def aise(l, r, c):
	if l > r:
		return 0
	if l == r:
		if s[l] == c:
			return 0
		else:
			return 1
	a = {}
	b = {}
	count = 0
	m = (l + r) // 2
	for i in range(l, m + 1):
		if s[i] in a:
			a[s[i]] += 1
		else:
			a[s[i]] = 1
	for i in range(m + 1, r + 1):
		if s[i] in b:
			b[s[i]] += 1
		else:
			b[s[i]] = 1
	f = ceil((r - l + 1) // 2)
	if c in a:
		e = f - a[c] + aise(m + 1, r, chr(ord(c) + 1))
	else:
		e = f + aise(m + 1, r, chr(ord(c) + 1))
	if c in b:
		w = f - b[c] + aise(l, m, chr(ord(c) + 1))
	else:
		w = f + aise(l, m, chr(ord(c) + 1))
	return min(e, w)
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(aise(0, n - 1, 'a'))


from collections import defaultdict

def solve(a, c, l, r):
	if l == r:
		if a[l] == c:
			return 0
		else:
			return 1
	c1 = 0
	c2 = 0
	mid = (l + r) // 2
	for i in range(l, mid + 1):
		if a[i] != c:
			c1 += 1
	for i in range(mid + 1, r + 1):
		if a[i] != c:
			c2 += 1
	x = solve(a, chr(ord(c) + 1), l, mid)
	y = solve(a, chr(ord(c) + 1), mid + 1, r)
	return min(c1 + y, c2 + x)
t = int(input())
while t:
	n = int(input())
	a = input()
	print(solve(a, 'a', 0, n - 1))
	t -= 1


def dfs(l, r, c, s):
	if len(s) == 1:
		return (s[0] == c) ^ 1
	mid = (l + r) // 2
	(L, R) = (s[:mid], s[mid:])
	(lC, rC) = (len(L) - L.count(c), len(R) - R.count(c))
	ans1 = lC + dfs(0, mid, chr(ord(c) + 1), R)
	ans2 = rC + dfs(0, mid, chr(ord(c) + 1), L)
	return min(ans1, ans2)

def solve():
	n = int(input())
	s = input()
	ans = dfs(0, n, 'a', s)
	print(ans)
t = int(input())
for _ in range(t):
	solve()


from string import ascii_lowercase
from functools import lru_cache
for _ in range(int(input())):
	n = int(input())
	s = input()

	@lru_cache(maxsize=None)
	def f(i, j, ci):
		c = ascii_lowercase[ci]
		if j - i == 1:
			return int(c != s[i])
		m = (i + j) // 2
		return min(m - i - s[i:m].count(c) + f(m, j, ci + 1), j - m - s[m:j].count(c) + f(i, m, ci + 1))
	print(f(0, n, 0))


import math
ALP = 'abcdefghijklmnopqrstuvwxyz'
for i in range(int(input())):
	t = int(input())
	u = int(math.log(t, 2))
	A = input().strip()
	CS = [[] for _ in range(26)]
	cost_min = 131072
	for l in range(u + 1):
		cnt = 0
		CS[l].append(0)
		for n in A:
			if ALP[l] == n:
				cnt += 1
			CS[l].append(cnt)
	for p in range(1 << u):
		cost = 0
		start = 0
		end = t
		tmp = t
		for j in range(u):
			tmp = tmp // 2
			if cost > cost_min:
				break
			if p >> j & 1:
				cost += tmp - (CS[j][end] - CS[j][start + tmp])
				end -= tmp
			else:
				cost += tmp - (CS[j][start + tmp] - CS[j][start])
				start += tmp
		else:
			cost += 1 - (CS[u][end] - CS[u][start])
			cost_min = min(cost_min, cost)
	print(cost_min)


import math
t = int(input())

def solve(ch, arr, l):
	ch1 = chr(ord(ch) + 1)
	if l == 1:
		return 0 if ch == arr[0] else 1
	t1 = solve(ch1, arr[l // 2:], l // 2)
	t2 = l // 2 - arr[:l // 2].count(ch)
	t3 = solve(ch1, arr[:l // 2], l // 2)
	t4 = l // 2 - arr[l // 2:].count(ch)
	return min(t1 + t2, t3 + t4)
for i in range(t):
	n = int(input())
	a = list(input())
	print(solve('a', a, n))


def minMoves(c, startIndex, endIndex):
	if startIndex == endIndex:
		if s[startIndex] == c:
			return 0
		else:
			return 1
	else:
		firstHalfChange = (endIndex + 1 - startIndex) // 2
		for i in range(startIndex, (startIndex + endIndex + 1) // 2):
			if s[i] == c:
				firstHalfChange -= 1
		firstHalfMin = firstHalfChange + minMoves(chr(ord(c) + 1), (startIndex + endIndex + 1) // 2, endIndex)
		secondHalfChange = (endIndex + 1 - startIndex) // 2
		for i in range((startIndex + endIndex + 1) // 2, endIndex + 1):
			if s[i] == c:
				secondHalfChange -= 1
		secondHalfMin = secondHalfChange + minMoves(chr(ord(c) + 1), startIndex, (startIndex + endIndex) // 2)
	return min(firstHalfMin, secondHalfMin)
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	ans = minMoves('a', 0, n - 1)
	print(ans)


import sys

def solve(s):

	def min_moves(s, c):
		if len(s) == 1:
			return 0 if s[0] == c else 1
		else:
			h = len(s) // 2
			nc_1st = sum([1 if s[i] == c else 0 for i in range(h)])
			nc_2nd = sum([1 if s[i] == c else 0 for i in range(h, 2 * h)])
			return min(h - nc_2nd + min_moves(s[:h], chr(ord(c) + 1)), h - nc_1st + min_moves(s[h:], chr(ord(c) + 1)))
	mn = float('inf')
	for c0 in 'a':
		count_c0 = min_moves(s, c0)
		mn = min(mn, count_c0)
	print(mn)
IN = [x.strip() for x in sys.stdin.readlines()]
T = int(IN[0])
cur = 1
for ti in range(T):
	n = int(IN[cur])
	s = IN[cur + 1]
	solve(s)
	cur += 2


import sys
import math
import heapq
from collections import defaultdict, deque
from decimal import *

def r():
	return int(input())

def rm():
	return map(int, input().split())

def rl():
	return list(map(int, input().split()))

def givegood(a, check):
	if len(a) == 1:
		if a[0] == check:
			return 0
		else:
			return 1
	lcheck = 0
	rcheck = 0
	n = len(a)
	for i in range(n // 2):
		if a[i] != check:
			lcheck += 1
	for i in range(n // 2, n):
		if a[i] != check:
			rcheck += 1
	la = a[:n // 2]
	ra = a[n // 2:]
	lans = givegood(ra, chr(ord(check) + 1))
	rans = givegood(la, chr(ord(check) + 1))
	return min(lcheck + lans, rcheck + rans)
for _ in range(r()):
	n = r()
	a = input()
	print(givegood(a, 'a'))


LETRAS = 'abcdefghijklmnopqrstuvwxyz'

def string_boa(indice, string):
	letras = LETRAS[indice]
	if len(string) == 1:
		if string != letras:
			return 1
		else:
			return 0
	direita = 0
	esquerda = 0
	for l in string[:len(string) // 2]:
		if l != letras:
			direita += 1
	for l in string[len(string) // 2:]:
		if l != letras:
			esquerda += 1
	lado_direito = direita + string_boa(indice + 1, string[len(string) // 2:])
	lado_esquerdo = esquerda + string_boa(indice + 1, string[:len(string) // 2])
	return min(lado_direito, lado_esquerdo)
entrada = int(input())
for i in range(entrada):
	n = int(input())
	string = input()
	print(string_boa(0, string))


import os
import sys
from io import BytesIO, IOBase
import math

def main():
	pass
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def getmin(s, ch):
	l = len(s)
	if l == 1:
		if s[0] != ch:
			return 1
		else:
			return 0
	else:
		s1 = s[0:l // 2]
		s2 = s[l // 2:l]
		f = l // 2 - s1.count(ch) + getmin(s2, chr(ord(ch) + 1))
		s = l // 2 - s2.count(ch) + getmin(s1, chr(ord(ch) + 1))
		return min(f, s)
for xyz in range(0, int(input())):
	n = int(input())
	s = input()
	print(getmin(s, 'a'))


import sys

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return map(int, minp().split())

def solve():
	n = mint()
	s = [ord(i) - ord('a') for i in minp()]
	for k in range(100):
		if 2 ** k == n:
			break
	p = [[0] * n for i in range(26)]
	for i in range(n):
		for j in range(s[i]):
			p[j][i] += 1
		for j in range(s[i] + 1, 26):
			p[j][i] += 1
	for i in range(26):
		p[i].insert(0, 0)
		for j in range(1, n + 1):
			p[i][j] += p[i][j - 1]
	dp = [[0] * (n // 2 ** i) for i in range(k + 1)]
	for i in range(n):
		dp[0][i] = int(s[i] != k)
	for i in range(1, k + 1):
		z = 2 ** (i - 1)
		zz = 2 * z
		pp = p[k - i]
		for j in range(n // zz):
			dp[i][j] = min(pp[j * zz + z] - pp[j * zz] + dp[i - 1][2 * j + 1], pp[j * zz + zz] - pp[j * zz + z] + dp[i - 1][2 * j])
	print(dp[k][0])
for i in range(mint()):
	solve()


from sys import *
setrecursionlimit(10 ** 6)

def make_good(string, cnt, piv):
	global Move
	L = len(string)
	if L == 1:
		if string == chr(piv):
			Move = min(Move, cnt)
			return
		else:
			Move = min(Move, cnt + 1)
			return
	mid = L // 2
	l_cnt = 0
	h_cnt = 0
	for i in range(L):
		if string[i] != chr(piv):
			if i < mid:
				l_cnt += 1
			else:
				h_cnt += 1
	make_good(string[mid:], cnt + l_cnt, piv + 1)
	make_good(string[:mid], cnt + h_cnt, piv + 1)
input = stdin.readline
t = int(input())
for case in range(t):
	n = int(input())
	s = input().rstrip()
	Move = n + 1
	make_good(s, 0, 97)
	print(Move)


def read_list():
	return list(map(int, input().split(' ')))

def print_list(l):
	print(' '.join(map(str, l)))

def count(c, start, end):
	res = 0
	for i in range(start, end):
		if s[i] != c:
			res += 1
	return res

def match(c, start, end):
	if start + 1 == end:
		if s[start] == c:
			return 0
		else:
			return 1
	l = end - start >> 1
	return min(count(c, start, start + l) + match(chr(1 + ord(c)), start + l, end), count(c, start + l, end) + match(chr(1 + ord(c)), start, start + l))
N = int(input())
for _ in range(N):
	n = int(input())
	s = input().strip()
	print(match('a', 0, n))


import sys
input = sys.stdin.readline
import heapq
import copy
import math
import decimal

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def insr2():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())

def pr_list(a):
	print(*a, sep=' ')

def main():
	tests = inp()
	mod = 1000000007
	limit = 10 ** 18
	ans = 0
	stack = []
	hashm = {}
	arr = []
	heapq.heapify(arr)
	for test in range(tests):
		n = inp()
		s = insr()
		pre_sum = [[0 for i in range(len(s) + 1)] for j in range(26)]
		for i in range(len(s)):
			for j in range(26):
				if ord(s[i]) - ord('a') != j:
					pre_sum[j][i + 1] = pre_sum[j][i]
				else:
					pre_sum[j][i + 1] = pre_sum[j][i] + 1
		for i in range(26):
			pre_sum[i].pop(0)

		def helper(s, l, r, hashm, c):
			if l == r:
				if ord(s[l]) - ord('a') == c:
					return 0
				else:
					return 1
			mid = (l + r + 1) // 2
			left = helper(s, l, mid - 1, hashm, c + 1) + (r - mid + 1) - (hashm[c][r] - hashm[c][mid] + (ord(s[mid]) - ord('a') == c))
			right = helper(s, mid, r, hashm, c + 1) + (mid - 1 - l + 1) - (hashm[c][mid - 1] - hashm[c][l] + (ord(s[l]) - ord('a') == c))
			return min(left, right)
		print(helper(s, 0, len(s) - 1, pre_sum, 0))
main()


def goodString(s, c):
	if len(s) == 1:
		if s[0] == c:
			return 0
		return 1
	n = len(s)
	half = int(n / 2)
	s1 = [c for i in range(half)]
	if s1 == s[:half]:
		return goodString(s[half:], chr(ord(c) + 1))
	elif s1 == s[half:]:
		return goodString(s[:half], chr(ord(c) + 1))
	else:
		x1 = 0
		for i in range(half):
			if s[i] != c:
				x1 += 1
		x1 += goodString(s[half:], chr(ord(c) + 1))
		x2 = 0
		for i in range(half, n):
			if s[i] != c:
				x2 += 1
		x2 += goodString(s[:half], chr(ord(c) + 1))
		return min(x1, x2)
t = int(input())
while True:
	if t == 0:
		break
	t -= 1
	n = int(input())
	s = input()
	print(goodString(s, 'a'))


def f(s, ch):
	if len(s) == 1:
		return 0 if s[0] == ch else 1
	n = len(s)
	a = s[0:n // 2]
	b = s[n // 2:n]
	cntA = a.count(ch)
	cntB = b.count(ch)
	ch = chr(ord(ch) + 1)
	return min(len(a) - cntA + f(b, ch), len(b) - cntB + f(a, ch))
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(f(s, 'a'))


def solve(c, l, h, s):
	c = chr(c)
	if l == h:
		return int(s[l] != c)
	k = h - l + 1
	cc = 0
	for i in range(l, l + k // 2):
		if s[i] != c:
			cc += 1
	kk = cc + solve(ord(c) + 1, l + k // 2, h, s)
	cc = 0
	for i in range(l + k // 2, h + 1):
		if s[i] != c:
			cc += 1
	pp = cc + solve(ord(c) + 1, l, l + k // 2 - 1, s)
	return min(pp, kk)
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(solve(ord('a'), 0, n - 1, s))


def task(char, list1):
	map1[char] = [0]
	count = 0
	for i in range(len(list1)):
		if list1[i] == char:
			count += 1
		map1[char].append(count)

def recur(char, start, end, moves, count):
	if 2 ** count == n:
		if list1[start] == char:
			ans.append(moves + 1)
		else:
			ans.append(moves)
		return
	recur(chr(ord(char) + 1), start, (start + end - 1) // 2, moves + map1[char][end + 1] - map1[char][(start + end + 1) // 2], count + 1)
	recur(chr(ord(char) + 1), (start + end + 1) // 2, end, moves + map1[char][(start + end - 1) // 2 + 1] - map1[char][start], count + 1)
t = int(input())
for _ in range(t):
	n = int(input())
	list1 = input().strip()
	map1 = dict()
	for i in range(97, 122 + 1):
		ch = chr(i)
		task(ch, list1)
	ans = []
	recur('a', 0, n - 1, 0, 0)
	print(n - max(ans))


import math

def mi():
	return map(int, input().split())

def ii():
	return int(input())

def li():
	return list(map(int, input().split()))

def si():
	return input().split()

def chk(s, ch):
	ans = 0
	for i in s:
		if i != ch:
			ans += 1
	return ans

def bin(n, ch, s):
	if n == 1:
		if s == ch:
			return 0
		else:
			return 1
	m1 = chk(s[:n // 2], ch) + bin(n // 2, chr(ord(ch) + 1), s[n // 2:])
	m2 = chk(s[n // 2:], ch) + bin(n // 2, chr(ord(ch) + 1), s[:n // 2])
	return min(m1, m2)
t = ii()
for _ in range(t):
	n = ii()
	s = input()
	print(bin(n, 'a', s))


def goodstring(s, h, impind):
	if len(s) == 1:
		if s[0] != h[impind]:
			return 1
		else:
			return 0
	mid = len(s) // 2
	p = goodstring(s[:mid], h, impind + 1) + mid - s[mid:].count(h[impind])
	q = goodstring(s[mid:], h, impind + 1) + mid - s[:mid].count(h[impind])
	return min(p, q)
T = int(input())
h = {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9: 'i', 10: 'j', 11: 'k', 12: 'l', 13: 'm', 14: 'n', 15: 'o', 16: 'p', 17: 'q', 18: 'r', 19: 's', 20: 't', 21: 'u', 22: 'v', 23: 'w', 24: 'x', 25: 'y', 26: 'z'}
for i in range(T):
	n = int(input())
	s = input()
	print(goodstring(s, h, 1))


def change_count(num, st, cur):
	if num == 1:
		if st == cur:
			return 0
		else:
			return 1
	else:
		mid = num // 2
		left = mid - st[:mid].count(cur)
		right = mid - st[mid:].count(cur)
		return min(left + change_count(mid, st[mid:], chr(ord(cur) + 1)), right + change_count(mid, st[:mid], chr(ord(cur) + 1)))
for i in range(int(input())):
	n = int(input())
	s = input()
	print(change_count(n, s, 'a'))


def calc_steps_c_good(string: str, letter_code: int, length: int) -> int:
	if length == 1:
		if string == chr(letter_code):
			return 0
		return 1
	mid = length >> 1
	start_string = string[:mid]
	end_string = string[mid:]
	start_ct = start_string.count(chr(letter_code))
	end_ct = end_string.count(chr(letter_code))
	front_steps = calc_steps_c_good(start_string, letter_code + 1, mid)
	end_steps = calc_steps_c_good(end_string, letter_code + 1, mid)
	return min(front_steps + mid - end_ct, end_steps + mid - start_ct)
for _ in range(int(input())):
	l = int(input())
	value = input()
	print(calc_steps_c_good(value, ord('a'), l))


import sys
from collections import defaultdict as dd
from collections import Counter as cc
from queue import Queue
import math
import itertools
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass

def a(w, l, c):
	if len(w) == 1:
		if w[0] == c:
			return 0
		else:
			return 1
	else:
		e = chr(ord(c) + 1)
		return min(l - w[:l].count(c) + a(w[l:], l // 2, e), l - w[l:].count(c) + a(w[:l], l // 2, e))
for _ in range(int(input())):
	q = int(input())
	w = input()
	print(a(w, q // 2, 'a'))


def get_next_char(char):
	return chr(ord(char) + 1)

def solve(string, size, char='a'):
	if len(string) == 1:
		if string[0] == char:
			return 0
		return 1
	half_size = size // 2
	fst_half = string[0:half_size]
	snd_half = string[half_size:]
	moves_on_left = half_size - fst_half.count(char)
	moves_on_right = half_size - snd_half.count(char)
	left = moves_on_left + solve(snd_half, half_size, get_next_char(char))
	right = moves_on_right + solve(fst_half, half_size, get_next_char(char))
	return min(left, right)
for _ in range(int(input())):
	size = int(input())
	string = list(input())
	print(solve(string, size))


def count(s, c, l, r):
	length = r - l + 1
	for i in range(l, r + 1):
		if s[i] == c:
			length -= 1
	return length

def getMin(l, r, c, s):
	length = r - l + 1
	if length == 1:
		if s[l] == c:
			return 0
		else:
			return 1
	else:
		nc = chr(ord(c) + 1)
		mid = length // 2
		return min(count(s, c, l + mid, r) + getMin(l, l + mid - 1, nc, s), count(s, c, l, l + mid - 1) + getMin(l + mid, r, nc, s))
t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	print(getMin(0, n - 1, 'a', s))


t = int(input())

def calc(l, r, c):
	if r - l == 1:
		if s[l] == c:
			return 0
		else:
			return 1
	m = (l + r) // 2
	nc = chr(ord(c) + 1)
	cnt1 = (r - l) // 2 - s[l:m].count(c) + calc(m, r, nc)
	cnt2 = (r - l) // 2 - s[m:r].count(c) + calc(l, m, nc)
	return min(cnt1, cnt2)
for _ in range(t):
	n = int(input())
	s = str(input())
	print(calc(0, n, 'a'))


import math

def calc(l, r, c, s):
	if r == l:
		if s[l] == c:
			return 0
		else:
			return 1
	m = (l + r) // 2
	c1 = (r - l + 1) // 2 - s[l:m + 1].count(c) + calc(m + 1, r, chr(ord(c) + 1), s)
	c2 = (r - l + 1) // 2 - s[m + 1:r + 1].count(c) + calc(l, m, chr(ord(c) + 1), s)
	return min(c1, c2)
t = int(input())
for w in range(t):
	n = int(input())
	s = input()
	if n == 1:
		if s == 'a':
			print(0)
		else:
			print(1)
	else:
		print(calc(0, n - 1, 'a', s))


def good(s, letter):
	if len(s) == 1:
		if s == letter:
			return 0
		else:
			return 1
	half = len(s) // 2
	first = s[:half]
	second = s[half:]
	return min(half - first.count(letter) + good(second, chr(ord(letter) + 1)), half - second.count(letter) + good(first, chr(ord(letter) + 1)))
total = int(input())
for _ in range(total):
	len_n = input()
	original = input().strip()
	print(good(original, 'a'))


def minimum_moves(s, c):
	if len(s) == 1:
		if s != c:
			return 1
		else:
			return 0
	middle = len(s) // 2
	left_part = s[:middle]
	right_part = s[middle:]
	right_moves = len(right_part) - right_part.count(c) + minimum_moves(left_part, chr(ord(c) + 1))
	left_moves = len(left_part) - left_part.count(c) + minimum_moves(right_part, chr(ord(c) + 1))
	return min(left_moves, right_moves)
n = int(input())
for i in range(n):
	size = int(input())
	s = input()
	print(minimum_moves(s, 'a'))


import time, math as mt, bisect, sys
sys.setrecursionlimit(132000 * 2)
from sys import stdin, stdout
from collections import deque
from fractions import Fraction
from collections import Counter
from collections import OrderedDict
pi = 3.141592653589793

def II():
	return int(stdin.readline())

def IO():
	return stdin.readline()

def IP():
	return map(int, stdin.readline().split())

def L():
	return list(map(int, stdin.readline().split()))

def P(x):
	return stdout.write(str(x) + '\n')

def PI(x, y):
	return stdout.write(str(x) + ' ' + str(y) + '\n')

def lcm(a, b):
	return a * b // gcd(a, b)

def gcd(a, b):
	if a == 0:
		return b
	elif b == 0:
		return a
	if a > b:
		return gcd(a % b, b)
	else:
		return gcd(a, b % a)

def readTree():
	v = int(input())
	adj = [set() for i in range(v + 1)]
	for i in range(v - 1):
		(u1, u2) = In()
		adj[u1].add(u2)
		adj[u2].add(u1)
	return (adj, v)

def bfs(adj, v):
	visited = [False] * (v + 1)
	q = deque()
	while q:
		pass

def sieve():
	li = [True] * 1000001
	(li[0], li[1]) = (False, False)
	for i in range(2, len(li), 1):
		if li[i] == True:
			for j in range(i * i, len(li), i):
				li[j] = False
	prime = []
	for i in range(1000001):
		if li[i] == True:
			prime.append(i)
	return prime

def setBit(n):
	count = 0
	while n != 0:
		n = n & n - 1
		count += 1
	return count
mx = 10 ** 7
spf = [mx] * (mx + 1)

def SPF():
	spf[1] = 1
	for i in range(2, mx + 1):
		if spf[i] == mx:
			spf[i] = i
			for j in range(i * i, mx + 1, i):
				if i < spf[j]:
					spf[j] = i
	return
mod = 10 ** 9 + 7

def good(s, c, st, n):
	if n - st == 1:
		if s[st] != c:
			return 1
		else:
			return 0
	else:
		(mv1, mv2) = (0, 0)
		for i in range(st, st + (n - st) // 2):
			if s[i] != c:
				mv1 += 1
		for i in range(st + (n - st) // 2, n):
			if s[i] != c:
				mv2 += 1
		c1 = good(s, chr(ord(c) + 1), st + (n - st) // 2, n)
		c2 = good(s, chr(ord(c) + 1), st, st + (n - st) // 2)
		if mv1 + c1 < mv2 + c2:
			return mv1 + c1
		else:
			return mv2 + c2

def solve():
	n = int(input())
	s = input()
	print(good(s, 'a', 0, n))
t = int(input())
for i in range(t):
	solve()


def val(s, c):
	n = len(s)
	if n == 1:
		return int(ord(s[0]) != c)
	n = n // 2
	return min(val(s[:n], c + 1) + n - s[n:].count(chr(c)), val(s[n:], c + 1) + n - s[:n].count(chr(c)))
t = int(input())
for i in range(t):
	input()
	print(val(input(), 97))


import sys
sys.setrecursionlimit(100000)
from collections import defaultdict
from collections import deque
import heapq
I = lambda : list(map(int, input().split(' ')))

def search(start, end, c, s, dist):
	mid = int(start + end) // 2
	if end > start:
		a = search(mid + 1, end, dct_a[1 + dct[c]], s, dist) + get_freq(start, mid, dist, c)
		b = search(start, mid, dct_a[1 + dct[c]], s, dist) + get_freq(mid + 1, end, dist, c)
		return max(a, b)
	elif end == start:
		return get_freq(start, end, dist, c)
	else:
		return 0

def get_freq(start, end, dist, c):
	j = dct[c] - 1
	return dist[end + 1][j] - dist[start][j]
for _ in range(int(input())):
	n = int(input())
	s = input()
	alphabet = 'abcdefghijklmnopqrstuvwxyz'
	dct = {}
	dct_a = {}
	for (i, a) in enumerate(alphabet):
		dct[a] = i + 1
		dct_a[i + 1] = a
	ss = [0 for i in range(26)]
	dist = [ss]
	for a in s:
		new_ = [ele for ele in dist[-1]]
		new_[dct[a] - 1] += 1
		dist.append(new_)
	print(n - search(0, n - 1, 'a', s, dist))


def check(string, ch, n):
	if n == 1:
		if string == ch:
			return 0
		else:
			return 1
	half = n // 2
	op1 = check(string[0:half], chr(ord(ch) + 1), half) + fix(string[half:], ch, half)
	op2 = fix(string[0:half], ch, half) + check(string[half:], chr(ord(ch) + 1), half)
	return min(op1, op2)

def fix(string, ch, n):
	counter = 0
	for x in string:
		if x != ch:
			counter += 1
	return counter
t = int(input())
for i in range(t):
	n = int(input())
	string = input()
	print(check(string, 'a', n))


import sys
import bisect as bi
import math
import string
from collections import defaultdict as dd
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)

def cin():
	return map(int, sin().split())

def ain():
	return list(map(int, sin().split()))

def sin():
	return input()

def inin():
	return int(input())
d = 'abcdefghijklmnopqrstuvwxyz'

def solve(s, n, ans, c):
	if n == 1:
		if ord(s) - ord('a') == c:
			return 0
		else:
			return 1
	c1 = s[0:n // 2].count(d[c])
	c2 = s[n // 2:n].count(d[c])
	s1 = s[n // 2:n]
	s2 = s[0:n // 2]
	ans += min(solve(s1, n // 2, ans, c + 1) + n // 2 - c1, solve(s2, n // 2, ans, c + 1) + n // 2 - c2)
	return ans
for _ in range(inin()):
	n = inin()
	s = sin().strip()
	print(solve(s, n, 0, 0))


def solve(s, c):
	n = len(s)
	if n == 1:
		return int(s != c)
	n //= 2
	return min(n - s[:n].count(c) + solve(s[n:], chr(ord(c) + 1)), n - s[n:].count(c) + solve(s[:n], chr(ord(c) + 1)))
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(solve(s, 'a'))


import sys, math
sys.setrecursionlimit(1000000)
INF = 1 << 100
mod = 1000000007
input = lambda : sys.stdin.readline().rstrip()
li = lambda : list(map(int, input().split()))

def f(S, depth):
	N = len(S)
	l = chr(ord('a') + depth)
	if N == 1:
		if l == S:
			return 0
		return 1
	x = N // 2 - S[N // 2:].count(l) + f(S[:N // 2], depth + 1)
	y = N // 2 - S[:N // 2].count(l) + f(S[N // 2:], depth + 1)
	return min(x, y)
t = int(input())
output = []
for _ in range(t):
	N = int(input())
	S = input()
	ans = f(S, 0)
	output.append(ans)
for o in output:
	print(o)


def think(A, mid, s):
	if len(s) == 1:
		if s[0] == A:
			return 0
		else:
			return 1
	f_half = think(A + 1, mid // 2, s[:mid])
	f_half += mid - s[mid:].count(A)
	s_half = think(A + 1, mid // 2, s[mid:])
	s_half += mid - s[:mid].count(A)
	return min(f_half, s_half)
for _ in range(int(input())):
	n = input()
	s = list(map(ord, str(input())))
	A = 97
	mid = len(s) // 2
	moves = think(A, mid, s)
	print(moves)


alpha = 'abcdefghijklmnopqrstuvqxyz'

def solve(s, lvl, cost):
	n = len(s)
	if n == 1:
		if s == alpha[lvl]:
			return cost
		else:
			return cost + 1
	c1 = n // 2 - s[:n // 2].count(alpha[lvl]) + solve(s[n // 2:], lvl + 1, cost)
	c2 = n // 2 - s[n // 2:].count(alpha[lvl]) + solve(s[:n // 2], lvl + 1, cost)
	return cost + min(c1, c2)

def tc():
	n = int(input())
	s = input()
	ans = solve(s, 0, 0)
	print(ans)
T = int(input())
for _ in range(T):
	tc()


def solve(s, c):
	if len(s) == 0:
		return 0
	if len(s) == 1:
		if s[0] == c:
			return 0
		else:
			return 1
	n = len(s)
	first = n // 2 - s[0:n // 2].count(c)
	second = n // 2 - s[n // 2:].count(c)
	return min(solve(s[n // 2:], chr(ord(c) + 1)) + first, solve(s[:n // 2], chr(ord(c) + 1)) + second)
from collections import defaultdict
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	print(solve(s, 'a'))


from string import ascii_lowercase as alph
from itertools import product as combinations

def gen(l, r, ch, s0, s1):
	if l == r:
		s0[l] = alph[ch]
		diff = 0
		for i in range(len(s0)):
			if s0[i] != s1[i]:
				diff += 1
		return diff
	h = (r - l + 1) // 2
	m = l + h
	sl = s0[:]
	for i in range(l, m):
		sl[i] = alph[ch]
	sr = s0
	for i in range(m, r + 1):
		sr[i] = alph[ch]
	return min(gen(l, m - 1, ch + 1, sr, s1), gen(m, r, ch + 1, sl, s1))

def min_turns(l, r, ch):
	if l == r:
		return 0 if s[l] == alph[ch] else 1
	h = (r - l + 1) // 2
	m = l + h
	lc = sum((1 for i in range(l, m) if s[i] != alph[ch]))
	rc = sum((1 for i in range(m, r + 1) if s[i] != alph[ch]))
	lm = min_turns(m, r, ch + 1)
	rm = min_turns(l, m - 1, ch + 1)
	if lc + lm < rc + rm:
		return lc + lm
	else:
		return rc + rm
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(min_turns(0, n - 1, 0))


def solve(s, c):
	if len(s) == 1:
		if s[0] == c:
			return 0
		else:
			return 1
	ans1 = sum([i != c for i in s[:len(s) // 2]]) + solve(s[len(s) // 2:], chr(ord(c) + 1))
	ans2 = sum([i != c for i in s[len(s) // 2:]]) + solve(s[:len(s) // 2], chr(ord(c) + 1))
	return min(ans1, ans2)
for _ in range(int(input())):
	input()
	print(solve(input(), 'a'))


for _ in range(int(input())):
	n = int(input())
	s = input()

	def doit(l, r, c):
		if l == r:
			return int(c != s[l])
		mid = l + r >> 1
		cntl = doit(l, mid, chr(ord(c) + 1)) + mid - l + 1 - s[mid + 1:r + 1].count(c)
		cntr = doit(mid + 1, r, chr(ord(c) + 1)) + r - mid - s[l:mid + 1].count(c)
		return min(cntl, cntr)
	print(doit(0, n - 1, 'a'))


def solution(s, l, r, c):
	if r - l == 1:
		if s[l] == chr(c):
			return 0
		return 1
	else:
		mid = (l + r) // 2
		x = solution(s, l, mid, c + 1)
		x += mid - l - s[mid:r].count(chr(c))
		y = solution(s, mid, r, c + 1)
		y += r - mid - s[l:mid].count(chr(c))
		return min(x, y)
t = int(input())
for i in range(t):
	n = int(input())
	s = str(input())
	print(solution(s, 0, n, 97))


import sys

def input():
	return sys.stdin.readline().rstrip()

def input_split():
	return [int(i) for i in input().split()]
testCases = int(input())
answers = []

def min_cost(arr, desired):
	if len(arr) == 1:
		if arr[0] == desired:
			return 0
		else:
			return 1
	else:
		m = len(arr)
		half = m // 2
		diffs = [1 if a != desired else 0 for a in arr]
		c1 = sum(diffs[:half])
		c2 = sum(diffs[half:])
		return min(c1 + min_cost(arr[half:], desired + 1), c2 + min_cost(arr[:half], desired + 1))
for _ in range(testCases):
	n = int(input())
	s = input()
	arr = [ord(i) for i in s]
	ans = min_cost(arr, ord('a'))
	answers.append(ans)
print(*answers, sep='\n')


try:

	def search(a, l, r, c):
		if len(a) == 1:
			if ord(a[0]) == c:
				return 0
			else:
				return 1
		p = a[:r // 2]
		q = a[r // 2:]
		count1 = 0
		for i in range(len(p)):
			if ord(p[i]) == c:
				count1 += 1
		s = search(q, 0, len(q), c + 1)
		total1 = (r - l) // 2 - count1 + s
		count2 = 0
		for i in range(len(q)):
			if ord(q[i]) == c:
				count2 += 1
		total2 = (r - l) // 2 - count2 + search(p, 0, len(p), c + 1)
		return min(total1, total2)
	t = int(input())
	for v in range(t):
		n = int(input())
		a = input()
		char = 97
		print(search(a, 0, n, 97))
except:
	pass


def calc(s, l, r, ch):
	if r - l == 1:
		if s[l] == ch:
			return 0
		else:
			return 1
	else:
		cnt1 = 0
		cnt2 = 0
		for i in range(l, (l + r) // 2):
			if s[i] != ch:
				cnt1 += 1
		cnt_left = cnt1 + calc(s, (l + r) // 2, r, chr(ord(ch) + 1))
		for i in range((l + r) // 2, r):
			if s[i] != ch:
				cnt2 += 1
		cnt_right = cnt2 + calc(s, l, (l + r) // 2, chr(ord(ch) + 1))
		return min(cnt_left, cnt_right)
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	print(calc(s, 0, n, 'a'))


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return list(map(int, input().split()))
hash_map = {}

def solve(c, s_list):
	if len(s_list) == 1:
		if s_list[0] != c:
			return 1
		return 0
	if (c, tuple(s_list)) in hash_map:
		return hash_map[c, tuple(s_list)]
	(left, right, _min) = (0, 0, float('inf'))
	mid = int(len(s_list) / 2)
	for _i in range(mid):
		if s_list[_i] != c:
			left += 1
	_min = min(_min, left + solve(chr(ord(c) + 1), s_list[mid:]))
	for _i in range(mid):
		if s_list[mid + _i] != c:
			right += 1
	_min = min(_min, right + solve(chr(ord(c) + 1), s_list[:mid]))
	hash_map[c, tuple(s_list)] = _min
	return _min
t = inp()
for i in range(t):
	n = inp()
	s = insr()
	print(solve('a', s))


import sys
sys.setrecursionlimit(10 ** 5)

def dp(a, ct, i, j):
	m = (i + j + 1) // 2
	if i >= j:
		return 0
	elif i + 1 == j:
		if a[i] == chr(ct) and a[j] == chr(ct + 1):
			return 0
		elif a[i] == chr(ct + 1) and a[j] == chr(ct):
			return 0
		elif a[i] != chr(ct) and a[j] == chr(ct + 1) or (a[i] == chr(ct) and a[j] != chr(ct + 1)) or (a[i] != chr(ct + 1) and a[j] == chr(ct)) or (a[i] == chr(ct + 1) and a[j] != chr(ct)):
			return 1
		else:
			return 2
	else:
		(l, r) = (0, 0)
		for k in range(i, m):
			if a[k] != chr(ct):
				l += 1
		for k in range(m, j + 1):
			if a[k] != chr(ct):
				r += 1
		ans = min(l + dp(a, ct + 1, m, j), r + dp(a, ct + 1, i, m - 1))
		return ans
for _ in range(int(input())):
	n = int(input())
	a = input().strip()
	if n == 1:
		if a[0] == 'a':
			print(0)
		else:
			print(1)
	else:
		b = dp(a, 97, 0, n - 1)
		print(b)


def cost(lst, n, ch):
	if n == 1:
		if lst[0] == ch:
			return 0
		return 1
	else:
		a1 = lst[:n // 2]
		a2 = lst[n // 2:]
		return min(n // 2 - a1.count(ch) + cost(a2, n // 2, chr(ord(ch) + 1)), n // 2 - a2.count(ch) + cost(a1, n // 2, chr(ord(ch) + 1)))
for _ in range(int(input())):
	m = int(input())
	a = list(input())
	print(cost(a, m, 'a'))


for t in range(int(input())):
	n = int(input())
	s = [ord(c) - ord('a') for c in input()]

	def solve(l, r, target):
		if r - l == 1:
			return 0 if s[l] == target else 1
		m = (l + r) // 2
		return min(sum((v != target for v in s[l:m])) + solve(m, r, target + 1), sum((v != target for v in s[m:r])) + solve(l, m, target + 1))
	print(solve(0, n, 0))


def s_half(string, c):
	score = 0
	for s in string:
		if s != c:
			score += 1
	return score

def sp1_half(string, c):
	score = 0
	if len(string) == 1:
		if string != c:
			score += 1
		return score
	l = string[:int(len(string) / 2)]
	r = string[int(len(string) / 2):]
	ls = s_half(l, c)
	lsp1 = sp1_half(l, chr(ord(c) + 1))
	rs = s_half(r, c)
	rsp1 = sp1_half(r, chr(ord(c) + 1))
	if ls + rsp1 <= rs + lsp1:
		return ls + rsp1
	else:
		return rs + lsp1
t = input()
for i in range(0, int(t)):
	n = input()
	s = input()
	score = sp1_half(s, 'a')
	print(score)


import sys, threading
sys.setrecursionlimit(10 ** 8)
threading.stack_size(10 ** 8)

def main():

	def findMinCost(s, targetChar):
		if len(s) == 1:
			return int(s[0] != targetChar)
		mid = len(s) // 2
		leftCost = sum((c != targetChar for c in s[:mid])) + findMinCost(s[mid:], chr(ord(targetChar) + 1))
		rightCost = sum((c != targetChar for c in s[mid:])) + findMinCost(s[:mid], chr(ord(targetChar) + 1))
		return min(leftCost, rightCost)
	t = int(input())
	for _ in range(t):
		(_, s) = (int(input()), input())
		print(findMinCost(s, 'a'))
threading.Thread(target=main).start()


t = int(input())
al = [chr(ord('a') + i) for i in range(26)]
for _ in range(t):
	n = int(input())
	a = input()
	if n == 1 and a[0] == 'a':
		print(0)
		continue
	elif n == 1:
		print(1)
		continue
	stack = [(a, 0, 0)]
	nim = int(1000000000.0)
	while stack:
		p = stack.pop()
		tar = p[0]
		tar_al = al[p[1]]
		cum = p[2]
		if len(tar) == 1:
			if tar[0] != tar_al:
				cum += 1
			nim = min(nim, cum)
		else:
			cnt = 0
			for i in range(len(tar) // 2):
				if tar[i] != tar_al:
					cnt += 1
			stack.append((tar[len(tar) // 2:], p[1] + 1, p[2] + cnt))
			cnt = 0
			for i in range(len(tar) // 2, len(tar)):
				if tar[i] != tar_al:
					cnt += 1
			stack.append((tar[:len(tar) // 2], p[1] + 1, p[2] + cnt))
	print(nim)


from sys import stdout, stdin, setrecursionlimit
setrecursionlimit(100000)
input = stdin.readline

def minchange(a, d):
	b = c = 0
	if len(a) == 1:
		if a[0] == chr(d):
			return 0
		else:
			return 1
	n = len(a)
	for i in range(0, n // 2):
		if a[i] != chr(d):
			b += 1
	for i in range(n // 2, n):
		if a[i] != chr(d):
			c += 1
	return min(c + minchange(a[:n // 2], d + 1), b + minchange(a[n // 2:n], d + 1))

def solve():
	n = int(input())
	a = input().strip()
	print(minchange(a, 97))

def main():
	for t in range(int(input())):
		solve()
main()


import bisect

def calc(v_i, l, r):
	l_i = bisect.bisect_left(v_i, l)
	r_i = bisect.bisect_left(v_i, r)
	return r_i - l_i

def ans(v, ch, l, r):
	if r - l == 0:
		return 0
	if ch == 25 or r - l == 1:
		return r - l - calc(v[ch], l, r)
	else:
		m = (l + r) // 2
		return min(ans(v, ch + 1, l, m) + r - m - calc(v[ch], m, r), ans(v, ch + 1, m, r) + m - l - calc(v[ch], l, m))
for _ in range(int(input())):
	n = int(input())
	s = input()
	v = []
	for i in range(26):
		v.append([])
	for i in range(n):
		s_i = s[i]
		v[ord(s_i) - ord('a')].append(i)
	print(ans(v, 0, 0, n))


def good_string(word, length, single):
	if length == 1:
		if word != single:
			return 1
		else:
			return 0
	single_2 = chr(1 + ord(single))
	n = int(length / 2)
	first_word = word[:n]
	last_word = word[n:]
	single_2 = chr(1 + ord(single))
	res_1 = len(first_word) - first_word.count(single) + good_string(last_word, len(last_word), single_2)
	res_2 = len(last_word) - last_word.count(single) + good_string(first_word, len(first_word), single_2)
	return min(res_1, res_2)
total = int(input())
for i in range(total):
	length = int(input())
	word = input()
	print(good_string(word, length, 'a'))


def not_equal_count(s, start, end, c):
	count = 0
	for i in range(start, end):
		if s[i] != c:
			count += 1
	return count

def good_string(s, start, end, c):
	count = 0
	if end - start == 1:
		if s[start] == c:
			return 0
		else:
			return 1
	else:
		a = good_string(s, start, start + int((end - start) / 2), chr(ord(c) + 1)) + not_equal_count(s, start + int((end - start) / 2), end, c)
		b = good_string(s, start + int((end - start) / 2), end, chr(ord(c) + 1)) + not_equal_count(s, start, start + int((end - start) / 2), c)
		return min(a, b)
n = int(input())
for i in range(n):
	l = int(input())
	s = input()
	count = good_string(s, 0, l, 'a')
	print(count)


from sys import stdin
input = lambda : stdin.readline().rstrip('\r\n')
from collections import deque as que, defaultdict as vector
from heapq import *
inin = lambda : int(input())
inar = lambda : list(map(int, input().split()))
from math import log2
alp = 'abcdefghijklmnopqrstuvwxyz'

def solve(c, l, r, s):
	mid = (l + r) // 2
	c1 = 0
	c2 = 0
	ch = alp[c]
	if l == r - 1:
		if ch == s[l]:
			return 0
		else:
			return 1
	for i in range(l, mid):
		if s[i] != ch:
			c1 += 1
	for i in range(mid, r):
		if s[i] != ch:
			c2 += 1
	return min(c1 + solve(c + 1, mid, r, s), c2 + solve(c + 1, l, mid, s))
Testcase = inin()
for i in range(Testcase):
	n = inin()
	s = input()
	k = int(log2(n) + 1)
	print(solve(0, 0, n, s))


def cost(a, b):
	return 0 if a == b else 1

def constrit(strg, char='a'):
	l = len(strg)
	if l == 1:
		return 0 if strg == char else 1
	newchar = chr(ord(char) + 1)
	return min(constrit(strg[:l // 2], newchar) + sum(map(lambda x: 0 if x == char else 1, strg[l // 2:])), constrit(strg[l // 2:], newchar) + sum(map(lambda x: 0 if x == char else 1, strg[:l // 2])))

def process():
	n = int(input())
	strg = input()
	print(constrit(strg))
for i in range(int(input())):
	process()


def rec(s, c):
	l = len(s)
	if l == 1 and s == chr(c):
		return 0
	elif l == 1 and s != chr(c):
		return 1
	return min(l // 2 - s[l // 2:l].count(chr(c)) + rec(s[0:l // 2], c + 1), l // 2 - s[0:l // 2].count(chr(c)) + rec(s[l // 2:l], c + 1))
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(rec(s, 97))


from __future__ import division, print_function
import itertools, os, sys, threading
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from math import sqrt, floor, factorial, gcd, log, ceil
from collections import deque, Counter, defaultdict
from itertools import permutations, combinations
from math import gcd
from bisect import bisect
from random import randrange
input = lambda : sys.stdin.readline().rstrip('\r\n')
read = lambda : list(map(int, input().strip().split(' ')))

def main():
	for _ in range(int(input())):
		n = int(input())
		s = input()

		def getCost(l, r, ch):
			return r - l - s[l:r].count(chr(ch))

		def getMin(l, r, ch):
			if l + 1 == r:
				return [0, 1][chr(ch) != s[l]]
			return min(getCost(l, (l + r) // 2, ch) + getMin((l + r) // 2, r, ch + 1), getCost((l + r) // 2, r, ch) + getMin(l, (l + r) // 2, ch + 1))
		print(getMin(0, n, 97))
main()


alphabet = 'abcdefghijklmnopqrstuvwxyz'
t = int(input())
responses = []

def good_string(i, string):
	i = i if i < len(alphabet) else 0
	c = alphabet[i]
	if len(string) == 1:
		return 0 if string == c else 1
	mid = len(string) // 2
	left = string[:mid]
	right = string[mid:]
	left_c_count = right_c_count = 0
	for j in range(mid):
		left_c_count += 1 if c == left[j] else 0
		right_c_count += 1 if c == right[j] else 0
	cost_left = good_string(i + 1, left) + (len(right) - right_c_count)
	cost_right = good_string(i + 1, right) + (len(left) - left_c_count)
	return min(cost_left, cost_right)
for _ in range(t):
	n = int(input())
	s = input()
	responses.append(good_string(0, s))
for response in responses:
	print(response)


from collections import Counter

def rec(s, n, y):
	out = 0
	if n > 1:
		p = Counter(s[:n // 2])[chr(97 + y)]
		q = Counter(s[n // 2:])[chr(97 + y)]
		out += min(rec(s[n // 2:], n // 2, y + 1) + n // 2 - p, rec(s[:n // 2], n // 2, y + 1) + n // 2 - q)
	elif s != chr(y + 97):
		out += 1
	return out
for _ in range(int(input())):
	n = int(input())
	s = input()
	if n > 1:
		ans = 0
		p = Counter(s[:n // 2])['a']
		q = Counter(s[n // 2:])['a']
		ans += min(n // 2 - p + rec(s[n // 2:], n // 2, 1), n // 2 - q + rec(s[:n // 2], n // 2, 1))
		print(ans)
	elif s != 'a':
		print(1)
	else:
		print(0)


def calc(l, r, s, letter):
	if r - l == 1:
		return 0 if s[l] == letter else 1
	mid = (l + r) // 2
	cnt_l = (r - l) // 2 - s[l:mid].count(letter)
	cnt_r = (r - l) // 2 - s[mid:r].count(letter)
	return min(cnt_l + calc(mid, r, s, chr(ord(letter) + 1)), cnt_r + calc(l, mid, s, chr(ord(letter) + 1)))
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	print(calc(0, n, s, 'a'))


a = None

def subsolve(l, r, c):
	nonlocal a
	if r - l <= 1:
		return 0 if a[l] == c else 1
	mid = (l + r) // 2
	u = subsolve(mid, r, c + 1)
	for i in range(l, mid):
		if a[i] != c:
			u += 1
	v = subsolve(l, mid, c + 1)
	for i in range(mid, r):
		if a[i] != c:
			v += 1
	return min(u, v)

def solve():
	nonlocal a
	n = int(input())
	a = list(map(ord, input()))
	print(subsolve(0, n, ord('a')))
t = int(input())
for _ in range(t):
	solve()


def rec(s, cc, oc):
	global ans
	ls = len(s)
	if ls == 1:
		if s[0] != cc:
			ans += 1
		return
	ss = s[ls // 2:]
	fs = s[:ls // 2]
	sans = ans
	for i in range(ls // 2):
		if s[i] != cc:
			s[i] = cc
			ans += 1
	rec(ss, chr(oc + 1), oc + 1)
	ans1 = ans
	ans = sans
	for i in range(ls // 2, ls):
		if s[i] != cc:
			s[i] = cc
			ans += 1
	rec(fs, chr(oc + 1), oc + 1)
	ans = min(ans1, ans)
for _ in range(int(input())):
	input()
	ss = list(input())
	ans = 0
	rec(ss, 'a', 97)
	print(ans)


from sys import stdin, stdout
stdin.readline

def mp():
	return list(map(int, stdin.readline().strip().split()))

def it():
	return int(stdin.readline().strip())
from collections import defaultdict as dd, Counter as C, deque
from math import ceil

def getcost(s, l, r, c):
	count = 0
	for i in range(l, r + 1):
		if s[i] != c:
			count += 1
	return count

def fun(s, l, r, c):
	if l == r:
		if s[l] == c:
			return 0
		return 1
	mid = (l + r) // 2
	return min(getcost(s, l, mid, chr(ord(c))) + fun(s, mid + 1, r, chr(ord(c) + 1)), getcost(s, mid + 1, r, chr(ord(c))) + fun(s, l, mid, chr(ord(c) + 1)))
for _ in range(it()):
	n = it()
	s = list(input())
	x = 'a'
	ans = fun(s, 0, n - 1, x)
	print(ans)


def findans(s, beg, end, ans, i, cv, fa):
	if beg == end:
		if ord(s[beg - 1]) - 97 != i:
			ans = ans + 1
		fa[0] = min(fa[0], ans)
		return
	mid = (beg + end) // 2
	findans(s, mid + 1, end, ans + cv[i][mid] - cv[i][beg - 1], i + 1, cv, fa)
	findans(s, beg, mid, ans + cv[i][end] - cv[i][mid], i + 1, cv, fa)
	return
T = int(input())
for _ in range(T):
	n = int(input())
	s = input()
	ans = 0
	cv = [[0 for j in range(n + 1)] for i in range(26)]
	A = [n // 2 ** (i + 1) for i in range(26)]
	for i in range(n):
		for j in range(26):
			if A[j] == 0:
				break
			elif ord(s[i]) - 97 != j:
				cv[j][i + 1] = 1
	for i in range(26):
		for j in range(1, n + 1):
			cv[i][j] = cv[i][j - 1] + cv[i][j]
	fa = [n]
	findans(s, 1, n, ans, 0, cv, fa)
	print(fa[0])


def want(l, r, depth):
	ret = [0] * 26
	if l + 1 == r:
		ret[ad[s[l]]] += 1
		if alp[depth] == s[l]:
			return (0, ret)
		else:
			return (1, ret)
	else:
		(Lmini, Llis) = want(l, (l + r) // 2, depth + 1)
		(Rmini, Rlis) = want((l + r) // 2, r, depth + 1)
		LL = (l + r) // 2 - l - Llis[depth]
		RR = (l + r) // 2 - l - Rlis[depth]
		for i in range(26):
			Llis[i] += Rlis[i]
		return (min(Lmini + RR, Rmini + LL), Llis)
from sys import stdin
alp = 'abcdefghijklmnopqrstuvwxyz'
ad = {}
for i in range(26):
	ad[alp[i]] = i
tt = int(stdin.readline())
for loop in range(tt):
	n = int(stdin.readline())
	s = stdin.readline()
	(ans, tmp) = want(0, n, 0)
	print(ans)


def count(l, x):
	c = 0
	for k in l:
		if k == x:
			c += 1
	return c

def calc(s, c):
	if len(s) == 1:
		return int(s != c)
	mid = len(s) // 2
	fps = s[:mid]
	sps = s[mid:]
	c1 = chr(ord(c) + 1)
	first_part = calc(fps, c1)
	first_part += len(s) // 2 - count(sps, c)
	second_part = calc(sps, c1)
	second_part += len(s) // 2 - count(fps, c)
	return min(first_part, second_part)
for i in range(int(input())):
	n = int(input())
	ipt = input()
	print(calc(ipt, 'a'))


import sys
from math import ceil, log2
base = ord('a')

def rec(lis, m, count):
	if len(lis) == 1:
		if lis[0] == chr(count + base):
			return 0
		else:
			return 1
	l = len(lis) // 2
	ans = 0
	c = 0
	total = 0
	for i in range(0, 2 * l):
		if lis[i] == chr(base + count):
			total += 1
	for i in range(0, l):
		if lis[i] == chr(base + count):
			c += 1
	return min(rec(lis[:l], m, count + 1) + l - (total - c), rec(lis[l:], m, count + 1) + l - c)
t = int(input())
while t:
	t -= 1
	n = int(input())
	lis = input().strip()
	a = int(log2(n)) + 1
	m = [0] * 26
	for i in range(n):
		m[ord(lis[i]) - base] += 1
	count = 0
	print(rec(lis, m, count))


inputs_cnt = int(input())

def get_replacements_for_good_string(string, letter):
	(left_side_cnt, right_side_cnt) = (0, 0)
	str_len = len(string)
	if str_len == 1:
		if letter == string:
			return 0
		else:
			return 1
	str_half_len = int(str_len / 2)
	for (index, str_letter) in enumerate(string):
		if str_letter == letter:
			if str_half_len > index:
				left_side_cnt += 1
			else:
				right_side_cnt += 1
	return min(str_half_len - left_side_cnt + get_replacements_for_good_string(string[str_half_len:], chr(ord(letter) + 1)), str_half_len - right_side_cnt + get_replacements_for_good_string(string[:str_half_len], chr(ord(letter) + 1)))
for input_index in range(inputs_cnt):
	str_length = int(input())
	print(get_replacements_for_good_string(input(), 'a'))


t = int(input())

def recurse(st, c, moves):
	if len(st) == 1:
		if st == c:
			return moves
		else:
			return moves + 1
	m = moves
	n = 0
	n1 = 0
	for a in range(len(st)):
		if st[a] == c and a < len(st) // 2:
			n += 1
		elif st[a] == c:
			n1 += 1
	return min(recurse(st[len(st) // 2:], chr(ord(c) + 1), m + len(st) // 2 - n), recurse(st[:len(st) // 2], chr(ord(c) + 1), m + len(st) // 2 - n1))
for t1 in range(t):
	x = int(input())
	st = input()
	m = recurse(st, 'a', 0)
	print(m)


def checkGoodString(string, char):
	if len(string) == 1:
		return string[0] == char
	h = len(string) // 2
	s1 = string[:h]
	s2 = string[h:]
	good = False
	for c in s1:
		if c != char:
			break
	else:
		good = checkGoodString(s2, chr(ord(char) + 1))
	if good:
		return True
	for c in s2:
		if c != char:
			return False
	return checkGoodString(s1, chr(ord(char) + 1))

def solve(string, char):
	if len(string) == 1:
		return 0 if string[0] == char else 1
	if checkGoodString(string, char):
		return 0
	h = len(string) // 2
	s1 = string[:h]
	s2 = string[h:]
	dif = 0
	for c in s1:
		if c != char:
			dif += 1
	dif += solve(s2, chr(ord(char) + 1))
	dif2 = 0
	for c in s2:
		if c != char:
			dif2 += 1
	dif2 += solve(s1, chr(ord(char) + 1))
	return min(dif, dif2)
n = int(input())
for i in range(n):
	input()
	print(solve(input(), 'a'))


def func(i, j, t):
	global l
	if i == j:
		if s[i] == l[t]:
			return 0
		else:
			return 1
	elif j == i + 1:
		x = 0
		y = 0
		if s[i] == l[t]:
			x += 1
		if s[j] == l[t]:
			y += 1
		return min(1 - x + func(j, j, t + 1), 1 - y + func(i, i, t + 1))
	else:
		mid = (i + j) // 2
		if i == 0:
			return min((j - i + 1) // 2 - d[l[t]][mid] + func(mid + 1, j, t + 1), (j - i + 1) // 2 - (d[l[t]][j] - d[l[t]][mid]) + func(i, mid, t + 1))
		return min((j - i + 1) // 2 - (d[l[t]][mid] - d[l[t]][i - 1]) + func(mid + 1, j, t + 1), (j - i + 1) // 2 - (d[l[t]][j] - d[l[t]][mid]) + func(i, mid, t + 1))
for w in range(int(input())):
	n = int(input())
	s = str(input())
	l = ['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']
	d = {}
	for i in l:
		d[i] = [0] * n
	for i in l:
		for j in range(n):
			if j == 0:
				if s[j] == i:
					d[i][j] = 1
			elif s[j] == i:
				d[i][j] = d[i][j - 1] + 1
			else:
				d[i][j] = d[i][j - 1]
	print(func(0, n - 1, 0))


def agood(string, length, entry):
	if length == 1:
		if string == entry:
			return 0
		else:
			return 1
	center = length // 2
	left = string[:center]
	right = string[center:]
	next_chr = chr(ord(entry) + 1)
	count_left = len(left) - left.count(entry) + agood(right, center, next_chr)
	count_right = len(right) - right.count(entry) + agood(left, center, next_chr)
	return count_left if count_left < count_right else count_right
entry = int(input())
initial_entry = 'a'
for _ in range(entry):
	length = int(input())
	string = input()
	print(agood(string.strip(), length, initial_entry))


def ss(s, c):
	n = len(s)
	if n == 1:
		if s[0] == c:
			return 0
		return 1
	c1 = n // 2 - s[0:n // 2].count(c)
	c2 = n // 2 - s[n // 2:n].count(c)
	x = c1 + ss(s[n // 2:], chr(ord(c) + 1))
	y = c2 + ss(s[0:n // 2], chr(ord(c) + 1))
	return min(x, y)
for _ in range(int(input())):
	n = int(input())
	s = input()
	m = ss(s, 'a')
	print(m)


import math
from collections import deque
from sys import stdin, stdout
from string import ascii_letters
input = stdin.readline
letters = ascii_letters[:26]
for _ in range(int(input())):
	n = int(input())
	nx = input().strip()
	buff = deque()
	res = 9999999
	buff.append((nx, 0, 0))
	while len(buff):
		x = buff.popleft()
		if len(x[0]) == 1:
			if x[0][0] != letters[x[1]]:
				res = min(res, x[2] + 1)
			else:
				res = min(res, x[2])
			continue
		left = x[0][:len(x[0]) // 2]
		right = x[0][len(x[0]) // 2:]
		buff.append((left, x[1] + 1, x[2] + (len(right) - right.count(letters[x[1]]))))
		buff.append((right, x[1] + 1, x[2] + (len(left) - left.count(letters[x[1]]))))
	print(res)


def search(arr, total, val):
	if len(arr) > 1:
		count = 0
		for i in range(len(arr) // 2, len(arr)):
			if arr[i] == chr(val):
				count += 1
		count1 = 0
		for i in range(0, len(arr) // 2):
			if arr[i] == chr(val):
				count1 += 1
		total1 = total
		total1 += len(arr) // 2 - count1
		p = search(arr[len(arr) // 2:len(arr)], total1, val + 1)
		total2 = total
		total2 += len(arr) // 2 - count
		k = search(arr[0:len(arr) // 2], total2, val + 1)
		return min(p, k)
	elif arr[0] == chr(val):
		return total
	else:
		return total + 1
t = int(input())
for _ in range(t):
	n = int(input())
	s = input().rstrip('\n')
	s = list(s)
	if len(s) == 1:
		if s[0] == 'a':
			print('0')
		else:
			print('1')
	else:
		print(search(s, 0, ord('a')))


def find(s, c):
	char = chr(97 + c)
	if len(s) == 1:
		if s[0] == char:
			return 0
		else:
			return 1
	else:
		mid = len(s) // 2
		cntL = len(s) // 2 - s[:mid].count(char) + find(s[mid:], c + 1)
		cntR = len(s) // 2 - s[mid:].count(char) + find(s[:mid], c + 1)
		return min(cntL, cntR)
for _ in range(int(input())):
	n = int(input())
	s = input().strip()
	print(find(s, 0))


def g(string, s):
	ans = 0
	for i in range(2):
		if string[i] != s[i]:
			ans = ans + 1
	return ans

def h(string, s, l):
	c = 0
	for i in range(l):
		if string[i] != s:
			c = c + 1
	return c

def f(string, s, n):
	if n == 1:
		if string == s:
			return 0
		else:
			return 1
	else:
		string1 = string[0:n // 2]
		string2 = string[n // 2:]
		y = h(string1, s, n // 2)
		z = h(string2, s, n // 2)
		c = chr(ord(s) + 1)
		return min(y + f(string2, c, n // 2), z + f(string1, c, n // 2))
t = int(input())
while t > 0:
	t = t - 1
	n = int(input())
	s = input()
	print(f(s, 'a', n))


def recur(s, ch, n):
	if not n:
		return 0
	elif n == 1:
		return 0 if chr(ch) == s else 1
	else:
		f = s[:n // 2]
		l = s[n // 2:]
		return min(n // 2 - f.count(chr(ch)) + recur(l, ch + 1, n // 2), n // 2 - l.count(chr(ch)) + recur(f, ch + 1, n // 2))
for _ in range(int(input())):
	n = int(input())
	st = input()
	print(recur(st, ord('a'), n))


import collections

def minMoves(c, startIndex, endIndex):
	if startIndex == endIndex:
		if s[startIndex] == c:
			return 0
		else:
			return 1
	else:
		firstHalfCnts = collections.Counter(s[startIndex:(startIndex + endIndex + 1) // 2])
		firstHalfChange = (endIndex + 1 - startIndex) // 2 - firstHalfCnts[c]
		firstHalfMin = firstHalfChange + minMoves(chr(ord(c) + 1), (startIndex + endIndex + 1) // 2, endIndex)
		secondHalfCnts = collections.Counter(s[(startIndex + endIndex + 1) // 2:endIndex + 1])
		secondHalfChange = (endIndex + 1 - startIndex) // 2 - secondHalfCnts[c]
		secondHalfMin = secondHalfChange + minMoves(chr(ord(c) + 1), startIndex, (startIndex + endIndex) // 2)
	return min(firstHalfMin, secondHalfMin)
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	ans = minMoves('a', 0, n - 1)
	print(ans)


def next(c):
	return chr(97 + (ord(c) - 96) % 26)

def find(s, c):
	if len(s) == 1:
		return 1 - (s[0] == c)
	(cl, cr) = (0, 0)
	for i in range(len(s) // 2):
		cl += 1 - (s[i] == c)
	for i in range(len(s) // 2, len(s)):
		cr += 1 - (s[i] == c)
	return min(cl + find(s[len(s) // 2:len(s)], next(c)), cr + find(s[0:len(s) // 2], next(c)))
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(find(s, 'a'))


def gs(s, l):
	a = chr(l)
	if len(s) == 1:
		if s == a:
			return 0
		else:
			return 1
	k = len(s) // 2
	s1 = k - s[:k].count(a) + gs(s[k:], l + 1)
	s2 = k - s[k:].count(a) + gs(s[:k], l + 1)
	return min(s1, s2)
for i in range(int(input())):
	n = int(input())
	s = input()
	print(gs(s, ord('a')))


def make_good(s, l):
	if len(s) == 1:
		if s[0] == l:
			return 0
		else:
			return 1
	else:
		left = s[0:int(len(s) / 2)]
		right = s[int(len(s) / 2):len(s)]
		ll = chr(ord(l) + 1)
		cl = count_l(left, l)
		ml = make_good(left, ll)
		cr = count_l(right, l)
		mr = make_good(right, ll)
		return min(cl + mr, cr + ml)

def count_l(s, l):
	return len(s) - s.count(l)
t = int(input())
for k in range(t):
	l = int(input())
	s = input()
	print(make_good(s, 'a'))


from math import log
from sys import stdin, stdout
for query in range(int(stdin.readline())):
	n = int(stdin.readline())
	s = [ord(x) - 96 for x in list(stdin.readline().strip())]
	mylist = [[] for x in range(int(log(n, 2)) + 1)]
	for x in range(1, int(log(n, 2) + 1)):
		for y in range(2 ** x):
			count = 0
			for z in range(n // 2 ** x * y, n // 2 ** x * (y + 1)):
				if s[z] == x:
					count += 1
			mylist[x - 1].append(n // 2 ** x - count)
	b = int(log(n, 2) + 1)
	for x in range(n):
		if s[x] == b:
			mylist[b - 1].append(0)
		else:
			mylist[b - 1].append(1)
	current = mylist[b - 1][:]
	for x in range(b - 2, -1, -1):
		newcurrent = []
		for y in range(0, 2 ** (x + 1), 2):
			p = current[y] + mylist[x][y + 1]
			q = current[y + 1] + mylist[x][y]
			newcurrent.append(min(p, q))
		current = newcurrent[:]
	stdout.write(str(current[0]) + '\n')


def find(st, count, ans, low, high, char):
	if high - low == 0:
		if chr(char) == st[high]:
			ans.append(count)
		else:
			ans.append(count + 1)
	else:
		check = 0
		mid = (high + low) // 2
		for k in st[low:mid + 1]:
			if k != chr(char):
				check += 1
		find(st, count + check, ans, mid + 1, high, char + 1)
		check = 0
		for k in st[mid + 1:high + 1]:
			if k != chr(char):
				check += 1
		find(st, count + check, ans, low, mid, char + 1)
for i in range(int(input())):
	n = int(input())
	st = input()
	ans = []
	find(st, 0, ans, 0, n - 1, 97)
	print(min(ans))


def Count1(string, char, n):
	return n - string.count(char)

def Count(string, char, n):
	if n == 1:
		if string == char:
			return 0
		else:
			return 1
	else:
		a = Count1(string[:n // 2], char, n // 2) + Count(string[n // 2:], chr(ord(char) + 1), n // 2)
		b = Count(string[:n // 2], chr(ord(char) + 1), n // 2) + Count1(string[n // 2:], char, n // 2)
		return min(a, b)
for _ in range(int(input())):
	n = int(input())
	string = input()
	print(Count(string, 'a', n))


import sys
input = sys.stdin.readline
t = int(input())

def func(arr, ind, start, end, ans, l):
	check = ind + 97
	n = end - start + 1
	if n == 1:
		if ord(arr[start]) != check:
			return ans + 1
		return ans
	else:
		n = n // 2
		if start == 0:
			extra1 = n - l[start + n - 1].get(chr(check), 0)
		else:
			extra1 = n + l[start - 1].get(chr(check), 0) - l[start + n - 1].get(chr(check), 0)
		extra2 = n + l[end - n].get(chr(check), 0) - l[end].get(chr(check), 0)
		return min(func(arr, ind + 1, start + n, end, ans + extra1, l), func(arr, ind + 1, start, end - n, ans + extra2, l))
for t1 in range(t):
	n = int(input())
	s = input()
	s = s.strip()
	l = []
	d = {}
	d[s[0]] = 1
	l.append(d)
	for i in range(1, n):
		t = {}
		d = l[-1]
		for j in d:
			t[j] = d[j]
		t[s[i]] = t.get(s[i], 0) + 1
		l.append(t)
	print(func(s, 0, 0, n - 1, 0, l))


def main():
	c = 'a'
	number = int(input())
	for i in range(number):
		length = int(input())
		string = input()
		print(minimum_number(string, length, c))

def minimum_number(string, length, c):
	if length == 1:
		if string == c:
			return 0
		else:
			return 1
	else:
		half = length // 2
		first = string[:half]
		last = string[half:]
		aux = chr(1 + ord(c))
		valueA = len(first) - first.count(c) + minimum_number(last, len(last), aux)
		valueB = len(last) - last.count(c) + minimum_number(first, len(first), aux)
		if valueA < valueB:
			return valueA
		else:
			return valueB
main()


def func(s, st, n, c):
	if n == 0:
		if s[st] == c:
			return 0
		else:
			return 1
	a = 0
	b = 0
	for i in range(n):
		if s[i + st] == c:
			a += 1
		if s[i + st + n] == c:
			b += 1
	c2 = chr(ord(c) + 1)
	a = n - a + func(s, st + n, n // 2, c2)
	b = n - b + func(s, st, n // 2, c2)
	return min(a, b)
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(func(s, 0, n // 2, 'a'))


def good_str(s, c, n):
	if n == 1:
		if s[0] == chr(c):
			return 0
		else:
			return 1
	else:
		s1 = s[:n // 2]
		s2 = s[n // 2:]
		p = s1.count(chr(c))
		q = s2.count(chr(c))
		right = n // 2 - p + good_str(s2, c + 1, n // 2)
		left = n // 2 - q + good_str(s1, c + 1, n // 2)
		return min(right, left)
for _ in range(int(input())):
	n = int(input())
	s = input()
	c = ord('a')
	result = good_str(s, c, n)
	print(result)


from sys import stdin

def inp():
	return stdin.buffer.readline().rstrip().decode('utf8')

def itg():
	return int(stdin.buffer.readline())

def mpint():
	return map(int, stdin.buffer.readline().split())

def dfs(s, c):
	if len(s) == 1:
		return 0 if s == c else 1
	size = len(s) >> 1
	s1 = s[:size]
	s2 = s[size:]
	left_c = s1.count(c)
	right_c = s2.count(c)
	cost1 = size - left_c
	cost2 = size - right_c
	next_char = chr(ord(c) + 1)
	return min(cost1 + dfs(s2, next_char), cost2 + dfs(s1, next_char))
for case in range(itg()):
	n = itg()
	S = inp()
	print(dfs(S, 'a'))


def dp(first, last, c, x):
	if first + 1 == last and x[first] != c:
		return 1
	if first + 1 == last and x[first] == c:
		return 0
	mid = (first + last) // 2
	out1 = count(first, mid, c, x) + dp(mid, last, chr(ord(c) + 1), x)
	out2 = count(mid, last, c, x) + dp(first, mid, chr(ord(c) + 1), x)
	return min(out1, out2)

def count(first, last, c, x):
	count = 0
	for i in range(first, last):
		if x[i] != c:
			count += 1
	return count
for _ in range(int(input())):
	y = int(input())
	x = input()
	print(dp(0, y, 'a', x))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)
mod = 998244353
INF = float('inf')
from math import factorial, log2
from collections import Counter, defaultdict, deque
from heapq import heapify, heappop, heappush

def main():
	for _ in range(N()):
		n = N()
		s = input()
		up = log2(n)
		res = INF

		def dfs(ind=0, ss=s, cost=0):
			nonlocal res
			now = chr(ord('a') + ind)
			if ind == up:
				if now != ss:
					cost += 1
				res = min(res, cost)
				return
			lc = rc = 0
			le = len(ss)
			for i in range(le):
				if i < le // 2:
					if ss[i] != now:
						lc += 1
				elif ss[i] != now:
					rc += 1
			dfs(ind + 1, ss[le // 2:], cost + lc)
			dfs(ind + 1, ss[:le // 2], cost + rc)
		dfs()
		print(res)
main()


def count(s, c):
	n = 0
	for i in s:
		if i != c:
			n += 1
	return n

def get_count(s, c):
	if len(s) == 1:
		return count(s, c)
	m = len(s) // 2
	c1 = count(s[:m], c) + get_count(s[m:], chr(ord(c) + 1))
	c2 = count(s[m:], c) + get_count(s[:m], chr(ord(c) + 1))
	return min(c1, c2)
inp = lambda : input()
t = int(inp())
for _ in range(t):
	n = int(inp())
	s = inp()
	print(get_count(s, 'a'))


import sys
from math import sqrt, gcd, ceil, log
from bisect import bisect
from collections import defaultdict, Counter, deque
from heapq import heapify, heappush, heappop
inp = sys.stdin.readline
read = lambda : list(map(int, inp().strip().split()))

def solve():
	ans = ''
	for _ in range(int(input())):
		n = int(input())
		s = input()
		arr = []
		min_cost = float('inf')

		def getCost(l, r, ch):
			return r - l - s[l:r].count(chr(ch))
		arr = [(0, n, 97, 0)]
		while arr:
			tem = []
			for i in arr:
				(l, r, ch, cost) = i
				if l + 1 == r:
					min_cost = min(min_cost, cost + (s[l] != chr(ch)))
				else:
					tem.extend([(l, (l + r) // 2, ch + 1, cost + getCost((l + r) // 2, r, ch)), ((l + r) // 2, r, ch + 1, cost + getCost(l, (l + r) // 2, ch))])
			arr = tem
		ans += str(min_cost) + '\n'
	print(ans)
solve()


def ans(string, c, n):
	if n == 1:
		if string[0] == c:
			return 0
		else:
			return 1
	count1 = int(n / 2) - string.count(c, 0, int(n / 2))
	count2 = int(n / 2) - string.count(c, int(n / 2), n)
	sub1 = ans(string[int(n / 2):n], chr(ord(c) + 1), int(n / 2))
	sub2 = ans(string[0:int(n / 2)], chr(ord(c) + 1), int(n / 2))
	return min(count1 + sub1, count2 + sub2)
T = int(input())
for t in range(0, T):
	n = int(input())
	string = input()
	print(ans(string, 'a', n))


def solve(s, c):
	if len(s) == 1:
		return 1 - s.count(c)
	return min(len(s) // 2 - s[len(s) // 2:len(s)].count(c) + solve(s[0:len(s) // 2], chr(ord(c) + 1)), len(s) // 2 - s[0:len(s) // 2].count(c) + solve(s[len(s) // 2:len(s)], chr(ord(c) + 1)))
t = int(input().split()[0])
for case in range(t):
	n = int(input().split()[0])
	s = input().split()[0]
	print(solve(s, 'a'))


def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())

def solve(begin, end, char, count, s):
	mid_point = (begin + end) // 2
	if end == begin + 1:
		if s[begin] == char:
			return count
		else:
			return count + 1
	counter1 = 0
	counter2 = 0
	for j in range(begin, mid_point):
		if s[j] != char:
			counter1 += 1
	for j in range(mid_point, end):
		if s[j] != char:
			counter2 += 1
	return min(solve(begin, mid_point, chr(ord(char) + 1), count + counter2, s), solve(mid_point, end, chr(ord(char) + 1), count + counter1, s))
n = inp()
for i in range(n):
	m = inp()
	s = insr()
	ans = solve(0, m, 'a', 0, s)
	print(ans)


from math import inf, log2

class SegmentTree:

	def __init__(self, array, func=max):
		self.n = len(array)
		self.size = 2 ** (int(log2(self.n - 1)) + 1) if self.n != 1 else 1
		self.func = func
		self.default = 0 if self.func != min else inf
		self.data = [self.default] * (2 * self.size)
		self.process(array)

	def process(self, array):
		self.data[self.size:self.size + self.n] = array
		for i in range(self.size - 1, -1, -1):
			self.data[i] = self.func(self.data[2 * i], self.data[2 * i + 1])

	def query(self, alpha, omega):
		if alpha == omega:
			return self.data[alpha + self.size]
		res = self.default
		alpha += self.size
		omega += self.size + 1
		while alpha < omega:
			if alpha & 1:
				res = self.func(res, self.data[alpha])
				alpha += 1
			if omega & 1:
				omega -= 1
				res = self.func(res, self.data[omega])
			alpha >>= 1
			omega >>= 1
		return res

	def update(self, index, value):
		index += self.size
		self.data[index] = value
		index >>= 1
		while index:
			self.data[index] = self.func(self.data[2 * index], self.data[2 * index + 1])
			index >>= 1
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import gcd, ceil

def pre(s):
	n = len(s)
	pi = [0] * n
	for i in range(1, n):
		j = pi[i - 1]
		while j and s[i] != s[j]:
			j = pi[j - 1]
		if s[i] == s[j]:
			j += 1
		pi[i] = j
	return pi

def prod(a):
	ans = 1
	for each in a:
		ans = ans * each
	return ans

def lcm(a, b):
	return a * b // gcd(a, b)

def binary(x, length=16):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y
for _ in range(int(input()) if True else 1):
	n = int(input())
	k = int(log2(n))
	s = input()
	if n == 1:
		print(0 if s == 'a' else 1)
		continue
	fu = [[0] * n for ___ in range(26)]
	for i in range(n):
		fu[ord(s[i]) - 97][i] += 1
	for i in range(26):
		fu[i] = SegmentTree(fu[i], func=lambda a, b: a + b)
	ans = n
	for i in range(n):
		b = binary(i, k)
		p = 0
		q = n - 1
		ind = -1
		anss = 0
		for c in b:
			ind += 1
			mid = (p + q) // 2
			if c == '0':
				anss += q - (mid + 1) + 1 - fu[ind].query(mid + 1, q)
				q = mid
			else:
				anss += mid - p + 1 - fu[ind].query(p, mid)
				p = mid + 1
		if fu[ind + 1].query(q, q) != 1:
			anss += 1
		ans = min(ans, anss)
	print(ans)


def isGood(s, c):
	if len(s) == 1:
		if s == c:
			return 0
		else:
			return 1
	n = len(s)
	left = s[:n // 2]
	right = s[n // 2:]
	c1 = 0
	c2 = 0
	for i in left:
		if i != c:
			c1 += 1
	for i in right:
		if i != c:
			c2 += 1
	return min(c1 + isGood(right, chr(ord(c) + 1)), c2 + isGood(left, chr(ord(c) + 1)))
for tc in range(int(input())):
	n = int(input())
	s = input()
	print(isGood(s, 'a'))


from collections import defaultdict as dd
import sys
input = sys.stdin.readline
t = int(input())

def solve(curr, i, j, s):
	if i == j:
		return int(s[i] != curr)
	if i > j:
		return 0
	mid = (i + j) // 2
	x = 0
	for ii in range(i, mid + 1):
		if s[ii] != curr:
			x += 1
	y = 0
	for ii in range(mid + 1, j + 1):
		if s[ii] != curr:
			y += 1
	x = x + solve(chr(ord(curr) + 1), mid + 1, j, s)
	y = y + solve(chr(ord(curr) + 1), i, mid, s)
	return min(x, y)
while t:
	n = int(input())
	s = input().split()[0]
	print(solve('a', 0, n - 1, s))
	t -= 1


from sys import stdin, stdout

def getcost(st, e, c):
	cnt = 0
	for i in range(st, e):
		if s[i] != c:
			cnt += 1
	return cnt

def getmin(start, end, ch):
	if start == end - 1:
		return 0 if s[start] == ch else 1
	mid = (start + end) // 2
	return min(getcost(start, mid, ch) + getmin(mid, end, chr(ord(ch) + 1)), getcost(mid, end, ch) + getmin(start, mid, chr(ord(ch) + 1)))
t = int(input())
for _ in range(t):
	n = int(stdin.readline())
	s = stdin.readline()
	stdout.write(str(getmin(0, n, 'a')) + '\n')


from collections import Counter
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def dice():
	dp = [0] * (n + 1)
	dp[1] = 1
	dp[0] = 1
	for i in range(2, n + 1):
		for j in range(1, 7):
			if i - j >= 0:
				dp[i] = (dp[i] + dp[i - j]) % (10 ** 9 + 7)
			else:
				break
import sys

def cost(l, r, ch, s):
	var = 0
	for i in range(l, r):
		if s[i] != ch:
			var += 1
	return var

def fxn(l, r, ch, s):
	if l + 1 == r:
		if s[l] == ch:
			return 0
		return 1
	mid = (l + r) // 2
	return min(cost(l, mid, ch, s) + fxn(mid, r, chr(ord(ch) + 1), s), fxn(l, mid, chr(ord(ch) + 1), s) + cost(mid, r, ch, s))
for _ in range(int(input())):
	n = int(input())
	s = input()
	s = s
	ans = fxn(0, n, 'a', s)
	print(ans)


def getCost(s, ch):
	return len(s) - s.count(chr(ch))

def getMin(s, h, l=0, alpha=97):
	if h - l == 1:
		if s[l:h] == chr(alpha):
			return 0
		return 1
	mid = (l + h) // 2
	return min(getCost(s[l:mid], alpha) + getMin(s, h, mid, alpha + 1), getCost(s[mid:h], alpha) + getMin(s, mid, l, alpha + 1))
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(getMin(s, n))


def goodString(s, len, charCode):
	(h, char) = (len // 2, chr(charCode))
	if len == 1:
		if s == char:
			return 0
		else:
			return 1
	(s1, s2) = (s[:h], s[h:])
	a1 = h - s1.count(char) + goodString(s2, h, charCode + 1)
	a2 = h - s2.count(char) + goodString(s1, h, charCode + 1)
	return min(a1, a2)
T = int(input())
for _ in range(T):
	N = int(input())
	A = input()
	ans = goodString(A, N, 97)
	print(ans)


def f(s, l, string, c):
	ch = chr(c + 97)
	if l == 1:
		if string[s] == ch:
			return 0
		return 1
	first = 0
	second = 0
	i = 0
	count = l
	while count > 0:
		if string[s + i] != ch:
			if i < l // 2:
				first += 1
			else:
				second += 1
		i += 1
		count -= 1
	return min(first + f(s + l // 2, l // 2, string, c + 1), second + f(s, l // 2, string, c + 1))
t = int(input())
while t > 0:
	n = int(input())
	string = input()
	print(f(0, n, string, 0))
	t -= 1


def fn(s, a):
	if len(s) == 1:
		if s == a:
			return 0
		else:
			return 1
	a1 = 0
	a2 = 0
	l = len(s) // 2
	t = s[:l]
	p = s[l:]
	for i in t:
		if i != a:
			a1 += 1
	for i in p:
		if i != a:
			a2 += 1
	return min(a1 + fn(p, chr(ord(a) + 1)), a2 + fn(t, chr(ord(a) + 1)))
for _ in range(int(input())):
	n = int(input())
	a = input()
	print(fn(a, 'a'))


from math import log2
for i in range(int(input())):
	n = int(input())
	s = input()

	def fun(s, dept, cr):
		if dept == 0:
			if s == chr(cr):
				return 0
			else:
				return 1
		n = 2 ** (dept - 1)
		ch = chr(cr)
		c1 = n - s[:n].count(ch)
		c2 = n - s[n:].count(ch)
		a = fun(s[:n], dept - 1, cr + 1)
		b = fun(s[n:], dept - 1, cr + 1)
		m1 = c1 + b
		m2 = c2 + a
		if m1 <= m2:
			return m1
		else:
			return m2
	dept = int(log2(n))
	print(fun(s, dept, 97))


def calc(s, letter):
	return len(s) - s.count(letter)

def find(s, letter):
	if len(s) == 1:
		return 1 - int(s == letter)
	return min([calc(s[:len(s) // 2], letter) + find(s[len(s) // 2:], chr(ord(letter) + 1)), calc(s[len(s) // 2:], letter) + find(s[:len(s) // 2], chr(ord(letter) + 1))])
t = int(input())
while t:
	t -= 1
	n = int(input())
	s = input()
	print(find(s, 'a'))


import itertools

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		k = n.bit_length()
		s = str(input())
		cum = [[-1] * 26 for _ in range(n + 1)]
		for i in range(26):
			cum[0][i] = 0
		for i in range(n):
			idx = ord(s[i]) - 97
			for j in range(26):
				if j == idx:
					cum[i + 1][j] = cum[i][j] + 1
				else:
					cum[i + 1][j] = cum[i][j]
		zeroone = [0, 1]
		LIST = itertools.product(zeroone, repeat=k)
		ans = pow(10, 18)
		for L in LIST:
			start = 0
			end = n
			cnt = 0
			tans = 0
			for sgn in L:
				center = (start + end) // 2
				section_num = (end - start) // 2
				if section_num == 0:
					section_num += 1
				if sgn == 0:
					no_change = cum[center][cnt] - cum[start][cnt]
					start = center
				else:
					no_change = cum[end][cnt] - cum[center][cnt]
					end = center
				tans += section_num - no_change
				cnt += 1
			ans = min(ans, tans)
		print(ans)
main()


from sys import stdin, stdout, setrecursionlimit
from collections import deque, defaultdict, Counter
from functools import lru_cache
from heapq import heappush, heappop
import math
setrecursionlimit(100000)
rl = lambda : stdin.readline()
rll = lambda : stdin.readline().split()
rli = lambda : map(int, stdin.readline().split())
rlf = lambda : map(float, stdin.readline().split())
(INF, NINF) = (float('inf'), float('-inf'))
tonum = lambda c: ord(c) - 97
tochr = lambda num: chr(num + 97)

def solv(s, i, j, need):
	if i > j:
		return 0
	if i == j:
		return 0 if s[i] == tochr(need) else 1
	mid = (i + j) // 2
	(cnt1, cnt2) = (0, 0)
	cneed = tochr(need)
	for x in range(i, mid + 1):
		if s[x] != cneed:
			cnt1 += 1
	for x in range(mid + 1, j + 1):
		if s[x] != cneed:
			cnt2 += 1
	return min(cnt1 + solv(s, mid + 1, j, need + 1), cnt2 + solv(s, i, mid, need + 1))

def main():
	T = int(rl())
	for _ in range(T):
		n = int(rl())
		s = rll()[0]
		x = solv(s, 0, len(s) - 1, 0)
		print(x)
	stdout.close()
main()


def make_good(first, last, c, size):
	if last - first == 1:
		if l[first:last] == [c]:
			return 0
		else:
			return 1
	return min(make_good(first + size // 2, last, chr(ord(c) + 1), size // 2) + (size // 2 - l[first:last - size // 2].count(c)), make_good(first, last - size // 2, chr(ord(c) + 1), size // 2) + (size // 2 - l[first + size // 2:last].count(c)))
for _ in range(0, int(input())):
	n = int(input())
	l = list(input().strip())
	print(make_good(0, n, 'a', n))


def find_cost(arr, a, b, l):
	le = alpha[l]
	if b - a == 1:
		return int(arr[a] != le)
	cost1 = 0
	cost2 = 0
	mid = (a + b) // 2
	for i in range(a, mid):
		if arr[i] != le:
			cost1 += 1
	for i in range(mid, b):
		if arr[i] != le:
			cost2 += 1
	return min(cost1 + find_cost(arr, mid, b, l + 1), cost2 + find_cost(arr, a, mid, l + 1))
alpha = list('abcdefghijklmnopqrstuvwxyz')

def main():
	for _ in range(int(input())):
		n = int(input())
		arr = input().strip()
		cost = find_cost(arr, 0, n, 0)
		print(cost)
main()


t = int(input())
b = 'abcdefghijklmnopqrstuvwxyz'

def solve(step, s, res):
	N = len(s)
	hs1 = s[0:N // 2]
	hs2 = s[N // 2:N]
	d1 = len(hs1) - hs1.count(b[step])
	d2 = len(hs2) - hs2.count(b[step])
	if N > 1:
		res1 = solve(step + 1, hs2, res + d1)
		res2 = solve(step + 1, hs1, res + d2)
		res = min(res1, res2)
	elif s != b[step]:
		res += 1
	return res
m = {}
for _ in range(0, t):
	n = int(input())
	s = input()
	print(solve(0, s, 0))


t = int(input())

def count(s, c):
	co = 0
	for i in s:
		if i != c:
			co += 1
	return co

def calc(s, l, r, c):
	if l == r:
		if s[l] == c:
			return 0
		else:
			return 1
	mid = (r + l) // 2
	cntl = count(s[l:mid + 1], c) + calc(s, mid + 1, r, chr(ord(c) + 1))
	cntr = count(s[mid + 1:r + 1], c) + calc(s, l, mid, chr(ord(c) + 1))
	return min(cntl, cntr)
for _ in range(t):
	n = int(input())
	s = input()
	print(calc(s, 0, n - 1, 'a'))


def find(list1, num):
	alpha = chr(ord('a') + num)
	if len(list1) == 1:
		if list1[0] == alpha:
			return 0
		else:
			return 1
	list12 = list1[:len(list1) // 2]
	list13 = list1[len(list1) // 2:]
	count1 = 0
	count2 = 0
	for i in range(len(list12)):
		if list12[i] != alpha:
			count1 += 1
	for i in range(len(list13)):
		if list13[i] != alpha:
			count2 += 1
	return min(count1 + find(list13, num + 1), count2 + find(list12, num + 1))
t = int(input())
while t != 0:
	n = int(input())
	s = input()
	print(find(s, 0))
	t -= 1


def f(l, r, c, s):
	mid = (l + r) // 2
	if r - l == 1:
		if s[l] != c:
			return '1'
		else:
			return '0'
	else:
		countl = (r - l) // 2 - int(s[l:mid].count(c)) + int(f(mid, r, str(chr(ord(c) + 1)), s))
		countr = (r - l) // 2 - int(s[mid:r].count(c)) + int(f(l, mid, str(chr(ord(c) + 1)), s))
		return min(countl, countr)
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(f(0, n, 'a', s))


def count_c(s, c):
	count = 0
	for character in s:
		if character != c:
			count += 1
	return count

def count_to_good(s, c):
	n = len(s)
	if n == 1:
		if s == c:
			return 0
		else:
			return 1
	c_in_first_half = count_c(s[:n // 2], c) + count_to_good(s[n // 2:], chr(ord(c) + 1))
	c_in_second_half = count_c(s[n // 2:], c) + count_to_good(s[:n // 2], chr(ord(c) + 1))
	return min(c_in_first_half, c_in_second_half)
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(count_to_good(s, 'a'))


import sys
import os, io

def read_list():
	return map(int, io.BytesIO(os.read(0, os.fstat(0).st_size)).readline().split())

def read_all():
	return map(int, io.BytesIO(os.read(0, os.fstat(0).st_size)).read().split())

def print_list(l):
	print(' '.join(map(str, l)))

def count(c, start, end):
	res = 0
	for i in range(start, end):
		if s[i] != c:
			res += 1
	return res

def match(c, start, end):
	if start + 1 == end:
		if s[start] == c:
			return 0
		else:
			return 1
	l = end - start >> 1
	return min(count(c, start, start + l) + match(chr(1 + ord(c)), start + l, end), count(c, start + l, end) + match(chr(1 + ord(c)), start, start + l))
N = int(input())
for _ in range(N):
	n = int(input())
	s = input().strip()
	print(match('a', 0, n))


def func(s, c='a'):
	c_num = ord(c)
	l = len(s)
	if l == 1:
		return 0 if s == c else 1
	mid = l // 2
	left = func(s[:mid], chr(c_num + 1))
	left += sum([1 for i in s[mid:] if i != c])
	right = func(s[mid:], chr(c_num + 1))
	right += sum([1 for i in s[:mid] if i != c])
	return min(left, right)

def main():
	t = int(input())
	t_input = []
	for i in range(t):
		input()
		t_input.append(input())
	for i in list(map(func, t_input)):
		print(i)
main()


def good_string(s, n, l, r):
	if l == r:
		if s[l] == chr(n):
			return 0
		else:
			return 1
	mid = (l + r) // 2
	c1 = 0
	c2 = 0
	for i in range(l, mid + 1):
		if s[i] != chr(n):
			c1 += 1
	for i in range(mid + 1, r + 1):
		if s[i] != chr(n):
			c2 += 1
	x = good_string(s, n + 1, l, mid)
	y = good_string(s, n + 1, mid + 1, r)
	return min(c1 + y, c2 + x)
q = int(input())
for o in range(q):
	n = int(input())
	s = input()
	m = ord('a')
	ans = good_string(s, m, 0, n - 1)
	print(ans)


import math, sys
from sys import stdin, stdout
from collections import Counter, defaultdict, deque
input = stdin.readline
I = lambda : int(input())
li = lambda : list(map(int, input().split()))

def find(a, c, d, p):
	if len(a) == 1:
		if ord(a[0]) == p:
			return 0
		else:
			return 1
	for i in range(0, len(a) // 2):
		if ord(a[i]) != p:
			c += 1
	for i in range(len(a) // 2, len(a)):
		if ord(a[i]) != p:
			d += 1
	c += find(a[len(a) // 2:], 0, 0, p + 1)
	d += find(a[0:len(a) // 2], 0, 0, p + 1)
	return min(c, d)

def solve():
	n = I()
	a = input().strip()
	if n == 1:
		if a == 'a':
			print(0)
		else:
			print(1)
		return
	p = find(a, 0, 0, 97)
	print(p)
for _ in range(I()):
	solve()


def find(a, v):
	return sum([1 for x in a if x != v])

def solve(a, n, level):
	if n == 1:
		return 0 if a[0] == level else 1
	n = n // 2
	a1 = a[0:n]
	a2 = a[n:2 * n]
	s1 = find(a1, level)
	if s1 == 0:
		return solve(a2, n, level + 1)
	s2 = find(a2, level)
	if s2 == 0:
		return solve(a1, n, level + 1)
	s12 = solve(a2, n, level + 1)
	s21 = solve(a1, n, level + 1)
	return min(s1 + s12, s2 + s21)

def main():
	(n,) = map(int, input().split(' '))
	a = [int(x, 36) - 10 for x in input()]
	print(solve(a, n, 0))
for _ in range(int(input())):
	main()


from math import *
from collections import *
from bisect import *
import sys
input = sys.stdin.readline
t = int(input())

def recfk(l, r, val, char):
	global gl
	if ord(char) >= 97 + 19 - 1 or r - l <= 0:
		return
	if r - l == 1:
		nex = chr(ord(char) + 1)
		lis = [s[l], s[r]]
		if lis.count(char) == 2 or lis.count(nex) == 2:
			gl.append(val + 1)
		elif char in lis and nex in lis:
			gl.append(val)
		elif char in lis or nex in lis:
			gl.append(val + 1)
		else:
			gl.append(val + 2)
		return
	mid = (l + r) // 2
	lc = bisect_right(ind[char], mid) - bisect_left(ind[char], l)
	rc = bisect_right(ind[char], r) - bisect_left(ind[char], mid + 1)
	lc = max(0, lc)
	rc = max(0, rc)
	recfk(l, mid, (r - l + 1) // 2 - rc + val, chr(ord(char) + 1))
	recfk(mid + 1, r, (r - l + 1) // 2 - lc + val, chr(ord(char) + 1))
while t:
	global gl
	t -= 1
	n = int(input())
	s = input().rstrip('\n')
	if n == 1:
		if s[0] == 'a':
			print(0)
		else:
			print(1)
		continue
	gl = []
	ind = {chr(i + 97): [] for i in range(26)}
	for i in range(n):
		ind[s[i]].append(i)
	recfk(0, n - 1, 0, 'a')
	try:
		print(min(gl))
	except:
		print(0)


def minCost(l, r, c):
	cost = 0
	for i in range(l - 1, r):
		if s[i] != c:
			cost += 1
	return cost

def minChar(l, r, c):
	if l == r:
		if s[l - 1] == c:
			return 0
		else:
			return 1
	mid = (l + r) // 2
	return min(minCost(l, mid, c) + minChar(mid + 1, r, chr(ord(c) + 1)), minChar(l, mid, chr(ord(c) + 1)) + minCost(mid + 1, r, c))
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(minChar(1, n, 'a'))


t = int(input())

def minimo(string, letra):
	if len(string) == 1:
		if string[0] == letra:
			return 0
		else:
			return 1
	primeira_metade = string[0:len(string) // 2]
	segunda_metade = string[len(string) // 2:len(string)]
	faltam_primeira_metade = len(primeira_metade) - primeira_metade.count(letra)
	faltam_segunda_metade = len(segunda_metade) - segunda_metade.count(letra)
	proxima_letra = chr(ord(letra) + 1)
	total_primeira_condicao = faltam_primeira_metade + minimo(segunda_metade, proxima_letra)
	total_segunda_condicao = faltam_segunda_metade + minimo(primeira_metade, proxima_letra)
	return min(total_primeira_condicao, total_segunda_condicao)
for i in range(t):
	n = int(input())
	s = input()
	print(minimo(s, 'a'))


__MULTITEST = True

def fix(sHalf, sNext, c):
	cost = len(sHalf) - sHalf.count(c)
	if len(sNext) == 1:
		cost += 1 if sNext[0] != chr(ord(c) + 1) else 0
	else:
		mid = len(sNext) // 2
		cost += min(fix(sNext[:mid], sNext[mid:], chr(ord(c) + 1)), fix(sNext[mid:], sNext[:mid], chr(ord(c) + 1)))
	return cost

def costCal(s):
	if len(s) == 1:
		return 1 if s[0] != 'a' else 0
	mid = len(s) // 2
	cost1 = fix(s[:mid], s[mid:], 'a')
	cost2 = fix(s[mid:], s[:mid], 'a')
	return min(cost1, cost2)

def solve():
	n = int(input())
	s = str(input())
	print(costCal(s))
t = int(input()) if __MULTITEST else 1
for tt in range(t):
	solve()


def ans(c, ss, se):
	if ss == se:
		if s[ss] == c:
			return 0
		else:
			return 1
	a = 0
	b = 0
	for i in range(ss, (ss + se) // 2 + 1):
		if s[i] != c:
			a += 1
	for i in range((ss + se) // 2 + 1, se + 1):
		if s[i] != c:
			b += 1
	x = min(a + ans(chr(ord(c) + 1), (ss + se) // 2 + 1, se), b + ans(chr(ord(c) + 1), ss, (ss + se) // 2))
	return x
t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	print(ans('a', 0, n - 1))


from collections import deque
import sys

def inp():
	return sys.stdin.readline().strip()

def f(l, r, c, s):
	if l > r:
		return 0
	if l == r:
		if s[l] == c:
			return 0
		return 1
	mid = (l + r) // 2
	left = 0
	for i in range(l, mid + 1):
		if s[i] != c:
			left += 1
	right = 0
	for i in range(mid + 1, r + 1):
		if s[i] != c:
			right += 1
	ans = min(left + f(mid + 1, r, c + 1, s), right + f(l, mid, c + 1, s))
	return ans
for _ in range(int(inp())):
	n = int(inp())
	s = inp()
	s = [ord(i) - ord('a') for i in s]
	print(f(0, n - 1, 0, s))


def f(s, c):
	if len(s) == 1:
		if s[0] == c:
			return 0
		else:
			return 1
	q = s[0:len(s) // 2].count(c)
	w = s[len(s) // 2:].count(c)
	e = len(s) // 2 - q + f(s[len(s) // 2:], chr(ord(c) + 1))
	ee = len(s) // 2 - w + f(s[0:len(s) // 2], chr(ord(c) + 1))
	return min(e, ee)
t = int(input())
for h in range(t):
	n = int(input())
	s = input()
	c = 'a'
	ans = f(s, c)
	print(ans)


d = {chr(ord('a') + i): i for i in range(26)}

def f(left, right, level) -> int:
	if left + 1 == right:
		if d[string[left]] != level:
			return 1
		return 0
	middle = (left + right) // 2
	ans_left = f(left, middle, level + 1)
	for i in range(middle, right):
		if d[string[i]] != level:
			ans_left += 1
	ans_right = f(middle, right, level + 1)
	for i in range(left, middle):
		if d[string[i]] != level:
			ans_right += 1
	return min(ans_right, ans_left)
t = int(input())
for _ in range(t):
	n = int(input())
	string = str(input())
	print(f(0, n, 0))


import math

def calc(s, c, count):
	if len(s) == 1:
		if s[0] != c:
			return 1 + count
		else:
			return count
	i = len(s) // 2
	a = s[:i]
	b = s[i:]
	ac = a.count(c)
	bc = b.count(c)
	c = chr(ord(c) + 1)
	count1 = len(a) - ac + calc(b, c, count)
	count2 = len(a) - bc + calc(a, c, count)
	return min(count1, count2)
for _ in range(int(input())):
	n = int(input())
	s = input()
	chars = int(math.log(n, 2)) + 1
	side = False
	i = n
	c = 'a'
	count = calc(s, c, 0)
	print(count)


import sys

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 19
MOD = 10 ** 9 + 7

def rec(i, ln, c):
	if ln == 1:
		return S[i] == c
	res = max(rec(i, ln // 2, c + 1) + S[i + ln // 2:i + ln].count(c), rec(i + ln // 2, ln // 2, c + 1) + S[i:i + ln // 2].count(c))
	return res
for _ in range(INT()):
	N = INT()
	S = [ord(c) - 97 for c in input()]
	res = rec(0, N, 0)
	ans = N - res
	print(ans)


def solution(inp, current_char='a'):
	length = len(inp)
	if length == 1:
		return 0 if inp == current_char else 1
	next_char = chr(ord(current_char) + 1)
	return min(solution(inp[length // 2:], next_char) + sum(map(lambda x: 0 if x == current_char else 1, inp[:length // 2])), solution(inp[:length // 2], next_char) + sum(map(lambda x: 0 if x == current_char else 1, inp[length // 2:])))
t = int(input())
for i in range(0, t):
	n = int(input())
	value = input()
	print(solution(value))


t = int(input())

def get_mincost(arr, n, ch):
	if n == 1:
		if arr[0] == ch:
			return 0
		return 1
	else:
		a1 = arr[:n // 2]
		a2 = arr[n // 2:]
		mini = min(n // 2 - a1.count(ch) + get_mincost(a2, n // 2, chr(ord(ch) + 1)), n // 2 - a2.count(ch) + get_mincost(a1, n // 2, chr(ord(ch) + 1)))
		return mini
for _ in range(t):
	n = int(input())
	a = list(input())
	ch = 'a'
	print(get_mincost(a, n, ch))


max_value = float('inf')

def next_char(x):
	return chr(ord(x) + 1)

def test():
	n = int(input())
	s = list(str(input()))

	def count(l, r, f):
		if ord(f) > ord('z'):
			return max_value
		if l == r:
			return int(s[l] != f)
		middle = int((l + r) / 2)
		left_cost = 0
		right_cost = 0
		for x in s[l:middle + 1]:
			left_cost += int(x != f)
		left_cost += count(middle + 1, r, next_char(f))
		for x in s[middle + 1:r + 1]:
			right_cost += int(x != f)
		right_cost += count(l, middle, next_char(f))
		return min(left_cost, right_cost)
	print(count(0, n - 1, 'a'))
n_tests = int(input())
while n_tests > 0:
	test()
	n_tests -= 1


def good_str(s, x, n):
	if n == 1:
		if s[0] == chr(x):
			return 0
		else:
			return 1
	else:
		s1 = s[:n // 2]
		s2 = s[n // 2:]
		p = s1.count(chr(x))
		q = s2.count(chr(x))
		sum1 = n // 2 - p + good_str(s2, x + 1, n // 2)
		sum2 = n // 2 - q + good_str(s1, x + 1, n // 2)
		return min(sum1, sum2)
for _ in range(int(input())):
	n = int(input())
	s = input()
	result = good_str(s, 97, n)
	print(result)


from collections import Counter

def solve(s, c='a'):
	if len(s) == 1:
		return int(s != c)
	count_l = Counter(s[:len(s) // 2])
	count_r = Counter(s[len(s) // 2:])
	diff = min(len(s) // 2 - count_r[c] + solve(s[:len(s) // 2], chr(ord(c) + 1)), len(s) // 2 - count_l[c] + solve(s[len(s) // 2:], chr(ord(c) + 1)))
	return diff
for _ in range(int(input())):
	n = range(int(input()))
	s = input()
	print(solve(s))


alphabet = list('abcdefghijklmnopqrstuvwxyz')

def query(index, string):
	if len(string) == 1:
		if alphabet[index] == string:
			return 0
		else:
			return 1
	else:
		n = len(string)
		fast = string[:n // 2]
		second = string[n // 2:]
		cnt_fast = 0
		cnt_second = 0
		for i in range(n // 2):
			if alphabet[index] != fast[i]:
				cnt_fast += 1
		for i in range(n // 2):
			if alphabet[index] != second[i]:
				cnt_second += 1
		return min(cnt_fast + query(index + 1, second), cnt_second + query(index + 1, fast))

def solve():
	n = int(input())
	s = input()
	ans = query(0, s)
	print(ans)
	return

def main():
	t = int(input())
	for i in range(t):
		solve()
	return
main()


q = 'abcdefghijklmnopqrstuvwxyz'

def fun(beg, end, index):
	if beg == end:
		if s[beg] != q[index]:
			return 1
		else:
			return 0
	else:
		mid = (beg + end) // 2
		k = min(mid - beg + 1 - s[beg:mid + 1].count(q[index]) + fun(mid + 1, end, index + 1), fun(beg, mid, index + 1) + end - mid - s[mid + 1:end + 1].count(q[index]))
		return k
for _ in range(int(input())):
	n = int(input())
	s = input()
	s = '0' + s
	print(fun(1, n, 0))


p = '0abcdefghijklmnopqrstuvwxyz'

def sol(s, k):
	if len(s) == 1:
		if s[0] == p[k]:
			return 0
		else:
			return 1
	l = 0
	r = 0
	for i in s[:len(s) // 2]:
		if i != p[k]:
			l += 1
	for i in s[len(s) // 2:]:
		if i != p[k]:
			r += 1
	return min(l + sol(s[len(s) // 2:], k + 1), r + sol(s[:len(s) // 2], k + 1))
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(sol(s, 1))


from collections import *
import sys

def ri():
	return int(input())

def rl():
	return list(map(int, input().split()))
import string
values = dict()
for (index, letter) in enumerate(string.ascii_lowercase):
	values[index + 1] = letter

def solve(s, n, nb):
	letter = values[nb]
	if n == 1:
		if s == letter:
			return 0
		else:
			return 1
	else:
		k = n // 2
		ans1 = 0
		ans2 = 0
		for i in range(k):
			if s[i] != letter:
				ans1 += 1
		for i in range(k, n):
			if s[i] != letter:
				ans2 += 1
		return min(ans1 + solve(s[k:], k, nb + 1), ans2 + solve(s[:k], k, nb + 1))
t = ri()
for _ in range(t):
	n = ri()
	s = input()
	print(solve(s, n, 1))


def f(s, m):
	c = chr(m)
	if len(s) == 1:
		if s == c:
			return 0
		return 1
	n = len(s)
	return min(f(s[n // 2:], m + 1) + n // 2 - s[:n // 2].count(c), f(s[:n // 2], m + 1) + n // 2 - s[n // 2:].count(c))
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	print(f(s, ord('a')))


import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict
from bisect import bisect_left as bl, bisect_right as br
from collections import Counter
ip = lambda : int(stdin.readline())
inp = lambda : map(int, stdin.readline().split())

def check(st, end, idx):
	cnt = 0
	for i in range(st, end):
		if s[i] != ch[idx]:
			cnt += 1
	return cnt

def solve(st, end, idx):
	if st + 1 == end:
		if s[st] != ch[idx]:
			return 1
		else:
			return 0
	else:
		alp = ch[idx]
		mid = (st + end) // 2
		ch1 = check(mid, end, idx) + solve(st, mid, idx + 1)
		ch2 = check(st, mid, idx) + solve(mid, end, idx + 1)
		return min(ch1, ch2)
t = ip()
for _ in range(t):
	n = ip()
	s = input()
	s = list(s)
	ch = 'abcdefghijklmnopqrstuvwxyz'
	ch = list(ch)
	ans = solve(0, n, 0)
	print(ans)


from math import log2
from sys import stdin, setrecursionlimit
setrecursionlimit(10 ** 8)
input = stdin.readline
T = int(input())
ans = 0

def isGood(msg, start, size, ch):
	if size == 1:
		cnt = 0
		if msg[start] != ch:
			cnt += 1
		return cnt
	if dp[start][int(log2(size))] != -1:
		return dp[start][int(log2(size))]
	half = start + size // 2
	tmp = 0
	for i in range(start, half):
		if msg[i] != ch:
			tmp += 1
	res = isGood(msg, half, size // 2, chr(ord(ch) + 1)) + tmp
	tmp = 0
	for i in range(half, half + size // 2):
		if msg[i] != ch:
			tmp += 1
	res = min(res, isGood(msg, start, size // 2, chr(ord(ch) + 1)) + tmp)
	dp[start][int(log2(size))] = res
	return dp[start][int(log2(size))]
for _ in range(T):
	size = int(input())
	msg = list(input().rstrip())
	dp = [[-1] * (int(log2(size)) + 1) for _ in range(size)]
	print(isGood(msg, 0, size, 'a'))


T = int(input())
for _ in range(T):
	(n, s) = (int(input()), input())
	(m, tn) = (0, n)
	while tn:
		tn //= 2
		m += 1
	mem = [0] * n
	for p in range(m):
		(r, ch) = (2 ** p, chr(ord('a') + m - p - 1))
		for i in range(0, n, r):
			if p == 0:
				mem[i] = 1 if s[i] != ch else 0
			else:
				lcc = sum([1 for j in range(i, i + r // 2) if s[j] != ch])
				rcc = sum([1 for j in range(i + r // 2, i + r) if s[j] != ch])
				mem[i] = min(mem[i] + rcc, lcc + mem[i + r // 2])
	print(mem[0])


t = int(input())
for i in range(t):
	n = int(input())
	s = input()

	def recur(start, end, curr):
		if start == end:
			if s[start] == curr:
				return 0
			return 1
		mid = (start + end) // 2
		startChange = 0
		for i in s[start:mid + 1]:
			if i != curr:
				startChange += 1
		endChange = 0
		for i in s[mid + 1:end + 1]:
			if i != curr:
				endChange += 1
		nextChar = chr(ord(curr) + 1)
		return min(startChange + recur(mid + 1, end, nextChar), endChange + recur(start, mid, nextChar))
	print(recur(0, n - 1, 'a'))


import sys
import math
from bisect import bisect_right as br
from statistics import mode
from itertools import combinations as cb

def int_arr():
	return list(map(int, sys.stdin.readline().strip().split()))

def str_arr():
	return list(map(str, sys.stdin.readline().strip().split()))

def input():
	return sys.stdin.readline().strip()

def check(st, ch):
	n = len(st)
	mid = n // 2
	if n == 1:
		if st == ch:
			return 0
		else:
			return 1
	return min(mid - st[:mid].count(ch) + check(st[mid:], chr(ord(ch) + 1)), mid - st[mid:].count(ch) + check(st[:mid], chr(ord(ch) + 1)))
for _ in range(int(input())):
	n = int(input())
	st = input()
	print(check(st, 'a'))


def F(l, r, c):
	if r - l == 1:
		if s[l] == c:
			return 0
		else:
			return 1
	mid = (l + r) // 2
	cntl = (r - l) // 2 - s.count(c, l, mid) + F(mid, r, chr(ord(c) + 1))
	cntr = (r - l) // 2 - s.count(c, mid, r) + F(l, mid, chr(ord(c) + 1))
	return min(cntl, cntr)
for z in range(int(input())):
	n = int(input())
	s = input()
	print(F(0, n, 'a'))


import io
import os
from collections import Counter, defaultdict, deque

def solve(N, S):
	pref = [[0] for i in range(26)]
	for x in S:
		j = ord(x) - ord('a')
		for i in range(26):
			pref[i].append(pref[i][-1] + int(i == j))
	cache = {}

	def getCost(i, j):
		assert i != j
		key = (i, j)
		if key not in cache:
			assert i != j
			ch = K - ((j - i).bit_length() - 1)
			if j - i == 1:
				cache[key] = int(ord(S[i]) - ord('a') != ch)
			else:
				mid = (i + j) // 2
				lCost = mid - i - (pref[ch][mid] - pref[ch][i]) + getCost(mid, j)
				rCost = j - mid - (pref[ch][j] - pref[ch][mid]) + getCost(i, mid)
				cache[key] = min(lCost, rCost)
		return cache[key]
	K = N.bit_length() - 1
	for k in range(K + 1):
		l = 1 << k
		for i in range(0, N, l):
			getCost(i, i + l)
	return getCost(0, N)
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
T = int(input())
for t in range(T):
	(N,) = [int(x) for x in input().split()]
	S = input().decode().rstrip()
	ans = solve(N, S)
	print(ans)


for x in range(int(input())):
	n = int(input())
	s = input()
	k = 65

	def rec(st, k):
		n = len(st)
		if n == 1:
			if st[0] == chr(k).lower():
				return 0
			else:
				return 1
		a = n // 2 - st[n // 2:].count(chr(k).lower()) + rec(st[:n // 2], k + 1)
		b = n // 2 - st[:n // 2].count(chr(k).lower()) + rec(st[n // 2:], k + 1)
		return min(a, b)
	print(rec(s, 65))


def f():
	t = int(input())
	ns = []
	arr = []
	for _ in range(t):
		ns.append(int(input()))
		arr.append(input())

	def rec(s, letter):
		if len(s) == 1:
			return int(s != letter)
		da = len(list(filter(lambda x: x != letter, s[len(s) // 2:])))
		db = len(list(filter(lambda x: x != letter, s[:len(s) // 2])))
		a = rec(s[:len(s) // 2], chr(ord(letter) + 1))
		b = rec(s[len(s) // 2:], chr(ord(letter) + 1))
		return min(da + a, db + b)
	for s in arr:
		print(rec(s, 'a'))
f()


from sys import setrecursionlimit, stdout, stdin
setrecursionlimit(100000)

def solve(l, r, c):
	if r - l + 1 == 1:
		return 1 - (s[l:r + 1] == c)
	else:
		mid = (r + l + 1) // 2
		p1 = 0
		p2 = 0
		for i in range(l, mid):
			p1 += s[i] != c
		for i in range(mid, r + 1):
			p2 += s[i] != c
		ans = min(p1 + solve(mid, r, chr(ord(c) + 1)), solve(l, mid - 1, chr(ord(c) + 1)) + p2)
		return ans
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	s = stdin.readline()
	print(solve(0, n - 1, 'a'))


import sys
import os
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import math
import random
import re
import heapq
t = int(input())
while t:
	t -= 1
	n = int(input())
	arr = list(map(ord, list(input().strip())))
	_a = ord('a')
	if len(arr) == 1:
		if arr[0] == _a:
			print(0)
		else:
			print(1)
		continue
	depth = 1
	while 2 ** depth < len(arr):
		depth += 1
	_a += depth
	dp = [1] * len(arr)
	for a in range(len(arr)):
		if arr[a] == _a:
			dp[a] = 0
	_a -= 1
	p = 1
	while _a >= ord('a'):
		i = 0
		while i < len(arr):
			st = i
			mid = i + 2 ** p // 2
			res1 = dp[st]
			for j in range(mid, mid + 2 ** p // 2):
				if arr[j] != _a:
					res1 += 1
			res2 = dp[mid]
			for j in range(st, st + 2 ** p // 2):
				if arr[j] != _a:
					res2 += 1
			dp[st] = min(res1, res2)
			i += 2 ** p
		_a -= 1
		p += 1
	print(dp[0])


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from copy import *
from collections import deque
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(a, b):
	ans = 1
	while b > 0:
		if b % 2 == 1:
			ans *= a
		a *= a
		b //= 2
	return ans

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def rec(s, curr):
	if len(s) == 1:
		if s[0] == curr:
			return 0
		else:
			return 1
	a1 = []
	a2 = []
	for i in range(0, len(s) // 2):
		a1.append(s[i])
		a2.append(s[i + len(s) // 2])
	cnt1 = 0
	cnt2 = 0
	for i in range(len(a1)):
		if a1[i] != curr:
			cnt1 += 1
		if a2[i] != curr:
			cnt2 += 1
	return min(cnt1 + rec(a2, curr + 1), cnt2 + rec(a1, curr + 1))

def solve():
	n = int(inp())
	s = list(inp())
	for i in range(n):
		s[i] = ord(s[i]) - 96
	curr = 1
	ans = rec(s, curr)
	print(ans)
testcase(int(inp()))


def solve(s, c):
	n = len(s)
	if n == 1:
		if s[0] == c:
			return 0
		else:
			return 1
	l = n // 2 - s[:n // 2].count(c) + solve(s[n // 2:], chr(ord(c) + 1))
	r = n // 2 - s[n // 2:].count(c) + solve(s[:n // 2], chr(ord(c) + 1))
	return min(l, r)
t = int(input())
for _ in range(t):
	input()
	print(solve(input(), 'a'))


import os
if 'LOCAL_COMPROG' in os.environ:
	filename = __file__.replace('py', 'in')
	if os.path.exists(filename):
		print('[WARN] swapping stdin for test file')
		import sys
		sys.stdin = open(filename, 'r')

def f(l, r, c):
	if l == r - 1:
		return int(ord(S[l]) != c)
	mid = (r + l) // 2
	moves_left = (r - l) // 2 - sum([1 for x in S[l:mid] if ord(x) == c])
	moves_right = (r - l) // 2 - sum([1 for x in S[mid:r] if ord(x) == c])
	moves_left += f(mid, r, c + 1)
	moves_right += f(l, mid, c + 1)
	return min(moves_left, moves_right)
for __ in range(int(input())):
	n = int(input())
	S = input()
	print(f(0, len(S), ord('a')))


def make(st, last, type):
	if st == last:
		return 0 if s[st] == alpha[type] else 1
	mid = (st + last) // 2
	lcost = 0
	rcost = make(mid + 1, last, type + 1)
	for i in range(st, mid + 1):
		if s[i] != alpha[type]:
			lcost += 1
	ans = lcost + rcost
	rcost = 0
	lcost = make(st, mid, type + 1)
	for i in range(mid + 1, last + 1):
		if s[i] != alpha[type]:
			rcost += 1
	return min(ans, lcost + rcost)
alpha = 'abcdefghijklmnopqrstuvwxyz'
for _ in range(int(input())):
	n = int(input())
	s = input().strip()
	print(make(0, n - 1, 0))


import sys, io, os, time
from collections import defaultdict
from collections import OrderedDict
from collections import deque
from itertools import combinations
from itertools import permutations
import bisect, math, heapq
alphabet = 'abcdefghijklmnopqrstuvwxyz'
input = sys.stdin.readline
s = ''

def dp(l, r, c):
	if l == r:
		return s[l] != c
	m = (l + r) // 2
	ans1 = dp(m + 1, r, chr(ord(c) + 1))
	ans2 = dp(l, m, chr(ord(c) + 1))
	for i in range(l, m + 1):
		ans1 += s[i] != c
	for i in range(m + 1, r + 1):
		ans2 += s[i] != c
	return min(ans1, ans2)
for ii in range(int(input())):
	n = int(input())
	s = input()
	print(int(dp(0, n - 1, 'a')))


testCases = int(input())
letters = 'abcdefghijklmnopqrstuvwxyz'

def offset(string, char):
	ret = 0
	if char in string:
		for k in range(len(string)):
			if string[k] == char:
				ret += 1
	return ret

def good_count(string, char):
	length = len(string)
	wanted_char = letters[char]
	if length == 0:
		return 0
	elif length == 1:
		if wanted_char in string:
			return 0
		else:
			return 1
	half_len = int(length / 2)
	first_half = string[0:half_len]
	second_half = string[half_len:length]
	fhalf_offset = offset(first_half, wanted_char)
	shalf_offset = offset(second_half, wanted_char)
	return min(half_len - fhalf_offset + good_count(second_half, char + 1), half_len - shalf_offset + good_count(first_half, char + 1))
for x in range(testCases):
	input()
	print(good_count(input(), 0))


import math

def make_good(word, ch):
	n = len(word)
	if n == 1:
		if word[0] == ch:
			return 0
		else:
			return 1
	moves1 = 0
	for i in range(0, int(n / 2)):
		if word[i] != ch:
			moves1 += 1
	moves1 += make_good(word[int(n / 2):], chr(ord(ch) + 1))
	moves2 = 0
	for i in range(int(n / 2), n):
		if word[i] != ch:
			moves2 += 1
	moves2 += make_good(word[:int(n / 2)], chr(ord(ch) + 1))
	return min(moves1, moves2)
num_cases = int(input())
for _ in range(num_cases):
	n = int(input())
	input_word = input().strip()
	print(make_good(input_word, 'a'))


def solve(x, z):
	if len(x) == 0:
		return 0
	if len(x) == 1:
		return int(x != z)
	m = len(x) // 2
	l = x[:m]
	r = x[m:]
	a = m - l.count(z) + solve(r, chr(ord(z) + 1))
	b = m - r.count(z) + solve(l, chr(ord(z) + 1))
	return min(a, b)

def getCost(l, r, ch):
	count = 0
	for i in range(l, r + 1):
		if arr[i] != ch:
			count += 1
	return count

def getMin(l, r, ch):
	print('yes')
	if l == r:
		if arr[l] == ch:
			return 0
		else:
			return 1
	mid = (l + r) // 2
	return min(getCost(l, mid, ch) + getMin(mid + 1, r, chr(ord(ch) + 1)), getCost(mid + 1, r, ch) + getMin(l, mid, chr(ord(ch) + 1)))
for _ in range(int(input())):
	n = int(input())
	s = str(input())
	print(solve(s, 'a'))


def ans(s, c):
	n = len(s)
	if n == 1:
		return int(ord(s[0]) != c)
	n = n // 2
	return min(ans(s[:n], c + 1) + n - s[n:].count(chr(c)), ans(s[n:], c + 1) + n - s[:n].count(chr(c)))
for _ in range(int(input())):
	input()
	print(ans(input(), 97))


import sys

def calc(l, r, c, s):
	if abs(l - r) < 1:
		if s[l] == c:
			return 0
		else:
			return 1
	else:
		m = (l + r) // 2
		if l == m:
			count1 = 1 if s[l] != c else 0
			count2 = 1 if s[r] != c else 0
		else:
			(count1, count2) = (0, 0)
			for i in range(l, m + 1):
				if s[i] != c:
					count1 += 1
			for i in range(m + 1, r + 1):
				if s[i] != c:
					count2 += 1
		ans1 = count1 + calc(m + 1, r, chr(ord(c) + 1), s)
		ans2 = count2 + calc(l, m, chr(ord(c) + 1), s)
		return min(ans1, ans2)

def solve(n, s):
	print(calc(0, n - 1, 'a', s))
t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	solve(n, s)


t = int(input())
for _ in range(t):
	n = int(input())
	s = input()

	def sol(l, r, a) -> int:
		if r - l == 1:
			return 1 if s[l] != a else 0
		m = (l + r) // 2
		c1 = sol(l, m, chr(ord(a) + 1))
		c2 = sol(m, r, chr(ord(a) + 1))
		for i in range(m, r):
			c1 += 1 if s[i] != a else 0
		for i in range(l, m):
			c2 += 1 if s[i] != a else 0
		return min(c1, c2)
	print(sol(0, n, 'a'))


def goodString(word, c):
	size = len(word)
	if size == 1:
		return 1 if word != c else 0
	nextC = chr(ord(c) + 1)
	center = size // 2
	one = word[:center]
	two = word[center:]
	countC1 = one.count(c)
	countC2 = two.count(c)
	stepOne = len(one) - countC1 + goodString(two, nextC)
	stepTwo = len(two) - countC2 + goodString(one, nextC)
	if stepOne < stepTwo:
		return stepOne
	else:
		return stepTwo
n = int(input())
for i in range(n):
	size = int(input())
	word = input()
	print(goodString(word, 'a'))


t = int(input())
char = 'abcdefghijklmnopqrstuvwxyz'
bj = [[0] * 131072] * 26
ans = [[0] * 131072] * 26

def work(l, r, b):
	if l == r - 1:
		if a[l] == char[b]:
			return 0
		else:
			return 1
	if bj[b][l]:
		return ans[b][l]
	ans1 = 0
	mid = int((l + r) / 2)
	for i in range(l, mid):
		if a[i] != char[b]:
			ans1 += 1
	ans1 += work(mid, r, b + 1)
	ans2 = 0
	for i in range(mid, r):
		if a[i] != char[b]:
			ans2 += 1
	ans2 += work(l, mid, b + 1)
	ans[b][l] = min(ans1, ans2)
	bj[b][l] = 1
	return ans[b][l]
for i in range(1, t + 1):
	n = int(input())
	a = input()
	for i in range(0, n):
		for j in range(0, 26):
			bj[j][i] = 0
	print(work(0, n, 0))


def good_string(x, y):
	ch = chr(y)
	if len(x) == 1:
		return 1 if ch != x[0] else 0
	k = len(x) // 2
	z = k - x[:k].count(ch) + good_string(x[k:], y + 1)
	w = k - x[k:].count(ch) + good_string(x[:k], y + 1)
	return min(z, w)
t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	print(good_string(a, ord('a')))


def helper(s, a):
	if len(s) == 1:
		if s[0] == a:
			return 0
		else:
			return 1
	l = len(s) // 2
	b = chr(ord(a) + 1)
	c1 = 0
	for i in s[:l]:
		if i == a:
			c1 += 1
	d1 = 0
	for i in s[l:]:
		if i == a:
			d1 += 1
	c2 = helper(s[l:], b)
	d2 = helper(s[:l], b)
	return min(l - c1 + c2, l - d1 + d2)
t = int(input())
for l in range(t):
	n = int(input())
	s = input()
	print(helper(s, 'a'))


def func(s, n, a):
	c = chr(a)
	if n == 1:
		if s[0] == c:
			return 0
		return 1
	c1 = 0
	c2 = 0
	for i in range(n // 2):
		if s[i] == c:
			c1 += 1
	for i in range(n // 2, n):
		if s[i] == c:
			c2 += 1
	return min(n // 2 - c1 + func(s[n // 2:], n // 2, a + 1), n // 2 - c2 + func(s[:n // 2], n // 2, a + 1))
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(func(s, n, 97))


import math
import time
from collections import defaultdict, deque
from sys import stdin, stdout
from bisect import bisect_left, bisect_right
import string
lower = string.ascii_lowercase

def same(s, i):
	if len(s) == 1:
		if s[0] == lower[i]:
			return 0
		else:
			return 1
	else:
		ans = 0
		for j in s:
			if j != lower[i]:
				ans += 1
		return ans

def find(s, i):
	if len(s) == 1:
		if s[0] == lower[i]:
			return 0
		else:
			return 1
	else:
		return min(same(s[:len(s) // 2], i) + find(s[len(s) // 2:], i + 1), same(s[len(s) // 2:], i) + find(s[:len(s) // 2], i + 1))
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	print(find(s, 0))


t = int(input())
alph = 'abcdefghijklmnopqrstuvwxyz'

def sim(s, it):
	if len(s) == 1:
		if s == alph[it]:
			return 0
		else:
			return 1
	l = s[:len(s) // 2]
	r = s[len(s) // 2:]
	sc1 = 0
	sc2 = 0
	for i in l:
		if i != alph[it]:
			sc1 += 1
	for i in r:
		if i != alph[it]:
			sc2 += 1
	return min(sc1 + sim(r, it + 1), sc2 + sim(l, it + 1))
for _ in range(t):
	n = int(input())
	s = input()
	print(sim(s, 0))


def count(s, c):
	c2 = chr(ord(c) + 1)
	if len(s) == 1:
		return 1 - int(s[0] == c)
	else:
		n = len(s)
		h = n // 2
		x1 = sum((1 for a in s[:h] if a != c))
		x2 = sum((1 for a in s[h:] if a != c))
		y1 = count(s[h:], c2)
		y2 = count(s[:h], c2)
		return min(x1 + y1, x2 + y2)
T = int(input())
for _ in range(T):
	n = int(input())
	s = input()
	ans = count(s, 'a')
	print(ans)


def result(s, n, z):
	a = z
	b = chr(ord(a) + 1)
	if n == 1:
		if s[0] == a:
			return 0
		else:
			return 1
	x = s[:n // 2]
	y = s[n // 2:]
	cx = x.count(a)
	cy = y.count(a)
	out = min(n // 2 - cx + result(y, n // 2, b), n // 2 - cy + result(x, n // 2, b))
	return out
t = int(input())
i = 0
while i < t:
	n = int(input())
	s = input()
	print(result(s, n, 'a'))
	i = i + 1


import sys
sys.setrecursionlimit(100000)
alphabets = 'abcdefghijklmnopqrstuvwxyz'

def convert(s, a, b, ind):
	ch = alphabets[ind]
	if b - a == 1:
		if s[a] != ch:
			return 1
		else:
			return 0
	mid = (a + b) // 2
	(c1, c2) = (0, 0)
	for i in range(a, mid):
		if s[i] != ch:
			c1 += 1
	c1 += convert(s, mid, b, ind + 1)
	for i in range(mid, b):
		if s[i] != ch:
			c2 += 1
	c2 += convert(s, a, mid, ind + 1)
	return min(c1, c2)
for _ in range(int(input())):
	n = int(input())
	s = input()
	ans = convert(s, 0, n, 0)
	print(ans)


t = int(input())

def recur(s, c, n):
	if n == 1 and s[0] == chr(c):
		return 0
	if n == 1 and s[0] != chr(c):
		return 1
	c1 = 0
	c2 = 0
	for i in range(n // 2):
		if s[i] != chr(c):
			c1 += 1
		if s[i + n // 2] != chr(c):
			c2 += 1
	return min(c1 + recur(s[n // 2:], c + 1, n // 2), c2 + recur(s[:n // 2], c + 1, n // 2))
while t > 0:
	n = int(input())
	s = input()
	print(recur(s, 97, n))
	t -= 1


for _ in range(int(input())):
	n = int(input())
	s = input()

	def f(s, c):
		if len(s) <= 1:
			return 0 if s == c else 1
		c1 = s[:len(s) // 2].count(c)
		c2 = s[len(s) // 2:].count(c)
		return min(f(s[len(s) // 2:], chr(ord(c) + 1)) + len(s) // 2 - c1, f(s[:len(s) // 2], chr(ord(c) + 1)) + len(s) // 2 - c2)
	print(f(s, 'a'))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
from math import log2
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()
for _ in range(int(ri())):
	n = int(ri())
	s = ri()
	dp = list2d(26, n + 1, 0)
	for i in range(26):
		for j in range(n):
			if ord(s[j]) - ord('a') == i:
				dp[i][j + 1] = dp[i][j] + 1
			else:
				dp[i][j + 1] = dp[i][j]

	def solve(sta, lenn, ch):
		if lenn == 0:
			if ord(s[sta]) - ord('a') == ch:
				return 0
			else:
				return 1
		end = sta + 2 ** lenn - 1
		mid = (sta + end) // 2
		tlen = mid - sta + 1
		ret = min(solve(sta, lenn - 1, ch + 1) + tlen - (dp[ch][end + 1] - dp[ch][mid + 1]), solve(sta + 2 ** (lenn - 1), lenn - 1, ch + 1) + tlen - (dp[ch][mid + 1] - dp[ch][sta]))
		return ret
	ret = solve(0, int(log2(n)), 0)
	print(ret)


from sys import stdin, gettrace
if gettrace():
	inputi = input
else:

	def input():
		return next(stdin)[:-1]

	def inputi():
		return stdin.buffer.readline()

def makegoodl(ss, c):
	res = makegood(ss[len(ss) // 2:], chr(ord(c) + 1))
	for s in ss[:len(ss) // 2]:
		if s != c:
			res += 1
	return res

def makegoodr(ss, c):
	res = makegood(ss[:len(ss) // 2], chr(ord(c) + 1))
	for s in ss[len(ss) // 2:]:
		if s != c:
			res += 1
	return res

def makegood(ss, c):
	if len(ss) == 1:
		return 0 if ss == c else 1
	return min(makegoodl(ss, c), makegoodr(ss, c))

def solve():
	_ = input()
	print(makegood(input(), 'a'))

def main():
	t = int(inputi())
	for _ in range(t):
		solve()
main()


def func(i, count, l):
	if count == 1:
		if s[i] == alf[l]:
			return 0
		else:
			return 1
	if (i, count // 2, l + 1) not in dict:
		dict[i, count // 2, l + 1] = func(i, count // 2, l + 1)
	if (i + count // 2, count // 2, l + 1) not in dict:
		dict[i + count // 2, count // 2, l + 1] = func(i + count // 2, count // 2, l + 1)
	first = dict[i, count // 2, l + 1]
	second = dict[i + count // 2, count // 2, l + 1]
	for el in range(i, i + count // 2):
		if s[el] != alf[l]:
			second += 1
	for el in range(i + count // 2, i + count):
		if s[el] != alf[l]:
			first += 1
	return min(first, second)
alf = 'abcdefghijklmnopqrstuvwxyz'
t = int(input())
s = []
for _ in range(t):
	dict = {}
	n = int(input())
	s = list(input())
	print(func(0, n, 0))


from sys import stdin
from collections import deque

def fun(l, r, ans, ch):
	global maxi, s
	ch = chr(ch)
	if l == r - 1:
		if s[l] != ch:
			ans += 1
		maxi = min(maxi, ans)
		return
	ans1 = ans
	mid = (l + r) // 2
	for j in range(l, mid):
		if s[j] != ch:
			ans += 1
	for j in range(mid, r):
		if s[j] != ch:
			ans1 += 1
	fun(mid, r, ans, ord(ch) + 1)
	fun(l, mid, ans1, ord(ch) + 1)
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	s = stdin.readline()
	maxi = 10 ** 10
	fun(0, n, 0, 97)
	print(maxi)


import math
from decimal import *
import random
import sys
sys.setrecursionlimit(10 ** 6)
mod = int(1000000000.0) + 7

def dfs(arr, ans, c):
	if len(arr) == 1:
		if arr[0] != c:
			ans += 1
		return ans
	if len(arr) == 0:
		return ans
	else:
		(tmpl, tmpr) = (int(ans), int(ans))
		for i in range(len(arr) // 2):
			if arr[i] != c:
				tmpl += 1
			if arr[len(arr) // 2 + i] != c:
				tmpr += 1
		ans = min(dfs(arr[:len(arr) // 2], tmpr, chr(ord(c) + 1)), dfs(arr[len(arr) // 2:], tmpl, chr(ord(c) + 1)))
	return ans
for _ in range(int(input())):
	n = int(input())
	s = list(input())
	ans = dfs(s, 0, 'a')
	print(ans)


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
from collections import defaultdict
from itertools import permutations
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def check(s, n, ch):
	if n == 0:
		return 0
	elif n == 1:
		if s[0] != ch:
			return 1
		else:
			return 0
	ans = 0
	ans1 = 0
	for i in range(0, n // 2):
		if s[i] != ch:
			ans += 1
	ans += check(s[n // 2:], n // 2, chr(ord(ch) + 1))
	for i in range(n // 2, n):
		if s[i] != ch:
			ans1 += 1
	ans1 += check(s[0:n // 2], n // 2, chr(ord(ch) + 1))
	return min(ans1, ans)
for ik in range(int(input())):
	n = int(input())
	s = input()
	if n == 1:
		if s == 'a':
			print(0)
		else:
			print(1)
		continue
	ans = check(s, n, 'a')
	print(ans)


l = 'abcdefghijklmnopqrstuvwxyz'

def g(s, i):
	return len(s) - s.count(l[i])

def f(s, i):
	k = int(math.log2(len(s)))
	if k == 0:
		if s == l[i]:
			return 0
		else:
			return 1
	s1 = s[:2 ** (k - 1)]
	s2 = s[2 ** (k - 1):]
	return min(g(s1, i) + f(s2, i + 1), f(s1, i + 1) + g(s2, i))
import math
for _ in range(int(input())):
	n = int(input())
	k = int(math.log2(n))
	s = input()
	ans = 0
	p = n // 2
	if k == 0:
		if s == 'a':
			print(0)
		else:
			print(1)
	else:
		print(f(s, 0))


rr = lambda : input()
rri = lambda : int(input())
rrm = lambda : list(map(int, input().split()))
INF = float('inf')

def solve(N, S):
	if N == 1:
		return +(S != 'a')

	def dc(s, c, all):
		if len(s) == 1:
			return +(s != c)
		if all:
			amt = 0
			for char in s:
				if char != c:
					amt += 1
			return amt
		n = len(s)
		first_half = s[:n // 2]
		second_half = s[n // 2:]
		cost1 = dc(first_half, chr(ord(c) + 1), False) + dc(second_half, c, True)
		cost2 = dc(second_half, chr(ord(c) + 1), False) + dc(first_half, c, True)
		return min(cost1, cost2)
	first_half = S[:N // 2]
	second_half = S[N // 2:]
	cost1 = dc(first_half, chr(ord('a') + 1), False) + dc(second_half, 'a', True)
	cost2 = dc(second_half, chr(ord('a') + 1), False) + dc(first_half, 'a', True)
	return min(cost1, cost2)
t = rri()
for _ in range(t):
	n = rri()
	s = rr()
	print(solve(n, s))


def calc(a, c):
	if len(a) == 1:
		return int(c != a[0])
	mid = int(len(a) / 2)
	leftside = mid - a[0:mid].count(c) + calc(a[mid:len(a)], chr(ord(c) + 1))
	rightside = mid - a[mid:len(a)].count(c) + calc(a[0:mid], chr(ord(c) + 1))
	return min(leftside, rightside)
t = int(input())
for _ in range(0, t):
	input()
	a = input()
	print(calc(a, 'a'))


def rec(arr, mapp, l, r, c):
	if l == r:
		if arr[l] == chr(ord('a') + c):
			return 0
		return 1
	if (l, r) in mapp:
		return mapp[l, r]
	A = (l + r) // 2
	count1 = 0
	for i in range(l, A + 1):
		if arr[i] != chr(ord('a') + c):
			count1 += 1
	count2 = 0
	for i in range(A + 1, r + 1):
		if arr[i] != chr(ord('a') + c):
			count2 += 1
	A = min(rec(arr, mapp, l, A, c + 1) + count2, rec(arr, mapp, A + 1, r, c + 1) + count1)
	mapp[l, r] = A
	return A
T = int(input())
for _ in range(T):
	n = int(input())
	Str = input()
	mapp = {}
	print(rec(Str, mapp, 0, n - 1, 0))


from collections import defaultdict
import sys, math
f = None
try:
	f = open('q1.input', 'r')
except IOError:
	f = sys.stdin
if 'xrange' in dir(__builtins__):
	range = xrange
sys.setrecursionlimit(10 ** 6)

def print_case_iterable(case_num, iterable):
	print('Case #{}: {}'.format(case_num, ' '.join(map(str, iterable))))

def print_case_number(case_num, iterable):
	print('Case #{}: {}'.format(case_num, iterable))

def print_iterable(A):
	print(' '.join(A))

def read_int():
	return int(f.readline().strip())

def read_int_array():
	return [int(x) for x in f.readline().strip().split(' ')]

def rns():
	a = [x for x in f.readline().split(' ')]
	return (int(a[0]), a[1].strip())

def read_string():
	return list(f.readline().strip())

def ri():
	return int(f.readline().strip())

def ria():
	return [int(x) for x in f.readline().strip().split(' ')]

def rns():
	a = [x for x in f.readline().split(' ')]
	return (int(a[0]), a[1].strip())

def rs():
	return list(f.readline().strip())

def bi(x):
	return bin(x)[2:]
from collections import deque
import math
NUMBER = 10 ** 9 + 7

def factorial(n):
	M = NUMBER
	f = 1
	for i in range(1, n + 1):
		f = f * i % M
	return f

def mult(a, b):
	return a * b % NUMBER

def minus(a, b):
	return (a - b) % NUMBER

def plus(a, b):
	return (a + b) % NUMBER

def egcd(a, b):
	if a == 0:
		return (b, 0, 1)
	else:
		(g, y, x) = egcd(b % a, a)
		return (g, x - b // a * y, y)

def modinv(a):
	m = NUMBER
	(g, x, y) = egcd(a, m)
	if g != 1:
		raise Exception('modular inverse does not exist')
	else:
		return x % m

def choose(n, k):
	if n < k:
		assert false
	return mult(factorial(n), modinv(mult(factorial(k), factorial(n - k)))) % NUMBER
from collections import deque, defaultdict
import heapq
from types import GeneratorType

def bootstrap(f, stack=[]):

	def wrappedfunc(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to
	return wrappedfunc

def dfs(g, timeIn, timeOut, depths, parents):
	cnt = 0
	stack = [[1, 0, 0, 0]]
	while stack:
		(v, neig_i, parent, depth) = stack[-1]
		parents[v] = parent
		depths[v] = depth
		if neig_i == 0:
			timeIn[v] = cnt
			cnt += 1
		while neig_i < len(g[v]):
			u = g[v][neig_i]
			if u == parent:
				neig_i += 1
				continue
			stack[-1][1] = neig_i + 1
			stack.append([u, 0, v, depth + 1])
			break
		if neig_i == len(g[v]):
			stack.pop()
			timeOut[v] = cnt
			cnt += 1
cnt = 0

@bootstrap
def dfs(v, adj, timeIn, timeOut, depths, parents, parent=0, depth=0):
	global cnt
	parents[v] = parent
	depths[v] = depth
	timeIn[v] = cnt
	cnt += 1
	for u in adj[v]:
		if u == parent:
			continue
		yield dfs(u, adj, timeIn, timeOut, depths, parents, v, depth + 1)
	timeOut[v] = cnt
	cnt += 1
	yield

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def lcm(a, b):
	return a * b / gcd(a, b)

def get_num_2_5(n):
	twos = 0
	fives = 0
	while n > 0 and n % 2 == 0:
		n //= 2
		twos += 1
	while n > 0 and n % 5 == 0:
		n //= 5
		fives += 1
	return (twos, fives)

def shift(a, i, num):
	for _ in range(num):
		(a[i], a[i + 1], a[i + 2]) = (a[i + 2], a[i], a[i + 1])

def equal(x, y):
	return abs(x - y) <= 1e-09

def getAngle(a, b, c):
	ang = math.degrees(math.atan2(c[1] - b[1], c[0] - b[0]) - math.atan2(a[1] - b[1], a[0] - b[0]))
	return ang + 360 if ang < 0 else ang

def getLength(a, b):
	return math.sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2)
from heapq import heapify, heappush, heappop

def bfs(adj):
	s = set()
	d = defaultdict(set)
	q = deque([])
	s.add(0)
	d[0].add(0)
	q.append((0, 0))
	while len(q) > 0:
		(v, depth) = q.popleft()
		for u in adj[v]:
			if u not in s:
				s.add(u)
				d[depth + 1].add(u)
				q.append((u, depth + 1))
	return d

def dfs(u, top, adj, vis):
	vis[u] = 1
	for (v, d) in adj[u]:
		if vis[v] == 2 or d == 0:
			continue
		if vis[v] == 1:
			return 'No'
		if dfs(v, top, adj, vis) == 'No':
			return 'No'
	vis[u] = 2
	top.append(u)

def calc_half(s, c):
	return len(s) - s.count(c)

def solution(s, n, c='a'):
	if len(s) == 1:
		return int(s[0] != c)
	left = calc_half(s[:n // 2], c) + solution(s[n // 2:], n // 2, chr(ord(c) + 1))
	right = calc_half(s[n // 2:], c) + solution(s[:n // 2], n // 2, chr(ord(c) + 1))
	return min(left, right)

def main():
	T = 1
	T = ri()
	for i in range(T):
		n = ri()
		s = rs()
		x = solution(s, n)
		if 'xrange' not in dir(__builtins__):
			print(x)
		else:
			(print >> output, str(x))
	if 'xrange' in dir(__builtins__):
		print(output.getvalue())
		output.close()
if 'xrange' in dir(__builtins__):
	import cStringIO
	output = cStringIO.StringIO()
main()


t = int(input())

def compt(s, lettre):
	n = len(s)
	if n == 1:
		if s[0] == lettre:
			return 0
		else:
			return 1
	else:
		c1 = s[:n // 2].count(lettre)
		c2 = s[n // 2:].count(lettre)
		return min(n // 2 - c1 + compt(s[n // 2:], chr(ord(lettre) + 1)), n // 2 - c2 + compt(s[:n // 2], chr(ord(lettre) + 1)))
for case in range(t):
	n = int(input())
	t = input()
	print(compt(t, 'a'))


import sys
input = sys.stdin.readline
bin_s = [1]
alpha = 'abcdefghijklmnopqrstu'
for _ in range(17):
	bin_s.append(bin_s[-1] * 2)

def main():
	import itertools
	n = int(input())
	S = input().strip()
	if n == 1:
		if S == 'a':
			print(0)
		else:
			print(1)
		return
	k = bin_s.index(n)
	band = n // 2
	cnt = 2
	lst = []
	for i in range(k + 1):
		tmp = []
		for j in range(cnt):
			tmp.append(band - S[j * band:(j + 1) * band].count(alpha[i]))
		lst.append(tmp)
		if band != 1:
			band //= 2
			cnt *= 2
	ans = n
	for tf_s in itertools.product([True, False], repeat=k):
		ans_sub = 0
		pos = 0
		for (i, tf) in enumerate(tf_s):
			if i != k - 1:
				if tf:
					ans_sub += lst[i][pos + 1]
					pos *= 2
				else:
					ans_sub += lst[i][pos]
					pos += 1
					pos *= 2
			elif tf:
				ans_sub += lst[i][pos + 1] + lst[i + 1][pos]
			else:
				ans_sub += lst[i][pos] + lst[i + 1][pos + 1]
		ans = min(ans, ans_sub)
	print(ans)
for _ in range(int(input())):
	main()


import sys
from math import log

def num(l, r, d):
	mid = (l + r) // 2
	if d >= k:
		return 0 if list_s[l] == n2a[d] else 1
	count_l = len([0 for i in range(l, mid + 1) if list_s[i] != n2a[d]])
	count_r = len([0 for i in range(mid + 1, r + 1) if list_s[i] != n2a[d]])
	return min(num(l, mid, d + 1) + count_r, num(mid + 1, r, d + 1) + count_l)
a2n = {chr(97 + i): i for i in range(26)}
n2a = {i: chr(97 + i) for i in range(26)}
T = int(sys.stdin.readline())
for _ in range(T):
	n = int(sys.stdin.readline())
	list_s = sys.stdin.readline().strip()
	k = int(log(n, 2))
	print(num(0, n - 1, 0))


def make_equal(st, n, c):
	num = 0
	for i in range(n):
		if st[i] != c:
			num += 1
	return num

def find(st, n, c):
	if n == 1:
		if st[0] == c:
			return 0
		else:
			return 1
	return min(make_equal(st[0:n // 2], n // 2, c) + find(st[n // 2:], n // 2, chr(ord(c) + 1)), make_equal(st[n // 2:], n // 2, c) + find(st[0:n // 2], n // 2, chr(ord(c) + 1)))
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	print(find(s, n, 'a'))


def rec(s, let, L, R):
	if L == R:
		if s[L] == let:
			return 0
		return 1
	mid = (L + R) // 2
	mid2 = (L + R) // 2
	change = 0
	chan = 0
	for i in range(L, mid + 1):
		if s[i] != let:
			change += 1
	for i in range(mid2 + 1, R + 1):
		if s[i] != let:
			chan += 1
	cntl = rec(s, dic2[dic[let] + 1], mid + 1, R) + change
	cntr = rec(s, dic2[dic[let] + 1], L, mid) + chan
	return min(cntl, cntr)

def rec2(s, let, L, R):
	if L == R:
		if s[L] != let:
			return 1
		return 0
	mid = (L + R) // 2
	chan = 0
	for i in range(mid + 1, R + 1):
		if s[i] != let:
			chan += 1
	return chan
for _ in range(int(input())):
	n = int(input())
	s = input()
	let = 'abcdefghijklmnopqrstuvwxyz'
	(dic, dic2) = ({}, {})
	for i in range(26):
		dic[let[i]] = i
		dic2[i] = let[i]
	print(rec(s, 'a', 0, n - 1))


import os
import sys
from io import BytesIO, IOBase

def recurse(s, c, n):
	if n == 1:
		if s == c:
			return 0
		else:
			return 1
	(lstr, rstr) = (s[:n // 2], s[n // 2:])
	(l, r) = (0, 0)
	for i in lstr:
		if i != c:
			l += 1
	for i in rstr:
		if i != c:
			r += 1
	nxt = chr(ord(c) + 1)
	left = recurse(rstr, nxt, n // 2)
	right = recurse(lstr, nxt, n // 2)
	return min(l + left, r + right)

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		s = input()
		print(recurse(s, 'a', n))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


t = int(input())

def string_change(s, character):
	(temp1, temp2) = (0, 0)
	if len(s) == 1 and s[0] == character:
		return 0
	if len(s) == 1 and s[0] != character:
		return 1
	for i in range(len(s) // 2):
		if s[i] == character:
			temp1 += 1
	for i in range(len(s) // 2, len(s)):
		if s[i] == character:
			temp2 += 1
	n = len(s)
	next_char = chr(ord(character) + 1)
	if temp1 > temp2:
		return min(n // 2 - temp1 + string_change(s[n // 2:], next_char), n // 2 - temp2 + string_change(s[:n // 2], next_char))
	elif temp2 > temp1:
		return min(n // 2 - temp1 + string_change(s[n // 2:], next_char), n // 2 - temp2 + string_change(s[:n // 2], next_char))
	else:
		return min(n // 2 - temp1 + string_change(s[n // 2:], next_char), n // 2 - temp2 + string_change(s[:n // 2], next_char))
for _ in range(t):
	n = int(input())
	s = str(input())
	print(string_change(s, 'a'))


from collections import defaultdict
from sys import setrecursionlimit, stdin, stdout
setrecursionlimit(10 ** 5)
el = list('abcdefghijklmnopqrstuvwxyz')
for _ in range(int(stdin.readline().strip())):
	n = int(stdin.readline().strip())
	s = list(stdin.readline().strip())

	def getans(s, char):
		n = len(s)
		if n == 1:
			if s[0] == el[char]:
				return 0
			else:
				return 1
		tot = n // 2
		sl = s[:tot]
		c1 = tot - sl.count(el[char])
		sr = s[tot:]
		c2 = tot - sr.count(el[char])
		return min(c1 + getans(sr, char + 1), c2 + getans(sl, char + 1))
	print(getans(s, 0))


t = int(input())
for x in range(t):
	n = input()
	s = input()

	def check(s, i):
		cnt = 0
		for j in s:
			if j != chr(ord('a') + i):
				cnt = cnt + 1
		return cnt

	def fun(s, i):
		mid = len(s) // 2
		if len(s) == 1 and s == chr(ord('a') + i):
			return 0
		elif len(s) == 1:
			return 1
		elif check(s[:mid], i) == 0:
			return fun(s[mid:], i + 1)
		elif check(s[mid:], i) == 0:
			return fun(s[:mid], i + 1)
		else:
			k = min(check(s[mid:], i) + fun(s[:mid], i + 1), check(s[:mid], i) + fun(s[mid:], i + 1))
			return k
	print(fun(s, 0))


def recursiveApproach(st, l, r, c):
	if r - l == 1:
		if st[l] == c:
			return 0
		else:
			return 1
	else:
		mid = (l + r) // 2
		firstType = 0
		for i in range(l, mid):
			if st[i] != c:
				firstType += 1
		secondType = 0
		for i in range(mid, r):
			if st[i] != c:
				secondType += 1
		return min(secondType + recursiveApproach(st, l, mid, chr(ord(c) + 1)), firstType + recursiveApproach(st, mid, r, chr(ord(c) + 1)))
t = int(input())
for i in range(t):
	n = int(input())
	st = input()
	print(recursiveApproach(st, 0, n, 'a'))


def dfs(l, r, n, s, letter):
	if n == 1:
		if s[l] != letter:
			return 1
		else:
			return 0
	m = l + (r - l) // 2
	cl = 0
	cr = 0
	for i in range(l, m + 1):
		if s[i] != letter:
			cl += 1
	for i in range(m + 1, r + 1):
		if s[i] != letter:
			cr += 1
	newletter = chr(ord(letter) + 1)
	return min(cl + dfs(m + 1, r, n // 2, s, newletter), cr + dfs(l, m, n // 2, s, newletter))

def solve():
	n = int(input())
	s = input()
	print(dfs(0, n - 1, n, s, 'a'))
t = int(input())
for _ in range(t):
	solve()


from sys import stdin, stdout

def a_good_string(n, s):
	ans = dfs(s, 0, n - 1, 'a')
	return ans

def dfs(s, b, e, c):
	if b == e:
		if s[b] == c:
			return 0
		else:
			return 1
	m = (b + e) // 2
	cnt1 = 0
	cnt2 = 0
	for i in range(b, m + 1):
		if s[i] != c:
			cnt1 += 1
	for i in range(m + 1, e + 1):
		if s[i] != c:
			cnt2 += 1
	ans1 = cnt1 + dfs(s, m + 1, e, chr(ord(c) + 1))
	ans2 = cnt2 + dfs(s, b, m, chr(ord(c) + 1))
	return min(ans1, ans2)
t = int(stdin.readline())
for i in range(t):
	n = int(stdin.readline())
	s = stdin.readline().strip()
	ans = a_good_string(n, s)
	stdout.write(str(ans) + '\n')


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	s = input()

	def getVal(low, high, ch):
		if low == high:
			return int(s[low] != chr(ch + 97))
		half = high - low + 1 >> 1
		mid = low + half
		return min(getVal(low, mid - 1, ch + 1) + half - s[mid:high + 1].count(chr(97 + ch)), getVal(mid, high, ch + 1) + half - s[low:mid].count(chr(ch + 97)))
	print(getVal(0, n - 1, 0))


def solve(string, character):
	long = len(string)
	if long == 1:
		if chr(character) == string:
			return 0
		return 1
	if long == 2:
		(q1, q2) = (chr(character), chr(character + 1))
		(a, b) = (string[0], string[1])
		if q1 == a and q2 == b or (q1 == b and q2 == a):
			return 0
		if q1 == a or q1 == b or q2 == a or (q2 == b):
			return 1
		return 2
	dp1 = long // 2 - string[:long // 2].count(chr(character)) + solve(string[long // 2:], character + 1)
	dp2 = long // 2 - string[long // 2:].count(chr(character)) + solve(string[:long // 2], character + 1)
	return min(dp1, dp2)
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(solve(s, ord('a')))


def best(s, c, l, r):
	if l + 1 == r:
		return int(s[l] != c)
	half = (r - l) // 2
	cost_l = half - s[l:l + half].count(c) + best(s, c + 1, l + half, r)
	cost_r = half - s[l + half:r].count(c) + best(s, c + 1, l, l + half)
	return min(cost_l, cost_r)
t = int(input())
for _ in range(t):
	n = int(input())
	s = [ord(c) - ord('a') for c in input()]
	print(best(s, 0, 0, n))


def function(length, s, l):
	if length == 1:
		if l[0] != s:
			return 1
		else:
			return 0
	else:
		length = int(length / 2)
		left = length - l[0:length].count(s) + function(length, chr(ord(s) + 1), l[length:2 * length])
		right = length - l[length:2 * length].count(s) + function(length, chr(ord(s) + 1), l[0:length])
		return min(left, right)
for _ in range(int(input())):
	str_length = int(input())
	string = input()
	ans = function(str_length, 'a', string)
	print(ans)


CASES = []

def compute_min_cost(case, match='a'):
	s = case
	if len(s) != 1:
		m = int(len(s) / 2)
		l = compute_min_cost(s[:m], chr(ord(match) + 1))
		l += m - s[m:].count(match)
		r = compute_min_cost(s[m:], chr(ord(match) + 1))
		r += m - s[:m].count(match)
		return min(l, r)
	else:
		if s == match:
			return 0
		return 1

def process():
	[print(compute_min_cost(case)) for case in CASES]

def entrypoint():
	t = int(input())
	for _ in range(t):
		_ = int(input())
		string = input()
		CASES.append(string)

def main():
	entrypoint()
	process()
main()


def dfs(arr, c):
	if len(arr) == 1:
		if arr[0] == c:
			return 0
		return 1
	pr = 0
	po = 0
	mid = len(arr) // 2
	for i in range(len(arr)):
		if arr[i] == c:
			if i < mid:
				pr += 1
			else:
				po += 1
	nex = chr(ord(c) + 1)
	x1 = len(arr) // 2 - po + dfs(arr[:mid], nex)
	x2 = len(arr) // 2 - pr + dfs(arr[mid:], nex)
	return min(x1, x2)
T = int(input())
while T > 0:
	n = int(input())
	arr = input()
	res = dfs(arr, 'a')
	print(res)
	T -= 1


def f(s, symb):
	if len(s) == 1:
		return int(s[0] != symb)
	f1 = f2 = 0
	c1 = ''
	c2 = ''
	for i in range(len(s) // 2):
		f1 += int(s[i] != symb)
		c1 += s[i]
	for i in range(len(s) // 2, len(s)):
		f2 += int(s[i] != symb)
		c2 += s[i]
	ns = chr(ord(symb) + 1)
	return min(f1 + f(c2, ns), f2 + f(c1, ns))
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(f(s, 'a'))


for _ in range(0, int(input())):
	n = int(input())
	s = input()

	def call(l, r, ch):
		if l == r:
			return (s[l] != ch) + 0
		c = 0
		for i in range(l, (l + r + 1) // 2):
			if s[i] != ch:
				c += 1
		left = c + call((l + r + 1) // 2, r, chr(ord(ch) + 1))
		c = 0
		for i in range(r, (l + r) // 2, -1):
			if s[i] != ch:
				c += 1
		right = c + call(l, (l + r) // 2, chr(ord(ch) + 1))
		return min(left, right)
	print(call(0, n - 1, 'a'))


from sys import stdin, setrecursionlimit
setrecursionlimit(100000)
input = stdin.readline

def solve(s, c):
	if len(s) == 1:
		if s[0] == chr(c + ord('a')):
			return 0
		else:
			return 1
	current = 0
	for i in range(len(s) // 2):
		if s[i] != chr(c + ord('a')):
			current += 1
	a = solve(s[len(s) // 2:], c + 1) + current
	current = 0
	for i in range(len(s) // 2, len(s)):
		if s[i] != chr(c + ord('a')):
			current += 1
	b = solve(s[:len(s) // 2], c + 1) + current
	return min(a, b)
t = int(input())
for _ in range(t):
	n = int(input())
	s = input().rstrip()
	ans = solve(s, 0)
	print(ans)


def solve(s, ans, c):
	if len(s) == 1:
		if s[0] != c:
			return ans + 1
		return ans
	cnt1 = 0
	cnt2 = 0
	for i in range(0, len(s) // 2):
		if s[i] != c:
			cnt1 += 1
	for i in range(len(s) // 2, len(s)):
		if s[i] != c:
			cnt2 += 1
	x = solve(s[:len(s) // 2], ans + cnt2, chr(ord(c) + 1))
	y = solve(s[len(s) // 2:], ans + cnt1, chr(ord(c) + 1))
	return min(x, y)
t = int(input())
while t > 0:
	n = int(input())
	s = input()
	print(solve(s, 0, 'a'))
	t -= 1


import bisect
import decimal
from decimal import Decimal
import os
from collections import Counter
import bisect
from collections import defaultdict
import math
import random
import heapq
from math import sqrt
import sys
from functools import reduce, cmp_to_key
from collections import deque
import threading
from itertools import combinations
from io import BytesIO, IOBase
from itertools import accumulate
mod = 10 ** 9 + 7
decimal.getcontext().prec = 46

def primeFactors(n):
	prime = set()
	while n % 2 == 0:
		prime.add(2)
		n = n // 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			prime.add(i)
			n = n // i
	if n > 2:
		prime.add(n)
	return list(prime)

def getFactors(n):
	factors = []
	i = 1
	while i <= math.sqrt(n):
		if n % i == 0:
			if n // i == i:
				factors.append(i)
			else:
				factors.append(i)
				factors.append(n // i)
		i = i + 1
	return factors

def modefiedSieve():
	mx = 10 ** 7 + 1
	sieve = [-1] * mx
	for i in range(2, mx):
		if sieve[i] == -1:
			sieve[i] = i
			for j in range(i * i, mx, i):
				if sieve[j] == -1:
					sieve[j] = i
	return sieve

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	num = []
	for p in range(2, n + 1):
		if prime[p]:
			num.append(p)
	return num

def lcm(a, b):
	return a * b // math.gcd(a, b)

def sort_dict(key_value):
	return sorted(key_value.items(), key=lambda kv: (kv[1], kv[0]), reverse=True)

def list_input():
	return list(map(int, input().split()))

def num_input():
	return map(int, input().split())

def string_list():
	return list(input())

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def binaryToDecimal(n):
	return int(n, 2)

def DFS(n, s, adj):
	visited = [False for i in range(n + 1)]
	stack = []
	stack.append(s)
	while len(stack):
		s = stack[-1]
		stack.pop()
		if not visited[s]:
			visited[s] = True
		for node in adj[s]:
			if not visited[node]:
				stack.append(node)

def maxSubArraySum(a, size):
	max_so_far = -sys.maxsize - 1
	max_ending_here = 0
	start = 0
	end = 0
	s = 0
	for i in range(0, size):
		max_ending_here += a[i]
		if max_so_far < max_ending_here:
			max_so_far = max_ending_here
			start = s
			end = i
		if max_ending_here < 0:
			max_ending_here = 0
			s = i + 1
	return (max_so_far, start, end)

def lis(arr):
	n = len(arr)
	lis = [1] * n
	for i in range(1, n):
		for j in range(0, i):
			if arr[i] >= arr[j] and lis[i] < lis[j] + 1:
				lis[i] = lis[j] + 1
	maximum = 0
	for i in range(n):
		maximum = max(maximum, lis[i])
	return maximum

def compute(s, ch):
	if len(s) == 1:
		if s[0] == ch:
			return 0
		return 1
	cost_1 = 0
	for i in range(len(s) // 2):
		if s[i] != ch:
			cost_1 += 1
	cost_2 = 0
	for i in range(len(s) // 2, len(s)):
		if s[i] != ch:
			cost_2 += 1
	return min(cost_1 + compute(s[len(s) // 2:], chr(ord(ch) + 1)), compute(s[0:len(s) // 2], chr(ord(ch) + 1)) + cost_2)

def solve():
	n = int(input())
	s = input()
	print(compute(s, 'a'))
t = 1
t = int(input())
for _ in range(t):
	solve()


from sys import stdin, stdout

def input():
	return stdin.readline().strip()
T = int(input())

def count_diff(c, S):
	c = chr(c + 97)
	ret = 0
	for s in S:
		if c != s:
			ret += 1
	return ret
memo = None

def ans(c, S):
	global memo
	if len(S) == 1:
		if S == chr(c + 97):
			return 0
		else:
			return 1
	if (c, S) in memo:
		return memo[c, S]
	mid = len(S) // 2
	left = S[0:mid]
	right = S[mid:]
	ret = min(count_diff(c, left) + ans(c + 1, right), ans(c + 1, left) + count_diff(c, right))
	memo[c, S] = ret
	return ret
for _ in range(T):
	N = int(input())
	S = input()
	memo = dict()
	print(ans(0, S))


def find_min_moves(s, aux):
	if len(s) == 1:
		if chr(aux) != s[0]:
			return 1
		else:
			return 0
	m = len(s) // 2
	l = m - s[:m].count(chr(aux)) + find_min_moves(s[m:], aux + 1)
	r = m - s[m:].count(chr(aux)) + find_min_moves(s[:m], aux + 1)
	return min(l, r)
t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	min_moves = find_min_moves(s, ord('a'))
	print(min_moves)


import os
import sys
from io import BytesIO, IOBase
input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return list(s[:len(s) - 1])

def invr():
	return map(int, input().split())

def calc(l, r, c, s):
	mid = (l + r) // 2
	if r - l == 1:
		if s[l] != chr(c):
			return 1
		else:
			return 0
	cl = (r - l) // 2 - s[l:mid].count(chr(c)) + calc(mid, r, c + 1, s)
	cr = (r - l) // 2 - s[mid:r].count(chr(c)) + calc(l, mid, c + 1, s)
	return min(cl, cr)

def main():
	try:
		for _ in range(inp()):
			n = inp()
			s = input()
			print(calc(0, n, 97, s))
	except Exception as e:
		print(e)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def count(s, c):
	ans = 0
	for i in range(len(s)):
		if s[i] == c:
			ans += 1
	return ans

def calcMin(s, l, r, c):
	if r - l == 1:
		if s[l] != c:
			return 1
		else:
			return 0
	mid = int((l + r) / 2)
	ansLeft = (r - l) / 2 - count(s[l:mid], c) + calcMin(s, mid, r, chr(ord(c) + 1))
	ansRight = calcMin(s, l, mid, chr(ord(c) + 1)) + (r - l) / 2 - count(s[mid:r], c)
	return int(min(ansLeft, ansRight))
t = int(input())
n_list = [0 for i in range(t)]
str_list = [[''] for i in range(t)]
for i in range(t):
	n_list[i] = int(input())
	str_list[i] = input()
output_list = [0 for i in range(t)]
for i in range(t):
	output_list[i] = calcMin(str_list[i], 0, n_list[i], 'a')
for i in range(t):
	print(output_list[i])


def aux(input_s, const):
	if len(input_s) == 1:
		return int(input_s != const)
	mid = len(input_s) // 2
	fps = input_s[:mid]
	sps = input_s[mid:]
	const_1 = chr(ord(const) + 1)
	first = aux(fps, const_1)
	first_result = 0
	for l in sps:
		if l == const:
			first_result += 1
	first += len(input_s) // 2 - first_result
	second = aux(sps, const_1)
	second_result = 0
	for l in fps:
		if l == const:
			second_result += 1
	second += len(input_s) // 2 - second_result
	return min(first, second)
input_n = int(input())
for i in range(input_n):
	n = int(input())
	input_s = input()
	print(aux(input_s, 'a'))


from sys import stdin, stdout, setrecursionlimit

def getCost(s, l, h, ch):
	cnt = 0
	for i in range(l, h + 1):
		if s[i] != ch:
			cnt += 1
	return cnt

def getMin(s, l, h, ch):
	if l == h:
		if s[l] == ch:
			return 0
		return 1
	mid = (l + h) // 2
	return min(getCost(s, l, mid, ch) + getMin(s, mid + 1, h, chr(ord(ch) + 1)), getCost(s, mid + 1, h, ch) + getMin(s, l, mid, chr(ord(ch) + 1)))
t = int(stdin.readline())
for _ in range(t):
	n = int(stdin.readline())
	s = stdin.readline()
	print(getMin(s, 0, n - 1, 'a'))


def main():

	def rec(s, d, n):
		tgt = chr(97 + d)
		if n == 0:
			return 0 if s == tgt else 1
		cnt = 0
		for c in s[:n]:
			cnt += c != tgt
		ret = cnt + rec(s[n:], d + 1, n // 2)
		cnt = 0
		for c in s[n:]:
			cnt += c != tgt
		ret = min(ret, cnt + rec(s[:n], d + 1, n // 2))
		return ret
	for __ in [0] * int(input()):
		N = int(input())
		S = input()
		print(rec(S, 0, N // 2))
main()


from sys import *

def num_good_string(s, c):
	if len(s) <= 0:
		return 0
	if len(s) == 1:
		if s[0] == c:
			return 0
		return 1
	middle_point = int(len(s) / 2)
	first_sec = cost_replace_char(s[:middle_point], c) + num_good_string(s[middle_point:], chr(ord(c) + 1))
	second_sec = num_good_string(s[:middle_point], chr(ord(c) + 1)) + cost_replace_char(s[middle_point:], c)
	return min(first_sec, second_sec)

def cost_replace_char(s, c):
	cost = 0
	for a in s:
		if a != c:
			cost += 1
	return cost

def solve_case(t):
	n = int(stdin.readline())
	s = stdin.readline().rstrip()
	result = num_good_string(s, 'a')
	print(result)

def main():
	T = int(stdin.readline())
	for t in range(T):
		solve_case(t)
main()


def f(s, c):
	n = len(s) // 2
	d = chr(c)
	return min((n - s.count(d, i, i + n) + f(s[n - i:n - i + n], c + 1) for i in (0, n))) if n else s[0] != d
for s in [*open(0)][2::2]:
	print(+f(s[:-1], 97))


infinity = 10 ** 9

def inc_letter(x: str):
	return chr(ord(x) + 1)

def test_case():
	n = int(input())
	s = str(input())
	s = list(s)

	def solve(l: int=0, r: int=n - 1, f: str='a'):
		if ord(f) > ord('z'):
			return infinity
		if l == r:
			return int(s[l] != f)
		mid = (l + r) // 2
		costL = 0
		for x in s[l:mid + 1]:
			costL += int(x != f)
		costL += solve(mid + 1, r, inc_letter(f))
		costR = 0
		for x in s[mid + 1:r + 1]:
			costR += int(x != f)
		costR += solve(l, mid, inc_letter(f))
		return min(costL, costR)
	print(solve())
test_cases = int(input())
while test_cases > 0:
	test_case()
	test_cases -= 1


from operator import le

def tinh(s, c):
	if len(s) == 1:
		if s[0] == c:
			return 0
		else:
			return 1
	mid = len(s) // 2
	left = s[0:mid]
	right = s[mid:len(s)]
	cntl = left.count(c)
	cntr = right.count(c)
	l = mid - cntl + tinh(right, chr(ord(c) + 1))
	r = mid - cntr + tinh(left, chr(ord(c) + 1))
	return min(l, r)
for t in range(int(input())):
	n = int(input())
	s = input()
	print(tinh(s, 'a'))


def fast(S):

	def dfs(left, right, plus):
		C = chr(ord('a') + plus)
		if left == right:
			if S[left] != C:
				return 1
			else:
				return 0
		mid = (left + right) // 2
		lhs = sum((1 if c != C else 0 for c in S[left:mid + 1]))
		rhs = sum((1 if c != C else 0 for c in S[mid + 1:right + 1]))
		return min(lhs + dfs(mid + 1, right, plus + 1), rhs + dfs(left, mid, plus + 1))
	result = dfs(0, len(S) - 1, 0)
	return result

def test():
	S = 'bbdcaaaa'
	f = fast(S)
	print(f)

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		S = input()
		result = fast(S)
		print(result)
main()


def getGood(left, right, ch, string):
	num = int(right - left)
	mid = int((left + right) / 2)
	changeL = 0
	changeR = 0
	if num == 1:
		return int(string[left] != ch)
	for i in range(num):
		if i < num / 2 and string[left + i] != ch:
			changeL += 1
		if i >= num / 2 and string[left + i] != ch:
			changeR += 1
	newCh = chr(ord(ch) + 1)
	return min(changeL + getGood(mid, right, newCh, string), changeR + getGood(left, mid, newCh, string))
times = input()
for i in range(int(times)):
	num = input()
	string = input()
	print(getGood(0, int(num), 'a', string))


from collections import defaultdict
lis = [chr(x) for x in range(ord('a'), ord('z') + 1)]

def go(l, r, length, char):
	if length == 1:
		if s[l] == lis[char]:
			return 0
		else:
			return 1
	count1 = count2 = 0
	mid = (l + r) // 2
	for i in range(l, mid + 1):
		if s[i] != lis[char]:
			count1 += 1
	count1 += go(mid + 1, r, r - mid, char + 1)
	for i in range(mid + 1, r + 1):
		if s[i] != lis[char]:
			count2 += 1
	count2 += go(l, mid, mid - l + 1, char + 1)
	return min(count1, count2)
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	print(go(0, n - 1, n, 0))


import os
import sys
from io import BytesIO, IOBase
import math
from decimal import Decimal
from decimal import *
from collections import defaultdict, deque
import heapq
getcontext().prec = 25
abcd = 'abcdefghijklmnopqrstuvwxyz'
MOD = pow(10, 9) + 7
BUFSIZE = 8192
from bisect import bisect_left, bisect_right

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def solve(s, start, end, c, v, idd):
	mid = (start + end) // 2
	c = chr(ord(c) + 1)
	if idd > n:
		x.append(v)
		return
	solve(s, start, mid, c, v + s[mid:end].count(c), idd * 2)
	solve(s, mid, end, c, v + s[start:mid].count(c), idd * 2)
for _ in range(int(input())):
	n = int(input())
	s = input()
	x = []
	v = 0
	idd = 1
	c = chr(96)
	solve(s, 0, n, c, v, idd)
	print(n - max(x))


from sys import stdin, stdout
from collections import Counter
from itertools import permutations
import bisect
import math
I = lambda : map(int, stdin.readline().split())
I1 = lambda : stdin.readline()

def fun(s, l, i, n):
	if n == 1:
		if s == l[i]:
			return 0
		else:
			return 1
	x = s[:n // 2]
	y = s[n // 2:]
	c1 = x.count(l[i])
	c2 = y.count(l[i])
	a = n // 2 - c1 + fun(y, l, i + 1, n // 2)
	b = n // 2 - c2 + fun(x, l, i + 1, n // 2)
	return min(a, b)
for _ in range(int(I1())):
	n = int(I1())
	s = I1().strip()
	l = 'abcdefghijklmnopqrstuvwxyz'
	r = fun(s, l, 0, n)
	print(r)


def calc(string, c):
	char = chr(c)
	if len(string) == 1:
		return 1 if string[0] != char else 0
	tot_chars = len(string) // 2
	cntl = tot_chars - string[0:tot_chars].count(char) + calc(string[tot_chars:], c + 1)
	cntr = tot_chars - string[tot_chars:].count(char) + calc(string[:tot_chars], c + 1)
	return min(cntl, cntr)
t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	print(calc(s, ord('a')))


def get(s, a):
	if len(s) == 1:
		return int(a != s)
	q = [0, 0]
	for i in range(len(s)):
		q[i * 2 // len(s)] += s[i] == a
	return min(len(s) // 2 - q[0] + get(s[len(s) // 2:], chr(ord(a) + 1)), len(s) // 2 - q[1] + get(s[:len(s) // 2], chr(ord(a) + 1)))
for _ in range(int(input())):
	n = input()
	print(get(input(), 'a'))


import sys
import math, bisect
from itertools import groupby, accumulate
from heapq import heapify, heappop, heappush
from collections import deque, Counter, OrderedDict

def neo():
	return map(int, input().split())

def Neo():
	return list(map(int, input().split()))

def fun(X, k):
	if len(X) == 0:
		return 0
	if len(X) == 1:
		return int(X[0] != k)
	m = len(X) // 2
	L = X[:m]
	R = X[m:]
	return min(m - L.count(k) + fun(R, chr(ord(k) + 1)), m - R.count(k) + fun(L, chr(ord(k) + 1)))
for _ in range(int(input())):
	N = int(input())
	S = input()
	print(fun(S, 'a'))


t = int(input())
f = int

def makgood(st, ch, n):
	if n == 1:
		if st == ch:
			return 0
		else:
			return 1
	k = f(n / 2)
	a = st[:k].count(ch)
	b = st[k:].count(ch)
	nx = chr(ord(ch) + 1)
	c = min(k - b + makgood(st[:k], nx, k), k - a + makgood(st[k:], nx, k))
	return c
for _ in range(t):
	n = int(input())
	s = input()
	print(makgood(s, 'a', n))


from __future__ import division, print_function
import os
import sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def solve(t):
	from functools import lru_cache
	n = int(input())
	data = input()

	@lru_cache(None)
	def dp(i, j, a):
		if i == j:
			return 0 if data[i] == a else 1
		m = (j + i) // 2
		f_nums = 0
		for k in range(i, m + 1):
			if data[k] != a:
				f_nums += 1
		l_nums = 0
		for k in range(m + 1, j + 1):
			if data[k] != a:
				l_nums += 1
		return min(f_nums + dp(m + 1, j, chr(ord(a) + 1)), l_nums + dp(i, m, chr(ord(a) + 1)))
	return dp(0, n - 1, 'a')

def main():
	tc = int(input().strip())
	for t in range(1, tc + 1):
		print(solve(t))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
input = sys.stdin.readline

def solve(cnt, l, r):
	if r - l == 1:
		if s[l] == alph[cnt]:
			return 0
		return 1
	mid = (l + r) // 2
	tmp1 = solve(cnt + 1, l, mid) + (r - mid - s[mid:r].count(alph[cnt]))
	tmp2 = solve(cnt + 1, mid, r) + (mid - l - s[l:mid].count(alph[cnt]))
	return min(tmp1, tmp2)
t = int(input())
INF = 10 ** 18
alph = 'abcdefghijklmnopqrstuvwxyz'
for _ in range(t):
	n = int(input())
	s = input()[:-1]
	print(solve(0, 0, n))


from operator import sub

def main():
	n_tests = int(input())
	return '\n'.join((nMoves() for i in range(n_tests)))
letters = 'abcdefghijklmnopqr'
log2 = {1: 0, 2: 1, 4: 2, 8: 3, 16: 4, 32: 5, 64: 6, 128: 7, 256: 8, 512: 9, 1024: 10, 2048: 11, 4096: 12, 8192: 13, 16384: 14, 32768: 15, 65536: 16, 131072: 17}
memo = {1: ((0, 1), (1, 0))}

def nMoves():
	input()
	badString = input()
	if len(badString) == 1:
		return '1' if badString != 'a' else '0'
	return str(sub(len(badString), analyze(badString, 0, 0, i_l=len(badString))))

def analyze(string, n, i_f, i_l):
	n_1 = n + 1
	if i_f + 2 == i_l:
		return max((string[i_f] == letters[n]) + (string[i_f + 1] == letters[n_1]), (string[i_f] == letters[n_1]) + (string[i_f + 1] == letters[n]))
	i_m = (i_f + i_l) // 2
	return max(string[i_f:i_m].count(letters[n]) + analyze(string, n_1, i_m, i_l), string[i_m:i_l].count(letters[n]) + analyze(string, n_1, i_f, i_m))
print(main())


t = int(input())

def func(a, b, c):
	if a == b:
		return int(s[a] != c)
	v = (b - a + 1) // 2
	v1 = 0
	for i in range(v):
		if s[a + i] != c:
			v1 += 1
	v2 = 0
	for i in range(v):
		if s[b - i] != c:
			v2 += 1
	return min(v1 + func(a + v, b, chr(ord(c) + 1)), v2 + func(a, b - v, chr(ord(c) + 1)))
for i in range(t):
	n = int(input())
	s = input()
	print(func(0, n - 1, 'a'))


import sys
inp = sys.stdin.read().split()
ii = 0
t = int(inp[0])
ii += 1
for _ in range(t):
	n = int(inp[ii])
	ii += 1
	s = inp[ii]
	ii += 1
	if n == 1:
		if 'a' in s:
			print(0)
		else:
			print(1)
		continue
	stack = [[0, n, ord('a'), 0]]
	ans = n
	while stack:
		(l, r, c, cost) = stack[-1]
		stack.pop()
		if r - l == 1:
			if chr(c) in s[l:r]:
				ans = min(ans, cost)
			else:
				ans = min(ans, cost + 1)
			continue
		mid = (l + r) // 2
		stack.append([l, mid, c + 1, r - mid + cost - s[mid:r].count(chr(c))])
		stack.append([mid, r, c + 1, mid - l + cost - s[l:mid].count(chr(c))])
	print(ans)


import sys
input = sys.stdin.readline
t = int(input())
letter = 'abcdefghijklmnopqrstuvwxyz'

def good(n, s, c):
	if n == 1 and s[0] == letter[c]:
		return 0
	elif n == 1 and s[0] != letter[c]:
		return 1
	first = s[:n // 2]
	second = s[n // 2:n]
	a = n // 2 - first.count(letter[c]) + good(n // 2, second, c + 1)
	b = n // 2 - second.count(letter[c]) + good(n // 2, first, c + 1)
	return min(a, b)
for _ in range(t):
	n = int(input())
	s = list(input())[:n]
	ans = min(good(n, s, 0), good(n, s[::-1], 0))
	print(int(ans))


def mc(s, i=0, j=None, c='a'):
	if j is None:
		j = len(s) - 1
	if i == j:
		return int(s[i] != c)
	mid = (i + j) // 2
	c1 = 0
	for idx in range(i, mid + 1):
		c1 += int(s[idx] != c)
	c1 += mc(s, mid + 1, j, chr(ord(c) + 1))
	c2 = 0
	for idx in range(mid + 1, j + 1):
		c2 += int(s[idx] != c)
	c2 += mc(s, i, mid, chr(ord(c) + 1))
	return min(c1, c2)
for _ in range(int(input())):
	input()
	print(mc(list(input())))


import sys
import math
from collections import defaultdict, Counter
input = sys.stdin.readline

def print(x):
	sys.stdout.write(str(x) + '\n')

def find(s1, v):
	l = len(s1)
	if l == 1:
		if s1 != chr(v):
			return 1
		return 0
	c1 = l // 2 - s1[:l // 2].count(chr(v))
	c2 = l // 2 - s1[l // 2:].count(chr(v))
	return min(c1 + find(s1[l // 2:], v + 1), c2 + find(s1[:l // 2], v + 1))
t = int(input())
for i in range(t):
	n = int(input())
	s = input().strip()
	ans = find(s, 97)
	print(ans)


def getcost(st, e, c):
	cnt = 0
	for i in range(st, e):
		if s[i] != c:
			cnt += 1
	return cnt

def getmin(start, end, ch):
	if start == end - 1:
		return 0 if s[start] == ch else 1
	mid = (start + end) // 2
	return min(getcost(start, mid, ch) + getmin(mid, end, chr(ord(ch) + 1)), getcost(mid, end, ch) + getmin(start, mid, chr(ord(ch) + 1)))
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	print(getmin(0, n, 'a'))


from sys import stdin
input = stdin.readline

def soln(s, i, j, r):
	if i == j:
		if s[i] == r:
			return 0
		else:
			return 1
	else:
		cost1 = 0
		for x in range(i, (i + j) // 2 + 1):
			if s[x] != r:
				cost1 += 1
		cost2 = 0
		for x in range((i + j) // 2 + 1, j + 1):
			if s[x] != r:
				cost2 += 1
		return min(cost1 + soln(s, (i + j) // 2 + 1, j, chr(ord(r) + 1)), cost2 + soln(s, i, (i + j) // 2, chr(ord(r) + 1)))
t = int(input())
for _ in range(t):
	n = int(input())
	s = input().rstrip()
	print(soln(s, 0, n - 1, 'a'))


from sys import stdin
input = stdin.readline
from collections import defaultdict

def solve(i, j, c, ans, mini=200000):
	if j == i:
		if s[i - 1] != c:
			ans += 1
		return min(ans, mini)
	first = i - 1 + (j - i + 1) // 2
	amt = (j - i + 1) // 2
	req = amt - (d[first][c] - d[i - 1][c])
	mini = min(mini, solve(first + 1, j, chr(ord(c) + 1), ans + req))
	req = amt - (d[j][c] - d[first][c])
	mini = min(mini, solve(i, first, chr(ord(c) + 1), ans + req))
	return mini
for _ in range(int(input())):
	n = int(input())
	s = input().strip()
	t = defaultdict(int)
	d = [t.copy()]
	for i in range(n):
		t[s[i]] += 1
		d.append(t.copy())
	res = solve(1, n, 'a', 0)
	print(res)


def good(s, left, c, length):
	if length == 1 and s[left] == c:
		return 0
	elif length == 1:
		return 1
	else:
		ul = left + length // 2
		(count_left, count_right, c) = (length // 2 - s[left:left + length // 2].count(c), length // 2 - s[ul:ul + length // 2].count(c), chr(ord(c) + 1))
		return min(good(s, left, c, length // 2) + count_right, good(s, ul, c, length // 2) + count_left)
t = int(input())
while t > 0:
	n = int(input())
	s = input()
	(left, ch) = (0, 'a')
	print(good(s, left, ch, n))
	t -= 1


def min_cost(s, start, size, c):
	if size == 1:
		if s[start] == c:
			return 0
		else:
			return 1
	new_size = size // 2
	a1 = new_size - s[start:start + new_size].count(c) + min_cost(s, start + new_size, new_size, chr(ord(c) + 1))
	b1 = min_cost(s, start, new_size, chr(ord(c) + 1)) + new_size - s[start + new_size:start + 2 * new_size].count(c)
	return min(a1, b1)
t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	ans = min_cost(s, 0, n, 'a')
	print(ans)


def problem1():
	(x, y, z) = sorted(list(map(int, input().split())))
	if y != z:
		print('NO')
		return
	print('YES')
	print(y, x, x)

def problem2():
	n = int(input())
	l = list(map(int, input().split()))
	s = set(l)
	d = {}
	for i in s:
		d[i] = 0
	a = []
	for i in l:
		if d[i] == 0:
			a.append(i)
			d[i] += 1
	print(*a)

def problem3():
	n = int(input())
	l = list(map(int, input().split()))
	l.append(-1)
	i = n - 1
	while i >= 0 and l[i] >= l[i + 1]:
		i -= 1
	while i >= 0 and l[i] <= l[i + 1]:
		i -= 1
	print(i + 1)

def p4r(l, c, n):
	if n == 1:
		return 0 if l[0] == c else 1
	a = p4r(l[n // 2:], chr(ord(c) + 1), n // 2) - l[:n // 2].count(c)
	b = p4r(l[:n // 2], chr(ord(c) + 1), n // 2) - l[n // 2:].count(c)
	return n // 2 + min(a, b)

def problem4():
	n = int(input())
	l = input().strip()
	print(p4r(l, 'a', n))
cases = int(input())
for _ in range(cases):
	problem4()


def moves(s, l, r, p):
	count = 0
	for i in range(l, r):
		if s[i] != p:
			count += 1
	return count

def mkgd(s, l, r, p):
	if abs(l - r) == 1:
		if s[l] == p:
			return 0
		else:
			return 1
	mid = (l + r) // 2
	return min(moves(s, l, mid, p) + mkgd(s, mid, r, chr(ord(p) + 1)), moves(s, mid, r, p) + mkgd(s, l, mid, chr(ord(p) + 1)))
for _ in range(int(input())):
	n = int(input())
	s = str(input())
	ans = mkgd(s, 0, n, 'a')
	print(ans)


def f(s, ch='a'):
	l = len(s)
	if l == 1:
		return 0 if s == ch else 1
	char = chr(ord(ch) + 1)
	return min(f(s[:l // 2], char) + sum(map(lambda x: 0 if x == ch else 1, s[l // 2:])), f(s[l // 2:], char) + sum(map(lambda x: 0 if x == ch else 1, s[:l // 2])))
for item in range(int(input())):
	n = int(input())
	s = input()
	print(f(s))


from functools import lru_cache
import sys
sys.setrecursionlimit(10 ** 7)
for _ in range(int(input())):
	n = int(input())
	s = input()

	@lru_cache(maxsize=None)
	def co(l, r, ch):
		return r - l - s[l:r].count(ch)

	@lru_cache(maxsize=None)
	def f(i, l, r):
		ch = chr(97 + i)
		if r - l == 1:
			if ch == s[l]:
				return 0
			else:
				return 1
		mid = l + (r - l) // 2
		return min(co(l, mid, ch) + f(i + 1, mid, r), co(mid, r, ch) + f(i + 1, l, mid))
	print(f(0, 0, n))


import sys
input = sys.stdin.readline

def counter(l, r, ch):
	count = 0
	for i in range(l, r + 1):
		if s[i] != ch:
			count += 1
	return count

def calc(l, r, code):
	letter = chr(97 + code)
	while l >= r:
		return int(s[l] != letter)
	mid = (l + r) // 2
	return min(counter(l, mid, letter) + calc(mid + 1, r, code + 1), counter(mid + 1, r, letter) + calc(l, mid, code + 1))
t = int(input())
while t:
	t -= 1
	n = int(input())
	s = input().strip()
	ans = calc(0, n - 1, 0)
	print(ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
testcases = int(input())
for j in range(testcases):
	n = int(input())
	vals = [ord(k) - 97 for k in input()]
	cv = 0
	moves = 0
	allmoves = set([])

	def divncon(seg, cv, moves, allmoves):
		if len(seg) == 1:
			if seg[0] != cv:
				allmoves.add(moves + 1)
				return allmoves
			else:
				allmoves.add(moves)
				return allmoves
		else:
			movel = moves
			mover = moves
			left = seg[:len(seg) // 2]
			right = seg[len(seg) // 2:len(seg)]
			for s in range(len(seg) // 2):
				if left[s] != cv:
					movel += 1
				if right[s] != cv:
					mover += 1
			divncon(left, cv + 1, mover, allmoves)
			divncon(right, cv + 1, movel, allmoves)
			return allmoves
	moves = divncon(vals, 0, moves, allmoves)
	print(min(moves))


def create_cases(data1, data2):
	cases = []
	answers = []
	data1 = data1.split('\n')[1:]
	data2 = data2.split('\n')
	for i in data1:
		if len(i) == 1:
			continue
		x = list(map(int, i.split()))
		cases.append(x)
	for i in data2:
		answers.append(list(map(int, i)))
	return (cases, answers)

def f(cases):
	answers = []
	for case in cases:
		answer = calc(case, 0)
		answers.append(answer)
	return answers
letters = 'abcdefghijklmnopqrstuvwxyz'

def calc(s, i):
	if len(s) == 1:
		if s == letters[i]:
			return 0
		return 1
	mid = len(s) // 2
	cntl = mid - s[mid:].count(letters[i]) + calc(s[:mid], i + 1)
	cntr = mid - s[:mid].count(letters[i]) + calc(s[mid:], i + 1)
	return min(cntl, cntr)
n = int(input())
cases = []
while n > 0:
	input()
	cases.append(input())
	n -= 1
answers = f(cases)
for answer in answers:
	print(answer)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time
sys.setrecursionlimit(5 * 10 ** 5)
inf = 10 ** 20
mod = 10 ** 9 + 7

def LI():
	return list(map(int, input().split()))

def II():
	return int(input())

def LS():
	return list(input().split())

def S():
	return input()

def dfs(s, x):
	if len(s) == 1:
		return 0 if s == x else 1
	n = len(s) // 2
	s_first = s[:n]
	s_second = s[n:]
	nx = chr(ord('a') + (ord(x) - ord('a') + 1) % 26)
	ans_first = n - s_first.count(x) + dfs(s_second, nx)
	ans_second = n - s_second.count(x) + dfs(s_first, nx)
	return min(ans_first, ans_second)

def solve():
	n = II()
	s = S()
	return dfs(s, 'a')

def main():
	t = II()
	res = list()
	for _ in range(t):
		res.append(solve())
	for ans in res:
		print(ans)
	return 0
main()


def f(l, r, c):
	global s
	global p
	idx = ord(c) - ord('a')
	n = r - l
	if n == 1:
		return 1 - int(s[l - 1] == c)
	return min(n // 2 - (p[l - 1 + n // 2][idx] - p[l - 1][idx]) + f(r - n // 2, r, chr(ord(c) + 1)), f(l, l + n // 2, chr(ord(c) + 1)) + n // 2 - (p[r - 1][idx] - p[r - 1 - n // 2][idx]))
for _ in range(int(input())):
	n = int(input())
	s = input()
	p = [[0] * 26 for i in range(n + 1)]
	for i in range(1, n + 1):
		for letter in range(26):
			p[i][letter] = p[i - 1][letter]
		p[i][ord(s[i - 1]) - ord('a')] += 1
	print(f(1, n + 1, 'a'))


def recur(s, n, ch):
	if n == 1:
		return 0 if s == ch else 1
	firsthalf = 0
	secondhalf = 0
	for c in s[:n // 2]:
		if c == ch:
			firsthalf += 1
	for c in s[n // 2:]:
		if c == ch:
			secondhalf += 1
	return min(n // 2 - secondhalf + recur(s[:n // 2], n // 2, chr(ord(ch) + 1)), n // 2 - firsthalf + recur(s[n // 2:], n // 2, chr(ord(ch) + 1)))
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(recur(s, n, 'a'))


def make_good(s, c):
	if len(s) == 1:
		return int(s[0] != c)
	mid = len(s) // 2
	ans = len(s)
	for (x, y, z, t) in [(0, mid, mid, len(s)), (mid, len(s), 0, mid)]:
		cur_ans = 0
		for i in s[x:y]:
			cur_ans += int(i != c)
		cur_ans += make_good(s[z:t], chr(ord(c) + 1))
		ans = min(ans, cur_ans)
	return ans
for testcase in range(int(input())):
	n = int(input())
	s = input().strip()
	print(make_good(s, 'a'))


def freq(s, c):
	count = 0
	for i in s:
		if i == c:
			count += 1
	return count

def good(s, c):
	if len(s) == 1:
		if s != c:
			return 1
		return 0
	mid = int(len(s) / 2)
	left = good(s[:mid], chr(ord(c) + 1)) + int(len(s) / 2 - freq(s[mid:], c))
	right = good(s[mid:], chr(ord(c) + 1)) + int(len(s) / 2 - freq(s[:mid], c))
	return min(left, right)
t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	s = input()
	print(good(s, 'a'))


from sys import stdout
from sys import stdin

def get():
	return stdin.readline().strip()

def getf():
	return [int(i) for i in get().split()]

def put(a, end='\n'):
	stdout.write(str(a) + end)

def putf(a, sep=' ', end='\n'):
	stdout.write(sep.join(map(str, a)) + end)

def dist(a, b, n):
	cnt = 0
	for i in range(n):
		if a[i] != b[i]:
			cnt += 1
	return cnt

def rec(s, n, c, cost):
	if n == 1:
		return cost + dist(s, chr(c), 1)
	(s1, s2) = (s[:n // 2], s[n // 2:])
	t = chr(c) * (n // 2)
	return min(rec(s2, n // 2, c + 1, cost + dist(s1, t, n // 2)), rec(s1, n // 2, c + 1, cost + dist(s2, t, n // 2)))

def solve(n, s):
	return rec(s, n, 97, 0)

def main():
	t = int(get())
	for i in range(t):
		(n, s) = (int(get()), get())
		put(solve(n, s))
main()


import copy

def next(ch):
	return chr(ord(ch) + 1)

def solve(f, ch, a, low, high):
	ans = 0
	if low < high:
		mid = low + (high - low) // 2
		if low == mid:
			if a[low] != ch:
				ans += 1
			return ans
		if f:
			f = 0
			i = low
			while i < mid:
				if a[i] != ch:
					ans += 1
				i += 1
			low = mid
			ch = next(ch)
		else:
			f = 1
			i = high - 1
			while i >= mid:
				if a[i] != ch:
					ans += 1
				i -= 1
			high = mid
			ch = next(ch)
		return ans + min(solve(0, ch, a, low, high), solve(1, ch, a, low, high))
	else:
		return 0
for _ in range(int(input())):
	n = int(input())
	s = list(input())
	ans1 = solve(1, 'a', s, 0, n)
	ans2 = solve(0, 'a', s, 0, n)
	print(min(ans1, ans2))


import os
import heapq
import sys
import math
import operator
from collections import defaultdict
from io import BytesIO, IOBase
sys.setrecursionlimit(10 ** 5)

def inpt():
	return [int(k) for k in input().split()]

def fun(l, r, ct, ar, s):
	if r - l == 1:
		c1 = chr(ct + 97)
		c2 = chr(ct + 97 + 1)
		if s[l - 1] == c1 and s[r - 1] == c2 or (s[l - 1] == c2 and s[r - 1] == c1):
			return 0
		if (s[l - 1] == c1 or s[r - 1] == c1) or (s[l - 1] == c2 or s[r - 1] == c2):
			return 1
		return 2
	mt = (l + r) // 2
	ans1 = mt - l - (ar[ct][mt] - ar[ct][l - 1]) + 1
	ans2 = r - mt - (ar[ct][r] - ar[ct][mt])
	v1 = fun(l, mt, ct + 1, ar, s)
	v2 = fun(mt + 1, r, ct + 1, ar, s)
	return min(ans1 + v2, ans2 + v1)

def main():
	for _ in range(int(input())):
		n = int(input())
		s = input()
		if n == 1:
			if s[0] == 'a':
				print(0)
			else:
				print(1)
			continue
		ar = [[0] * (n + 1) for i in range(26)]
		for i in range(n):
			ar[ord(s[i]) - 97][i + 1] += 1
			for j in range(26):
				ar[j][i + 1] += ar[j][i]
		print(fun(1, n, 0, ar, s))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def f(x, z):
	if len(x) == 0:
		return 0
	if len(x) == 1:
		return int(x != z)
	m = len(x) // 2
	L = x[:m]
	R = x[m:]
	a = m - L.count(z) + f(R, chr(ord(z) + 1))
	b = m - R.count(z) + f(L, chr(ord(z) + 1))
	return min(a, b)
t = int(input())
while t:
	s = int(input())
	word = input()
	print(f(word, 'a'))
	t -= 1


def cnt(s, c):
	x = 0
	for i in s:
		if i != c:
			x += 1
	return x

def search(s, n, cho):
	c = chr(cho)
	if n == 1:
		return 0 if s[0] == c else 1
	m = n // 2
	s1 = s[:m]
	s2 = s[m:]
	x = cnt(s1, c) + search(s2, m, cho + 1)
	y = search(s1, m, cho + 1) + cnt(s2, c)
	return min(x, y)
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	l = 0
	r = n
	moves = 0
	cho = ord('a')
	print(search(s, n, cho))
	continue
	while r - l > 1:
		ch = chr(cho)
		m = (l + r) // 2
		x = cnt(s, l, m, ch)
		y = cnt(s, m, r, ch)
		lng = m - l
		print(f'ch={ch}, l={l}, m={m}, r={r}, x={x}, y={y}')
		if x > y:
			moves += lng - x
			l = m
		else:
			moves += lng - y
			r = m
		cho += 1
		print(f'l={l}, moves={moves}, ch=>{chr(cho)}')
	else:
		if s[l] != chr(cho):
			moves += 1
	print(moves)


def go(arr, c):
	if len(arr) == 1:
		if arr[0] == c:
			return 0
		else:
			return 1
	(l, r) = (0, 0)
	n = len(arr)
	for i in range(0, n // 2):
		if arr[i] == c:
			l += 1
	for i in range(n // 2, n):
		if arr[i] == c:
			r += 1
	d = chr(ord(c) + 1)
	(b, a) = (r + (n // 2 - l), l + (n // 2 - r))
	return min(a + max(0, go(arr[:n // 2], d) - l), b + max(0, go(arr[n // 2:], d) - r))
n = int(input())
for i in range(n):
	n = int(input())
	arr = input()
	ans = go(arr, 'a')
	print(ans)


def dfs(s, c):
	if len(s) == 1:
		if s == c:
			return 0
		else:
			return 1
	else:
		mid = len(s) // 2
		return min(mid - s[:mid].count(c) + dfs(s[mid:], chr(ord(c) + 1)), mid - s[mid:].count(c) + dfs(s[:mid], chr(ord(c) + 1)))

def main():
	T = int(input())
	while T:
		n = int(input())
		s = str(input())
		ans = dfs(s, 'a')
		print(ans)
		T -= 1
main()


def g(s, i, j, c):
	ans = 0
	for i in range(i, j + 1):
		if s[i] != c:
			ans += 1
	return ans

def f(s, c, i, j):
	if j == i:
		if s[i] == c:
			return 0
		else:
			return 1
	mid = (i + j) // 2
	l = g(s, i, mid, c)
	r = g(s, mid + 1, j, c)
	return min(l + f(s, chr(1 + ord(c)), mid + 1, j), r + f(s, chr(1 + ord(c)), i, mid))
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	x = f(s, 'a', 0, n - 1)
	print(x)


def fun(st, c):
	le = len(st)
	if le == 1:
		if st[0] == c:
			return 0
		else:
			return 1
	xx = 0
	for i in st[0:le // 2]:
		if i == c:
			xx += 1
	xx = le // 2 - xx
	yy = 0
	for i in st[le // 2:le]:
		if i == c:
			yy += 1
	yy = le // 2 - yy
	ans = min(xx + fun(st[le // 2:], chr(ord(c) + 1)), yy + fun(st[0:le // 2], chr(ord(c) + 1)))
	return ans
for _ in range(int(input())):
	n = int(input())
	st = input()
	print(fun(st, 'a'))


import datetime
import collections
import statistics
import itertools

def is_prime(num):
	for i in range(2, int(math.sqrt(num)) + 1):
		if num % i == 0:
			return False
	return True

def input_list():
	ll = list(map(int, input().split(' ')))
	return ll

def recr(ch, lef, rig, ss):
	if lef == rig:
		if ss[lef] != ch:
			return 1
		else:
			return 0
	l = rig - lef + 1
	cnt = 0
	for i in range(lef, lef + l // 2):
		if ss[i] != ch:
			cnt += 1
	ret = cnt + recr(chr(ord(ch) + 1), lef + l // 2, rig, ss)
	cnt = 0
	for i in range(lef + l // 2, rig + 1):
		if ss[i] != ch:
			cnt += 1
	ret = min(ret, cnt + recr(chr(ord(ch) + 1), lef, lef + l // 2 - 1, ss))
	return ret
tc = int(input())
for _ in range(tc):
	n = int(input())
	ss = input()
	ch = 'a'
	print(recr(ch, 0, n - 1, ss))


def f(s, c):
	n = len(s)
	if n == 1:
		if s[0] == c:
			return 0
		else:
			return 1
	a = s[:n // 2].count(c)
	b = s[n // 2:].count(c)
	return min(n // 2 - a + f(s[n // 2:], c + 1), n // 2 - b + f(s[:n // 2], c + 1))

def f2(s):
	arr = []
	for i in s:
		arr.append(ord(i) - 96)
	return arr
for t in range(int(input())):
	n = int(input())
	s = str(input())
	arr = f2(s)
	print(f(arr, 1))


from sys import stdin

def good(l, s, e, c):
	n = e - s
	if n == 1:
		if l[s] == c:
			return 0
		else:
			return 1
	mid = (s + e) // 2
	left = 0
	right = 0
	for x in range(s, mid):
		if l[x] == c:
			left += 1
	for x in range(mid, e):
		if l[x] == c:
			right += 1
	return min(n // 2 - left + good(l, mid, e, c + 1), n // 2 - right + good(l, s, mid, c + 1))
for case in range(int(stdin.readline())):
	n = int(stdin.readline())
	s = list([ord(x) - 97 for x in stdin.readline().strip()])
	print(good(s, 0, n, 0))


def solve(s, idx, c):
	if len(s) == 1:
		if alpha[idx] != s[0]:
			return c + 1
		else:
			return c
	else:
		a1 = c
		a2 = c
		for i in range(len(s) // 2):
			if s[i] != alpha[idx]:
				a1 = a1 + 1
		j = len(s) - 1
		while j >= len(s) // 2:
			if s[j] != alpha[idx]:
				a2 = a2 + 1
			j = j - 1
		return min(solve(s[len(s) // 2:len(s)], idx + 1, a1), solve(s[0:len(s) // 2], idx + 1, a2))
alpha = []
x = 'abcdefghijklmnopqrstuvwxyz'
for item in x:
	alpha.append(item)
for _ in range(int(input())):
	n = int(input())
	s = input()
	ans = solve(s, 0, 0)
	print(ans)


from functools import lru_cache
from sys import stdin, stdout
import sys
from math import *

def count(i, j, c):
	ans = 0
	for x in range(i, j):
		if s[x] != c:
			ans += 1
	return ans
lru_cache(None)

def func(c, i, j):
	if i + 1 == j:
		if s[i] == c:
			return 0
		else:
			return 1
	else:
		temp1 = func(chr(ord(c) + 1), i, i + (j - i) // 2)
		temp2 = func(chr(ord(c) + 1), i + ceil((j - i) / 2), j)
		t1 = count(i, i + (j - i) // 2, c)
		t2 = count(i + ceil((j - i) / 2), j, c)
		return min(temp2 + t1, temp1 + t2)
for __ in range(int(input())):
	n = int(input())
	s = input()
	tt = len(bin(n)[2:])
	a = func(chr(97), 0, n)
	print(a)


from sys import stdin, stdout

def cost(s, c):
	cost = 0
	for i in s:
		if i != chr(c):
			cost += 1
	return cost

def count_min(s, left, right, c):
	if right - left == 1:
		if s[left:right] == chr(c):
			return 0
		else:
			return 1
	mid = (left + right) // 2
	first_half = s[left:mid]
	second_half = s[mid:right]
	return min(cost(first_half, c) + count_min(s, mid, right, c + 1), cost(second_half, c) + count_min(s, left, mid, c + 1))
t = int(stdin.readline())
for i in range(t):
	n = int(stdin.readline())
	s = stdin.readline()
	moves = count_min(s, 0, n, ord('a'))
	stdout.write(str(moves) + '\n')


def checkWord(word, c):
	if len(word) == 1:
		if word == c:
			return 0
		else:
			return 1
	mid = int(len(word) / 2)
	left = checkWord(word[:mid], chr(ord(c) + 1))
	left += mid - word[mid:].count(c)
	right = checkWord(word[mid:], chr(ord(c) + 1))
	right += mid - word[:mid].count(c)
	return min(left, right)
cases = int(input())
for _ in range(cases):
	size = int(input())
	word = input()
	result = checkWord(word, 'a')
	print(result)


def solve(s, ch):
	if len(s) == 1:
		if ch == s:
			return 0
		else:
			return 1
	first = s[:len(s) // 2]
	second = s[len(s) // 2:]
	cnt1 = len(first) - first.count(ch) + solve(second, chr(ord(ch) + 1))
	cnt2 = len(second) - second.count(ch) + solve(first, chr(ord(ch) + 1))
	return min(cnt1, cnt2)

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		s = input()
		print(solve(s, 'a'))
main()


ans = [1000000]

def helper(present, s, start, end, letter):
	if start >= end:
		if s[start] != letter:
			present += 1
		ans[0] = min(ans[0], present)
		return
	else:
		mid = start + (end - start) // 2
		left = 0
		right = 0
		for i in range(start, mid + 1):
			if s[i] != letter:
				left += 1
		for i in range(mid + 1, end + 1):
			if s[i] != letter:
				right += 1
		helper(present + left, s, mid + 1, end, chr(ord(letter) + 1))
		helper(present + right, s, start, mid, chr(ord(letter) + 1))
z = []
t = int(input())
for _ in range(t):
	n = int(input())
	ans[0] = n
	s = input()
	helper(0, s, 0, n - 1, 'a')
	z.append(ans[0])
for i in range(t):
	print(z[i])


from sys import *

def ans(l, r, c):
	if l == r:
		if s[l] == c:
			return 0
		else:
			return 1
	mid = l + (r - l + 1) // 2
	count = 0
	for i in range(l, mid):
		if s[i] != c:
			count += 1
	ans1 = count + ans(mid, r, chr(ord(c) + 1))
	count = 0
	for i in range(mid, r + 1):
		if s[i] != c:
			count += 1
	ans2 = count + ans(l, mid - 1, chr(ord(c) + 1))
	return min(ans1, ans2)
t = int(stdin.readline())
while t > 0:
	n = int(stdin.readline())
	s = stdin.readline()
	print(ans(0, n - 1, 'a'))
	t -= 1


def findCost(li, c):
	cost = 0
	for i in li:
		if i != c:
			cost += 1
	return cost

def getMinCost(li, c):
	if len(li) == 1:
		if li[0] == c:
			return 0
		else:
			return 1
	mid = int(len(li) / 2)
	result = min(getMinCost(li[:mid], chr(ord(c) + 1)) + findCost(li[mid:], c), getMinCost(li[mid:], chr(ord(c) + 1)) + findCost(li[:mid], c))
	return result
t = int(input())
for i in range(t):
	n = int(input())
	li = list(input())
	print(getMinCost(li, 'a'))


def dfs(s, b, e, c):
	if b == e:
		if s[b] == c:
			return 0
		else:
			return 1
	m = (b + e) // 2
	c1 = 0
	c2 = 0
	for i in range(b, m + 1):
		if s[i] != c:
			c1 += 1
	for i in range(m + 1, e + 1):
		if s[i] != c:
			c2 += 1
	a1 = c1 + dfs(s, m + 1, e, chr(ord(c) + 1))
	a2 = c2 + dfs(s, b, m, chr(ord(c) + 1))
	return min(a1, a2)
t = int(input())
for _ in range(t):
	l = int(input())
	s = input()
	i = 0
	print(dfs(s, 0, l - 1, 'a'))


for _ in range(int(input())):
	n = int(input())
	a = input()

	def solve(a, k, low, high):
		if low == high:
			if a[low] == chr(ord('a') + k):
				return 0
			else:
				return 1
		else:
			(ans1, ans2) = (0, 0)
			mid = (low + high) // 2
			for i in range(low, mid + 1):
				if a[i] != chr(ord('a') + k):
					ans1 += 1
			for i in range(mid + 1, high + 1):
				if a[i] != chr(ord('a') + k):
					ans2 += 1
			val1 = ans2 + solve(a, k + 1, low, mid)
			val2 = ans1 + solve(a, k + 1, mid + 1, high)
			return min(val1, val2)
	print(solve(a, 0, 0, n - 1))


def solve(array, letter):
	if len(array) == 1:
		if array[0] == letter:
			return 0
		else:
			return 1
	mid = len(array) // 2
	l_side = array[:mid]
	r_side = array[mid:]
	l_occ = mid - l_side.count(letter) + solve(r_side, chr(ord(letter) + 1))
	r_occ = mid - r_side.count(letter) + solve(l_side, chr(ord(letter) + 1))
	return min(l_occ, r_occ)
for _ in range(int(input())):
	n = int(input())
	s = list(input())
	print(solve(s, 'a'))


def num():
	return int(input())

def line():
	return map(int, input().split())

def ans(lst):
	return ' '.join(map(str, lst))
from itertools import repeat
from string import ascii_lowercase as alph

def q(c, i):
	l = len(c)
	if l == 1:
		return int(c[0] != alph[i])
	p1 = sum((x != alph[i] for x in c[:l // 2]))
	p2 = sum((x != alph[i] for x in c[l // 2:]))
	return min(p1 + q(c[l // 2:], i + 1), p2 + q(c[:l // 2], i + 1))
t = num()
for _ in repeat(None, t):
	n = num()
	s = list(input())
	print(q(s, 0))


def func(s, c, l):
	if len(s) == 1:
		if s[0] == c:
			return 0
		else:
			return 1
	else:
		n = chr(ord(c) + 1)
		return min(l - s[:l].count(c) + func(s[l:], n, l // 2), l - s[l:].count(c) + func(s[:l], n, l // 2))
for i in range(int(input())):
	n = int(input())
	s = input()
	print(func(s, 'a', n // 2))


def count(s, c):
	tot = 0
	for char in s:
		if char == c:
			tot += 1
	return tot

def solve(n, s, c):
	if n == 1:
		if s == c:
			return 0
		else:
			return 1
	count_1 = len(s) // 2 - count(s[:len(s) // 2], c) + solve(n // 2, s[len(s) // 2:], chr(ord(c) + 1))
	count_2 = len(s) // 2 - count(s[len(s) // 2:], c) + solve(n // 2, s[:len(s) // 2], chr(ord(c) + 1))
	return min(count_1, count_2)
t = int(input())
for case in range(1, t + 1):
	n = int(input())
	s = input()
	print(solve(n, s, 'a'))


import sys
sys.setrecursionlimit(10 ** 7)
for _ in range(int(input())):
	n = int(input())
	a = input().strip()

	def rec(l, r, cur):
		if l == r:
			return (cur != a[l]) * 1
		mid = l + r >> 1
		res = r - mid - a[mid + 1:r + 1].count(cur) + rec(l, mid, chr(ord(cur) + 1))
		res = min(res, mid - l + 1 - a[l:mid + 1].count(cur) + rec(mid + 1, r, chr(ord(cur) + 1)))
		return res
	print(rec(0, n - 1, 'a'))


stlst = ['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']

def minim(s, n, lent):
	c = stlst[n - 1]
	if lent == 1:
		if s == c:
			return 0
		else:
			return 1
	lent = lent // 2
	s1 = s[:lent]
	s2 = s[lent:]
	c1 = 0
	c2 = 0
	for j in range(lent):
		if s1[j] == c:
			c1 += 1
		if s2[j] == c:
			c2 += 1
	c1 = lent - c1
	c2 = lent - c2
	return min(c1 + minim(s2, n + 1, lent), c2 + minim(s1, n + 1, lent))
for i in range(int(input())):
	n = int(input())
	st = input()
	print(minim(st, 1, n))


import os
import sys
from io import BytesIO, IOBase
from collections import defaultdict as dd

def next(c):
	return chr(97 + (ord(c) - 96) % 26)

def find(s, c):
	if len(s) == 1:
		return 1 - (s[0] == c)
	(cl, cr) = (0, 0)
	for i in range(len(s) // 2):
		cl += 1 - (s[i] == c)
	for i in range(len(s) // 2, len(s)):
		cr += 1 - (s[i] == c)
	return min(cl + find(s[len(s) // 2:len(s)], next(c)), cr + find(s[0:len(s) // 2], next(c)))

def main():
	for _ in range(int(input())):
		n = int(input())
		s = input()
		print(find(s, 'a'))
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
mass = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r']
take = 1024
ops = []
a = ''

def gen1(n, l, r, cur, string, res):
	if r - l > 1:
		res1 = res
		r1 = r
		l1 = (r + l + 1) // 2
		while l1 <= r1:
			if string[l1] != mass[cur]:
				res1 += 1
			l1 += 1
		gen1(n // 2, l, (l + r + 1) // 2 - 1, cur + 1, string, res1)
		res1 = res
		r1 = (r + l + 1) // 2 - 1
		l1 = l
		while l1 <= r1:
			if string[l1] != mass[cur]:
				res1 += 1
			l1 += 1
		gen1(n // 2, (l + r + 1) // 2, r, cur + 1, string, res1)
	else:
		if string[l:r + 1] == mass[cur] + mass[cur + 1]:
			res += 0
		elif string[l:r + 1] == mass[cur + 1] + mass[cur]:
			res += 0
		elif string[l] == mass[cur] or string[l] == mass[cur + 1]:
			res += 1
		elif string[r] == mass[cur] or string[r] == mass[cur + 1]:
			res += 1
		else:
			res += 2
		global take
		if res < take:
			take = res
		return
q = int(input())
for i in range(q):
	n = int(sys.stdin.readline())
	a = sys.stdin.readline().strip()
	take = n
	gen1(n, 0, n - 1, 0, a, 0)
	if a == 'a':
		print(0)
	else:
		print(take)


import sys
input = sys.stdin.readline

def f(s, n):
	if len(s) == 1:
		if s[0] != n:
			return 1
		else:
			return 0
	a = 0
	for i in range(len(s) // 2):
		if s[i] != n:
			a += 1
	b = 0
	for i in range(len(s) // 2):
		if s[len(s) - 1 - i] != n:
			b += 1
	return min(a + f(s[len(s) // 2:], n + 1), b + f(s[:len(s) // 2], n + 1))

def main():
	for t in range(int(input())):
		n = int(input())
		s = list(input())[:n]
		for i in range(len(s)):
			s[i] = ord(s[i]) - ord('a')
		print(f(s, 0))
main()


import sys

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s)
	sys.stdout.write('\n')

def wi(n):
	sys.stdout.write(str(n))
	sys.stdout.write('\n')

def wia(a, sep=' '):
	sys.stdout.write(sep.join([str(x) for x in a]))
	sys.stdout.write('\n')

def solve(n, s, c):
	if n == 1:
		return 0 if s[0] == c else 1
	mid = n // 2
	c1 = s[:mid].count(c)
	c2 = s[mid:].count(c)
	return min(mid - c1 + solve(mid, s[mid:], chr(ord(c) + 1)), mid - c2 + solve(mid, s[:mid], chr(ord(c) + 1)))

def main():
	for _ in range(ri()):
		n = ri()
		s = rs()
		wi(solve(n, s, 'a'))
main()


from collections import Counter
import sys
sys.setrecursionlimit(10000000)

def dfs(lis, le, ch):
	if le == 2:
		if chr(96 + ch) in lis:
			if chr(96 + ch + 1) in lis:
				return 0
			else:
				return 1
		elif chr(96 + ch + 1) in lis:
			return 1
		else:
			return 2
	else:
		uh = lis[:le // 2]
		lh = lis[le // 2:]
		uc = Counter(uh)
		lc = Counter(lh)
		return min(le // 2 - uc[chr(96 + ch)] + dfs(lh, le // 2, ch + 1), le // 2 - lc[chr(96 + ch)] + dfs(uh, le // 2, ch + 1))
(t, *f) = open(0).read().split()
t = int(t)
ns = [f[i * 2:i * 2 + 2] for i in range(t)]
for (n, s) in ns:
	n = int(n)
	if n == 1:
		if s == 'a':
			print(0)
		else:
			print(1)
	else:
		print(dfs(s, n, 1))


def count(s, c):
	ct = 0
	for i in s:
		if i == c:
			ct += 1
	return ct

def func(s, n, a):
	c = chr(a)
	if n == 1:
		if s[0] == c:
			return 0
		return 1
	return min(n // 2 - count(s[:n // 2], c) + func(s[n // 2:], n // 2, a + 1), n // 2 - count(s[n // 2:], c) + func(s[:n // 2], n // 2, a + 1))
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(func(s, n, 97))


def fun(s, n, l, c):
	if n == 1:
		if s[0] == c:
			return l
		return l + 1
	return min(fun(s[n // 2:], n // 2, l + n // 2 - s[:n // 2].count(c), chr(ord(c) + 1)), fun(s[:n // 2], n // 2, l + n // 2 - s[n // 2:].count(c), chr(ord(c) + 1)))
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(fun(s, n, 0, 'a'))


def pre(s):
	dp = []
	letters = [0] * 26
	dp.append(letters)
	for i in s:
		letters = dp[-1][:]
		letters[ord(i) - ord('a')] += 1
		dp.append(letters)
	return dp

def solve(s, n, ans):
	dp = pre(s)
	moves = {}
	stack = [(1, n, 0)]
	while stack:
		curr = stack[-1]
		(l, r, index) = curr
		terms = r - l + 1
		if l == r:
			total = int(not ord(s[l - 1]) - ord('a') == index)
			moves[l, r, index] = total
			stack.pop()
			continue
		left = l + terms // 2 - 1
		right = left + 1
		count = 0
		if (l, left, index + 1) in moves.keys():
			count += 1
		else:
			stack.append((l, left, index + 1))
		if (right, r, index + 1) in moves.keys():
			count += 1
		else:
			stack.append((right, r, index + 1))
		if count == 2:
			total = terms // 2 - (dp[left][index] - dp[l - 1][index]) + moves[right, r, index + 1]
			total = min(total, moves[l, left, index + 1] + terms // 2 - (dp[r][index] - dp[right - 1][index]))
			moves[l, r, index] = total
			stack.pop()
	ans.append(moves[1, n, 0])

def main():
	t = int(input())
	ans = []
	for i in range(t):
		n = int(input())
		s = input()
		solve(s, n, ans)
	for i in ans:
		print(i)
main()


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
mod2 = 998244353
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def JA(a, sep):
	return sep.join(map(str, a))

def JAA(a, s, t):
	return s.join((t.join(map(str, b)) for b in a))

def IF(c, t, f):
	return t if c else f

def YES(c):
	return IF(c, 'YES', 'NO')

def Yes(c):
	return IF(c, 'Yes', 'No')

def main():
	t = I()
	rr = []
	for _ in range(t):
		n = I()
		s = S()
		s = [ord(c) - ord('a') for c in s]

		def f(s, c):
			l = len(s)
			if l == 1:
				if s[0] == c:
					return 0
				return 1
			t = f(s[l // 2:], c + 1)
			for k in s[:l // 2]:
				if k != c:
					t += 1
			u = f(s[:l // 2], c + 1)
			for k in s[l // 2:]:
				if k != c:
					u += 1
			return min(t, u)
		r = f(s, 0)
		rr.append(r)
	return JA(rr, '\n')
print(main())


def calc(s, n, c):
	if n == 1:
		if s[0] != c:
			return 1
		else:
			return 0
	mid = n // 2
	s1 = s[:n // 2]
	l1 = s1.count(c)
	s2 = s[n // 2:]
	r2 = s2.count(c)
	cntl = mid - l1 + calc(s2, n // 2, chr(ord(c) + 1))
	cntr = mid - r2 + calc(s1, n // 2, chr(ord(c) + 1))
	return min(cntl, cntr)
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(calc(s, n, 'a'))


for _ in range(int(input())):
	n = int(input())
	s = str(input())

	def fun(sx, ch, n):
		if n == 0:
			return 0
		elif n == 1:
			if ch == sx:
				return 0
			else:
				return 1
		n = len(sx) // 2
		a = sx[:n].count(ch)
		b = sx[n:].count(ch)
		ch = chr(ord(ch) + 1)
		c = min(n - a + fun(sx[n:], ch, n), n - b + fun(sx[:n], ch, n))
		return c
	print(fun(s, 'a', n))


import sys
tcs = int(input())

def f(s, c):
	if len(s) == 1:
		if s[0] == c:
			return 0
		else:
			return 1
	mid = len(s) // 2
	cntl = f(s[:mid], chr(ord(c) + 1))
	cntl += len(s) // 2 - s[mid:].count(c)
	cntr = f(s[mid:], chr(ord(c) + 1))
	cntr += len(s) // 2 - s[:mid].count(c)
	return min(cntl, cntr)
for tc in range(tcs):
	n = int(input())
	s = list(input())
	print(f(s, 'a'))


t = int(input())

def getFreq(arr):
	n = len(arr)
	freq = [[0 for _ in range(n)] for i in range(26)]
	for i in range(n):
		indx = ord(arr[i]) - ord('a')
		if i == 0:
			freq[indx][i] = 1
		else:
			for j in range(26):
				if j == indx:
					freq[j][i] = 1 + freq[j][i - 1]
				else:
					freq[j][i] = freq[j][i - 1]
	return freq

def makeItGood(s, c, f, start, end):
	indx = ord(c) - ord('a')
	n = end - start + 1
	if n == 1:
		if s[start] == c:
			return 0
		else:
			return 1
	mid = (start + end) // 2 + 1
	freqInLeft = f[indx][mid - 1]
	if start != 0:
		freqInLeft -= f[indx][start - 1]
	freqInRight = f[indx][end] - freqInLeft
	if start != 0:
		freqInRight -= f[indx][start - 1]
	makeLeftGood = makeItGood(s, chr(indx + ord('a') + 1), f, start, mid - 1)
	makeRightGood = makeItGood(s, chr(indx + ord('a') + 1), f, mid, end)
	a = n // 2 - freqInLeft
	b = n // 2 - freqInRight
	return min(a + makeRightGood, b + makeLeftGood)

def solve(arr):
	freq = getFreq(arr)
	n = len(arr)
	return makeItGood(arr, 'a', freq, 0, n - 1)
while t > 0:
	n = int(input())
	s = input()
	print(solve(s))
	t -= 1


nCases = int(input())
alpha = 'abcdefghijklmnopqrstuvwxyz'

def changeToLetter(string, sIndex, dIndex, good):
	letter = alpha[good]
	change = 0
	for i in range(sIndex, dIndex, 1):
		if string[i] != letter:
			change += 1
	return change

def changeToGood(string, sIndex, dIndex, good=0):
	midpoint = int((dIndex - sIndex) / 2 + sIndex)
	if midpoint == sIndex:
		n = changeToLetter(string, sIndex, dIndex, good)
		return n
	nLeft = 0
	nLeft += changeToLetter(string, sIndex, midpoint, good)
	nLeft += changeToGood(string, midpoint, dIndex, good + 1)
	nRight = 0
	nRight += changeToLetter(string, midpoint, dIndex, good)
	nRight += changeToGood(string, sIndex, midpoint, good + 1)
	return min(nLeft, nRight)
for _ in range(nCases):
	sLen = int(input())
	s = list(input())
	changes = changeToGood(s, 0, sLen)
	print(changes)


def solve(s, l, r, c):
	ch = chr(c)
	if l == r:
		if s[l] == ch:
			return 0
		return 1
	(left, right) = (0, 0)
	for i in range(l, (l + r >> 1) + 1):
		if s[i] == ch:
			left += 1
	for i in range((l + r >> 1) + 1, r + 1):
		if s[i] == ch:
			right += 1
	return min((r - l + 1 >> 1) - left + solve(s, (l + r >> 1) + 1, r, c + 1), (r - l + 1 >> 1) - right + solve(s, l, l + r >> 1, c + 1))
for T in range(int(input())):
	n = int(input())
	s = input()
	print(solve(s, 0, n - 1, 97))


def answer(a, n, k):
	if n == 1:
		if a == chr(k):
			return 0
		else:
			return 1
	l = 0
	r = 0
	for i in range(n // 2):
		if a[i] != chr(k):
			l += 1
	for i in range(n // 2, n):
		if a[i] != chr(k):
			r += 1
	return min(l + answer(a[n // 2:], n // 2, k + 1), r + answer(a[:n // 2], n // 2, k + 1))
t = int(input())
for i in range(t):
	n = int(input())
	a = input()
	x = answer(a, n, 97)
	print(x)


def dp(s, ty):
	if len(s) == 1:
		if s == ty:
			return 0
		else:
			return 1
	left = 0
	right = 0
	l = len(s)
	for i in range(l // 2):
		if s[i] != ty:
			left += 1
	for i in range(l // 2, l):
		if s[i] != ty:
			right += 1
	return min(right + dp(s[:l // 2], chr(ord(ty) + 1)), left + dp(s[l // 2:], chr(ord(ty) + 1)))
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(dp(s, 'a'))


import string
t = int(input())

def find_num(s, i, suma):
	if len(s) == 1:
		if s[0] == string.ascii_lowercase[i]:
			return 0
		else:
			return 1
	else:
		x = len(s) // 2
		s1 = s[:x]
		s2 = s[x:]
		c1 = x - s1.count(string.ascii_lowercase[i])
		c2 = x - s2.count(string.ascii_lowercase[i])
		suma += min(c1 + find_num(s2, i + 1, suma), c2 + find_num(s1, i + 1, suma))
	return suma
while t:
	t -= 1
	input()
	s = input()
	print(find_num(s, 0, 0))


def solve(s, low, high, character):
	if low == high:
		return 1 if s[high] != character else 0
	mid = (low + high) // 2
	(temp1, temp2) = (0, 0)
	for i in range(low, mid + 1):
		temp1 += s[i] != character
	for i in range(mid + 1, high + 1):
		temp2 += s[i] != character
	temp1 += solve(s, mid + 1, high, character + 1)
	temp2 += solve(s, low, mid, character + 1)
	return min(temp1, temp2)
for _ in range(int(input())):
	n = int(input())
	s = input()
	k = [ord(i) for i in s]
	print(solve(k, 0, n - 1, 97))


def check_left(s, left, right, cur, ch):
	if right - left == 1:
		return 0 if s[left] == ch else 1
	mid = (left + right) // 2
	cnt = 0
	ii = left
	while right - ii != ii - left:
		if s[ii] != ch:
			cnt += 1
		ii += 1
	return cnt + check(s, mid, right, cur, ch + 1)

def check_right(s, left, right, cur, ch):
	if right - left == 1:
		return 0 if s[left] == ch else 1
	mid = (left + right) // 2
	cnt = 0
	ii = right
	while right - ii != ii - left:
		ii -= 1
		if s[ii] != ch:
			cnt += 1
	return cnt + check(s, left, mid, cur, ch + 1)

def check(s, left=None, right=None, cur=0, ch=0):
	if left is None:
		left = 0
	if right is None:
		right = len(s)
	return cur + min(check_left(s, left, right, cur, ch), check_right(s, left, right, cur, ch))
ans = []
for _ in range(int(input())):
	n = int(input())
	s = input()
	s = [ord(ch) - ord('a') for ch in s]
	ans.append(check(s))
print('\n'.join(map(str, ans)))


import sys
sys.setrecursionlimit(10 ** 7)
from functools import lru_cache

@lru_cache(None)
def good(s, start, end, c):
	if start == end:
		if s[start] == c:
			return 0
		else:
			return 1
	if end == start + 1:
		if s[start] == c and s[end] == chr(ord(c) + 1) or (s[end] == c and s[start] == chr(ord(c) + 1)):
			return 0
		if s[start] == c and s[end] != chr(ord(c) + 1):
			return 1
		if s[start] != chr(ord(c) + 1) and s[end] == c:
			return 1
		if s[start] == chr(ord(c) + 1):
			return 1
		if s[end] == chr(ord(c) + 1):
			return 1
		return 2
	mid = (start + end) // 2
	(count1, count2) = (0, 0)
	for i in range(start, mid + 1):
		if s[i] != c:
			count1 += 1
	for i in range(mid + 1, end + 1):
		if s[i] != c:
			count2 += 1
	next = chr(ord(c) + 1)
	return min(count1 + good(s, mid + 1, end, next), count2 + good(s, start, mid, next))

def string(s):
	c = 'a'
	start = 0
	end = len(s) - 1
	return good(s, start, end, c)
t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	print(string(s))


def dfs(string, c_code):
	c = chr(c_code)
	if len(string) == 1:
		if string[0] == c:
			return 0
		else:
			return 1
	mid = len(string) // 2
	left = mid - string[:mid].count(c) + dfs(string[mid:], c_code + 1)
	right = mid - string[mid:].count(c) + dfs(string[:mid], c_code + 1)
	return min(left, right)
t = int(input())
for _t in range(t):
	n = int(input())
	s = input()
	print(dfs(s, ord('a')))


for tt in range(int(input())):
	n = int(input())
	s = input()
	global count
	count = 0

	def moves(l, r, ch):
		if r - l == 1:
			if s[l] == ch:
				return 0
			else:
				return 1
		else:
			mid = (l + r) // 2
			countl = (r - l) // 2 - s[l:mid].count(ch) + moves(mid, r, chr(ord(ch) + 1))
			countr = (r - l) // 2 - s[mid:r].count(ch) + moves(l, mid, chr(ord(ch) + 1))
			return min(countl, countr)
	print(moves(0, n, 'a'))


from collections import deque
t = int(input())
for _ in range(t):
	ans = 9999999
	n = int(input())
	st = input()
	stck = deque()
	stck.append([0, n, 0, 0])
	while len(stck):
		(l, r, c, s) = stck.pop()
		(s1, s2) = (0, 0)
		if r - l == 1:
			ans = min(ans, s + (1 - int(st[l] == chr(97 + c))))
			continue
		for i in range(l, (l + r) // 2):
			if st[i] != chr(97 + c):
				s1 += 1
		for i in range((l + r) // 2, r):
			if st[i] != chr(97 + c):
				s2 += 1
		stck.append([(l + r) // 2, r, c + 1, s1 + s])
		stck.append([l, (l + r) // 2, c + 1, s2 + s])
	print(ans)


from functools import lru_cache
for _ in range(int(input())):
	n = int(input())
	a = input().strip()

	@lru_cache(None)
	def rec(l, r, cur):
		if l == r:
			return (cur != a[l]) * 1
		mid = l + r >> 1
		res = r - mid - a[mid + 1:r + 1].count(cur) + rec(l, mid, chr(ord(cur) + 1))
		res = min(res, mid - l + 1 - a[l:mid + 1].count(cur) + rec(mid + 1, r, chr(ord(cur) + 1)))
		return res
	print(rec(0, n - 1, 'a'))


def cost(l, r, li, ch):
	cnt = 0
	for i in range(l, r):
		if li[i] != ch:
			cnt += 1
	return cnt

def getmin(l, r, li, ch):
	if l == r - 1:
		if li[l] == ch:
			return 0
		else:
			return 1
	mid = (l + r) // 2
	return min(cost(l, mid, li, ch) + getmin(mid, r, li, chr(ord(ch) + 1)), cost(mid, r, li, ch) + getmin(l, mid, li, chr(ord(ch) + 1)))
for _ in range(int(input())):
	n = int(input())
	j = input()
	print(getmin(0, n, j, 'a'))


A = 'abcdefghijklmnopqrstuvwxyz'

def fun(s, i):
	k = len(s)
	c = A[i]
	if k == 1:
		if s[0] == c:
			return 0
		return 1
	t1 = s[:k // 2].count(c)
	t2 = s[k // 2:].count(c)
	return min(k // 2 - t2 + fun(s[:k // 2], i + 1), k // 2 - t1 + fun(s[k // 2:], i + 1))
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(fun(s, 0))


import sys
input = sys.stdin.readline

def aGood(string, n, l, r, a):
	if l == r:
		if ord(string[l]) - a == 0:
			return 0
		else:
			return 1
	else:
		cost1 = 0
		cost2 = 0
		mid = (l + r) // 2
		for i in range(l, mid + 1):
			if ord(string[i]) != a:
				cost1 += 1
		for i in range(mid + 1, r + 1):
			if ord(string[i]) != a:
				cost2 += 1
		return min(cost1 + aGood(string, n, (l + r) // 2 + 1, r, a + 1), cost2 + aGood(string, n, l, (l + r) // 2, a + 1))

def solution():
	n = int(input())
	string = input()
	print(aGood(string, n, 0, n - 1, 97))
t = int(input())
for i in range(0, t):
	solution()


ALPHA = 'abcdefghijklmnopqrstuvwxyz'

def toGood(i, s):
	l = ALPHA[i]
	if len(s) == 1:
		if s != l:
			return 1
		else:
			return 0
	left = 0
	right = 0
	for letter in s[:len(s) // 2]:
		if letter != l:
			left += 1
	for letter in s[len(s) // 2:]:
		if letter != l:
			right += 1
	return min(left + toGood(i + 1, s[len(s) // 2:]), right + toGood(i + 1, s[:len(s) // 2]))
T = int(input())
for _ in range(T):
	N = int(input())
	S = input()
	print(toGood(0, S))


def helper(s, i):
	if len(s) == 1:
		if ord(s) == i:
			return 0
		else:
			return 1
	mid = len(s) // 2
	ans1 = 0
	for j in s[:mid]:
		if j != chr(i):
			ans1 += 1
	ans1 += helper(s[mid:], i + 1)
	ans2 = 0
	for j in s[mid:]:
		if j != chr(i):
			ans2 += 1
	ans2 += helper(s[:mid], i + 1)
	return min(ans1, ans2)
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(helper(s, ord('a')))


def count(l, r, s, character):
	if l + 1 == r:
		if s[l] == character:
			return 0
		else:
			return 1
	mid = int((l + r) / 2)
	l_count = s[l:mid].count(character)
	r_count = s[mid:r].count(character)
	answer1 = int((r - l) / 2) - l_count
	answer1 += count(mid, r, s, chr(ord(character) + 1))
	answer2 = int((r - l) / 2) - r_count
	answer2 += count(l, mid, s, chr(ord(character) + 1))
	return min(answer1, answer2)
t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	print(count(0, n, s, 'a'))


import math

def f(i):
	global m
	if i == k:
		(a, b) = (0, n)
		cnt = 0
		for j in range(k):
			c = (a + b) // 2
			if s[j] == 1:
				cnt += x[j][c] - x[j][a]
				a = c
			else:
				cnt += x[j][b] - x[j][c]
				b = c
		cnt += x[k][b] - x[k][a]
		m = max(cnt, m)
		return
	s[i] = 1
	f(i + 1)
	s[i] = 0
	f(i + 1)
t = int(input())
for _ in range(t):
	n = int(input())
	s = list(input())
	k = int(math.log2(n))
	x = [[0] * (n + 1) for _ in range(k + 1)]
	for i in range(n):
		j = ord(s[i]) - 97
		if j < k + 1:
			x[j][i + 1] = 1
	for i in range(k + 1):
		for j in range(1, n + 1):
			x[i][j] += x[i][j - 1]
	s = [0] * (k + 1)
	m = 0
	f(0)
	ans = n - m
	print(ans)


from math import ceil

def mlt():
	return map(int, input().split())

def rec(st, cr):
	if len(st) == 1:
		return int(st[0] != cr)
	a = 0
	mid = len(st) // 2
	for n in range(mid):
		if st[n] != cr:
			a += 1
	b = 0
	for n in range(mid, len(st)):
		if st[n] != cr:
			b += 1
	y = rec(st[mid:], chr(ord(cr) + 1)) + a
	x = b + rec(st[:mid], chr(ord(cr) + 1))
	return min(x, y)
for _ in range(int(input())):
	input()
	print(rec(input(), 'a'))


def fun(s, ch):
	if len(s) == 1:
		return int(s != ch)
	mid = len(s) // 2
	left = s[:mid]
	left_count = left.count(ch)
	right = s[mid:]
	right_count = right.count(ch)
	nxt = chr(ord(ch) + 1)
	return min(mid - left_count + fun(right, nxt), mid - right_count + fun(left, nxt))
for t in range(int(input())):
	n = int(input())
	s = input()
	ans = fun(s, 'a')
	print(ans)


import math

def go(s, n, x):
	if n == 1:
		if s[0] == x:
			return 0
		else:
			return 1
	val1 = 0
	for c in s[:int(n / 2)]:
		if c != x:
			val1 += 1
	val2 = 0
	for c in s[int(n / 2):]:
		if c != x:
			val2 += 1
	return min(val2 + go(s[:int(n / 2)], n / 2, chr(ord(x) + 1)), val1 + go(s[int(n / 2):], n / 2, chr(ord(x) + 1)))
t = int(input().strip())
for _ in range(t):
	n = int(input().strip())
	s = input().strip()
	print(go(s, n, 'a'))


def solve(s, c):
	n = len(s)
	if n == 1:
		if s[0] == c:
			return 0
		else:
			return 1
	left = n // 2 - s[:n // 2].count(c) + solve(s[n // 2:], chr(ord(c) + 1))
	right = n // 2 - s[n // 2:].count(c) + solve(s[:n // 2], chr(ord(c) + 1))
	return min(left, right)
for _ in range(int(input())):
	input()
	print(solve(input(), 'a'))


def solve(s, l, r, c):
	if l == r:
		return 0 if s[l] == c else 1
	mid = (r + l) // 2
	l_moves = 0
	for i in range(l, mid + 1):
		if s[i] != c:
			l_moves += 1
	l_moves += solve(s, mid + 1, r, chr(ord(c) + 1))
	r_moves = 0
	for i in range(mid + 1, r + 1):
		if s[i] != c:
			r_moves += 1
	r_moves += solve(s, l, mid, chr(ord(c) + 1))
	return min(l_moves, r_moves)
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	print(solve(s, 0, n - 1, 'a'))


for _ in range(int(input())):
	n = int(input())
	s = input()

	def getcost(l, r, c):
		cnt = 0
		for i in range(l, r + 1):
			if s[i] != c:
				cnt += 1
		return cnt

	def getmin(l, r, c):
		if r == l:
			if s[l] == c:
				return 0
			else:
				return 1
		mid = (l + r) // 2
		return min(getcost(l, mid, c) + getmin(mid + 1, r, chr(ord(c) + 1)), getcost(mid + 1, r, c) + getmin(l, mid, chr(ord(c) + 1)))
	ans = getmin(0, n - 1, 'a')
	print(ans)


def calculer(s, char, n):
	a = 0
	if n == 1:
		a = 1
	else:
		a = n // 2
	ch1 = s[0:a]
	ch2 = s[a:n]
	c1 = ch1.count(char)
	c2 = ch2.count(char)
	if n == 0:
		return 0
	else:
		return min(a - c1 + calculer(ch2, chr(ord(char) + 1), n // 2), a - c2 + calculer(ch1, chr(ord(char) + 1), n // 2))

def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		s = input()
		l = calculer(s, 'a', n)
		print(l)
main()


import sys
input = sys.stdin.readline
from collections import *
sys.setrecursionlimit(100)

def f(l, r, d):
	if r - l == 1:
		return 0 if ord(s[l]) - ord('a') == d else 1
	m = (l + r) // 2
	res = 10 ** 18
	res = min(res, m - l - (acc[d][m] - acc[d][l]) + f(m, r, d + 1))
	res = min(res, f(l, m, d + 1) + r - m - (acc[d][r] - acc[d][m]))
	return res
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()[:-1]
	acc = [[0] for _ in range(26)]
	for i in range(n):
		for j in range(26):
			acc[j].append(acc[j][-1] + (1 if ord(s[i]) - ord('a') == j else 0))
	print(f(0, n, 0))


def main():
	from string import ascii_lowercase
	dic = {i: a for (i, a) in enumerate(ascii_lowercase)}
	T = int(input())

	def dfs(N, S, i=0):
		if N == 1:
			return 1 if dic[i] != S[0] else 0
		(ret1, ret2) = (0, 0)
		ret1 = sum((dic[i] != s for s in S[N // 2:]))
		ret2 = sum((dic[i] != s for s in S[:N // 2]))
		ret = min(dfs(N // 2, S[:N // 2], i + 1) + ret1, dfs(N // 2, S[N // 2:], i + 1) + ret2)
		return ret
	for _ in range(T):
		N = int(input())
		S = [s for s in input()]
		print(dfs(N, S))
main()


import sys
import math
import collections
import operator as op
from collections import deque
from math import gcd, inf, sqrt, pi, cos, sin, ceil, log2
from bisect import bisect_right, bisect_left
from functools import reduce
from sys import stdin, stdout, setrecursionlimit
setrecursionlimit(2 ** 20)

def factorial(n):
	if n == 0:
		return 1
	return n * factorial(n - 1)

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def prime_factors(n):
	i = 2
	factors = []
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.append(i)
	if n > 1:
		factors.append(n)
	return len(set(factors))

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def factors(n):
	return list(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def solve(s, d, n, c):
	if n == 1:
		if d[c] == s[0]:
			return 0
		else:
			return 1
	ans1 = 0
	for i in s[:n // 2]:
		if i != d[c]:
			ans1 += 1
	ans2 = 0
	for i in s[n // 2:]:
		if i != d[c]:
			ans2 += 1
	return min(ans2 + solve(s[:n // 2], d, n // 2, c + 1), ans1 + solve(s[n // 2:], d, n // 2, c + 1))
MOD = 1000000007
PMOD = 998244353
T = 1
T = int(stdin.readline())
for _ in range(T):
	n = int(stdin.readline())
	s = str(stdin.readline().strip('\n'))
	d = {}
	c = 1
	for i in 'abcdefghijklmnopqrstuvwxyz':
		d[c] = i
		c += 1
	res = solve(s, d, n, 1)
	print(res)


import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__
import math

def gift():
	for _ in range(t):
		n = int(input())
		arry = input()
		if n == 1:
			if arry[0] == 'a':
				yield 0
			else:
				yield 1
		else:

			def getminmove(array, lenit, char, type1):
				half = lenit // 2
				if lenit == 2 and type1 == False:
					return min(getminmove(array[:half], half, chr(ord(char) + 1), True) + getminmove(array[half:], half, chr(ord(char) + 2), True), getminmove(array[:half], half, chr(ord(char) + 2), True) + getminmove(array[half:], half, chr(ord(char) + 1), True))
				elif type1:
					ans = 0
					for ele in array:
						if ele != char:
							ans += 1
					return ans
				else:
					return min(getminmove(array[:half], half, chr(ord(char) + 1), True) + getminmove(array[half:], half, chr(ord(char) + 1), False), getminmove(array[:half], half, chr(ord(char) + 1), False) + getminmove(array[half:], half, chr(ord(char) + 1), True))
			res = getminmove(arry, n, chr(ord('a') - 1), False)
			yield res
t = int(input())
ans = gift()
print(*ans, sep='\n')


import math
import sys
input = sys.stdin.readline
t = int(input())

def solve(s, l, r, c):
	(ans1, ans2) = (0, 0)
	mid = (l + r) // 2
	c1 = chr(ord(c) + 1)
	if len(s[l:r + 1]) == 1:
		if s[l] == c:
			return 0
		return 1
	elif len(s[l:r + 1]) > 1:
		for i in range(l, mid + 1):
			if s[i] != c:
				ans1 += 1
		for i in range(mid + 1, r + 1):
			if s[i] != c:
				ans2 += 1
		ans1 += solve(s, mid + 1, r, c1)
		ans2 += solve(s, l, mid, c1)
		return min(ans1, ans2)
for _ in range(t):
	n = int(input())
	s = input()
	print(solve(s, 0, n - 1, 'a'))


import sys
import math
sys.setrecursionlimit(100000)

def solve(start, end, c):
	n = len(s)
	if start == end:
		if ord(s[start]) == c:
			return 0
		else:
			return 1
	if start == 0:
		count1 = (end - start + 1) // 2 - count[c - 97][(start + end) // 2]
	else:
		count1 = (end - start + 1) // 2 - (count[c - 97][(start + end) // 2] - count[c - 97][start - 1])
	count2 = (end - start + 1) // 2 - (count[c - 97][end] - count[c - 97][(start + end) // 2])
	return min(count2 + solve(start, (start + end) // 2, c + 1), count1 + solve((start + end) // 2 + 1, end, c + 1))
for nt in range(int(input())):
	n = int(input())
	s = list(input())
	count = [[0 for i in range(n)] for j in range(26)]
	for i in range(n):
		if i > 0:
			for j in range(26):
				count[j][i] = count[j][i - 1]
		count[ord(s[i]) - 97][i] += 1
	print(solve(0, n - 1, 97))


def pizda(x, y, z):
	if len(s[x:y]) <= 1:
		if ord(s[x:y]) != z:
			return 1
		else:
			return 0
	else:
		a = s[x:(x + y) // 2]
		b = s[(x + y) // 2:y]
		a1 = 0
		b1 = 0
		for i in range(len(a)):
			if ord(a[i]) != z:
				a1 += 1
		for i in range(len(b)):
			if ord(b[i]) != z:
				b1 += 1
		return min(pizda(x, (x + y) // 2, z + 1) + b1, pizda((x + y) // 2, y, z + 1) + a1)
t = int(input())
for i in range(t):
	k = 0
	good = ord('a')
	n = int(input())
	s = input()
	print(pizda(0, n, ord('a')))


import sys
import collections

def input():
	return sys.stdin.readline().rstrip()

def split_input():
	return [int(i) for i in input().split()]
tests = int(input())

def ans(s, start, end, char):
	if end < start:
		return 0
	elif end == start:
		if s[start] == char:
			return 0
		else:
			return 1
	else:
		mid = (start + end) // 2
		cost1 = 0
		for i in range(start, mid + 1):
			if s[i] != char:
				cost1 += 1
		cost1 += ans(s, mid + 1, end, chr(ord(char) + 1))
		cost2 = 0
		for i in range(mid + 1, end + 1):
			if s[i] != char:
				cost2 += 1
		cost2 += ans(s, start, mid, chr(ord(char) + 1))
		return min(cost1, cost2)
for _ in range(tests):
	n = int(input())
	s = input()
	print(ans(s, 0, n - 1, 'a'))


d = {}

def equal(s, a):
	c = s.count(a) if a in s else 0
	return len(s) - c

def solve(s, a):
	if d.get((s, a), -1) != -1:
		return d[s, a]
	n = len(s)
	if n == 1:
		if s == a:
			return 0
		else:
			return 1
	x = solve(s[:n // 2], chr(ord(a) + 1)) + equal(s[n // 2:], a)
	y = solve(s[n // 2:], chr(ord(a) + 1)) + equal(s[:n // 2], a)
	t = min(x, y)
	d[s, a] = t
	return t
for _ in range(int(input())):
	n = int(input())
	s = input()
	if n == 1:
		if s == 'a':
			print(0)
		else:
			print(1)
		continue
	a = 'a'
	print(min(equal(s[:n // 2], a) + solve(s[n // 2:], chr(ord(a) + 1)), equal(s[n // 2:], a) + solve(s[:n // 2], chr(ord(a) + 1))))


def fun(s, r):
	if len(s) == 1:
		if s[0] == r:
			return 0
		else:
			return 1
	z = chr(ord(r) + 1)
	x = len(s) // 2
	r11 = s[:x].count(r)
	r12 = s[x:].count(r)
	r21 = s[:x].count(z)
	r22 = s[x:].count(z)
	return min(fun(s[:x], z) + (x - r12), fun(s[x:], z) + (x - r11))
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(fun(s, 'a'))


from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush, heappop, heapify
import math
from collections import *
from functools import reduce, cmp_to_key
import sys
input = sys.stdin.readline
M = mod = 998244353

def factors(n):
	return sorted(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def inv_mod(n):
	return pow(n, mod - 2, mod)

def li():
	return [int(i) for i in input().rstrip('\n').split()]

def st():
	return input().rstrip('\n')

def val():
	return int(input().rstrip('\n'))

def li2():
	return [i for i in input().rstrip('\n')]

def li3():
	return [int(i) for i in input().rstrip('\n')]
for _ in range(val()):
	n = val()
	l = [ord(i) - ord('a') for i in st()]
	cnt = Counter()
	l1 = []
	l1.append(cnt.copy())
	for i in l:
		cnt[i] += 1
		l1.append(cnt.copy())
	he = deque()
	he.append([0, n - 1, 0, 0])
	ans = n + 1
	while he:
		(left, right, costyet, curr) = he.popleft()
		if left == right:
			costyet += 1 if l[left] != curr else 0
			ans = min(ans, costyet)
			continue
		le = right - left + 1
		(i, j) = (left, right)
		c1 = le // 2 - (l1[left + le // 2][curr] - l1[left][curr])
		c2 = le // 2 - (l1[right + 1][curr] - l1[left + le // 2][curr])
		he.append([i, i + le // 2 - 1, costyet + c2, curr + 1])
		he.append([i + le // 2, j, costyet + c1, curr + 1])
	print(ans)


from sys import stdin, stdout
from math import gcd, sqrt
from collections import deque
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
P = lambda x: stdout.write(x)
lcm = lambda x, y: x * y // gcd(x, y)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 0 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
sm = lambda x: (x ** 2 + x) // 2
N = 10 ** 9 + 7
c = ''.join([chr(i + 97) for i in range(26)])

def solve(s, n, p):
	if n == 1:
		return 0 if s == c[p] else 1
	cnt1 = cnt2 = 0
	for i in range(n // 2):
		if s[i] != c[p]:
			cnt1 += 1
		if s[-i - 1] != c[p]:
			cnt2 += 1
	return min(cnt1 + solve(s[n // 2:], n // 2, p + 1), cnt2 + solve(s[:n // 2], n // 2, p + 1))
for i in range(I()):
	n = I()
	a = S()
	print(solve(a, n, 0))


import sys
sys.setrecursionlimit(10 ** 6)
ANS = [1000000]

def rec(c, st, en, l, n, need, req):
	nx = chr(ord(c) + 1)
	if en - st == 1:
		if nx != req[st]:
			need += 1
		if need < ANS[0]:
			ANS[0] = need
		return
	(need1, need2) = (need, need)
	for i in range(st, (st + en) // 2):
		if nx != req[i]:
			need1 += 1
	for i in range((st + en) // 2, en):
		if nx != req[i]:
			need2 += 1
	l += (en - st) // 2
	if l > n:
		if need1 < ANS[0]:
			ANS[0] = need1
		elif need2 < ANS[0]:
			ANS[0] = need2
		return
	else:
		rec(nx, (st + en) // 2, en, l, n, need1, req)
		rec(nx, st, (st + en) // 2, l, n, need2, req)

def solve(req, n):
	rec('`', 0, n, 0, n, 0, req)
	print(ANS[0])
for _ in range(int(input())):
	n = int(input())
	req = input()
	solve(req, n)
	ANS[0] = 1000000


def good(x, c):
	if len(x) == 1:
		return x[0] != c
	mid = int(len(x) / 2)
	cntl = good(x[:mid], chr(ord(c) + 1))
	cntl += int(len(x) / 2) - x[mid:len(x)].count(c)
	cntr = good(x[mid:len(x)], chr(ord(c) + 1))
	cntr += int(len(x) / 2) - x[:mid].count(c)
	return min(cntr, cntl)
n = int(input())
for i in range(n):
	y = int(input())
	x = str(input())
	c = 'a'
	if len(x) == 1:
		if x[0] != 'a':
			print('1')
			continue
		else:
			print('0')
			continue
	print(good(x, c))


import math
t = int(input())
for _ in range(t):
	n = int(input())
	string = input()
	min_cost = float('inf')

	def diff(char, a, b):
		c = 0
		for i in range(a, b + 1):
			if string[i] != char:
				c += 1
		return c

	def rec(a, b, char, cost):
		if a == b:
			global min_cost
			curr = string[a]
			cost += 0 if curr == char else 1
			min_cost = min(min_cost, cost)
			return
		n = b - a + 1
		h = n // 2
		l_cost = diff(char, a, a + h - 1) + cost
		r_cost = diff(char, a + h, b) + cost
		next_char = chr(ord(char) + 1)
		rec(a, a + h - 1, next_char, r_cost)
		rec(a + h, b, next_char, l_cost)
	rec(0, n - 1, 'a', 0)
	print(min_cost)


def code(start, end, l, letter):
	ans = 0
	if start != end:
		mid = start + (end - start - 1) // 2
		arr1 = l[start:mid + 1]
		arr2 = l[mid + 1:end + 1]
		arr1_count = arr2_count = 0
		mini = len(arr1)
		for i in range(mini):
			if arr1[i] == chr(letter):
				arr1_count += 1
			if arr2[i] == chr(letter):
				arr2_count += 1
		ans1 = mini - arr1_count + code(mid + 1, end, l, letter + 1)
		ans2 = mini - arr2_count + code(start, mid, l, letter + 1)
		return min(ans1, ans2)
	elif l[start] != chr(letter):
		ans += 1
	return ans
t = int(input(''))
while t > 0:
	n = int(input(''))
	l = input('')
	start = 0
	end = n - 1
	letter = 97
	ans = 0
	print(code(start, end, l, letter))
	t -= 1


import sys
sys.setrecursionlimit(2147483647)
input = sys.stdin.readline
alpha = 'abcdefghijklmnopqrstuvwxyz'

def count_insert(arr, index):
	length = len(arr)
	char = alpha[index % 26]
	if length == 1:
		if arr == char:
			return 0
		else:
			return 1
	return min(count_insert(arr[0:length // 2], index + 1) + length // 2 - arr[length // 2:].count(char), count_insert(arr[length // 2:], index + 1) + length // 2 - arr[:length // 2].count(char))

def main():
	n = int(input())
	arr = []
	for _ in range(n):
		t = int(input())
		arr.append(input().rstrip())
	for i in range(n):
		print(count_insert(arr[i], 0))
main()


def recur(s, char, start, end, curr):
	global ans
	if end - start == 0:
		if s[start] != char:
			curr += 1
		if curr < ans:
			ans = curr
		return
	val1 = 0
	val2 = 0
	l = (end - start + 1) // 2
	for i in range(start, start + l):
		if s[i] != char:
			val1 += 1
	for i in range(start + l, end + 1):
		if s[i] != char:
			val2 += 1
	char = chr(ord(char) + 1)
	recur(s, char, start, start + l - 1, curr + val2)
	recur(s, char, start + l, end, curr + val1)
t = int(input())
for _ in range(t):
	global ans
	ans = 10000000
	n = int(input())
	s = input()
	recur(s, 'a', 0, n - 1, 0)
	print(ans)


import math

def get_matches(string, letter, start, end):
	count = 0
	for i in range(start, end + 1):
		if string[i] == letter:
			count += 1
	return count

def make_good_string(string, letter_index, start, end):
	letter = LETTERS[letter_index]
	if start == end:
		if string[start] == letter:
			return 0
		return 1
	middle = math.floor((start + end) / 2)
	start1 = start
	end1 = middle
	start2 = middle + 1
	end2 = end
	stringACount = get_matches(string, letter, start1, end1)
	stringBCount = get_matches(string, letter, start2, end2)
	costA = end1 - start1 + 1 - stringACount
	costB = end2 - start2 + 1 - stringBCount
	op1 = costA + make_good_string(string, letter_index + 1, start2, end2)
	op2 = costB + make_good_string(string, letter_index + 1, start1, end1)
	if op1 < op2:
		return op1
	else:
		return op2
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']
queries = int(input())
for i in range(queries):
	length = int(input())
	string = input()
	result = make_good_string(list(string), 0, 0, length - 1)
	print(result)


def check(s, ch, n):
	if n == 1:
		if s == ch:
			return 0
		else:
			return 1
	count = 0
	for i in range(n // 2):
		if s[i] != ch:
			count += 1
	co = 0
	for i in range(n // 2, n):
		if s[i] != ch:
			co += 1
	return min(count + check(s[n // 2:n], chr(ord(ch) + 1), n // 2), co + check(s[0:n // 2], chr(ord(ch) + 1), n // 2))
for _ in range(int(input())):
	n = int(input())
	s = input()
	if n == 1:
		if s == 'a':
			print(0)
		else:
			print(1)
	else:
		print(check(s, 'a', n))


def read_int():
	return int(input().strip())

def read_ints():
	return list(map(int, input().strip().split(' ')))

def cost(s, c, start, end):
	if start == end:
		return int(c != s[end])
	mid = (start + end) // 2
	cost0 = sum((c0 != c for c0 in s[start:mid + 1])) + cost(s, c + 1, mid + 1, end)
	cost1 = sum((c0 != c for c0 in s[mid + 1:end + 1])) + cost(s, c + 1, start, mid)
	return min(cost0, cost1)

def solve():
	n = read_int()
	s = [ord(c) - ord('a') for c in input().strip()]
	return cost(s, 0, 0, n - 1)
T = read_int()
for _ in range(T):
	print(solve())


import sys
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
sys.setrecursionlimit(10 ** 5)

def main():
	global s
	(t,) = I()
	for _ in range(t):
		(n,) = I()
		s = list(input())
		if n == 1:
			print(1 - int('a' == s[0]))
		else:

			def ck(ct, ix, cr):
				if ct == 0:
					return 1 - int(s[ix] == chr(cr))
				p = q = 0
				for i in range(ix, ix + ct):
					p += s[i] != chr(cr)
				p += ck(ct // 2, ix + ct, cr + 1)
				for i in range(ix + ct, ix + 2 * ct):
					q += s[i] != chr(cr)
				q += ck(ct // 2, ix, cr + 1)
				return min(p, q)
			p = ck(n // 2, 0, ord('a'))
			print(p)
main()


def makeGood(n, s):

	def isAll(sub, c):
		count = 0
		for i in range(len(sub)):
			if sub[i] != c:
				count = count + 1
		return count

	def isGood(sub, c):
		n = len(sub)
		if n == 1 and sub[0] == c:
			return 0
		elif n == 1:
			return 1
		mid = n // 2
		return min(isAll(sub[:mid], c) + isGood(sub[mid:], chr(ord(c) + 1)), isAll(sub[mid:], c) + isGood(sub[:mid], chr(ord(c) + 1)))
	return isGood(s, 'a')
t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	print(makeGood(n, s))


def solve(s, i, j, char, cnt):
	if j - i == 1:
		if s[i] == char:
			return cnt
		else:
			return cnt + 1
	left = s[i:i + (j - i) // 2].count(char)
	right = s[i + (j - i) // 2:j].count(char)
	cnt1 = (j - i) // 2 - left
	l = solve(s, i + (j - i) // 2, j, chr(ord(char) + 1), cnt + cnt1)
	cnt2 = (j - i) // 2 - right
	r = solve(s, i, i + (j - i) // 2, chr(ord(char) + 1), cnt + cnt2)
	return min(l, r)
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	print(solve(s, 0, n, 'a', 0))


import sys, math
from collections import deque
import random

def f(x, h, c):
	if len(x) == 1:
		if x[0] == c:
			return 0
		else:
			return 1
	if h == 0:
		cnt = 0
		for i in range(len(x) // 2):
			if not x[i] == c:
				cnt += 1
		return cnt + min(f(x[len(x) // 2:], 0, chr(ord(c) + 1)), f(x[len(x) // 2:], 1, chr(ord(c) + 1)))
	else:
		cnt = 0
		for i in range(len(x) // 2, len(x)):
			if not x[i] == c:
				cnt += 1
		return cnt + min(f(x[:len(x) // 2], 0, chr(ord(c) + 1)), f(x[:len(x) // 2], 1, chr(ord(c) + 1)))
for i in range(int(input())):
	n = int(input())
	s = input()
	print(min(f(s, 0, 'a'), f(s, 1, 'a')))


def g(a, n, k):
	s = 0
	for i in range(n):
		if a[i] != k:
			s += 1
	return s

def f(a, n, k):
	if n == 1:
		if a[0] == k:
			return 0
		else:
			return 1
	a1 = a[:n // 2]
	a2 = a[n // 2:]
	ans = min(f(a1, n // 2, k + 1) + g(a2, n // 2, k), f(a2, n // 2, k + 1) + g(a1, n // 2, k))
	return ans
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	a = [ord(c) - ord('a') for c in s]
	ans = f(a, n, 0)
	print(ans)


from sys import stdin, setrecursionlimit, stdout
from collections import deque
from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin
from heapq import heapify, heappop, heappush, heapreplace, heappushpop

def ii():
	return int(stdin.readline())

def fi():
	return float(stdin.readline())

def mi():
	return map(int, stdin.readline().split())

def fmi():
	return map(float, stdin.readline().split())

def li():
	return list(mi())

def si():
	return stdin.readline().rstrip()

def lsi():
	return list(si())
mod = 1000000007
res = ['YES', 'NO']

def solve(l, r, x):
	if l == r:
		if ord(a[l]) == x + 97:
			return 0
		return 1
	cnt = cnt1 = 0
	m = (l + r) // 2
	for i in a[l:m + 1]:
		if ord(i) - 97 != x:
			cnt += 1
	for i in a[m + 1:r + 1]:
		if ord(i) - 97 != x:
			cnt1 += 1
	return min(solve(m + 1, r, x + 1) + cnt, solve(l, m, x + 1) + cnt1)
test = 1
test_case = ii()
while test <= test_case:
	test += 1
	n = ii()
	a = lsi()
	print(solve(0, n - 1, 0))


def cost(start, end, c):
	count = 0
	for i in range(start, end + 1):
		if s[i] != c:
			count += 1
	return count

def divider(start, end, c='a'):
	mid = (start + end) // 2
	if start == end:
		if s[start] == c:
			return 0
		else:
			return 1
	return min(cost(start, mid, c) + divider(mid + 1, end, chr(ord(c) + 1)), cost(mid + 1, end, c) + divider(start, mid, chr(ord(c) + 1)))
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	start = 0
	end = n - 1
	print(divider(start, end))


import math
import sys
LI = lambda : list(map(int, input().split()))
MI = lambda : map(int, input().split())
SI = lambda : input()
II = lambda : int(input())
t = II()
for q in range(t):
	n = II()
	s = SI()
	d = [0] * 26
	dp = [0] * n

	def rec(a, z):
		if len(a) == 0:
			return 0
		elif len(a) == 1:
			return int(a != z)
		mid = len(a) // 2
		l = a[:mid]
		r = a[mid:]
		f = mid - l.count(z) + rec(r, chr(ord(z) + 1))
		s = mid - r.count(z) + rec(l, chr(ord(z) + 1))
		return min(f, s)
	print(rec(s, 'a'))


def fun1(s, n, k):
	cnt = s.count(k)
	return n - cnt

def fun(s, n, k):
	if n == 1:
		if s == k:
			return 0
		else:
			return 1
	else:
		n = n // 2
		o = ord(k) - 97
		k1 = chr(o + 98)
		return min(fun1(s[:n], n, k) + fun(s[n:], n, k1), fun(s[:n], n, k1) + fun1(s[n:], n, k))
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(fun(s, n, 'a'))


def yes(s, n, lett):
	z = lett
	lett = chr(ord(lett) + 1)
	if n == 1:
		if z == s:
			return 0
		else:
			return 1
	x = s[:n // 2]
	y = s[n // 2:]
	cx = n // 2 - x.count(z)
	cy = n // 2 - y.count(z)
	out = 0
	out += min(cy + yes(x, n // 2, lett), cx + yes(y, n // 2, lett))
	return out
t = int(input())
while t:
	t = t - 1
	n = int(input())
	s = input()
	print(yes(s, n, 'a'))


alphabet = 'abcdefghijklmnopqrstuvwxyz'

def recursion(s, start, end, height):
	if end - start == 1:
		if s[start] == alphabet[height]:
			return 0
		else:
			return 1
	else:
		middle = (start + end) // 2
		return min(recursion(s, middle, end, height + 1) + (end - start) // 2 - s[start:middle].count(alphabet[height]), recursion(s, start, middle, height + 1) + (end - start) // 2 - s[middle:end].count(alphabet[height]))

def solve():
	n = int(input())
	s = input()
	print(recursion(s, 0, n, 0))
for _ in range(int(input())):
	solve()


def ans(arr, n, a):
	n = len(arr)
	if n == 1:
		if arr[0] == a:
			return 0
		return 1
	h1 = 0
	h2 = 0
	i = 0
	while i < n // 2:
		if arr[i] != a:
			h1 += 1
		i += 1
	while i < n:
		if arr[i] != a:
			h2 += 1
		i += 1
	callOp1 = ans(arr[0:n // 2], n // 2, a + 1) + h2
	callOp2 = ans(arr[n // 2:n], n // 2, a + 1) + h1
	return min((callOp1, callOp2))
for _ in range(int(input())):
	n = int(input())
	sLi = input()
	arr = []
	for i in sLi:
		arr.append(ord(i) - 96)
	print(ans(arr, n, 1))


T = int(input())
import itertools
import math
for _ in range(T):
	n = int(input())
	nn = int(math.log2(n))
	s = input()
	ans = 0
	x = [[0] * (n + 1) for _ in range(nn + 1)]
	for i in range(n):
		r = ord(s[i]) - 97
		if r <= nn:
			x[r][i + 1] = 1
		for j in range(nn + 1):
			x[j][i + 1] += x[j][i]
	h_list = list(itertools.product([0, 1], repeat=nn))
	ans = n
	for ll in h_list:
		ss = n
		(l, r) = (0, n)
		(l2, r2) = (0, n)
		aa = 0
		for i in range(nn):
			ss //= 2
			if ll[i] == 0:
				r2 = r
				r = l + ss
				l2 = r
				aa += ss - (x[i][r2] - x[i][l2])
			else:
				l2 = l
				l = r - ss
				r2 = l
				aa += ss - (x[i][r2] - x[i][l2])
		aa += 1 - (x[nn][r] - x[nn][l])
		ans = min(aa, ans)
	print(ans)


from math import *

def delta(s, left, right, ch):
	dif = 0
	for ind in range(left, right + 1):
		if s[ind] != ch:
			dif += 1
	return dif
tests = int(input())
for t in range(tests):
	n = int(input())
	s = input()
	chars = ['a']
	k = 0
	pw = 1
	ch = 'a'
	while pw < n:
		k += 1
		pw *= 2
		ch = chr(ord(ch) + 1)
		chars.append(ch)
	fixes = [0] * n
	for i in range(n):
		fx = 0 if s[i] == chars[k] else 1
		fixes[i] = [0] * 20
		fixes[i][0] = fx
	shift = 1
	for j in range(1, k + 1):
		ch = chars[k - j]
		shift2 = 2 * shift
		max_i = n - shift2 + 1
		for i in range(0, max_i, shift2):
			fx1 = delta(s, i, i + shift - 1, ch) + fixes[i + shift][j - 1]
			fx2 = fixes[i][j - 1] + delta(s, i + shift, i + shift2 - 1, ch)
			fixes[i][j] = min(fx1, fx2)
		shift = shift2
	print(fixes[0][k])


from sys import stdin
t = int(stdin.readline().strip())
for i in range(t):
	n = int(stdin.readline().strip())
	S = stdin.readline().strip()
	S = [ord(c) - ord('a') for c in S]
	k = 0
	while n > 1:
		k += 1
		n = n >> 1
	last = [0 if c == k else 1 for c in S]
	for i in range(k - 1, -1, -1):
		block_length = int((1 << k) / len(last))
		next = []
		for j in range(len(last)):
			total = 0
			for l in range(block_length):
				if S[j * block_length + l] != i:
					total += 1
			next.append(total)
		newlast = []
		for j in range(0, len(last), 2):
			newlast.append(min(last[j] + next[j + 1], last[j + 1] + next[j]))
		last = newlast
	print(last[0])


import time
from collections import deque

def inpt():
	return int(input())

def inpl():
	return list(map(int, input().split()))

def inpm():
	return map(int, input().split())

def change(s, i):
	ss = chr(ord('a') + i)
	return sum([j != ss for j in s])

def get(s, i):
	n = len(s)
	if n == 1:
		return 1 if s != chr(ord('a') + i) else 0
	return min(change(s[:n // 2], i) + get(s[n // 2:], i + 1), change(s[n // 2:], i) + get(s[:n // 2], i + 1))

def solve():
	n = inpt()
	s = input()
	print(get(s, 0))
	return

def main():
	m = 10 ** 9 + 7
	t = int(input())
	while t:
		t -= 1
		solve()
main()


alp = ['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']

def gt(s, x):
	if len(s) == 1:
		if alp[x] == s:
			return 0
		else:
			return 1
	else:
		l = len(s)
		req = alp[x] * (l // 2)
		c1 = len([i for i in range(l // 2) if s[:l // 2][i] == req[i]])
		c2 = len([i for i in range(l // 2) if s[l // 2:][i] == req[i]])
		return min(l // 2 - c1 + gt(s[l // 2:], x + 1), l // 2 - c2 + gt(s[:l // 2], x + 1))
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(gt(s, 0))


def cost(l, r, a):
	c = 0
	for i in range(l, r + 1):
		if s[i] != a:
			c += 1
	return c

def getmin(l, r, a):
	if l == r:
		if s[l] == a:
			return 0
		else:
			return 1
	m = (l + r) // 2
	return min(cost(l, m, a) + getmin(m + 1, r, chr(ord(a) + 1)), cost(m + 1, r, a) + getmin(l, m, chr(ord(a) + 1)))
t = int(input())
while t:
	n = int(input())
	s = input()
	print(getmin(0, n - 1, 'a'))
	t -= 1


def cgood(s, l, c) -> int:
	if l == 1:
		if s[0] == c:
			return 0
		else:
			return 1
	fh = s[0:l // 2]
	sh = s[l // 2:]
	fhc = 0
	shc = 0
	for i in fh:
		if i == c:
			fhc += 1
	for i in sh:
		if i == c:
			shc += 1
	return min(l // 2 - fhc + cgood(sh, l // 2, chr(ord(c) + 1)), l // 2 - shc + cgood(fh, l // 2, chr(ord(c) + 1)))
t = int(input())
for tc in range(t):
	l = int(input())
	s = input()
	print(cgood(s, l, 'a'))


from sys import stdin, stdout
import math, queue, heapq
fastinput = stdin.readline
fastout = stdout.write
t = int(fastinput())

def result(s, c):
	n = len(s)
	if n == 1 and s[0] == c:
		return 0
	elif n == 1:
		return 1
	else:
		a = s[0:n // 2]
		b = s[n // 2:n]
		acount = a.count(c)
		bcount = b.count(c)
		c = chr(ord(c) + 1)
		return min(len(a) - acount + result(b, c), len(b) - bcount + result(a, c))
while t:
	t -= 1
	n = int(fastinput())
	s = input()
	print(result(s, 'a'))


import math, sys, bisect, heapq
from collections import defaultdict, Counter, deque
from itertools import groupby, accumulate
ilele = lambda : map(int, input().split())
alele = lambda : list(map(int, input().split()))

def Y(c):
	print(['NO', 'YES'][c])

def y(c):
	print(['no', 'yes'][c])

def Yy(c):
	print(['No', 'Yes'][c])
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']

def mid(i, j):
	return (i + j - 1) // 2
mark = []
S = ''

def fun(a, b, k):
	if a == b:
		if S[a] == A[k]:
			return 0
		return 1
	m = mid(a, b)
	x = mark[m + 1][k] - mark[a][k]
	y = mark[b + 1][k] - mark[m + 1][k]
	return min(m - a + 1 - x + fun(m + 1, b, k + 1), b - m - y + fun(a, m, k + 1))
for _ in range(int(input())):
	N = int(input())
	S = input()
	i = 0
	j = N - 1
	mark = []
	temp = [0] * 26
	a = ord('a')
	mark.append(temp.copy())
	for i in S:
		temp[ord(i) - a] += 1
		mark.append(temp.copy())
	print(fun(0, N - 1, 0))


def abc(s, i, j, k):
	n = j - i + 1
	if n == 1:
		if s[i] == chr(k):
			return 0
		return 1
	x = 0
	y = 0
	for m in range(i, i + n // 2):
		if s[m] != chr(k):
			x += 1
	for m in range(i + n // 2, j + 1):
		if s[m] != chr(k):
			y += 1
	return min(x + abc(s, i + n // 2, j, k + 1), y + abc(s, i, i + n // 2 - 1, k + 1))
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	print(abc(s, 0, n - 1, 97))


import sys
input = sys.stdin.readline

def print(val):
	sys.stdout.write(str(val) + '\n')

def solve(s, l, r, c):
	if l + 1 == r:
		return int(s[l] != c)
	replace1 = replace2 = 0
	for i in range(l, (l + r) // 2):
		if s[i] != c:
			replace1 += 1
	for i in range((l + r) // 2, r):
		if s[i] != c:
			replace2 += 1
	return min(replace2 + solve(s, l, (l + r) // 2, chr(ord(c) + 1)), replace1 + solve(s, (l + r) // 2, r, chr(ord(c) + 1)))

def prog():
	for _ in range(int(input())):
		n = int(input())
		s = list(input().strip())
		print(solve(s, 0, n, 'a'))
prog()


import sys
input = lambda : sys.stdin.readline().rstrip()
v = lambda x: ord(x) - ord('a')

def f(l, r, ch):
	ct = 0
	for i in range(l, r + 1):
		if a[i] == ch:
			ct += 1
	return ct

def h(i, j, ch):
	ans = 0
	v1 = f(i, i, ch) + f(j, j, ch)
	v2 = f(i, i, ch + 1) + f(j, j, ch + 1)
	if v1 == 0:
		ans += 1
	if v2 == 0:
		ans += 1
	return ans
for _ in range(int(input())):
	n = int(input())
	a = list(map(lambda x: v(x), input()))
	if n == 1:
		print(0 if a == [0] else 1)
		continue

	def g(i, j, l, ch):
		if l == 0:
			return 0
		if l == 1:
			return h(i, j, ch)
		op1 = f(i, i + l - 1, ch)
		op2 = f(j - l + 1, j, ch)
		return min(l - op1 + g(i + l, j, l >> 1, ch + 1), l - op2 + g(i, j - l, l >> 1, ch + 1))
	print(g(0, n - 1, n >> 1, 0))


def solve(query, good, length):
	if length == 1:
		if query == good:
			return 0
		else:
			return 1
	else:
		mid = int(length / 2)
		left = query[0:mid]
		right = query[mid:length]
		leftCost = 0
		rightCost = 0
		for s in left:
			if s != good:
				leftCost += 1
		for s in right:
			if s != good:
				rightCost += 1
		good = ord(good)
		good += 1
		good = chr(good)
		leftSolve = leftCost + solve(right, good, int(length / 2))
		rightSolve = rightCost + solve(left, good, int(length / 2))
		return min(leftSolve, rightSolve)
TC = int(input())
while TC:
	n = int(input())
	S = input()
	print(solve(S, 'a', n))
	TC -= 1


import sys
input = sys.stdin.readline

def mins(S, c):
	if len(S) == 1:
		if S == c:
			return 0
		else:
			return 1
	LEN = len(S) // 2
	X = S[:LEN]
	Y = S[LEN:]
	ANS1 = 0
	ANS2 = 0
	for x in X:
		if x != c:
			ANS1 += 1
	for y in Y:
		if y != c:
			ANS2 += 1
	return min(ANS1 + mins(Y, chr(ord(c) + 1)), ANS2 + mins(X, chr(ord(c) + 1)))
t = int(input())
for tests in range(t):
	n = int(input())
	S = input().strip()
	print(mins(S, 'a'))


def cost(str, n=97, same=False):
	if same:
		c = 0
		for i in str:
			if ord(i) != n:
				c += 1
		return c
	w = len(str)
	if w == 1:
		if ord(str[0]) == n:
			return 0
		else:
			return 1
	else:
		a = cost(str[:w // 2], n + 1) + cost(str[w // 2:], n, same=True)
		b = cost(str[:w // 2], n, same=True) + cost(str[w // 2:], n + 1)
		return min(a, b)
t = int(input())
for q in range(t):
	n = int(input())
	str = input()
	print(cost(str))


import sys
input = sys.stdin.readline
t = int(input())
letters = 'abcdefghijklmnopqrst'
for _ in range(t):
	b = int(input())
	a = input().rstrip()
	z = list(a)
	ans = [[] for x in range(b)]
	l = 2
	c = 0
	while True:
		length = max(1, b // l)
		for j in range(min(l, b)):
			let = letters[c]
			count = z[length * j:length * (j + 1)].count(let)
			if length == 1:
				if j % 2 == 0:
					tt = j
				else:
					tt = j - 1
				count = min(1, z[length * tt:length * (tt + 2)].count(let))
			for k in range(length):
				if j % 2 == 0:
					hh = j + 1
				else:
					hh = j - 1
				if length > 1:
					ans[length * hh + k].append(count)
				if length < 2:
					ans[length * j + k].append(count)
		if l > b:
			break
		c += 1
		l *= 2
	best = 99999999
	for el in ans:
		best = min(best, b - sum(el))
	print(best)


t = int(input())
az = 'abcdefghijklmnopqrstuvwxyz'

def aze(i, j, nb):
	if i == j:
		if s[i] == az[nb]:
			return 0
		return 1
	d = (j - i) // 2
	diff = 0
	for k in range(i, i + d + 1):
		if s[k] != az[nb]:
			diff += 1
	dif = 0
	for k in range(i + d + 1, j + 1):
		if s[k] != az[nb]:
			dif += 1
	return min(aze(i, i + d, nb + 1) + dif, aze(i + d + 1, j, nb + 1) + diff)
for _ in range(t):
	n = int(input())
	s = str(input())
	print(aze(0, n - 1, 0))


def get_steps_all(string, x):
	n = 0
	for a in string:
		if a != x:
			n += 1
	return n

def get_steps_good(string, x):
	if len(string) == 1:
		if string[0] == x:
			return 0
		else:
			return 1
	mid = int(len(string) / 2)
	result = min(get_steps_all(string[:mid], x) + get_steps_good(string[mid:], chr(ord(x) + 1)), get_steps_good(string[:mid], chr(ord(x) + 1)) + get_steps_all(string[mid:], x))
	return result
n_cases = int(input())
for i in range(0, n_cases):
	length = int(input())
	input_string = input()
	print(get_steps_good(input_string, 'a'))


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nsmallest
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
from decimal import Decimal
mod = pow(10, 9) + 7
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	sys.stdout.write(' '.join(map(str, var)) + end)

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]

def recur(i, j, c):
	if i == j:
		return int(s[i] != chr(c + 97))
	half = j - i + 1 >> 1
	mid = i + half
	return min(recur(i, mid - 1, c + 1) + half - s[mid:j + 1].count(chr(c + 97)), recur(mid, j, c + 1) + half - s[i:mid].count(chr(c + 97)))
for _ in range(int(data())):
	n = int(data())
	s = list(data())
	out(recur(0, n - 1, 0))


def good(s, letter):
	if len(s) == 1:
		return 0 if s == letter else 1
	half = len(s) // 2
	(first, second) = (s[:half], s[half:])
	return min(half - first.count(letter) + good(second, chr(ord(letter) + 1)), half - second.count(letter) + good(first, chr(ord(letter) + 1)))

def good_greedy(s, letter):
	if len(s) == 1:
		return 0 if s == letter else 1
	half = len(s) // 2
	(first, second) = (s[:half], s[half:])
	if first.count(letter) > second.count(letter):
		return half - first.count(letter) + good_greedy(second, chr(ord(letter) + 1))
	else:
		return half - second.count(letter) + good_greedy(first, chr(ord(letter) + 1))
T = int(input())
for _ in range(T):
	n = int(input())
	s = input().strip()
	print(good(s, 'a'))


import sys
import math

def readStr():
	return sys.stdin.readline().rstrip()

def readInts():
	return list(map(int, readStr().split(' ')))

def readLatins():
	return [ord(c) - 97 for c in readStr()]

def main(c, n, s):
	if n == 1:
		if s[0] == c:
			return 0
		else:
			return 1
	n1 = n // 2
	c1 = 0
	c2 = 0
	for i in range(n1):
		if s[i] == c:
			c1 += 1
		if s[-i - 1] == c:
			c2 += 1
	k1 = n1 - c1 + main(c + 1, n1, s[n1:])
	k2 = n1 - c2 + main(c + 1, n1, s[:n1])
	return k1 if k1 < k2 else k2
(t,) = readInts()
for _ in range(t):
	(n,) = readInts()
	s = readLatins()
	k = main(0, n, s)
	print(k)


def fu(l, start, end, e):
	if start > end:
		return 0
	if start == end:
		if l[start] == e:
			return 0
		else:
			return 1
	mid = (end + start) // 2
	left = fu(l, start, mid, e + 1)
	right = fu(l, mid + 1, end, e + 1)
	return min(left + (end - start) // 2 - l[mid + 1:end + 1].count(e), right + (end - start) // 2 - l[start:mid + 1].count(e)) + 1
for i in range(int(input())):
	n = int(input())
	s = input()
	l = []
	for i in s:
		l.append(ord(i) - ord('a') + 1)
	print(fu(l, 0, n - 1, 1))


def res(x, b, l, r):
	s = chr(x)
	if l == r:
		if b[l] == s:
			return 0
		else:
			return 1
	else:
		m = (l + r) // 2
		if b[l] == s:
			k1 = a[x - 97][m] - a[x - 97][l] + 1
		else:
			k1 = a[x - 97][m] - a[x - 97][l]
		if b[m + 1] == s:
			k2 = a[x - 97][r] - a[x - 97][m + 1] + 1
		else:
			k2 = a[x - 97][r] - a[x - 97][m + 1]
		return min(m + 1 - l - k1 + res(x + 1, b, m + 1, r), m + 1 - l - k2 + res(x + 1, b, l, m))
t = int(input())
for i in range(t):
	n = int(input())
	b = []
	s = input()
	a = [[0] * n for j in range(26)]
	for j in s:
		b.append(j)
	for j in range(n):
		q = ord(b[j]) - 97
		for p in range(26):
			if p == q:
				if j != 0:
					a[p][j] = a[p][j - 1] + 1
				else:
					a[p][j] = 1
			elif j != 0:
				a[p][j] = a[p][j - 1]
	print(res(97, b, 0, n - 1))


import math

def ans(arr, n, d, n_global):
	v = n_global / n
	v = math.log(v, 2) + 1
	var = d[v]
	if n == 1:
		if arr[0] == var:
			return 0
		return 1
	sub1 = arr[:n // 2]
	sub2 = arr[n // 2:]
	count1 = 0
	count2 = 0
	for i in sub1:
		if i == var:
			count1 += 1
	for i in sub2:
		if i == var:
			count2 += 1
	v += 1
	ans1 = len(sub1) - count1 + ans(sub2, n // 2, d, n_global)
	ans2 = len(sub2) - count2 + ans(sub1, n // 2, d, n_global)
	return min(ans1, ans2)
m = int(input())
for i in range(m):
	n = int(input())
	arr = input()
	answer = 0
	d = {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f', 7: 'g', 8: 'h', 9: 'i', 10: 'j', 11: 'k', 12: 'l', 13: 'm', 14: 'n', 15: 'o', 16: 'p', 17: 'q', 18: 'r', 19: 's', 20: 't', 21: 'u', 22: 'v', 23: 'w', 24: 'x', 25: 'y', 26: 'z'}
	n_global = n
	print(ans(arr, n, d, n_global))


import sys, math
from collections import deque, defaultdict
import operator as op
from functools import reduce
sys.setrecursionlimit(10 ** 6)
I = sys.stdin.readline

def ii():
	return int(I().strip())

def li():
	return list(map(int, I().strip().split()))

def mi():
	return map(int, I().strip().split())

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def cnt(c, l, r, s):
	x = s[l:r + 1].count(c)
	return r + 1 - l - x

def getmin(l, r, c, s):
	if l == r:
		if s[l] == c:
			return 0
		else:
			return 1
	mid = l + (r - l) // 2
	left = cnt(c, l, mid, s) + getmin(mid + 1, r, chr(ord(c) + 1), s)
	right = cnt(c, mid + 1, r, s) + getmin(l, mid, chr(ord(c) + 1), s)
	return min(left, right)

def main():
	ans = ''
	for _ in range(ii()):
		n = ii()
		s = I().strip()
		x = getmin(0, len(s) - 1, 'a', s)
		ans += str(x) + '\n'
	print(ans)
main()


def goodstring(s, alpha):
	n = len(s)
	if n == 1:
		if s == alpha:
			return 0
		else:
			return 1
	mid = n // 2
	t1 = goodstring(s[:mid], chr(ord(alpha) + 1))
	t2 = goodstring(s[mid:], chr(ord(alpha) + 1))
	t1dash = 0
	t2dash = 0
	for i in range(n):
		if i < mid and s[i] != alpha:
			t2dash += 1
		if i >= mid and s[i] != alpha:
			t1dash += 1
	return min(t1 + t1dash, t2 + t2dash)
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(goodstring(s, 'a'))


def calc(string, i):
	if len(string) == 1:
		return string[0] != i
	s1 = 0
	for t in range(len(string) // 2):
		s1 += string[t] != i
	s1 += calc(string[len(string) // 2:], i + 1)
	s2 = 0
	for t in range(len(string) // 2, len(string)):
		s2 += string[t] != i
	s2 += calc(string[:len(string) // 2], i + 1)
	return min(s1, s2)
for _ in range(int(input())):
	n = int(input())
	s = input()
	s = ['abcdefghijklmnopqrstuvwxyz'.index(t) for t in s]
	print(int(calc(s, 0)))


def make_good(instr, ind1, ind2, inchar):
	if ind1 == ind2:
		if instr[ind1] == inchar:
			return 0
		else:
			return 1
	mid = (ind1 + ind2) // 2
	nextchar = chr(ord(inchar) + 1)
	c1 = make_good(instr, ind1, mid, nextchar) + make_all_char_same(instr, mid + 1, ind2, inchar)
	c2 = make_all_char_same(instr, ind1, mid, inchar) + make_good(instr, mid + 1, ind2, nextchar)
	return min(c1, c2)

def make_all_char_same(instr, ind1, ind2, inchar):
	count = 0
	for i in range(ind1, ind2 + 1):
		if instr[i] != inchar:
			count += 1
	return count
t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	c = make_good(s, 0, n - 1, 'a')
	print(c)


def parser(leng, line, index, how):
	if not how:
		if leng % 4 == 0:
			return leng // 2 - line[:leng // 2].count(let[index]) + min(parser(leng // 2, line[leng // 2:], index + 1, False), parser(leng // 2, line[leng // 2:], index + 1, True))
		elif line[0] == let[index] and line[1] == let[index + 1]:
			return 0
		elif line[0] == let[index]:
			return 1
		elif line[1] == let[index + 1]:
			return 1
		else:
			return 2
	elif leng % 4 == 0:
		return leng // 2 - line[leng // 2:].count(let[index]) + min(parser(leng // 2, line[:leng // 2], index + 1, False), parser(leng // 2, line[:leng // 2], index + 1, True))
	elif line[0] == let[index + 1] and line[1] == let[index]:
		return 0
	elif line[1] == let[index]:
		return 1
	elif line[0] == let[index + 1]:
		return 1
	else:
		return 2
let = 'abcdefghijklmnopqrstuvwxyz'
for i in range(int(input())):
	n = int(input())
	line = input()
	leng = len(line)
	if leng == 1:
		if line == 'a':
			print(0)
		else:
			print(1)
	else:
		res = min(parser(leng, line, 0, False), parser(leng, line, 0, True))
		print(res)


from sys import stdin

def calcgood(l, r, c, sta):
	slices = sta[l:r]
	half = (l + r) // 2
	res = 0
	if l == r - 1:
		if sta[l] == c:
			return 0
		return 1
	fh = 0
	sh = 0
	for i in range(0, len(slices) // 2):
		if slices[i] == c:
			fh += 1
	sh = slices.count(c) - fh
	res = min(len(slices) // 2 - fh + calcgood(half, r, chr(ord(c) + 1), sta), len(slices) // 2 - sh + calcgood(l, half, chr(ord(c) + 1), sta))
	return res
for _ in range(int(stdin.readline())):
	length = int(stdin.readline())
	string = stdin.readline().strip()
	print(calcgood(0, length, 'a', string))


import os
import operator
import sys
from io import BytesIO, IOBase

def inar():
	return [int(k) for k in input().split()]

def recur(prefix, suffix, char):
	ln = len(prefix)
	ans = 0
	if ln == 1:
		if ord(prefix[0]) - 97 != char:
			ans += 1
		if ord(suffix[0]) - 97 != char + 1:
			ans += 1
		return ans
	for i in range(ln):
		if ord(prefix[i]) - 97 != char:
			ans += 1
	st1 = suffix[0:ln // 2]
	st2 = suffix[ln // 2:ln]
	res = min(recur(st1, st2, char + 1), recur(st2, st1, char + 1))
	return ans + res

def main():
	global st, prefix, suffix, ans, lev
	for _ in range(int(input())):
		n = int(input())
		st = input()
		if n == 1:
			if st[0] == 'a':
				print(0)
			else:
				print(1)
			continue
		prefix = st[0:n // 2]
		suffix = st[n // 2:n]
		result = min(recur(prefix, suffix, 0), recur(suffix, prefix, 0))
		print(result)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def func(s, i):
	l = len(s)
	if l == 1:
		if s[0] == tab[i]:
			return 0
		else:
			return 1
	aa = min(func(s[:l // 2], i + 1) + l // 2 - s[l // 2:].count(tab[i]), l // 2 - s[:l // 2].count(tab[i]) + func(s[l // 2:], i + 1))
	return aa
tab = 'abcdefghijklmnopqrstuvwxyz'
for _ in range(int(input())):
	n = int(input())
	s = list(input())
	i = 0
	ans = func(s, i)
	print(ans)


def is_good(s, c):
	return s.count(c) == len(s)

def good(s, c):
	if len(s) == 1:
		return 0 if s[0] == c else 1
	left = s[0:len(s) // 2]
	right = s[len(s) // 2:]
	moves_1 = (0 if is_good(left, c) else len(left) - left.count(c)) + good(right, chr(ord(c) + 1))
	moves_2 = (0 if is_good(right, c) else len(right) - right.count(c)) + good(left, chr(ord(c) + 1))
	return min(moves_1, moves_2)
T = int(input())
for _ in range(T):
	N = input()
	s = input()
	print(good(s, 'a'))


import math
t = int(input())
infinty = math.inf

def solve(char, l, r):
	if l > r:
		return infinty
	c = chr(char)
	if l == r:
		return int(s[l] != c)
	mid = (l + r) // 2
	s_left = s[l:mid + 1]
	s_right = s[mid + 1:r + 1]
	nby2 = len(s_left)
	cnt_right = nby2 - s_right.count(c)
	cnt_right += solve(char + 1, l, mid)
	cnt_left = nby2 - s_left.count(c)
	cnt_left += solve(char + 1, mid + 1, r)
	return min(cnt_right, cnt_left)
for _ in range(t):
	n = int(input())
	s = input()
	print(solve(97, 0, n - 1))


import math
from decimal import Decimal
import heapq
import copy
import heapq
from collections import deque

def na():
	n = int(input())
	b = [int(x) for x in input().split()]
	return (n, b)

def nab():
	n = int(input())
	b = [int(x) for x in input().split()]
	c = [int(x) for x in input().split()]
	return (n, b, c)

def dv():
	(n, m) = map(int, input().split())
	return (n, m)

def da():
	(n, m) = map(int, input().split())
	a = list(map(int, input().split()))
	return (n, m, a)

def dva():
	(n, m) = map(int, input().split())
	a = [int(x) for x in input().split()]
	b = [int(x) for x in input().split()]
	return (n, m, b)

def eratosthenes(n):
	sieve = list(range(n + 1))
	for i in sieve:
		if i > 1:
			for j in range(i + i, len(sieve), i):
				sieve[j] = 0
	return sorted(set(sieve))

def lol(lst, k):
	k = k % len(lst)
	ret = [0] * len(lst)
	for i in range(len(lst)):
		if i + k < len(lst) and i + k >= 0:
			ret[i] = lst[i + k]
		if i + k >= len(lst):
			ret[i] = lst[i + k - len(lst)]
		if i + k < 0:
			ret[i] = lst[i + k + len(lst)]
	return ret

def nm():
	n = int(input())
	b = [int(x) for x in input().split()]
	m = int(input())
	c = [int(x) for x in input().split()]
	return (n, b, m, c)

def dvs():
	n = int(input())
	m = int(input())
	return (n, m)

def fact(n):
	tc = []
	ans = {}
	d = 2
	while d * d <= n:
		if n % d == 0:
			tc.append(d)
			n //= d
		else:
			d += 1
	if n > 1:
		tc.append(n)
	for i in tc:
		ans[i] = ans.get(i, 0) + 1
	return ans

def how_many(l, r, c):
	m = (l + r) // 2
	if r - l == 1:
		if s[l] == c:
			return 0
		else:
			return 1
	d = s[l:m]
	k1 = (r - l) // 2 - s[l:m].count(c) + how_many(m, r, chr(ord(c) + 1))
	k2 = (r - l) // 2 - s[m:r].count(c) + how_many(l, m, chr(ord(c) + 1))
	return min(k1, k2)
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(how_many(0, n, 'a'))


from collections import Counter

def cal(l, r, i):
	if l == r:
		if s[l] == i:
			return 0
		else:
			return 1
	m = (l + r) // 2
	cnt1 = Counter(s[l:m + 1])
	cnt2 = Counter(s[m + 1:r + 1])
	l1 = (r - l + 1) // 2 - cnt1.get(i, 0) + cal(m + 1, r, i + 1)
	l2 = (r - l + 1) // 2 - cnt2.get(i, 0) + cal(l, m, i + 1)
	return min(l1, l2)
x = int(input())
for i in range(x):
	n = int(input())
	s = input()
	s = [ord(i) - ord('a') for i in s]
	print(cal(0, len(s) - 1, 0))


t = int(input())
global s

def cost(n, c, f):
	if n == 1:
		if s[f] == c:
			return 0
		else:
			return 1
	half = n // 2
	return min(cost(half, chr(ord(c) + 1), f) + half - s[f + half:f + n].count(c), cost(half, chr(ord(c) + 1), f + half) + half - s[f:f + half].count(c))
for test in range(t):
	n = int(input())
	s = input()
	print(cost(n, 'a', 0))


from sys import setrecursionlimit
from collections import deque
Z = 'abcdefghijklmnopqrstuvwxyz'

def count(S, c):
	if len(S) == 1:
		if S[0] == Z[c]:
			return 0
		else:
			return 1
	else:
		n = len(S)
		n2 = n // 2
		(count0, count1) = (0, 0)
		for i in range(n2):
			if S[i] == Z[c]:
				count0 += 1
			if S[i + n // 2] == Z[c]:
				count1 += 1
		ans1 = n2 - count0 + count(S[n2:], c + 1)
		ans2 = n2 - count1 + count(S[:n2], c + 1)
		return min(ans1, ans2)

def solve():
	n = int(input())
	S = input()
	print(count(S, 0))
t = int(input())
while t > 0:
	t -= 1
	solve()


def recur(s, p):
	if len(s) == 1:
		c = chr(96 + p)
		if s == c:
			return 0
		return 1
	n = len(s) // 2
	left = s[:n]
	right = s[n:]
	c = chr(96 + p)
	d1 = {}
	d2 = {}
	for i in range(n):
		d1[left[i]] = d1.get(left[i], 0) + 1
		d2[right[i]] = d2.get(right[i], 0) + 1
	val1 = d1.get(c, 0)
	val2 = d2.get(c, 0)
	left_ans = n - val1 + recur(right, p + 1)
	right_ans = n - val2 + recur(left, p + 1)
	return min(left_ans, right_ans)
for _ in range(int(input())):
	n = int(input())
	s = input()
	val = recur(s, 1)
	print(val)


def calc(s, c):
	cnt = 0
	for i in s:
		if i != c:
			cnt += 1
	return cnt

def getAns(s, c):
	if len(s) == 1:
		if s[0] == c:
			return 0
		else:
			return 1
	else:
		mid = len(s) >> 1
		left = calc(s[:mid], c) + getAns(s[mid:], chr(ord(c) + 1))
		right = getAns(s[:mid], chr(ord(c) + 1)) + calc(s[mid:], c)
		return min(left, right)
for t in range(int(input())):
	n = int(input())
	s = input()
	ans = getAns(s, 'a')
	print(ans)


def distance(s, left, right, num):
	length = right - left
	if length == 1:
		return 0 if s[left] == num else 1
	mid = left + length // 2
	cnt = 0
	for i in range(left, mid):
		if s[i] != num:
			cnt += 1
	left_case = cnt + distance(s, mid, right, num + 1)
	cnt = 0
	for i in range(mid, right):
		if s[i] != num:
			cnt += 1
	right_case = distance(s, left, mid, num + 1) + cnt
	return min(left_case, right_case)
for _ in range(int(input())):
	n = int(input())
	s = list(map(lambda c: ord(c) - ord('a'), list(input())))
	print(distance(s, 0, n, 0))


def c_good(sarr, size, value):
	if size == 1:
		if sarr[0] == chr(value):
			return 0
		else:
			return 1
	else:
		hsize = int(size / 2)
		fhalf = sarr[:hsize]
		shalf = sarr[hsize:]
		chtr = chr(value)
		c1 = len([x for x in fhalf if x != chtr])
		c2 = len([x for x in shalf if x != chtr])
		m1 = c2 + c_good(fhalf, hsize, value + 1)
		m2 = c1 + c_good(shalf, hsize, value + 1)
		return min(m1, m2)
T = int(input())
for t in range(T):
	n = int(input())
	st = input()
	value = 97
	move = c_good(st, n, value)
	print(move)


def recur(s, a):
	if len(s) == 0:
		return 0
	if len(s) == 1:
		if s == a:
			return 0
		else:
			return 1
	m = len(s) // 2
	start = s[:m]
	end = s[m:]
	op1 = m - start.count(a) + recur(end, chr(ord(a) + 1))
	op2 = m - end.count(a) + recur(start, chr(ord(a) + 1))
	return min(op1, op2)
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(recur(s, 'a'))


def cost(L, R, i, PST):
	return R - (L - 1) - (PST[i][R] - PST[i][L - 1])

def solve(L, R, i, PST, s):
	if L == R:
		return 0 if s[L] == i else 1
	else:
		M = (L + R) // 2
		return min(cost(L, M, i, PST) + solve(M + 1, R, i + 1, PST, s), solve(L, M, i + 1, PST, s) + cost(M + 1, R, i, PST))
for t in range(int(input())):
	n = int(input())
	s = [27] + [ord(d) - ord('a') for d in input()]
	PST = [[0 for i in range(n + 1)] for j in range(26)]
	for c in range(26):
		for i in range(1, n + 1):
			PST[c][i] = PST[c][i - 1] + (1 if s[i] == c else 0)
	print(solve(1, n, 0, PST, s))


import math
import bisect
import copy
import time
from collections import Counter

def f(cur, le, re, ln):
	if ln == 0:
		if l[le] != cur:
			return 1
		else:
			return 0
	count1 = 0
	count2 = 0
	for i in range(le, le + ln):
		if l[i] != cur:
			count1 += 1
	for i in range(le + ln, re + 1):
		if l[i] != cur:
			count2 += 1
	count1 += f(cur + 1, le + ln, re, ln >> 1)
	count2 += f(cur + 1, le, le + ln - 1, ln >> 1)
	return min(count1, count2)
for _ in range(int(input())):
	n = int(input())
	x = input()
	l = []
	for i in range(n):
		l.append(ord(x[i]) - 97 + 1)
	print(f(1, 0, n - 1, n >> 1))


def isGoodString(s, n, c, ans):
	if n == 1:
		if s[0] != c:
			ans += 1
		return ans
	staage = s[:n // 2]
	aage = n // 2 - staage.count(c)
	stpiche = s[n // 2:]
	piche = n // 2 - stpiche.count(c)
	return min(isGoodString(s[n // 2:], n // 2, chr(ord(c) + 1), ans + aage), isGoodString(s[:n // 2], n // 2, chr(ord(c) + 1), ans + piche))
for _ in range(int(input())):
	n = int(input())
	s = input()
	ans = 0
	if n == 1:
		if s != 'a':
			ans = 1
	else:
		ans = isGoodString(s, n, 'a', 0)
	print(ans)


alpha = 'abcdefghijklmnopqrstuvwxyz'
pref = [[0 for i in range(131073)] for j in range(26)]
res = 10 ** 8

def query(l, r, cur):
	if l == 0:
		return pref[ord(cur) - 97][r]
	else:
		return pref[ord(cur) - 97][r] - pref[ord(cur) - 97][l - 1]

def rec(s, l, r, ans, cur):
	global res, pref
	if l == r:
		if s[l] != cur:
			ans += 1
		res = min(ans, res)
		return
	mid = (l + r) // 2
	rec(s, mid + 1, r, ans + (mid - l + 1) - query(l, mid, cur), chr(ord(cur) + 1))
	rec(s, l, mid, ans + (r - mid) - query(mid + 1, r, cur), chr(ord(cur) + 1))
for _ in range(int(input())):
	res = 10 ** 8
	n = int(input())
	s = input()
	for i in range(26):
		for j in range(n):
			pref[i][j] = 0
	for i in range(n):
		if i > 0:
			for j in range(26):
				pref[j][i] = pref[j][i - 1]
		pref[ord(s[i]) - 97][i] += 1
	rec(s, 0, n - 1, 0, 'a')
	print(res)


import sys
INP = lambda : sys.stdin.readline().strip()
INT = lambda : int(INP())
MAP = lambda : map(int, INP().split())
ARR = lambda : [int(i) for i in INP().split()]

def JOIN(arr, x=' '):
	return x.join([str(i) for i in arr])

def EXIT(x='NO'):
	print(x)
	exit()

def solve(n, s, c='a'):
	if n == 1:
		return int(s != c)
	(m, d) = (n // 2, chr(ord(c) + 1))
	(l, r) = (s[:m].count(c), s[m:].count(c))
	return min(m - l + solve(m, s[m:], d), m - r + solve(m, s[:m], d))
for _ in range(INT()):
	n = INT()
	s = INP()
	print(solve(n, s))


def goodString(s, a, i, j):
	if i == j:
		return 0 if s[i] == a else 1
	mid = (i + j) // 2
	d1 = 0
	for k in range(i, mid + 1):
		d1 += 1 if s[k] != a else 0
	d1 += goodString(s, chr(ord(a) + 1), mid + 1, j)
	d2 = 0
	for k in range(mid + 1, j + 1):
		d2 += 1 if s[k] != a else 0
	d2 += goodString(s, chr(ord(a) + 1), i, mid)
	return min(d1, d2)
for _ in range(int(input())):
	n = int(input())
	s = list(input())
	print(goodString(s, 'a', 0, n - 1))


import sys, math
import io, os
from bisect import bisect_left as bl, bisect_right as br, insort
from collections import defaultdict as dd, deque, Counter

def data():
	return sys.stdin.readline().strip()

def mdata():
	return list(map(int, data().split()))

def outl(var):
	sys.stdout.write(' '.join(map(str, var)) + '\n')

def out(var):
	sys.stdout.write(str(var) + '\n')
from fractions import Fraction
INF = float('inf')
mod = int(1000000000.0) + 7
ans = INF

def recur(l, r, c, v):
	global ans
	if l - r == 0:
		if s[l] != c:
			v += 1
		ans = min(ans, v)
		return
	v1 = 0
	for i in range(l, (l + r) // 2 + 1):
		if s[i] != c:
			v1 += 1
	recur((l + r) // 2 + 1, r, chr(ord(c) + 1), v + v1)
	v2 = 0
	for i in range((l + r) // 2 + 1, r + 1):
		if s[i] != c:
			v2 += 1
	recur(l, (l + r) // 2, chr(ord(c) + 1), v + v2)
for t in range(int(data())):
	n = int(data())
	s = data()
	ans = INF
	recur(0, n - 1, 'a', 0)
	out(ans)


testcases = int(input())

def dfs(strs, prev):
	x = chr(ord(prev) + 1)
	n = len(strs)
	if n == 1 and strs == x:
		return 0
	if n == 1:
		return 1
	half = n // 2
	firststr = strs[:half]
	secstr = strs[half:]
	flen = 0
	slen = 0
	for i in firststr:
		if i != x:
			flen += 1
	for i in secstr:
		if i != x:
			slen += 1
	return min(flen + dfs(strs[half:], x), slen + dfs(strs[:half], x))
for testcase in range(testcases):
	n = int(input())
	strs = input()
	if n == 1 and strs == 'a':
		print(0)
		continue
	if n == 1:
		print(1)
		continue
	half = n // 2
	firststr = strs[:half]
	secstr = strs[half:]
	ans = n
	flen = 0
	slen = 0
	for i in firststr:
		if i != 'a':
			flen += 1
	for i in secstr:
		if i != 'a':
			slen += 1
	ans = min(flen + dfs(strs[half:], 'a'), slen + dfs(strs[:half], 'a'))
	print(ans)


def good_string(n, i, j, c, s):
	if i == j:
		if s[i] == c:
			return 0
		else:
			return 1
	mid = (j + i + 1) // 2
	count = 0
	for k in range(i, mid):
		if s[k] != c:
			count += 1
	ans1 = count + good_string(n, mid, j, chr(ord(c) + 1), s)
	count = 0
	for k in range(mid, j + 1):
		if s[k] != c:
			count += 1
	ans2 = count + good_string(n, i, mid - 1, chr(ord(c) + 1), s)
	return min(ans1, ans2)
t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	print(good_string(n, 0, n - 1, 'a', s))


s = ''
f = 0

def bracktrack(a, b, val, ans):
	global s
	global f
	if a == b:
		if s[a] == chr(val):
			f = min(f, ans)
		else:
			f = min(f, ans + 1)
		return
	m = (a + b) // 2
	temp = ans
	for i in range(a, m + 1):
		if s[i] != chr(val):
			temp += 1
	bracktrack(m + 1, b, val + 1, temp)
	temp = ans
	for i in range(m + 1, b + 1):
		if s[i] != chr(val):
			temp += 1
	bracktrack(a, m, val + 1, temp)
import sys
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	f = n
	bracktrack(0, n - 1, 97, 0)
	print(f)


from math import log2

def getmax(arr):
	max1 = 0
	max2 = 0
	ind = -1
	for i in range(len(arr)):
		if arr[i] > max1:
			max1 = arr[i]
			ind = i
		elif arr[i] > max2:
			max2 = arr[i]
	return (max1, max2, ind)
for _ in range(int(input())):
	n = int(input())
	val = int(log2(n))
	a = list(input())
	if n == 1:
		if a[0] == 'a':
			print(0)
		else:
			print(1)
		continue
	for i in range(n):
		a[i] = ord(a[i]) - ord('a')
	arr = []
	x = 2
	for i in range(val):
		v = [0] * x
		arr.append(v)
		x *= 2
	for i in range(n):
		for j in range(val):
			if a[i] == j:
				arr[j][i * len(arr[j]) // n] += 1
	x = 4
	for i in range(1, val):
		for j in range(x):
			v = j // 2
			if v % 2 == 0:
				arr[i][j] += arr[i - 1][v + 1]
			else:
				arr[i][j] += arr[i - 1][v - 1]
		x *= 2
	for i in range(n):
		if i % 2 == 0:
			if a[i + 1] == val:
				arr[val - 1][i] += 1
		elif a[i - 1] == val:
			arr[val - 1][i] += 1
	print(n - max(arr[-1]))


import sys
sys.setrecursionlimit(200000)

def mapper(c):
	return ord(c) - ord('a')

def remapper(c):
	return chr(c + ord('a'))
for _ in range(int(input())):
	n = int(input())
	s = input()

	def func(st, end, ch):
		if st == end:
			if s[st] == ch:
				return 0
			else:
				return 1
		m = (st + end) // 2
		c1 = func(m + 1, end, remapper(mapper(ch) + 1))
		c2 = func(st, m, remapper(mapper(ch) + 1))
		for i in range(st, m + 1):
			if s[i] != ch:
				c1 += 1
		for i in range(m + 1, end + 1):
			if s[i] != ch:
				c2 += 1
		return min(c1, c2)
	print(func(0, n - 1, 'a'))


def movestomakecgood(c, s):
	if len(s) == 1:
		return int(s != c)
	return min(sum((x != c for x in s[len(s) // 2:])) + movestomakecgood(chr(ord(c) + 1), s[:len(s) // 2]), movestomakecgood(chr(ord(c) + 1), s[len(s) // 2:]) + sum((x != c for x in s[:len(s) // 2])))

def solve(s):
	return movestomakecgood('a', s)
for _ in range(int(input())):
	n = int(input())
	s = input().rstrip()
	print(solve(s))


import os
import sys
from io import BytesIO, IOBase
import threading
from bisect import bisect_right
from math import gcd, log
from collections import Counter

def main():
	for _ in range(int(input())):
		n = int(input())
		s = input()
		cnt = [[0] * 26 for i in range(n + 1)]
		for i in range(n):
			for j in range(26):
				cnt[i + 1][j] = cnt[i][j]
			ch = ord(s[i]) - 97
			cnt[i + 1][ch] += 1

		def recur(l, r, s, cc):
			if l > r:
				return
			mid = l + (r - l) // 2
			if l == r:
				return 1 - cnt[mid][cc] + cnt[l - 1][cc]
			c1 = cnt[r][cc] - cnt[mid][cc]
			c2 = cnt[mid][cc] - cnt[l - 1][cc]
			r1 = r2 = float('inf')
			r1 = recur(l, mid, max(s // 2, 1), cc + 1)
			r2 = recur(mid + 1, r, max(1, s // 2), cc + 1)
			a = min(r1 + s - c1, r2 + s - c2)
			return a
		l = 1
		r = n
		cc = 0
		s = max(n // 2, 1)
		ans = recur(l, r, s, cc)
		print(ans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def solve(n, s):

	def _doit(l, r, c):
		ch = chr(ord('a') + c)
		if l + 1 == r:
			return int(s[l] != ch)
		mid = l + (r - l) // 2
		non1 = sum((int(s[i] != ch) for i in range(l, mid)))
		non2 = sum((int(s[i] != ch) for i in range(mid, r)))
		return min(non1 + _doit(mid, r, c + 1), non2 + _doit(l, mid, c + 1))
	return _doit(0, n, 0)
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	print(solve(n, s))


def count_changes(s, c):
	return sum([1 for x in s if x != c])

def next_alpha(c):
	assert len(c) == 1
	return chr(ord(c) + 1)

def solve(s, c):
	n = len(s)
	if n == 1:
		return 1 if s != c else 0
	return min(solve(s[n // 2:], next_alpha(c)) + count_changes(s[:n // 2], c), count_changes(s[n // 2:], c) + solve(s[:n // 2], next_alpha(c)))
t = int(input())
while t:
	n = int(input())
	s = input()
	print(solve(s, 'a'))
	t -= 1


def get_cost(ls, start, end, letter):
	if start < end:
		mid = (end - start) // 2 + start
		cost_1 = 0
		for item in ls[start:mid + 1]:
			if item != letter:
				cost_1 += 1
		cost_2 = 0
		for item in ls[mid + 1:end + 1]:
			if item != letter:
				cost_2 += 1
		cost_1 += get_cost(ls, mid + 1, end, chr(ord(letter) + 1))
		cost_2 += get_cost(ls, start, mid, chr(ord(letter) + 1))
		return min(cost_1, cost_2)
	elif start == end:
		if ls[start] == letter:
			return 0
		else:
			return 1
	else:
		return 0
tests = int(input())
for t in range(tests):
	n = int(input())
	ls = input()
	ls_len = len(ls)
	print(get_cost(ls, 0, len(ls) - 1, 'a'))


def rec(s, low, high, c):
	low = 0
	high = len(s)
	if len(s) == 1:
		if s[0] == c:
			return 0
		else:
			return 1
	mid = (low + high) // 2
	return min(len(s) // 2 - s[low:mid].count(c) + rec(s[mid:high], mid, high, chr(ord(c) + 1)), len(s) // 2 - s[mid:high].count(c) + rec(s[low:mid], low, mid, chr(ord(c) + 1)))
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(rec(s, 0, n, 'a'))


def cost(s, l, r, ch):
	count = 0
	for i in range(l, r):
		if s[i] != ch:
			count += 1
	return count

def solve(s, l, r, ch):
	if l + 1 == r:
		if s[l] == ch:
			return 0
		return 1
	mid = (l + r) // 2
	return min(cost(s, l, mid, ch) + solve(s, mid, r, chr(ord(ch) + 1)), cost(s, mid, r, ch) + solve(s, l, mid, chr(ord(ch) + 1)))
for _ in range(int(input())):
	n = int(input())
	s = input()
	ch = 'a'
	print(solve(s, 0, n, ch))


import sys
input = sys.stdin.readline
t = int(input())

def distance(s, c):
	m = len(s)
	if m == 1:
		return int(s != c)
	else:
		left_good = distance(s[0:m // 2], chr(ord(c) + 1))
		right_good = distance(s[m // 2:], chr(ord(c) + 1))
		left_distance = sum([c != i for i in s[0:m // 2]])
		right_distance = sum([c != i for i in s[m // 2:]])
		dist = min(left_good + right_distance, right_good + left_distance)
		return dist
for _ in range(t):
	n = int(input())
	input_string = ''.join(input().split())
	print(distance(input_string, 'a'))


from collections import deque
from collections import OrderedDict
import math
import sys
import os
import threading
import bisect
import operator
import heapq
from atexit import register
from io import BytesIO
import io
input = lambda : sys.stdin.readline().rstrip('\r\n')
for t in range(int(input())):
	n = int(input())
	s = [' '] + list(str(input()))
	if n == 1:
		if s[1] == 'a':
			print(0)
		else:
			print(1)
		continue
	if n == 2:
		if s[1] == 'a' or s[2] == 'a':
			if s[1] == 'b' or s[2] == 'b':
				print(0)
			else:
				print(1)
		elif s[1] == 'b' or s[2] == 'b':
			print(1)
		else:
			print(2)
		continue
	ar26 = [[0] * (n + 2) for i in range(26)]
	for i in range(26):
		vA = ord('a') + i
		for j in range(1, n + 1):
			ar26[i][j] = ar26[i][j - 1]
			if ord(s[j]) == vA:
				ar26[i][j] += 1
	dp = [0] * (n * 3)
	(step, idp, sym) = (n // 2, 3, 1)
	dp[1] = step - (ar26[0][step] - ar26[0][0])
	dp[2] = step - (ar26[0][step * 2] - ar26[0][step])
	step //= 2
	answer = 999999999
	while step >= 1:
		bol = 1
		for i in range(1, n + 1, step):
			per = step - (ar26[sym][i + step - 1] - ar26[sym][i - 1])
			if bol % 4 == 1 or bol % 4 == 2:
				dp[idp] = dp[(idp - 1) // 2 + 1] + per
			else:
				dp[idp] = dp[(idp - 1) // 2 - 1] + per
			if step == 1:
				st = chr(97 + sym + 1)
				if bol % 4 % 2 == 1 and s[i + 1] != st:
					dp[idp] += 1
				elif bol % 4 % 2 == 0 and s[i - 1] != st:
					dp[idp] += 1
				answer = min(answer, dp[idp])
			bol += 1
			idp += 1
		sym += 1
		step //= 2
	print(answer)


def f(s, n, c):
	if n == 1:
		if s[0] == c:
			return 0
		return 1
	count_1 = 0
	count_2 = 0
	for char in s[:n // 2]:
		if char != c:
			count_1 += 1
	for char in s[n // 2:]:
		if char != c:
			count_2 += 1
	return min(f(s[n // 2:], n // 2, chr(ord(c) + 1)) + count_1, f(s[:n // 2], n // 2, chr(ord(c) + 1)) + count_2)
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	print(f(s, n, 'a'))


t = int(input())
cnt = 0
while cnt < t:
	cnt += 1
	n = int(input())
	s = input()
	dp = [0] * n
	tmp = n
	deg = 0
	while tmp != 1:
		tmp //= 2
		deg += 1
	p = deg
	for i in range(n):
		if s[i] == str(chr(97 + deg)):
			dp[i] = 0
		else:
			dp[i] = 1
	while n > 1:
		n //= 2
		deg -= 1
		tmp = 2 ** (p - deg - 1)
		for i in range(n):
			dp[i] = min(dp[2 * i] + tmp - s[2 * tmp * i + tmp:2 * tmp * i + 2 * tmp].count(chr(97 + deg)), dp[2 * i + 1] + tmp - s[2 * tmp * i:2 * tmp * i + tmp].count(chr(97 + deg)))
	print(dp[0])


import sys
input = sys.stdin.readline

def counter(char, l, r):
	(mina1, mina2) = (0, 0)
	mid = (l + r) // 2
	if l == r:
		if s[l] == char:
			return 0
		else:
			return 1
	for i in range(l, mid + 1):
		if s[i] != char:
			mina1 += 1
	for i in range(mid + 1, r + 1):
		if s[i] != char:
			mina2 += 1
	return min(mina1 + counter(chr(ord(char) + 1), mid + 1, r), mina2 + counter(chr(ord(char) + 1), l, mid))
t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	print(counter('a', 0, n - 1))


def cgood(palavra, c):
	tamanho = len(palavra)
	if tamanho == 1:
		return 1 if palavra != c else 0
	proximo_c = chr(ord(c) + 1)
	centro = tamanho // 2
	parte1 = palavra[:centro]
	parte2 = palavra[centro:]
	countC1 = parte1.count(c)
	countC2 = parte2.count(c)
	passosParte1 = len(parte1) - countC1 + cgood(parte2, proximo_c)
	passosParte2 = len(parte2) - countC2 + cgood(parte1, proximo_c)
	if passosParte1 < passosParte2:
		return passosParte1
	else:
		return passosParte2
n = int(input())
for i in range(n):
	tamanho = int(input())
	palavra = input()
	print(cgood(palavra, 'a'))


for _ in range(int(input())):
	n = int(input())
	s = input()

	def stupid(strin, char):
		if len(strin) == 1:
			if strin[0] == char:
				return 0
			else:
				return 1
		else:
			(ct1, ct2) = (0, 0)
			for i in range(len(strin) // 2):
				if strin[i] != char:
					ct1 += 1
			for i in range(len(strin) // 2, len(strin)):
				if strin[i] != char:
					ct2 += 1
			return min(ct2 + stupid(strin[:len(strin) // 2], chr(ord(char) + 1)), ct1 + stupid(strin[len(strin) // 2:], chr(ord(char) + 1)))
	print(stupid(s, 'a'))


def countGood(s, c):
	curlen = len(s)
	if curlen == 1:
		if s[0] == c:
			return 0
		else:
			return 1
	else:
		countL = 0
		for letter in s[:curlen // 2]:
			if letter != c:
				countL += 1
		countL += countGood(s[curlen // 2:], chr(ord(c) + 1))
		countR = 0
		for letter in s[curlen // 2:]:
			if letter != c:
				countR += 1
		countR += countGood(s[:curlen // 2], chr(ord(c) + 1))
		return countL if countL < countR else countR
t = int(input())
for j in range(t):
	n = int(input())
	s = input()
	print(countGood(s, 'a'))


import sys
sys.setrecursionlimit(int(1000000.0))

def solve(L, target, i, j):
	targetChar = chr(ord('a') + target)
	if i == j:
		if L[i] == targetChar:
			return 0
		else:
			return 1
	n = j - i + 1
	mid = (i + j) // 2
	countFirst = 0
	for k in range(i, mid + 1):
		if L[k] == targetChar:
			countFirst += 1
	ans1 = n // 2 - countFirst + solve(L, target + 1, mid + 1, j)
	countSecond = 0
	for k in range(mid + 1, j + 1):
		if L[k] == targetChar:
			countSecond += 1
	ans2 = n // 2 - countSecond + solve(L, target + 1, i, mid)
	return min(ans1, ans2)
t = int(input())
for _ in range(t):
	_ = int(input())
	s = input()
	print(solve(s, 0, 0, len(s) - 1))


def agoodString(s, c):
	if len(s) == 1:
		if s[0] == c:
			return 0
		else:
			return 1
	mid = len(s) // 2
	cntl = agoodString(s[:mid], chr(ord(c) + 1))
	cntl += len(s) // 2 - s[mid:].count(c)
	cntr = agoodString(s[mid:], chr(ord(c) + 1))
	cntr += len(s) // 2 - s[:mid].count(c)
	return min(cntl, cntr)
t = int(input())
for _ in range(t):
	n = int(input())
	s = input()
	print(agoodString(s, 'a'))


from sys import stdin

def mina(s, letter):
	if len(s) == 0:
		return 0
	if len(s) == 1:
		if s[0] == letter:
			return 0
		else:
			return 1
	else:
		l = len(s) // 2
		letter2 = chr(ord(letter) + 1)
		(L, R) = (s[:l], s[l:])
		costLeft = l - L.count(letter) + mina(R, letter2)
		costRight = l - R.count(letter) + mina(L, letter2)
		return min(costLeft, costRight)
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	s = stdin.readline().strip()
	print(mina(s, 'a'))


def good(s, k, el, cost, record):
	if len(s) == 1:
		if s == el:
			record.append(cost)
		else:
			record.append(cost + 1)
		return 0
	nxt = chr(ord(el) + 1)
	k1 = k - 1
	left = 0
	right = 0
	for i in range(2 ** k1):
		if s[i] == el:
			left += 1
	for i in range(2 ** k1, 2 ** k):
		if s[i] == el:
			right += 1
	if left == right:
		c1 = cost + (2 ** k1 - left)
		good(s[:2 ** k1], k1, nxt, c1, record)
		good(s[2 ** k1:], k1, nxt, c1, record)
	elif left > right:
		if left > 2 ** (k1 - 1):
			c2 = cost + (2 ** k1 - left)
			good(s[2 ** k1:], k1, nxt, c2, record)
		else:
			c3 = cost + (2 ** k1 - right)
			c4 = cost + (2 ** k1 - left)
			good(s[:2 ** k1], k1, nxt, c3, record)
			good(s[2 ** k1:], k1, nxt, c4, record)
	elif right > 2 ** (k1 - 1):
		c2 = cost + (2 ** k1 - right)
		good(s[:2 ** k1], k1, nxt, c2, record)
	else:
		c3 = cost + (2 ** k1 - right)
		c4 = cost + (2 ** k1 - left)
		good(s[:2 ** k1], k1, nxt, c3, record)
		good(s[2 ** k1:], k1, nxt, c4, record)
	return 0
import math
t = int(input())
for _ in range(t):
	n = int(input())
	k = int(math.log(n, 2))
	s = input()
	rec = []
	good(s, k, 'a', 0, rec)
	print(min(rec))


from sys import *
input = stdin.readline
t = 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']

def fun(s, k):
	n = len(s)
	if k == 26 or s == '':
		return 0
	a = s[:n // 2]
	b = s[n // 2:]
	c1 = a.count(alpha[k])
	c2 = b.count(alpha[k])
	n = n // 2
	if n == 0:
		n = 1
	return min(n - c2 + fun(a, k + 1), n - c1 + fun(b, k + 1))
for _ in range(t):
	n = int(input())
	s = input().strip()
	if s == 'a':
		print(0)
	elif n == 1:
		print(1)
	else:
		print(fun(s, 0))


from __future__ import division, print_function
MOD = 998244353
mod = 10 ** 9 + 7

def prepare_factorial():
	fact = [1]
	for i in range(1, 100005):
		fact.append(fact[-1] * i % mod)
	ifact = [0] * 100005
	ifact[100004] = pow(fact[100004], mod - 2, mod)
	for i in range(100004, 0, -1):
		ifact[i - 1] = i * ifact[i] % mod
	return (fact, ifact)
import sys
from bisect import bisect_left, bisect_right, insort
from math import floor, ceil, sqrt, degrees, atan, pi, log, sin, radians
from heapq import heappop, heapify, heappush
from collections import Counter, defaultdict, deque

def modinv(n, p):
	return pow(n, p - 2, p)

def ncr(n, r, fact, ifact):
	t = fact[n] * (ifact[r] * ifact[n - r]) % mod % mod
	return t

def intarray():
	return map(int, sys.stdin.readline().strip().split())

def array():
	return list(map(int, sys.stdin.readline().strip().split()))

def input():
	return sys.stdin.readline().strip()

def GCD(x, y):
	while y:
		(x, y) = (y, x % y)
	return x

def lcm(x, y):
	return x * y // GCD(x, y)

def get_xor(n):
	return [n, 1, n + 1, 0][n % 4]

def fast_expo(a, b):
	res = 1
	while b:
		if b & 1:
			res = res * a
			res %= MOD
			b -= 1
		else:
			a = a * a
			a %= MOD
			b >>= 1
	res %= MOD
	return res

def get_n(P):
	ans = (-1 + sqrt(1 + 8 * P)) // 2
	return ans
mini = 0

def string(s, ch):
	if len(s) == 1:
		if s[0] == chr(ch):
			return 0
		else:
			return 1
	k1 = string(s[len(s) // 2:], ch + 1) + len(s) // 2 - s[:len(s) // 2].count(chr(ch))
	k2 = string(s[:len(s) // 2], ch + 1) + len(s) // 2 - s[len(s) // 2:].count(chr(ch))
	return min(k1, k2)

def solve():
	n = int(input())
	s = input()
	print(string(s, 97))
	return

def main():
	T = int(input())
	while T:
		solve()
		T -= 1
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def main():
	t = int(input())
	for _ in range(t):
		n = int(input())
		s = input()
		l = []
		k = len(bin(n)) - 2
		alph = 'abcdefghijklmnopqrstuvwxyz'
		last_letter = alph[k - 1]
		for i in range(n):
			if s[i] == last_letter:
				l.append(0)
			else:
				l.append(1)
		for idx in range(k - 1):
			letter = alph[idx]
			num_groups = 2 ** idx
			group_size = n // num_groups
			for group_idx in range(num_groups):
				group_start = group_idx * group_size
				midpoint = group_start + group_size // 2
				first_half = group_size // 2 - s.count(letter, group_start, midpoint)
				second_half = group_size // 2 - s.count(letter, midpoint, group_start + group_size)
				for x1 in range(group_start, midpoint):
					l[x1] += second_half
				for x2 in range(midpoint, group_start + group_size):
					l[x2] += first_half
		print(min(l))
main()


import math
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n
avl = AvlTree()

class SegmentTree1:

	def __init__(self, data, default='z', func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] <= key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater

def check(s, n, ch):
	if n == 0:
		return 0
	elif n == 1:
		if s[0] != ch:
			return 1
		else:
			return 0
	ans = 0
	ans1 = 0
	for i in range(0, n // 2):
		if s[i] != ch:
			ans += 1
	ans += check(s[n // 2:], n // 2, chr(ord(ch) + 1))
	for i in range(n // 2, n):
		if s[i] != ch:
			ans1 += 1
	ans1 += check(s[0:n // 2], n // 2, chr(ord(ch) + 1))
	return min(ans1, ans)
for ik in range(int(input())):
	n = int(input())
	s = input()
	if n == 1:
		if s == 'a':
			print(0)
		else:
			print(1)
		continue
	ans = check(s, n, 'a')
	print(ans)


from sys import stdin, stdout
from collections import Counter
t = int(stdin.readline().strip())
next = {}
alpha = 'abcdefghijklmnopqrstuvwxyz'
for i in range(25):
	next[alpha[i]] = alpha[i + 1]
for _ in range(t):
	n = int(stdin.readline().strip())
	s = stdin.readline().strip()
	C = {}

	def count(i, l):
		if l == 1:
			cnt = Counter()
			cnt[s[i]] += 1
		else:
			ll = l // 2
			cnt = count(i, ll) + count(i + ll, ll)
		C[i, i + l] = cnt
		return cnt
	count(0, n)

	def solve(i, j, c):
		if j - i == 1 or c == 'z':
			return j - i - C[i, j][c]
		l = (j - i) // 2
		a1 = l - C[i, i + l][c] + solve(i + l, j, next[c])
		a2 = l - C[i + l, j][c] + solve(i, i + l, next[c])
		return min(a1, a2)
	ans = solve(0, n, 'a')
	stdout.write('{}\n'.format(ans))


def verifica(string, c):
	if len(string) == 1:
		return int(string[0] != c)
	tamanho = len(string)
	return min(tamanho // 2 - string[:tamanho // 2].count(c) + verifica(string[tamanho // 2:], chr(ord(c) + 1)), tamanho // 2 - string[tamanho // 2:].count(c) + verifica(string[:tamanho // 2], chr(ord(c) + 1)))
t = int(input())
for _ in range(t):
	tamanho = int(input())
	string = input()
	print(verifica(string, 'a'))


def solve(s, ch):
	if len(s) == 1:
		if s[0] == ch:
			return 0
		else:
			return 1
	n = len(s)
	a = s[0:n // 2]
	b = s[n // 2:n]
	acount = a.count(ch)
	bcount = b.count(ch)
	ch = chr(ord(ch) + 1)
	op1 = len(a) - acount + solve(b, ch)
	op2 = len(b) - bcount + solve(a, ch)
	return min(op1, op2)
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(solve(s, 'a'))


from math import log2

def dfs(a, b, num, kek, symb):
	global ans
	if num == 0:
		if ord(ar[b]) != symb:
			ans = min(ans, kek + 1)
		else:
			ans = min(ans, kek)
		return
	for i in range(a, b):
		if ord(ar[i]) != symb:
			kek += 1
	dfs(b, b + (b - a) // 2, num - 1, kek, symb + 1)
	for i in range(a, b):
		if ord(ar[i]) != symb:
			kek -= 1
	for i in range(b, b + (b - a)):
		if ord(ar[i]) != symb:
			kek += 1
	dfs(a, a + (b - a) // 2, num - 1, kek, symb + 1)
for __ in range(int(input())):
	n = int(input())
	ar = list(input())
	ans = 10 ** 9
	dfs(0, n // 2, log2(n), 0, 97)
	print(ans)


def fact(x):
	if x == 0:
		return 1
	else:
		return x * fact(x - 1)

def lower_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] >= num:
			answer = middle
			end = middle - 1
		else:
			start = middle + 1
	return answer

def upper_bound(li, num):
	answer = -1
	start = 0
	end = len(li) - 1
	while start <= end:
		middle = (end + start) // 2
		if li[middle] <= num:
			answer = middle
			start = middle + 1
		else:
			end = middle - 1
	return answer

def abs(x):
	return x if x >= 0 else -x

def binary_search(li, val, lb, ub):
	ans = 0
	while lb <= ub:
		mid = (lb + ub) // 2
		if li[mid] > val:
			ub = mid - 1
		elif val > li[mid]:
			lb = mid + 1
		else:
			ans = 1
			break
	return ans

def sieve_of_eratosthenes(n):
	ans = []
	arr = [1] * (n + 1)
	(arr[0], arr[1], i) = (0, 0, 2)
	while i * i <= n:
		if arr[i] == 1:
			j = i + i
			while j <= n:
				arr[j] = 0
				j += i
		i += 1
	for k in range(n):
		if arr[k] == 1:
			ans.append(k)
	return ans

def nc2(x):
	if x == 1:
		return 0
	else:
		return x * (x - 1) // 2

def kadane(x):
	sum_so_far = 0
	current_sum = 0
	for i in x:
		current_sum += i
		if current_sum < 0:
			current_sum = 0
		else:
			sum_so_far = max(sum_so_far, current_sum)
	return sum_so_far

def mex(li):
	check = [0] * 1001
	for i in li:
		check[i] += 1
	for i in range(len(check)):
		if check[i] == 0:
			return i

def sumdigits(n):
	ans = 0
	while n != 0:
		ans += n % 10
		n //= 10
	return ans

def product(li):
	ans = 1
	for i in li:
		ans *= i
	return ans

def maxpower(n, k):
	cnt = 0
	while n > 1:
		cnt += 1
		n //= k
	return cnt
from math import *
from collections import deque
max_steps = 100005

def check(s, n, ch):
	if n == 0:
		return 0
	if n == 1:
		if s == ch:
			return 0
		else:
			return 1
	ans = 0
	ans1 = 0
	for i in range(n // 2):
		if s[i] != ch:
			ans += 1
	ans += check(s[n // 2:], n // 2, chr(ord(ch) + 1))
	for i in range(n // 2, n):
		if s[i] != ch:
			ans1 += 1
	ans1 += check(s[:n // 2], n // 2, chr(ord(ch) + 1))
	return min(ans, ans1)
for _ in range(int(input())):
	n = int(input())
	st = input()
	if n == 1:
		print(0 if st == 'a' else 1)
	else:
		ans = check(st, n, 'a')
		print(ans)


def ans(s, n, c):
	if n == 1:
		if s[0] == c:
			return 0
		else:
			return 1
	str1 = s[:n // 2]
	str2 = s[n // 2:]
	c1 = str1.count(c)
	s1 = n // 2 - c1 + ans(str2, n // 2, chr(ord(c) + 1))
	c2 = str2.count(c)
	s2 = n // 2 - c2 + ans(str1, n // 2, chr(ord(c) + 1))
	return min(s1, s2)
for i in range(int(input())):
	n = int(input())
	x = input()
	print(ans(x, n, 'a'))


from sys import stdin
input = stdin.readline
INF = 10 ** 9 + 7
MAX = 10 ** 7 + 7
MOD = 10 ** 9 + 7
for Ti in range(int(input().strip())):
	n = int(input().strip())
	s = input().strip()
	s = [ord(x) - ord('a') for x in s]
	cnt = [[0] * 26 for _ in range(len(s) + 1)]
	cnt[0][s[0]] = 1
	for si in range(1, len(s)):
		for sj in range(26):
			cnt[si][sj] = cnt[si - 1][sj] + (1 if s[si] == sj else 0)

	def solve(l, r, ind):
		if l == r:
			if ind == s[l]:
				return 0
			else:
				return 1
		mid = l + (r - l) // 2
		span = mid - l + 1
		l_ind = cnt[mid][ind] - cnt[l - 1][ind]
		r_ind = cnt[r][ind] - cnt[mid][ind]
		l_ind = span - l_ind
		r_ind = span - r_ind
		ln = solve(l, mid, ind + 1)
		rn = solve(mid + 1, r, ind + 1)
		return min(ln + r_ind, rn + l_ind)
	print(solve(0, n - 1, 0))


(T,) = map(int, input().split())
alpha = 'abcdefghijklmnopqrstuvwxyz'
for _ in range(T):
	(N,) = map(int, input().split())
	s = input().strip()
	S = [[0] * (len(s) + 1) for i in range(26)]
	for c in range(26):
		for i in range(len(s)):
			S[c][i + 1] = S[c][i] + (alpha[c] == s[i])
	tmp = N
	mk = 0
	while tmp:
		tmp //= 2
		mk += 1
	RR = 10 ** 18
	for k in range(2 * N):
		R = 0
		(st, en) = (0, len(s))
		for i in range(mk):
			(k, m) = divmod(k, 2)
			if m:
				st2 = st + (en - st) // 2
				R += en - st2 - (S[i][en] - S[i][st2])
				en = en - (en - st) // 2
			else:
				en2 = en - (en - st) // 2
				R += en2 - st - (S[i][en2] - S[i][st])
				st = st + (en - st) // 2
		RR = min(R, RR)
	print(RR)


import math
from collections import *
import sys
from functools import reduce
from itertools import groupby
sys.setrecursionlimit(10 ** 6)

def get_ints():
	return map(int, input().strip().split())

def get_list():
	return list(get_ints())

def get_string():
	return list(input().strip().split())

def printxsp(*args):
	return print(*args, end='')

def printsp(*args):
	return print(*args, end=' ')
DIRECTIONS = [[0, 1], [0, -1], [1, 0], [1, -1]]
NEIGHBOURS = [(i, j) for i in range(-1, 2) for j in range(-1, 2) if i != 0 or j != 0]
OrdUnicode_a = ord('a')
OrdUnicode_A = ord('A')
CAPS_ALPHABETS = {chr(i + OrdUnicode_A): i for i in range(26)}
SMOL_ALPHABETS = {i: chr(i + OrdUnicode_a) for i in range(26)}
MOD_JOHAN = int(1000000000.0) + 7
MOD_LIGHT = 998244353
INFINITY = float('inf')
MAXN_EYEPATCH = int(100000.0) + 1
MAXN_FULLMETAL = 501

def solver(l, r, sz, chatr):
	if sz == 0:
		if s[l] == SMOL_ALPHABETS[chatr]:
			return 0
		else:
			return 1
	cnt1 = cnt2 = 0
	for ind in range(l, l + sz):
		if s[ind] != SMOL_ALPHABETS[chatr]:
			cnt1 += 1
	for ind in range(r - sz, r):
		if s[ind] != SMOL_ALPHABETS[chatr]:
			cnt2 += 1
	zizzag = min(cnt1 + solver(r - sz, r, sz // 2, chatr + 1), cnt2 + solver(l, l + sz, sz // 2, chatr + 1))
	return zizzag
for _testcases_ in range(int(input())):
	n = int(input())
	s = input()
	print(solver(0, n, n // 2, 0))


def calc(s, c):
	return len(s) - s.count(c)

def ans(s, c):
	if len(s) == 1:
		if s == c:
			return 0
		else:
			return 1
	return min(calc(s[:len(s) // 2], c) + ans(s[len(s) // 2:], chr(ord(c) + 1)), calc(s[len(s) // 2:], c) + ans(s[:len(s) // 2], chr(ord(c) + 1)))
t = int(input())
for i in range(t):
	n = int(input())
	s = input()
	print(ans(s, 'a'))


import sys, math, itertools
from collections import Counter, deque, defaultdict
from bisect import bisect_left, bisect_right
sys.setrecursionlimit(10 ** 8)
mod = 10 ** 9 + 7
INF = float('inf')

def inp():
	return int(sys.stdin.readline())

def inpl():
	return list(map(int, sys.stdin.readline().split()))

def dfs(l, r, c, res):
	if r - l == 1:
		if s[l] != chr(ord('a') + c):
			res += 1
		global ans
		ans = min(ans, res)
		return
	ch = chr(ord('a') + c)
	L = R = 0
	mid = (l + r) // 2
	for i in range(l, mid):
		if s[i] != ch:
			L += 1
	for i in range(mid, r):
		if s[i] != ch:
			R += 1
	dfs(mid, r, c + 1, res + L)
	dfs(l, mid, c + 1, res + R)
for _ in range(inp()):
	n = inp()
	s = sys.stdin.readline()
	s = s[:len(s) - 1]
	ans = INF
	dfs(0, n, 0, 0)
	print(ans)


import sys, bisect, string, math, time, functools, random, fractions
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
from itertools import permutations, combinations, groupby
rep = range
R = range

def Golf():
	(n, *t) = map(int, open(0).read().split())

def I():
	return int(input())

def S_():
	return input()

def IS():
	return input().split()

def LS():
	return [i for i in input().split()]

def MI():
	return map(int, input().split())

def LI():
	return [int(i) for i in input().split()]

def LI_():
	return [int(i) - 1 for i in input().split()]

def NI(n):
	return [int(input()) for i in range(n)]

def NI_(n):
	return [int(input()) - 1 for i in range(n)]

def StoLI():
	return [ord(i) - 97 for i in input()]

def ItoS(n):
	return chr(n + 97)

def LtoS(ls):
	return ''.join([chr(i + 97) for i in ls])

def RA():
	return map(int, open(0).read().split())

def GI(V, E, ls=None, Directed=False, index=1):
	org_inp = []
	g = [[] for i in range(V)]
	FromStdin = True if ls == None else False
	for i in range(E):
		if FromStdin:
			inp = LI()
			org_inp.append(inp)
		else:
			inp = ls[i]
		if len(inp) == 2:
			(a, b) = inp
			c = 1
		else:
			(a, b, c) = inp
		if index == 1:
			a -= 1
			b -= 1
		aa = (a, c)
		bb = (b, c)
		g[a].append(bb)
		if not Directed:
			g[b].append(aa)
	return (g, org_inp)

def GGI(h, w, search=None, replacement_of_found='.', mp_def={'#': 1, '.': 0}, boundary=1):
	mp = [boundary] * (w + 2)
	found = {}
	for i in R(h):
		s = input()
		for char in search:
			if char in s:
				found[char] = (i + 1) * (w + 2) + s.index(char) + 1
				mp_def[char] = mp_def[replacement_of_found]
		mp += [boundary] + [mp_def[j] for j in s] + [boundary]
	mp += [boundary] * (w + 2)
	return (h + 2, w + 2, mp, found)

def TI(n):
	return GI(n, n - 1)

def accum(ls):
	rt = [0]
	for i in ls:
		rt += [rt[-1] + i]
	return rt

def bit_combination(n, base=2):
	rt = []
	for tb in R(base ** n):
		s = [tb // base ** bt % base for bt in R(n)]
		rt += [s]
	return rt

def gcd(x, y):
	if y == 0:
		return x
	if x % y == 0:
		return y
	while x % y != 0:
		(x, y) = (y, x % y)
	return y

def YN(x):
	print(['NO', 'YES'][x])

def Yn(x):
	print(['No', 'Yes'][x])

def show(*inp, end='\n'):
	if show_flg:
		print(*inp, end=end)
mo = 10 ** 9 + 7
inf = float('inf')
FourNb = [(-1, 0), (1, 0), (0, 1), (0, -1)]
EightNb = [(-1, 0), (1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)]
compas = dict(zip('WENS', FourNb))
cursol = dict(zip('LRUD', FourNb))
l_alp = string.ascii_lowercase
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
input = lambda : sys.stdin.readline().rstrip()

def primeFactor(N):
	(i, n) = (2, N)
	ret = {}
	(d, sq) = (2, 99)
	while i <= sq:
		k = 0
		while n % i == 0:
			(n, k, ret[i]) = (n // i, k + 1, k + 1)
		if k > 0 or i == 97:
			sq = int(n ** (1 / 2) + 0.5)
		if i < 4:
			i = i * 2 - 1
		else:
			(i, d) = (i + d, d ^ 6)
	if n > 1:
		ret[n] = 1
	return ret

def divisor(n):
	div = [1]
	for (i, j) in primeFactor(n).items():
		div = [i ** k * d for d in div for k in range(j + 1)]
	return div

def PrimeNumSet(N):
	M = int(N ** 0.5)
	seachList = [i for i in range(2, N + 1)]
	primes = []
	while seachList:
		if seachList[0] > M:
			break
		primes.append(seachList[0])
		tmp = seachList[0]
		seachList = [i for i in seachList if i % tmp != 0]
	return primes + seachList

def LCM(b, mo=10 ** 9 + 7):
	prs = PrimeNumSet(max(b))
	M = dict(zip(prs, [0] * len(prs)))
	for i in b:
		dc = primeFactor(i)
		for (j, k) in dc.items():
			M[j] = max(M[j], k)
	r = 1
	for (j, k) in M.items():
		if k != 0:
			r *= pow(j, k, mo)
			r %= mo
	return r
show_flg = False
show_flg = True

def naive(n, t):
	(s, m, e) = (0, n // 2, n)
	x = 0
	ans = 0
	while e - s > 1:
		f = sum((t[i] != x for i in range(s, m)))
		l = sum((t[i] != x for i in range(m, e)))
		if f > l:
			e -= (e - s) // 2
			m -= (e - s) // 2
			ans += l
		else:
			s += (e - s) // 2
			m += (e - s) // 2
			ans += f
		x += 1
	if t[s] != x:
		ans += 1
	return ans

def solve(n, t):
	global show_flg
	k = n.bit_length() - 1
	c = [[0] for x in range(26)]
	for i in t:
		for x in range(26):
			if x == i:
				c[x] += (1 + c[x][-1],)
			else:
				c[x] += (c[x][-1],)
	ans = 2 << k
	for i in range(1 << k):
		tmp = 0
		(s, e) = (0, n)
		l = 0
		if i == (1 << k) - 1:
			show_flg = True
		else:
			show_flg = False
		ln = 1 << k
		for j in range(k + 1):
			ln >>= 1
			if i >> j & 1:
				s = l + ln
				e = s + ln
			else:
				s = l
				e = s + ln
				l += ln
			tmp += e - s - (c[j][e] - c[j][s])
		tmp += 1 - (c[j][s + 1] - c[j][s])
		ans = min(ans, tmp)
	return ans
ans = 0
for _ in range(I()):
	n = I()
	t = StoLI()
	ans = naive(n, t)
	ans = solve(n, t)
	print(ans)


def ans(i, a, s):
	if a == 1:
		if s == q[i]:
			return 0
		else:
			return 1
	else:
		(z, x) = (0, 0)
		r = s[0:a // 2]
		t = s[a // 2:a]
		for j in r:
			if j != q[i]:
				z += 1
		for j in t:
			if j != q[i]:
				x += 1
		return min(z + ans(i + 1, a // 2, t), x + ans(i + 1, a // 2, r))
q = list('abcdefghijklmnopqrstuvwxyz')
for _ in range(int(input())):
	a = int(input())
	s = input()
	print(ans(0, a, s))


from sys import stdin
input = lambda : stdin.readline().rstrip('\r\n')
from collections import deque as que, defaultdict as vector
from heapq import *
inin = lambda : int(input())
inar = lambda : list(map(int, input().split()))
from types import GeneratorType
from math import log2
alp = 'abcdefghijklmnopqrstuvwxyz'

def bootstrap(f, stack=[]):

	def wrappedfunc(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		else:
			to = f(*args, **kwargs)
			while True:
				if type(to) is GeneratorType:
					stack.append(to)
					to = next(to)
				else:
					stack.pop()
					if not stack:
						break
					to = stack[-1].send(to)
			return to
	return wrappedfunc

@bootstrap
def solve(c, l, r, s):
	mid = (l + r) // 2
	c1 = 0
	c2 = 0
	ch = alp[c]
	if l == r - 1:
		if ch == s[l]:
			yield 0
		else:
			yield 1
	for i in range(l, mid):
		if s[i] != ch:
			c1 += 1
	for i in range(mid, r):
		if s[i] != ch:
			c2 += 1
	yield min(c1 + (yield solve(c + 1, mid, r, s)), c2 + (yield solve(c + 1, l, mid, s)))
Testcase = inin()
for i in range(Testcase):
	n = inin()
	s = input()
	k = int(log2(n) + 1)
	print(solve(0, 0, n, s))


s = ''

def count_replacement(lo: int, hi: int, b: str):
	count = 0
	for i in range(lo, hi + 1):
		char = s[i]
		if char != b:
			count += 1
	return count

def good_moves(lo: int, hi: int, c: str):
	mid = (lo + hi) // 2
	if lo == hi:
		if s[lo] == c:
			return 0
		else:
			return 1
	first_half = count_replacement(lo, mid, c) + good_moves(mid + 1, hi, chr(ord(c) + 1))
	second_half = count_replacement(mid + 1, hi, c) + good_moves(lo, mid, chr(ord(c) + 1))
	return min(first_half, second_half)
for _ in range(int(input())):
	n = int(input())
	s = input()
	print(good_moves(0, n - 1, 'a'))


production = True
import sys, math, collections

def input(input_format=0, multi=0):
	if multi > 0:
		return [input(input_format) for i in range(multi)]
	else:
		next_line = sys.stdin.readline()[:-1]
		if input_format >= 10:
			use_list = False
			input_format = int(str(input_format)[-1])
		else:
			use_list = True
		if input_format == 0:
			formatted_input = [next_line]
		elif input_format == 1:
			formatted_input = list(map(int, next_line.split()))
		elif input_format == 2:
			formatted_input = list(map(float, next_line.split()))
		elif input_format == 3:
			formatted_input = list(next_line)
		elif input_format == 4:
			formatted_input = list(map(int, list(next_line)))
		elif input_format == 5:
			formatted_input = next_line.split()
		else:
			formatted_input = [next_line]
		return formatted_input if use_list else formatted_input[0]

def out(output_line, output_format=0, newline=True):
	formatted_output = ''
	if output_format == 0:
		formatted_output = str(output_line)
	elif output_format == 1:
		formatted_output = ' '.join(map(str, output_line))
	elif output_format == 2:
		formatted_output = '\n'.join(map(str, output_line))
	print(formatted_output, end='\n' if newline else '')

def log(*args):
	if not production:
		print('$$$', end='')
		print(*args)
enu = enumerate
ter = lambda a, b, c: b if a else c
ceil = lambda a, b: -(-a // b)

def mapl(iterable, format=0):
	if format == 0:
		return list(map(int, iterable))
	elif format == 1:
		return list(map(str, iterable))
	elif format == 2:
		return list(map(list, iterable))
a = []
w = dict()

def f(l, r, c):
	return w[c][r] - w[c][l]

def b(l, r, c):
	if r - l == 1:
		return ter(a[l] == c, 0, 1)
	m = l + (r - l >> 1)
	h1 = a[l:m]
	h2 = a[m:r]
	t1 = f(l, m, c)
	t2 = f(m, r, c)
	la = m - l
	k1 = la - t1
	k2 = la - t2
	log(c, h1, k1, t1, l, m)
	log(c, h2, k2, t2, m, r)
	nc = chr(ord(c) + 1)
	ee = min(k1 + b(m, r, nc), k2 + b(l, m, nc))
	log(l, r, ee, k1, k2)
	log('')
	return ee

def solve():
	global a
	global w
	n = input(11)
	a = input(3)
	u = [chr(i) for i in range(97, 97 + 26)]
	for i in u:
		w[i] = [0]
		for j in a:
			if j == i:
				w[i].append(w[i][-1] + 1)
			else:
				w[i].append(w[i][-1])
		w[i].append(w[i][-1])
	log(a)
	for (i, j) in w.items():
		if i == 'e':
			break
		log(i, j)
	out(b(0, n, 'a'))
	log('')
	return
for i in range(input(11)):
	solve()


import os
is_dev = 'vscode' in os.environ
if is_dev:
	inF = open('in.txt', 'r')
	outF = open('out.txt', 'w')

def ins(r):
	return list(map(int, r.split(' ')))

def inputT():
	if is_dev:
		return inF.readline()[:-1]
	else:
		return input()

def printT(data):
	if is_dev:
		return outF.write(str(data) + '\n')
	else:
		return print(data)
dp = {}

def solve(s, c):
	if (s, c) in dp:
		return dp[s, c]
	if len(s) == 1:
		return 0 if s == c or s == '*' else 1
	if s == '*' * len(s):
		return 0
	half_len = int(len(s) / 2)
	left_str = s[:half_len]
	right_str = s[half_len:]
	modified_left = left_str.replace(c, '*')
	modified_right = right_str.replace(c, '*')
	left_str_c_count = left_str.count(c)
	right_str_c_count = right_str.count(c)
	left_star = left_str.count('*')
	right_star = right_str.count('*')
	take_left = solve(modified_right, chr(ord(c) + 1)) + right_str_c_count + half_len - left_str_c_count - left_star
	take_right = solve(modified_left, chr(ord(c) + 1)) + left_str_c_count + half_len - right_str_c_count - right_star
	dp[s, c] = min(take_left, take_right)
	return dp[s, c]
for _ in range(int(inputT())):
	n = int(inputT())
	x = inputT()
	print(solve(x, 'a'))
if is_dev:
	print(open('out.txt', 'r') == open('outactual.txt', 'r'))


alf = 'abcdefghijklmnopqrstuvwxyz'

def solve(s, l, r, z):
	if r - l == 1:
		if s[l] == alf[z]:
			return 0
		else:
			return 1
	else:
		m = (l + r) // 2
		return min(solve(s, l, m, z + 1) + (r - l) // 2 - s[m:r].count(alf[z]), solve(s, m, r, z + 1) + (r - l) // 2 - s[l:m].count(alf[z]))
for _ in range(int(input())):
	n = int(input())
	s = list(input())
	print(solve(s, 0, n, 0))