Word $s$ of length $n$ is called $k$-complete if   $s$ is a palindrome, i.e. $s_i=s_{n+1-i}$ for all $1 \le i \le n$;  $s$ has a period of $k$, i.e. $s_i=s_{k+i}$ for all $1 \le i \le n-k$. 

For example, "abaaba" is a $3$-complete word, while "abccba" is not.

Bob is given a word $s$ of length $n$ consisting of only lowercase Latin letters and an integer $k$, such that $n$ is divisible by $k$. He wants to convert $s$ to any $k$-complete word.

To do this Bob can choose some $i$ ($1 \le i \le n$) and replace the letter at position $i$ with some other lowercase Latin letter.

So now Bob wants to know the minimum number of letters he has to replace to convert $s$ to any $k$-complete word.

Note that Bob can do zero changes if the word $s$ is already $k$-complete.

You are required to answer $t$ test cases independently.


-----Input-----

The first line contains a single integer $t$ ($1 \le t\le 10^5$) — the number of test cases.

The first line of each test case contains two integers $n$ and $k$ ($1 \le k < n \le 2 \cdot 10^5$, $n$ is divisible by $k$).

The second line of each test case contains a word $s$ of length $n$.

It is guaranteed that word $s$ only contains lowercase Latin letters. And it is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.


-----Output-----

For each test case, output one integer, representing the minimum number of characters he has to replace to convert $s$ to any $k$-complete word.


-----Example-----
Input
4
6 2
abaaba
6 3
abaaba
36 9
hippopotomonstrosesquippedaliophobia
21 7
wudixiaoxingxingheclp

Output
2
0
23
16



-----Note-----

In the first test case, one optimal solution is aaaaaa.

In the second test case, the given word itself is $k$-complete.

Difficulty: MEDIUM
Tags: Graph traversal

String algorithms

Implementation

Greedy algorithms

Spanning trees
Skill types: Greedy algorithms

Starter code:

Solutions:
def main():
	return '\n'.join((nCharactersToReplaceMin() for _ in range(int(input()))))

def nCharactersToReplaceMin():

	def nDesiredCharacters(limit1, limit2):

		def countOfMostRepeatedCharacter(i1, i2):
			lettersCount = {}
			for (j1, j2) in zip(range(i1, len(word), period), range(i2, len(word), period)):
				if word[j1] not in lettersCount:
					lettersCount[word[j1]] = 1
				else:
					lettersCount[word[j1]] += 1
				if word[j2] not in lettersCount:
					lettersCount[word[j2]] = 1
				else:
					lettersCount[word[j2]] += 1
			return max(lettersCount.values())
		return sum((countOfMostRepeatedCharacter(i1, i2) for (i1, i2) in zip(range(limit1), range(period - 1, limit2, -1))))
	(word_sLength, period) = map(int, input().split())
	word = input()
	limit = period // 2
	if period % 2:
		middleLettersCount = {}
		for i in range(limit, word_sLength, period):
			if word[i] not in middleLettersCount:
				middleLettersCount[word[i]] = 1
			else:
				middleLettersCount[word[i]] += 1
		return str(word_sLength - max(middleLettersCount.values()) - nDesiredCharacters(limit, limit))
	else:
		return str(word_sLength - nDesiredCharacters(limit, limit - 1))
print(main())


import sys
import os
from io import BytesIO, IOBase

def cel(n, k):
	return n // k + (n % k != 0)

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

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

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

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

def prr(a, sep=' '):
	print(sep.join(map(str, a)))

def dd():
	return defaultdict(int)

def ddl():
	return defaultdict(list)

def ddd():
	return defaultdict(defaultdict(int))
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 collections import defaultdict
from math import gcd
for _ in range(ii()):
	(n, k) = mi()
	s = input()
	cnt = n // k
	res = 0
	for i in range(k):
		alp = [0 for i in range(26)]
		for j in range(cnt):
			tmpi = i + j * k
			alp[ord(s[tmpi]) - 97] += 1
			alp[ord(s[n - 1 - tmpi]) - 97] += 1
		res += sum(alp) - max(alp)
	print(res // 2)


t = int(input())
for T in range(t):
	(n, k) = [int(x) for x in input().split()]
	s = input()
	ans = 0
	for i in range(k):
		alpha = [0] * 26
		for j in range(i, n, k):
			alpha[ord(s[j]) - 97] += 1
			alpha[ord(s[k - j - 1]) - 97] += 1
		ans += sum(alpha) - max(alpha)
	print(ans // 2)


from sys import stdin
import math
a = int(stdin.readline())
for b in range(0, a):
	A = stdin.readline().split()
	n = int(A[0])
	k = int(A[1])
	L = list()
	B = stdin.readline()
	count = 0
	for t in range(0, int(n / k)):
		s = ''
		for q in range(0, k):
			s += B[count]
			count += 1
		L.append(s)
	ans = 0
	for u in range(0, math.ceil(k / 2)):
		Y = list()
		if k % 2 == 1 and u == math.ceil(k / 2) - 1:
			for y in range(0, int(n / k)):
				Y.append(L[y][u])
			W = set(Y)
			record = 0
			for t in W:
				if Y.count(t) > record:
					record = Y.count(t)
			ans += int(n / k) - record
			break
		for y in range(0, int(n / k)):
			Y.append(L[y][u])
			Y.append(L[y][k - u - 1])
		W = set(Y)
		record = 0
		for t in W:
			if Y.count(t) > record:
				record = Y.count(t)
		ans += int(2 * (n / k)) - record
	print(ans)


for x in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	ans = 0
	if k % 2 == 0:
		for i in range(k // 2):
			li = [0] * 27
			for j in range(n // k):
				li[ord(s[k * j + i]) - 96] += 1
				li[ord(s[k * (j + 1) - (i + 1)]) - 96] += 1
			ans += 2 * (n // k) - max(li)
		print(ans)
	else:
		for i in range(k // 2):
			li = [0] * 27
			for j in range(n // k):
				li[ord(s[k * j + i]) - 96] += 1
				li[ord(s[k * (j + 1) - (i + 1)]) - 96] += 1
			ans += 2 * (n // k) - max(li)
		li = [0] * 27
		for j in range(n // k):
			li[ord(s[k * j + k // 2]) - 96] += 1
		ans += n // k - max(li)
		print(ans)


t = int(input())
while t:
	(n, k) = map(int, input().split())
	s = input()
	s = [s[i] for i in range(n)]
	ans = 0
	if k % 2 == 0:
		for i in range(k // 2):
			freq = [0 for j in range(26)]
			for j in range(i, n, k):
				freq[ord(s[j]) - 97] += 1
			for j in range(k - 1 - i, n, k):
				freq[ord(s[j]) - 97] += 1
			total = 0
			maxi = 0
			for j in range(26):
				total += freq[j]
				maxi = max(maxi, freq[j])
			ans += total - maxi
	else:
		for i in range(k // 2):
			freq = [0 for j in range(26)]
			for j in range(i, n, k):
				freq[ord(s[j]) - 97] += 1
			for j in range(k - 1 - i, n, k):
				freq[ord(s[j]) - 97] += 1
			total = 0
			maxi = 0
			for j in range(26):
				total += freq[j]
				maxi = max(maxi, freq[j])
			ans += total - maxi
		freq = [0 for j in range(26)]
		for j in range(k // 2, n, k):
			freq[ord(s[j]) - 97] += 1
		total = 0
		maxi = 0
		for j in range(26):
			total += freq[j]
			maxi = max(maxi, freq[j])
		ans += total - maxi
	print(ans)
	t -= 1


from collections import Counter
import sys
input = sys.stdin.readline
for ti in range(int(input().strip())):
	(n, k) = [int(x) for x in input().strip().split()]
	s = input().strip()
	g = [[] for _ in range(n)]
	for ni in range(n):
		if ni + k < n:
			g[ni].append(ni + k)
			g[ni + k].append(ni)
		g[ni].append(n - ni - 1)
	v = [0] * n

	def dfs(node, freq):
		stack = [node]
		while stack:
			node = stack.pop()
			v[node] = 1
			freq[s[node]] += 1
			for ni in g[node]:
				if not v[ni]:
					v[ni] = 1
					stack.append(ni)
		return freq
	ans = 0
	for ni in range(n):
		if not v[ni]:
			freq = Counter()
			freq = dfs(ni, freq)
			ans += sum(freq.values()) - freq.most_common(1)[0][1]
	print(ans)


import os
import sys
from io import BytesIO, IOBase

def differ(cnt, a, b):
	(total, max_occur) = (sum(cnt[a] + cnt[b]), 0)
	for i in range(26):
		max_occur = max(cnt[a][i] + cnt[b][i], max_occur)
	return total - max_occur

def solution(s, n, k):
	cnt = [[0] * 26 for _ in range(k)]
	for i in range(n):
		cnt[i % k][ord(s[i]) - ord('a')] += 1
	res = 0
	for i in range(k):
		res += differ(cnt, i, k - i - 1)
	write(res // 2)

def main():
	for _ in range(r_int()):
		(n, k) = r_array()
		s = input()
		solution(s, n, k)
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))

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

def write(*args, end='\n'):
	for x in args:
		sys.stdout.write(str(x) + ' ')
	sys.stdout.write(end)

def r_array():
	return [int(x) for x in input().split()]

def r_int():
	return int(input())
main()


import sys

def run_case():
	(n, k) = map(int, input().split())
	s = input()
	cnt = [[0] * 26 for j in range((k + 1) // 2)]
	ch = list(map(lambda x: ord(x) - ord('a'), s))
	for i in range(n):
		cnt[min(i % k, k - i % k - 1)][ch[i]] += 1
	res = 0
	for i in range(k // 2):
		res += 2 * n // k - max(cnt[i])
	if k % 2 == 1:
		res += n // k - max(cnt[k // 2])
	print(res)
t = int(input())
for _ in range(t):
	run_case()


for t in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	div = n // k
	ans = 0
	if k % 2 == 0:
		a = k // 2
	else:
		a = k // 2 + 1
	for i in range(a):
		arr = [0] * 26
		for j in range(div):
			if i != k - 1 - i:
				arr[ord(s[j * k + i]) - 97] += 1
				arr[ord(s[j * k + k - i - 1]) - 97] += 1
			else:
				arr[ord(s[j * k + i]) - 97] += 1
		ans += sum(arr) - max(arr)
	print(ans)


T = int(input())
for _ in range(T):
	(n, k) = map(int, input().split())
	s = input()
	i = 0
	tc = 0
	k1 = k
	while i < k:
		A = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
		m = 0
		if (n - 2 * i - 1) % k1 == 0:
			for j in range(i, n, k1):
				A[ord(s[j]) - 97] = A[ord(s[j]) - 97] + 1
				m = max(m, A[ord(s[j]) - 97])
			tc = tc + n // k1 - m
		else:
			for j in range(i, n, k1):
				A[ord(s[n - j - 1]) - 97] = A[ord(s[n - j - 1]) - 97] + 1
				A[ord(s[j]) - 97] = A[ord(s[j]) - 97] + 1
				m = max(m, A[ord(s[j]) - 97], A[ord(s[n - j - 1]) - 97])
			k = k - 1
			tc = tc + n // k1 * 2 - m
		i = i + 1
	print(tc)


import operator
T = int(input())
n = [0] * T
k = [0] * T
ss = [0] * T
for t in range(T):
	(n[t], k[t]) = [int(i) for i in input().split(' ')]
	ss[t] = input()
for t in range(T):
	ans = n[t]
	g = n[t] // k[t]
	for i in range(int((k[t] + 1) * 0.5)):
		uns = {}
		for j in range(g):
			if j * k[t] + i != (j + 1) * k[t] - i - 1:
				m = 2
				for ind in (j * k[t] + i, (j + 1) * k[t] - i - 1):
					if ss[t][ind] in uns:
						uns[ss[t][ind]] += 1
					else:
						uns[ss[t][ind]] = 1
			else:
				ind = j * k[t] + i
				m = 1
				if ss[t][ind] in uns:
					uns[ss[t][ind]] += 1
				else:
					uns[ss[t][ind]] = 1
		ans -= max(uns.items(), key=operator.itemgetter(1))[1]
	print(ans)


import collections

def mostRepeatedLetter(st):
	d = collections.defaultdict(int)
	for c in st:
		d[c] += 1
	return d[sorted(d, key=d.get, reverse=True)[0]]

def run(n, k, string):
	numChanges = 0
	for u in range(int(k / 2)):
		letters = ''
		for j in range(int(n / k)):
			curPalindrome = string[j * k:j * k + k]
			letters = letters + curPalindrome[u]
			letters = letters + curPalindrome[-(u + 1)]
		numChanges += len(letters) - mostRepeatedLetter(letters)
	if k % 2 == 1:
		u = int(k / 2)
		letters = ''
		for j in range(int(n / k)):
			curPalindrome = string[j * k:j * k + k]
			letters = letters + curPalindrome[u]
		numChanges += len(letters) - mostRepeatedLetter(letters)
	print(numChanges)
times = int(input())
for i in range(times):
	(n, k) = map(int, input().split())
	string = input()
	run(n, k, string)


for i in range(int(input())):
	[n, k] = list(map(int, input().split()))
	p = int(n / k)
	s = list(input())
	total = 0
	for j in range(1, int((k + 1) / 2) + 1):
		dom = {}
		for l in range(p):
			dom[s[l * k + j - 1]] = dom.get(s[l * k + j - 1], 0) + 1
			dom[s[(l + 1) * k - j]] = dom.get(s[(l + 1) * k - j], 0) + 1
		largest = max(dom.values())
		if j == k + 1 - j:
			total += p - largest / 2
		else:
			total += 2 * p - largest
	print(int(total))


for h in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	matrix = [s[i:i + k] for i in range(0, n, k)]
	ans = 0
	if k % 2 == 1:
		cnt = [0] * 26
		for i in range(n // k):
			cnt[ord(matrix[i][k // 2]) - 97] += 1
		ans += n // k - max(cnt)
	else:
		for i in range(n // k):
			matrix[i] = matrix[i][:k // 2] + '#' + matrix[i][k // 2:]
		k += 1
	for j in range(k // 2):
		cnt = [0] * 26
		for i in range(len(matrix)):
			cnt[ord(matrix[i][j]) - 97] += 1
			cnt[ord(matrix[i][k - j - 1]) - 97] += 1
		ans += 2 * len(matrix) - max(cnt)
	print(ans)


from collections import defaultdict
from math import ceil

def solve(st, l, d):
	arr = []
	for i in range(0, l, d):
		arr.append(st[i:i + d])
	nd = ceil(d / 2)
	ans = 0
	for i in range(nd):
		m = defaultdict(int)
		for j in range(l // d):
			m[arr[j][i]] += 1
			if i != d - (i + 1):
				m[arr[j][d - (i + 1)]] += 1
		ans += sum(m.values()) - max(m.values())
	print(ans)
t = int(input())
for l in range(t):
	(l, d) = map(int, input().split())
	st = input()
	solve(st, l, d)


from sys import stdin, stdout
from math import *
from heapq import *
from collections import *

def main():
	ntest = int(stdin.readline())
	for ncase in range(ntest):
		(n, k) = [int(x) for x in stdin.readline().split()]
		s = stdin.readline().strip()
		c = [{} for _ in range(k)]
		m = n // k
		for (i, ch) in enumerate(s):
			c[i % k][ch] = c[i % k].get(ch, 0) + 1
		f = [3 * m] * k
		res = 0
		for i in range((k + 1) // 2):
			for ch in list(c[i].keys()) + list(c[k - 1 - i].keys()):
				f[i] = min(f[i], 2 * m - (c[i].get(ch, 0) + c[k - i - 1].get(ch, 0)))
			if k % 2 > 0 and i == k // 2:
				res += f[i] // 2
			else:
				res += f[i]
		stdout.write('%d\n' % res)
	return 0
main()


from collections import defaultdict
import math
t = int(input())
for _ in range(t):
	(n, k) = tuple(map(int, input().split()))
	s = input()
	out = 0
	for i in range(0, math.ceil(k / 2.0)):
		c = defaultdict(int)
		for j in range(i, n, k):
			c[s[j]] += 1
		if j != n - 1 - i:
			for j in range(n - 1 - i, 0, -k):
				c[s[j]] += 1
		out += sum(c.values()) - max(c.values())
	print(out)


def solve(s, k):
	L = []
	for j in range(k // 2):
		M = []
		for i in range(len(s) // k):
			M.append(s[i * k + j])
			M.append(s[i * k + k - j - 1])
		L.append(M)
	if k % 2 == 1:
		M = []
		for i in range(len(s) // k):
			M.append(s[i * k + k // 2])
		L.append(M)
	c = 0
	for i in L:
		cc = max(count(i))
		c += len(i) - cc
	return c

def count(s):
	L = [0] * 26
	for i in s:
		L[ord(i) - 97] += 1
	return L
for i in ' ' * int(input()):
	(n, k) = map(int, input().split())
	print(solve(input(), k))


import atexit
import io
import sys
_INPUT_LINES = sys.stdin.read().splitlines()
input = iter(_INPUT_LINES).__next__
_OUTPUT_BUFFER = io.StringIO()
sys.stdout = _OUTPUT_BUFFER

@atexit.register
def write():
	sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
testcases = int(input())
for case in range(testcases):
	(n, k) = [int(zax) for zax in input().split()]
	s = input()
	l = [{} for i in range(k)]
	answers = [0 for i in range(k // 2 + 1)]
	total1 = 0
	for i in range(n):
		ind = min(i % k, k - 1 - i % k)
		curr = l[ind]
		if s[i] in curr:
			curr[s[i]] += 1
			temp = curr[s[i]]
		else:
			curr[s[i]] = 1
			temp = 1
		if temp > answers[ind]:
			answers[ind] = temp
			total1 += 1
	print(n - total1)


def count(s):
	a = [0] * 26
	for x in s:
		a[ord(x) - 97] += 1
	return len(s) - max(a)
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	i = 0
	p = []
	for _ in range((k + 1) // 2):
		p.append([])
	while i <= n - k:
		for j in range(0, (k + 1) // 2):
			if i + j != i + k - 1 - j:
				p[j].append(s[i + k - 1 - j])
			p[j].append(s[i + j])
		i += k
	print(sum(list(map(count, p))))


from collections import defaultdict
t = int(input())
for i in range(t):
	(n, k) = [int(x) for x in input().split()]
	s = input()
	blocks = n // k
	d = defaultdict(int)
	result = 0
	right_bound = k // 2
	right_bound += k % 2
	for i in range(right_bound):
		for b in range(blocks):
			index = b * k + i
			d[s[index]] += 1
			if index != (b + 1) * k - i - 1:
				index = (b + 1) * k - i - 1
				d[s[index]] += 1
		result += sum(d.values()) - max(d.values())
		d.clear()
	print(result)


import sys

def set_debug(debug_mode=False):
	if debug_mode:
		fin = open('input.txt', 'r')
		sys.stdin = fin
set_debug(False)
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	word = input()
	res = 0
	for i in range(k):
		count = [0] * 26
		for j in range(i, n, k):
			count[ord(word[j]) - ord('a')] += 1
			count[ord(word[k - 1 - j]) - ord('a')] += 1
		res += sum(count) - max(count)
	print(res // 2)


T = int(input())
while T > 0:
	T -= 1
	(n, k) = map(int, input().split())
	s = input()
	length = n / k
	ans = 0
	halfk = k // 2 if k % 2 == 0 else k // 2 + 1
	for i in range(halfk):
		d = {}
		for ch in s[i::k]:
			ch = ord(ch)
			if ch in d:
				d[ch] += 1
			else:
				d[ch] = 1
		if i * 2 + 1 != k:
			for ch in s[k - 1 - i::k]:
				ch = ord(ch)
				if ch in d:
					d[ch] += 1
				else:
					d[ch] = 1
		if i * 2 + 1 != k:
			ans += max(d.values())
		else:
			ans += max(d.values())
	print(n - ans)


from sys import stdin
t = int(stdin.readline())
for _ in range(t):
	(n, k) = map(int, stdin.readline().split())
	s = stdin.readline()
	cnt = [[0 for i in range(26)] for j in range((k + 1) // 2)]
	for i in range(n):
		cnt[min(i % k, k - i % k - 1)][ord(s[i]) - ord('a')] += 1
	ans = 0
	for i in range(k // 2):
		ans += 2 * n // k - max(cnt[i])
	if k % 2 == 1:
		ans += n // k - max(cnt[k // 2])
	print(ans)


from collections import Counter, defaultdict, deque, namedtuple
from math import gcd
import sys

def read_ints():
	return [int(token) for token in input().split()]

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

def main():
	T = read_ints()[0]
	for case_no in range(1, T + 1):
		(n, k) = read_ints()
		S = input()
		counts = defaultdict(Counter)
		for i in range(n):
			index = min(i % k, (n - 1 - i) % k)
			counts[index][S[i]] += 1
		ret = 0
		for (k, v) in counts.items():
			ret += sum(v.values()) - v.most_common(1)[0][1]
		print(ret)
main()


for i in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	ans = 0
	for j in range(k):
		d = {}
		for l in range(j, n, k):
			if s[l] in d:
				d[s[l]] += 1
			else:
				d[s[l]] = 1
		for l in range(k - j - 1, n, k):
			if s[l] in d:
				d[s[l]] += 1
			else:
				d[s[l]] = 1
		ans += n // k * 2 - max(d.values())
	print(ans // 2)


from collections import defaultdict

def solve(s: str, n: int, k: int) -> int:
	counters = [defaultdict(int) for _ in range((k + 1) // 2)]
	for i in range(n):
		counters[min(i % k, k - i % k - 1)][s[i]] += 1
	chars = [max((c for c in cnt), key=lambda c: cnt[c]) for cnt in counters]
	target = ''.join(chars + chars[:k // 2][::-1]) * (n // k)
	res = 0
	for (c1, c2) in zip(s, target):
		if c1 != c2:
			res += 1
	return res
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	print(solve(s, n, k))


from collections import Counter

def algo(n, k, s):
	x = n // k
	is_odd = True if k % 2 != 0 else False
	count = 0
	for i in range((k - 1) // 2):
		ss = dict.fromkeys('abcdefghijklmnopqrstuvwxyz', 0)
		for j in range(x):
			ss[s[j * k + i]] += 1
			ss[s[j * k + k - 1 - i]] += 1
		count += 2 * x - max(ss.values())
	else:
		if is_odd:
			count += x - max(Counter([s[i] for i in range(k // 2, n, k)]).values())
		else:
			ss = dict.fromkeys('abcdefghijklmnopqrstuvwxyz', 0)
			i = k // 2 - 1
			for j in range(x):
				ss[s[j * k + i]] += 1
				ss[s[j * k + k - 1 - i]] += 1
			count += 2 * x - max(ss.values())
	return count
for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	s = input()
	print(algo(n, k, s))


import math
t = int(input())

def inc_char(chars, char):
	if char in chars:
		chars[char] += 1
	else:
		chars[char] = 1
for i in range(t):
	sol = 0
	(n, k) = map(int, input().split())
	s = input()
	for i in range(math.ceil(k / 2)):
		chars = {}
		checked = 0
		for j in range(i, n, k):
			inc_char(chars, s[j])
			checked += 1
		if i != k - i - 1:
			for j in range(k - i - 1, n, k):
				inc_char(chars, s[j])
				checked += 1
		max_cnt = max(chars.items(), key=lambda x: x[1])[1]
		sol += checked - max_cnt
	print(sol)


class Uf:

	def __init__(self, N):
		self.p = list(range(N))
		self.rank = [0] * N
		self.size = [1] * N

	def root(self, x):
		if self.p[x] != x:
			self.p[x] = self.root(self.p[x])
		return self.p[x]

	def same(self, x, y):
		return self.root(x) == self.root(y)

	def unite(self, x, y):
		u = self.root(x)
		v = self.root(y)
		if u == v:
			return
		if self.rank[u] < self.rank[v]:
			self.p[u] = v
			self.size[v] += self.size[u]
			self.size[u] = 0
		else:
			self.p[v] = u
			self.size[u] += self.size[v]
			self.size[v] = 0
			if self.rank[u] == self.rank[v]:
				self.rank[u] += 1

	def count(self, x):
		return self.size[self.root(x)]
from collections import defaultdict, Counter
import sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
	(N, K) = map(int, input().split())
	S = input()[:-1]
	dd = defaultdict(list)
	uf = Uf(N)
	for i in range(K, N):
		uf.unite(i - K, i)
	for i in range(N // 2):
		uf.unite(i, N - 1 - i)
	for (i, c) in enumerate(S):
		dd[uf.root(i)].append(c)
	ans = N
	for v in dd.values():
		cnt = Counter(v)
		ma = 0
		for vv in cnt.values():
			if ma < vv:
				ma = vv
		ans -= ma
	print(ans)


import math
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	ss = []
	aaaa = [[0] * 26 for _ in range(math.ceil(k / 2))]
	for i in range(0, n, k):
		ss.append(s[i:i + k])
		temp = s[i:i + k]
		ttt = math.ceil(k / 2)
		for j in range(ttt):
			aaaa[j][ord(temp[j]) - 97] += 1
			if j != k - j - 1:
				aaaa[j][ord(temp[k - j - 1]) - 97] += 1
	ans = 0
	for jj in aaaa:
		mmm = max(jj)
		tt = int(n / k)
		ans += 2 * tt - mmm
	if math.ceil(k / 2) != int(k / 2):
		ans -= int(n / k)
	print(ans)


t = int(input())
for _ in range(t):
	(n, k) = (int(i) for i in input().split())
	s = input()
	ans = ''
	for i in range((k + 1) // 2):
		ch = [0] * 26
		for j in range(i, n, k):
			ch[ord(s[j]) - 97] += 1
		for j in range(k - i - 1, n, k):
			ch[ord(s[j]) - 97] += 1
		ind = max(ch)
		for temp in range(26):
			if ch[temp] == ind:
				ind = temp
				break
		ans = ans + chr(97 + ind)
	count = 0
	for ii in range(k // 2 - 1, -1, -1):
		ans = ans + ans[ii]
	j = 0
	for i in range(n):
		if s[i] != ans[j]:
			count += 1
		j += 1
		if j == k:
			j = 0
	print(count)


from collections import defaultdict
for _ in range(int(input())):
	(N, K) = map(int, input().split())
	S = input()
	Hash = [[0 for i in range(26)] for i in range(K)]
	for i in range(N):
		Hash[i % K][ord(S[i]) - 97] += 1
	ans = 0
	for i in range((K + 1) // 2):
		j = K - i - 1
		if i != j:
			m = 0
			for k in range(26):
				m = max(Hash[i][k] + Hash[j][k], m)
			ans += 2 * N // K - m
		else:
			m = 0
			for k in range(26):
				m = max(Hash[i][k], m)
			ans += N // K - m
	print(ans)


from itertools import accumulate
from bisect import bisect_left
import itertools
standard_input = '4\n6 2\nabaaba\n6 3\nabaaba\n36 9\nhippopotomonstrosesquippedaliophobia\n21 7\nwudixiaoxingxingheclp\n'
clrs = [2, 3, 5, 7, 11, 13, 17, 19, 29, 31, 37]

def solve():
	(n, k) = map(int, input().split(' '))
	s = input()
	fp = s[:n // 2 + n % 2]
	sp = s[:n // 2 - 1:-1]
	pp = []
	for i in range(0, n, k):
		pp.append(s[i:i + k])
	ans = 0
	for i in range(k // 2 + k % 2):
		cnt = [0] * 26
		for p in pp:
			cnt[ord(p[i]) - ord('a')] += 1
			if i < k // 2:
				cnt[ord(p[-i - 1]) - ord('a')] += 1
		ans += sum(cnt) - max(cnt)
	print(ans)
t = int(input())
for i in range(t):
	solve()


from sys import stdin
inp = stdin.readline
t = int(inp().strip())
for c in range(t):
	(n, k) = [int(x) for x in inp().strip().split()]
	array = list(inp().strip())
	d1 = {}
	for i in range(n):
		if d1.get(min(i % k, k - i % k - 1), 0) == 0:
			d1[min(i % k, k - i % k - 1)] = {array[i]: 1}
		elif d1[min(i % k, k - i % k - 1)].get(array[i], 0) == 0:
			d1[min(i % k, k - i % k - 1)][array[i]] = 1
		else:
			d1[min(i % k, k - i % k - 1)][array[i]] += 1
	number = n // k
	numberOfMoves = 0
	for i in d1.keys():
		maximum = 0
		for j in d1[i].keys():
			if d1[i][j] > maximum:
				maximum = d1[i][j]
		if k % 2 == 1 and i == k // 2:
			numberOfMoves += number - maximum
		else:
			numberOfMoves += 2 * number - maximum
	print(numberOfMoves)


def slv(s, n, k):
	ans = 0
	for i in range((k + 1) // 2):
		c = {}
		sm = 0
		for j in range(n // k):
			a = s[j * k + i]
			if a not in c:
				c[a] = 0
			c[a] += 1
			sm += 1
			if i != k - i - 1:
				a = s[j * k + k - i - 1]
				if a not in c:
					c[a] = 0
				c[a] += 1
				sm += 1
		m = max([c[i] for i in c])
		ans += sm - m
	return ans
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	print(slv(s, n, k))


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = str(input())
	c = [[0] * 26 for _ in range((k + 1) // 2)]
	for i in range(n):
		c[min(i % k, k - i % k - 1)][ord(s[i]) - ord('a')] += 1
	ans = 0
	if k % 2 == 0:
		for i in range(len(c)):
			ans += n // k * 2 - max(c[i])
	else:
		for i in range(len(c)):
			if i != len(c) - 1:
				ans += n // k * 2 - max(c[i])
			else:
				ans += n // k - max(c[i])
	print(ans)


from sys import stdin
input = stdin.readline
from collections import Counter
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	r = 0
	for i in range(k // 2 + 1):
		if (n - 1 - i) % k < i:
			continue
		l = (n - i - 1) // k + 1
		c = Counter((s[j] for j in range(i, n, k)))
		if i != k - i - 1:
			l *= 2
			for j in range(n - i - 1, -1, -k):
				c[s[j]] += 1
		r += l - c.most_common(1)[0][1]
	print(r)


ans = []
for s in range(int(input())):
	(n, k) = [int(x) for x in input().split()]
	s1 = input()
	final = 0
	for i in range(0, k):
		arr = [0] * 26
		j = i
		while j < n:
			arr[ord(s1[j]) - 97] += 1
			arr[ord(s1[k - 1 - j]) - 97] += 1
			j += k
		final += sum(arr) - max(arr)
	ans.append(final // 2)
for it in ans:
	print(it)


from sys import stdin, stdout
print2 = print
input = stdin.readline
from collections import defaultdict

def cost(chars):
	freqs = defaultdict(int)
	for c in chars:
		freqs[c] += 1
	return sum(freqs.values()) - max(freqs.values())

def ans(S, N, K):
	to_match = []
	for i in range(N // K):
		to_match.append(''.join(S[K * i:K * (i + 1)]))
	S = S[::-1]
	for i in range(N // K):
		to_match.append(''.join(S[K * i:K * (i + 1)]))
	ret = 0
	for i in range(K):
		chars = [s[i] for s in to_match]
		ret += cost(chars)
	return ret // 2
T = int(input())
for t in range(T):
	(N, K) = input().split(' ')
	(N, K) = (int(N), int(K))
	S = input().strip()
	print(str(ans(S, N, K)) + '\n')


from sys import stdin
L = int(stdin.readline())
X = [0] * L
for _ in range(L):
	(N, K) = map(int, stdin.readline().split())
	S = stdin.readline()[:N]
	T = list(S)
	ANS = 0
	for I in range(K // 2 + 1):
		A = {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': 0, 'g': 0, 'h': 0, 'i': 0, 'j': 0, 'k': 0, 'l': 0, 'm': 0, 'n': 0, 'o': 0, 'p': 0, 'q': 0, 'r': 0, 's': 0, 't': 0, 'u': 0, 'v': 0, 'w': 0, 'x': 0, 'y': 0, 'z': 0}
		for J in range(I, N, K):
			A[S[J]] += 1
			A[S[N - J - 1]] += 1
		Z = max(A, key=A.get)
		for J in range(I, N, K):
			if T[J] != Z:
				T[J] = Z
				ANS += 1
			if T[N - J - 1] != Z:
				T[N - J - 1] = Z
				ANS += 1
	X[_] = ANS
print(*X, sep='\n')


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	s = input()
	answer = 0
	for j in range((k + 1) // 2):
		d = dict()
		o = j
		d[s[o]] = 1
		o += k - 1 - j * 2
		count = 1
		if k == j * 2 + 1:
			if k == 1:
				o += 1
			else:
				o += j * 2 + 1
		while o < n:
			if s[o] not in d:
				d[s[o]] = 0
			d[s[o]] += 1
			count += 1
			if o + j * 2 + 1 < n:
				o += j * 2 + 1
				if s[o] not in d:
					d[s[o]] = 0
				d[s[o]] += 1
				count += 1
			else:
				break
			if k == j * 2 + 1:
				if k == 1:
					o += 1
				else:
					o += j * 2 + 1
			else:
				o += k - 1 - j * 2
		answer += count - max(d.values())
	print(answer)


for _ in range(int(input())):
	(a, b) = map(int, input().split(' '))
	c = input()
	rat = a // b
	cost = 0
	for i in range(b):
		f = []
		g = []
		for j in range(rat):
			f.append(c[i + j * b])
			f.append(c[a - 1 - i - j * b])
		h = set(f)
		for k in h:
			g.append(f.count(k))
		cost += len(f) - max(g)
	print(cost // 2)


t = int(input())
while t:
	(n, k) = [int(i) for i in input().split()]
	s = input()
	ans = 0
	reps = n // k
	for i in range((k - 1) // 2 + 1):
		mem = {}
		for j in range(i, n, k):
			if s[j] in mem:
				mem[s[j]] += 1
			else:
				mem[s[j]] = 1
			if s[k - 1 - i + k * (j // k)] in mem:
				mem[s[k - 1 - i + k * (j // k)]] += 1
			else:
				mem[s[k - 1 - i + k * (j // k)]] = 1
		change = 2 * reps - max(mem.values())
		if k % 2 == 1 and i == k // 2:
			change = reps - max(mem.values()) // 2
		ans += change
	print(ans)
	t -= 1


import os
import heapq
import sys, threading
import math as mt
import operator
from copy import copy
from collections import defaultdict, deque
from io import BytesIO, IOBase
sys.setrecursionlimit(10 ** 5)

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

def inpt():
	return [int(k) for k in input().split()]

def main():
	for _ in range(int(input())):
		(n, k) = inpt()
		s = input()
		d = defaultdict(list)
		for i in range(n):
			pos = i % k
			d[pos].append(s[i])
		(i, j) = (0, k - 1)
		ans = 0
		while i < j:
			fr = [0] * 26
			(ct, mx) = (0, 0)
			for k in d[i]:
				fr[ord(k) - ord('a')] += 1
				ct += 1
				mx = max(mx, fr[ord(k) - ord('a')])
			for k in d[j]:
				fr[ord(k) - ord('a')] += 1
				ct += 1
				mx = max(mx, fr[ord(k) - ord('a')])
			ans += ct - mx
			i += 1
			j -= 1
		fr = [0] * 26
		(ct, mx) = (0, 0)
		if i == j:
			for k in d[i]:
				fr[ord(k) - ord('a')] += 1
				ct += 1
				mx = max(mx, fr[ord(k) - ord('a')])
			ans += ct - mx
		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()


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	a = 0
	for i in range(0, k):
		b = [0] * 26
		for j in range(i, n, k):
			b[ord(s[j]) - 97] += 1
			b[ord(s[k - 1 - j]) - 97] += 1
		a += sum(b) - max(b)
	print(a // 2)


import bisect
import os
import io
from collections import Counter
from collections import defaultdict
import math
import random
import heapq as hq
from math import sqrt
import sys
from functools import reduce
from collections import deque
import threading

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

def iinput():
	return int(input())

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

def rinput():
	return map(int, tinput())

def rlinput():
	return list(rinput())
mod = int(1000000000.0) + 7

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))
for _ in range(iinput()):
	(n, k) = rinput()
	s = list(input())
	cost = 0
	ideal = []
	for m in range(int(math.ceil(k / 2))):
		d = defaultdict(int)
		mx = 0
		val = ''
		for i in range(0, n, k):
			d[s[i + m]] += 1
			d[s[i + k - m - 1]] += 1
			if d[s[i + m]] > mx:
				mx = d[s[i + m]]
				val = s[i + m]
			if d[s[i + k - m - 1]] > mx:
				mx = d[s[i + k - m - 1]]
				val = s[i + k - m - 1]
		ideal.append(val)
	finalideal = []
	if k % 2:
		finalideal = ideal[:-1] + ideal[::-1]
	else:
		finalideal = ideal + ideal[::-1]
	for i in range(n):
		if s[i] != finalideal[i % k]:
			cost += 1
	print(cost)


def freq(word, K, idx):
	is_middle = K % 2 != 0 and K // 2 == idx
	letters = [0] * 26
	off = K - idx * 2 - 1
	while idx < len(word):
		letters[ord(word[idx]) - ord('a')] += 1
		if not is_middle:
			letters[ord(word[idx + off]) - ord('a')] += 1
		idx += K
	if is_middle:
		return len(word) // K - max(letters)
	return len(word) // K * 2 - max(letters)
T = int(input())
for _ in range(T):
	(N, K) = map(int, input().split())
	word = list(input())
	sum = 0
	for idx in range((K + 1) // 2):
		sum += freq(word, K, idx)
	print(sum)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	l = []
	for i in s:
		l.append(i)
	a = 0
	for i in range(k // 2):
		c = []
		for j in range(26):
			c.append(0)
		for j in range(i, n, k):
			c[ord(s[j]) - 97] += 1
		for j in range(k - i - 1, n, k):
			c[ord(s[j]) - 97] += 1
		m = max(c)
		m = c.index(m)
		z = chr(m + 97)
		for j in range(i, n, k):
			if l[j] != z:
				a += 1
			l[j] = z
		for j in range(k - i - 1, n, k):
			if l[j] != z:
				a += 1
			l[j] = z
	if k % 2 == 1:
		c = []
		for j in range(26):
			c.append(0)
		for j in range(k // 2, n, k):
			c[ord(s[j]) - 97] += 1
		m = max(c)
		m = c.index(m)
		z = chr(m + 97)
		for j in range(k // 2, n, k):
			if l[j] != z:
				a += 1
			l[j] = z
	print(a)


t = int(input())
cases = []
for _ in range(t):
	a = [int(i) for i in input().split()]
	n = a[0]
	k = a[1]
	string = input()
	cases.append((k, string))
for (k, string) in cases:
	l = len(string)
	n = l // k
	ans = 0
	for i in range(k // 2):
		d = {}
		for j in range(i, l, k):
			s = string[j]
			d[s] = d.get(s, 0) + 1
		for j in range(k - 1 - i, l, k):
			s = string[j]
			d[s] = d.get(s, 0) + 1
		res = max(d.values())
		diff = n * 2 - res
		ans += diff
	if k % 2 == 1:
		d = {}
		for j in range(k // 2, l, k):
			s = string[j]
			d[s] = d.get(s, 0) + 1
		res = max(d.values())
		diff = n - res
		ans += diff
	print(ans)


t = int(input())
cases = []
for i in range(t):
	(length, k) = map(int, input().strip().split())
	word = input()
	cases.append([word, k])

def solve(case):
	word = case[0]
	k = case[1]
	reps = len(word) // k
	ans = 0
	for i in range(k // 2 if k % 2 == 0 else k // 2 + 1):
		print()
		count = {}
		for j in range(reps + 1):
			(left, right) = ((j + 1) * k - 1 - i, j * k + i)
			if 0 <= left < len(word):
				if word[left] in count:
					count[word[left]] += 1
				else:
					count[word[left]] = 1
			if 0 <= right < len(word) and left != right:
				if word[right] in count:
					count[word[right]] += 1
				else:
					count[word[right]] = 1
		replace = sum(count.values()) - max(count.values())
		ans += replace
	print(ans)
for case in cases:
	solve(case)


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 pre(s):
	n = len(s)
	pi = [0] * n
	for i in range(1, n):
		j = pi[i - 1]
		while j > 0 and s[i] != s[j]:
			j = pi[j - 1]
		if s[i] == s[j]:
			j += 1
		pi[i] = j
	return pi
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = [k for k in input()]
	pi = pre(s)
	groups = [[0] * 26 for i in range(k // 2 + 1)]
	for ii in range(n):
		i = ii % k
		group = 0
		if k % 2:
			if i >= k // 2:
				group = k - i - 1
			else:
				group = i
		elif i < k // 2:
			group = i
		else:
			group = k - i - 1
		groups[group][ord(s[ii]) - 97] += 1
	print(n - sum([max(group) for group in groups]))


def Bfs(id, n, k, visited: list):
	(queue1, queue2) = ([], [])
	visited[id] = True
	queue1.append(id)
	while queue1:
		i = queue1.pop()
		queue2.append(i)
		if i + k < n and (not visited[i + k]):
			visited[i + k] = True
			queue1.append(i + k)
		if i - k >= 0 and (not visited[i - k]):
			visited[i - k] = True
			queue1.append(i - k)
		if not visited[n - 1 - i]:
			visited[n - 1 - i] = True
			queue1.append(n - 1 - i)
	return queue2

def countReplacement(string: str, queue: list):
	(total, count, chrcount) = (len(queue), 0, {})
	while queue:
		i = queue.pop()
		chrcount[string[i]] = chrcount.get(string[i], 0) + 1
		count = max(count, chrcount[string[i]])
	return total - count
t = int(input())
while t > 0:
	(n, k) = [int(x) for x in input().split()]
	(string, count) = (input(), 0)
	visited = [False] * len(string)
	for id in range(len(string)):
		if not visited[id]:
			queue = Bfs(id, n, k, visited)
			count += countReplacement(string, queue)
	print(count)
	t -= 1


def max_counter(a):
	d = {}
	for i in a:
		d[i] = d.get(i, 0) + 1
	return max(d.values())
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	ans = 0
	for i in range(k):
		ans += len(s[i::k]) + len(s[n - i - 1::-k]) - max_counter(s[i::k] + s[n - i - 1::-k])
	print(ans // 2)


import sys
from collections import defaultdict
t = int(input())
for i in range(t):
	(a, b) = list(map(int, sys.stdin.readline().strip().split()))
	d = defaultdict(int)
	x = list(input())
	op = 0
	str = []
	xx = a // b
	for i in range(xx):
		str.append(x[i * b:i * b + b])
	zz = b // 2
	for i in range(zz):
		d = defaultdict(int)
		for j in range(xx):
			d[str[j][i]] += 1
			d[str[j][b - i - 1]] += 1
		zzz = d[max(d, key=d.get)]
		xxx = max(d, key=d.get)
		op += xx * 2 - zzz
	if b % 2 == 1:
		i = zz
		d = defaultdict(int)
		for j in range(xx):
			d[str[j][i]] += 1
		zzz = d[max(d, key=d.get)]
		op += xx - zzz
	print(op)


def solve(s, k):
	n = len(s)
	r = n // k
	res = 0
	for j in range(k // 2):
		d = [0] * 26
		for i in range(r):
			d[ord(s[i * k + j]) - 97] += 1
			d[ord(s[i * k + k - 1 - j]) - 97] += 1
		res += 2 * r - max(d)
	if k % 2 == 1:
		d = [0] * 26
		for i in range(r):
			d[ord(s[i * k + k // 2]) - 97] += 1
		res += r - max(d)
	return res
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	print(solve(s, k))


from sys import stdin
input = stdin.readline
import bisect
import math
for xoxo in range(1):
	for _ in range(int(input())):
		(n, k) = map(int, input().split())
		ans = 0
		s = input()
		v = [0] * k
		a = []
		cnt = 1
		t = [0] * n
		for i in range(k):
			q = [i]
			if v[i] == 0:
				j = 0
				while len(q) > j:
					v[q[j]] = cnt
					for l in range(n // k):
						t[k * l + q[j]] = cnt
						pi = n - 1 - (k * l + q[j])
						t[pi] = cnt
						pi = pi % k
						if v[pi] == 0:
							v[pi] = cnt
							q.append(pi)
					j += 1
				cnt += 1
		p = []
		for i in range(max(v)):
			p.append([0 for i in range(26)])
		for i in range(len(t)):
			p[t[i] - 1][97 - ord(s[i])] += 1
		for i in range(len(p)):
			ans += sum(p[i]) - max(p[i])
		print(ans)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = list(input())
	data = []
	for j in range(k):
		temp = []
		for l in range(n // k):
			temp.append(s[l * k + j])
		data.append(temp)
	ans = 0
	for i in range(k // 2):
		l1 = data[i]
		l1.extend(data[k - i - 1])
		l1 = sorted(l1)
		c = 1
		m = 1
		for j in range(1, len(l1)):
			if l1[j] == l1[j - 1]:
				c += 1
			else:
				if c > m:
					m = c
				c = 1
		if c > m:
			m = c
		ans += 2 * (n // k) - m
	if k % 2 != 0:
		l1 = data[k // 2]
		l1 = sorted(l1)
		c = 1
		m = 1
		for j in range(1, len(l1)):
			if l1[j] == l1[j - 1]:
				c += 1
			else:
				if c > m:
					m = c
				c = 1
		if c > m:
			m = c
		ans += n // k - m
	print(ans)


def solve():
	(n, k) = map(int, input().split())
	s = list(input())
	c = 0
	for (i, s_i) in enumerate(s[:k // 2 + 1]):
		idxs = [*range(i, n, k), *range(k - i - 1, n, k)]
		cd = {}
		max_count = 0
		max_char = None
		for j in idxs:
			s_j = s[j]
			count = cd.get(s_j, 0) + 1
			if count > max_count:
				max_count = count
				max_char = s_j
			cd[s_j] = count
		for j in idxs:
			if s[j] != max_char:
				s[j] = max_char
				c += 1
	print(c)

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


from functools import reduce
import os
import sys
from collections import *
from math import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase
input = lambda : sys.stdin.readline().rstrip('\r\n')

def value():
	return tuple(map(int, input().split()))

def arr():
	return [int(i) for i in input().split()]

def sarr():
	return [int(i) for i in input()]

def starr():
	return [str(x) for x in input().split()]

def inn():
	return int(input())

def svalue():
	return tuple(map(str, input().split()))

def parr():
	return [value() for i in range(n)]
mo = 1000000007
for _ in range(inn()):
	(n, k) = value()
	s = input()
	ans = [0] * n
	d = defaultdict(list)
	c = 1
	for i in range(k):
		for j in range(i, n, k):
			ans[j] = c
		c += 1
	for i in range(1, c + 1):
		d[i] = [0 for x in range(26)]
	i = 0
	j = n - 1
	cnt = defaultdict(int)
	while i <= j:
		if ans[i] != ans[j]:
			mx = max(ans[j], ans[i])
			mn = min(ans[i], ans[j])
			cnt[mx] = mn
		i += 1
		j -= 1
	for i in range(n):
		if ans[i] in cnt:
			ans[i] = cnt[ans[i]]
	ln = -1
	for i in range(n):
		ln = max(ln, ans[i])
		res = ord(s[i]) - 97
		d[ans[i]][res] += 1
	res = 0
	for i in range(1, ln + 1):
		c = 0
		mx = -1
		for j in range(26):
			mx = max(mx, d[i][j])
			c += d[i][j]
		res += c - mx
	print(res)


import sys, math

def petrolium(s):
	c = [0] * 26
	n = len(s)
	xzvbc = 0
	for i in range(len(s)):
		c[ord(s[i]) - 97] += 1
	return n - max(c)
t = int(input())
for _ in range(t):
	(n, k) = map(int, sys.stdin.readline().split())
	s = input()
	if k == 1:
		print(petrolium(s))
	else:
		q = n // k
		ash = ['' for i in range(math.ceil(k / 2))]
		w = 0
		for i in range(q):
			struct = s[i * k:i * k + k]
			for j in range(math.ceil(k / 2)):
				if j == k - j - 1:
					ash[j] += struct[j]
				else:
					ash[j] += struct[j] + struct[k - j - 1]
		a = 0
		bbb = 0
		fff = 0
		for i in range(math.ceil(k / 2)):
			l = list(ash[i])
			a += petrolium(ash[i])
		print(a)


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 binary(n):
	return bin(n).replace('0b', '')

def decimal(s):
	return int(s, 2)

def pow2(n):
	p = 0
	while n > 1:
		n //= 2
		p += 1
	return p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(i)
			n = n / i
	if n > 2:
		l.append(int(n))
	return l

def isPrime(n):
	if n == 1:
		return False
	else:
		root = int(n ** 0.5)
		root += 1
		for i in range(2, root):
			if n % i == 0:
				return False
		return True

def maxPrimeFactors(n):
	maxPrime = -1
	while n % 2 == 0:
		maxPrime = 2
		n >>= 1
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			maxPrime = i
			n = n / i
	if n > 2:
		maxPrime = n
	return int(maxPrime)
for xyz in range(0, int(input())):
	(n, k) = map(int, input().split())
	s = input()
	a = 0
	if k % 2 == 0:
		for i in range(0, k // 2):
			f = [0] * 26
			tc = 0
			for j in range(i, n, k):
				ma = k * (j // k) + k - 1
				f[ord(s[j]) - 97] += 1
				f[ord(s[ma - i]) - 97] += 1
				tc += 2
			a += tc - max(f)
	else:
		for i in range(0, k // 2):
			f = [0] * 26
			tc = 0
			for j in range(i, n, k):
				ma = k * (j // k) + k - 1
				f[ord(s[j]) - 97] += 1
				f[ord(s[ma - i]) - 97] += 1
				tc += 2
			a += tc - max(f)
		f = [0] * 26
		for i in range(k // 2, n, k):
			f[ord(s[i]) - 97] += 1
		a += n // k - max(f)
	print(a)


ttt = int(input())
for tttt in range(ttt):
	nk = input().split(' ')
	n = int(nk[0])
	k = int(nk[1])
	s = input()
	m = round(n / k)
	a = []
	for i in range(m):
		row = []
		for j in range(k):
			row.append(s[i * k + j])
		a.append(row)
	q = 0
	if k % 2 == 0:
		for i in range(round(k / 2)):
			listt = []
			for j in range(m):
				listt.append(a[j][i])
			for j in range(m):
				listt.append(a[j][k - i - 1])
			sett = set(listt)
			count = []
			for x in sett:
				count.append(listt.count(x))
			q += len(listt) - max(count)
		print(q)
	else:
		for i in range(round((k - 1) / 2)):
			listt = []
			for j in range(m):
				listt.append(a[j][i])
			for j in range(m):
				listt.append(a[j][k - i - 1])
			sett = set(listt)
			count = []
			for x in sett:
				count.append(listt.count(x))
			q += len(listt) - max(count)
		listt = []
		for j in range(m):
			listt.append(a[j][int(k / 2)])
		sett = set(listt)
		count = []
		for x in sett:
			count.append(listt.count(x))
		q += len(listt) - max(count)
		print(q)


I = lambda : list(map(int, input().split()))
for _ in range(int(input())):
	(n, k) = I()
	s = input()
	x = 0
	for i in range(k // 2):
		al = [0] * 26
		for j in range(i, n, k):
			al[ord(s[j]) - 97] += 1
			al[ord(s[(j // k + 1) * k - j % k - 1]) - 97] += 1
		x += sum(al) - max(al)
	if k % 2:
		al = [0] * 26
		for j in range(k // 2, n, k):
			al[ord(s[j]) - 97] += 1
		x += sum(al) - max(al)
	print(x)


from collections import Counter
n = int(input())
for _ in range(n):
	(n, k) = map(int, input().split())
	s = input()
	ans = 0
	for offset in range((k + 1) // 2):
		counter = Counter()
		left = offset
		right = k - offset - 1
		for jump in range(0, n, k):
			counter[s[jump + left]] += 1
			if k % 2 == 1 and offset == k // 2:
				continue
			counter[s[jump + right]] += 1
		ans += sum(counter.values()) - max(counter.values())
	print(ans)


for _ in ' ' * int(input()):
	(a, b) = map(int, input().split())
	d = input()
	f = 0
	for i in range(b):
		c = [0] * 26
		for j in range(i, a, b):
			c[ord(d[j]) - 97] += 1
			c[ord(d[b - j - 1]) - 97] += 1
		f += sum(c) - max(c)
	print(f // 2)


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
from collections import Counter, defaultdict, deque
from heapq import heapify, heappop, heappush

def main():
	for _ in range(N()):
		(n, k) = RL()
		s = input()
		ts = n // k
		res = 0
		if k % 2 == 1:
			dic = defaultdict(int)
			now = k // 2
			for i in range(ts):
				dic[s[now]] += 1
				now += k
			ma = 0
			for i in dic:
				ma = max(ma, dic[i])
			res += ts - ma
		for i in range(k // 2):
			dic = defaultdict(int)
			for j in range(0, n, k):
				(l, r) = (j + i, j + k - 1 - i)
				dic[s[l]] += 1
				dic[s[r]] += 1
			ma = sm = 0
			for j in dic:
				sm += dic[j]
				ma = max(dic[j], ma)
			res += sm - ma
		print(res)
main()


from itertools import groupby
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	x = n // k
	c = 0
	if k % 2 != 0:
		k2 = k // 2 + 1
	else:
		k2 = k // 2
	for i in range(k2):
		l = []
		l1 = []
		for j in range(0, x):
			if i != k // 2:
				y = j * k + i
				y1 = j * k + k - i - 1
				l.append(s[y])
				l.append(s[y1])
				l1.append(y)
				l1.append(y1)
			else:
				y = j * k + i
				l.append(s[y])
				l1.append(y)
		l.sort()
		m = 0
		for (key, group) in groupby(l):
			k1 = len(list(group))
			if k1 > m:
				m = k1
		c = c + len(l) - m
	print(c)


import sys

def solve():
	split = str.split
	t = int(sys.stdin.readline())
	while t > 0:
		(n, k) = map(int, split(sys.stdin.readline()))
		s = sys.stdin.readline()
		ans = 0
		index = 0
		max_chars = []
		mappend = max_chars.append
		while index <= (k - 1) // 2:
			mx = 0
			mx_char = 'a'
			chars = dict()
			pindex = k - index - 1
			cur_index = index
			while cur_index < n:
				count = chars.get(s[cur_index], 0) + 1
				chars[s[cur_index]] = count
				if count > mx:
					mx_char = s[cur_index]
					mx = count
				cur_index += k
			while pindex < n:
				count = chars.get(s[pindex], 0) + 1
				chars[s[pindex]] = count
				if count > mx:
					mx_char = s[pindex]
					mx = count
				pindex += k
			mappend(mx_char)
			index += 1
		index = 0
		while index <= (k - 1) // 2:
			max_char = max_chars[index]
			pindex = k - index - 1
			cur_index = index
			if pindex != cur_index:
				while pindex < n:
					if s[pindex] != max_char:
						ans += 1
					pindex += k
			while cur_index < n:
				if s[cur_index] != max_char:
					ans += 1
				cur_index += k
			index += 1
		print(ans)
		t -= 1
solve()


import sys
from collections import Counter
t = int(input())

class DisjSet:

	def __init__(self, n):
		self.rank = [1] * n
		self.parent = [i for i in range(n)]
		self.extra = []

	def find(self, x):
		if self.parent[x] != x:
			self.parent[x] = self.find(self.parent[x])
		return self.parent[x]

	def Union(self, x, y):
		xset = self.find(x)
		yset = self.find(y)
		if xset == yset:
			self.extra.append((x, y))
			return
		if self.rank[xset] < self.rank[yset]:
			self.parent[xset] = yset
		elif self.rank[xset] > self.rank[yset]:
			self.parent[yset] = xset
		else:
			self.parent[yset] = xset
			self.rank[xset] = self.rank[xset] + 1
for _ in range(t):
	(n, k) = list(map(lambda x: int(x), sys.stdin.readline().split()))
	obj = DisjSet(n)
	a = sys.stdin.readline()
	for i in range(0, k):
		for j in range(i + k, n, k):
			obj.Union(i, j)
	for i in range(0, len(a) // 2):
		obj.Union(i, n - i - 1)
	hashmap = {}
	for i in range(n):
		x = obj.find(i)
		if x not in hashmap:
			hashmap[x] = []
		hashmap[x].append(a[i])
	total = 0
	for i in hashmap:
		x = sorted(dict(Counter(hashmap[i])).items(), key=lambda x: x[1], reverse=True)
		s = sum([a[1] for a in x])
		total += s - x[0][1]
	print(total)


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, bisect, fractions, statistics
from collections import deque, defaultdict, OrderedDict, Counter
from fractions import Fraction
from decimal import Decimal
from sys import stdout

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, k) = ria()
		a = rs()
		cost = 0
		ans = [''] * k
		ansf = ''
		kabtak = math.ceil(k / 2)
		for i in range(kabtak):
			d = {}
			ind1 = i
			ind2 = k - i - 1
			for j in range(ind1, n, k):
				if a[j] in d:
					d[a[j]] += 1
				else:
					d[a[j]] = 1
			for j in range(ind2, n, k):
				if a[j] in d:
					d[a[j]] += 1
				else:
					d[a[j]] = 1
			ans[i] = max(d, key=d.get)
		ansf = ansf.join(ans)
		finalansf = ansf
		revansf = ansf[::-1]
		if k % 2:
			for i in range(0, k // 2):
				finalansf += revansf[i + 1]
		else:
			for i in range(0, k // 2):
				finalansf += revansf[i]
		finalansf = finalansf * (n // k)
		for i in range(n):
			if a[i] != finalansf[i]:
				cost += 1
		print(cost)
	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()


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	a = []
	for i in range(n // k):
		a.append(s[k * i:k * (i + 1)])
	ans = 0
	for j in range(k // 2):
		c = [0] * 26
		for i in range(len(a)):
			c[ord(a[i][j]) - ord('a')] += 1
			c[ord(a[i][-j - 1]) - ord('a')] += 1
		ans += len(a) * 2 - max(c)
	if k % 2 == 1:
		c = [0] * 26
		for i in range(len(a)):
			c[ord(a[i][k // 2]) - ord('a')] += 1
		ans += len(a) - max(c)
	print(ans)


import sys
input = sys.stdin.readline
from collections import Counter
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = list(input().strip())
	ans = 0
	for start in range((k + 1) // 2):
		rev_start = k - 1 - start
		group = s[start::k]
		if start != rev_start:
			group += s[rev_start::k]
		length = len(group)
		cnt = Counter(group)
		max_cnt = 0
		for val in cnt.values():
			max_cnt = max(max_cnt, val)
		ans += length - max_cnt
	print(ans)


t = int(input())
for _ in range(t):
	(n, k) = list(map(int, input().split()))
	s = str(input())
	lis = [[0] * 26 for i in range((k + 1) // 2)]
	for i in range(n):
		lis[min(i % k, k - i % k - 1)][ord(s[i]) - 97] += 1
	ans = 0
	for i in range(k // 2):
		a = max(lis[i])
		b = sum(lis[i])
		ans += b - a
	if k % 2 == 1:
		lis = [0] * 26
		for i in range(n):
			if i % k == k // 2:
				lis[ord(s[i]) - 97] += 1
		a = max(lis)
		b = sum(lis)
		ans += b - a
	print(ans)


from collections import *
from math import *
from sys import *
input = stdin.readline
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	b = []
	s = input()
	for i in range(n // k):
		b.append(s[i * k:(i + 1) * k])
	c = [0] * k
	s1 = ''
	for i in range(int(-(-k // 2))):
		d = defaultdict(lambda : 0)
		for j in range(n // k):
			d[b[j][i]] += 1
			d[b[j][k - i - 1]] += 1
		q = 'abcdefghijklmnopqrstuvwxyz'
		ch = 'a'
		mx = 0
		for r in q:
			if d[r] > mx:
				mx = d[r]
				ch = r
		s1 += ch
		c[i] = mx
	if k % 2 == 0:
		s1 = s1 + s1[::-1]
	else:
		ch = s1[k // 2]
		s1 = s1[:k // 2]
		s1 = s1 + ch + s1[::-1]
	s2 = s1
	s1 = []
	for i in s2:
		s1.append(i)
	cn = 0
	l = k // 2
	for i in range(l):
		if s1[i] == s1[k - i - 1]:
			cn += 0
		else:
			cn += 1
			if c[i] >= c[k - i - 1]:
				s1[k - i - 1] = s1[i]
			else:
				s1[i] = s1[k - i - 1]
	c = Counter(b)
	cn = 0
	b = list(set(b))
	for i in range(len(b)):
		h = b[i]
		m = 0
		for j in range(k):
			if s1[j] == h[j]:
				m += 0
			else:
				m += 1
		cn += c[h] * m
	print(cn)


import sys
import collections
input = sys.stdin.readline

def main():
	t = int(input())
	for _ in range(t):
		(N, K) = [int(x) for x in input().split()]
		S = input().strip()
		X = [[0] * 26 for _ in range(K)]
		added = set()
		ans = 0
		for i in range(N // 2):
			if i in added:
				continue
			c = collections.Counter()
			c[S[i]] += 1
			added.add(i)
			q = collections.deque()
			q.append(i)
			cnt = 1
			while q:
				cc = q.pop()
				if cc + K < N and cc + K not in added:
					added.add(cc + K)
					q.append(cc + K)
					c[S[cc + K]] += 1
					cnt += 1
				if N - 1 - cc >= 0 and N - 1 - cc not in added:
					added.add(N - 1 - cc)
					q.append(N - 1 - cc)
					c[S[N - 1 - cc]] += 1
					cnt += 1
				if cc - K >= 0 and cc - K not in added:
					added.add(cc - K)
					q.append(cc - K)
					c[S[cc - K]] += 1
					cnt += 1
			ans += cnt - c.most_common()[0][1]
		print(ans)
main()


from sys import stdin, stdout
import math, sys
from itertools import permutations, combinations
from collections import defaultdict, deque, OrderedDict
from os import path
import bisect as bi
import heapq

def yes():
	print('YES')

def no():
	print('NO')
if path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

	def I():
		return int(input())

	def In():
		return map(int, input().split())
else:

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

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

def dict(a):
	d = {}
	for x in a:
		if d.get(x, -1) != -1:
			d[x] += 1
		else:
			d[x] = 1
	return d

def main():
	try:
		(n, k) = In()
		l = input()
		ans = []
		for x in range(n // k):
			ans.append(l[k * x:k * x + k])
		t = k // 2
		word = []
		for x in range(t):
			d = {}
			ma = 1
			for t1 in ans:
				if d.get(t1[x], -1) != -1:
					d[t1[x]] += 1
					ma = max(d[t1[x]], ma)
				else:
					d[t1[x]] = 1
				if d.get(t1[k - x - 1], -1) != -1:
					d[t1[k - x - 1]] += 1
					ma = max(d[t1[k - x - 1]], ma)
				else:
					d[t1[k - x - 1]] = 1
			for x in d:
				if d[x] == ma:
					word.append(x)
					break
		if k & 1 == 1:
			d = {}
			ma = 1
			for x in ans:
				if d.get(x[k // 2], -1) != -1:
					d[x[k // 2]] += 1
					ma = max(d[x[k // 2]], ma)
				else:
					d[x[k // 2]] = 1
			for x in d:
				if d[x] == ma:
					word.append(x)
					break
			a1 = ''.join(word)
			word.pop(-1)
			word.reverse()
			a1 += ''.join(word)
		else:
			a1 = ''.join(word)
			a1 += a1[::-1]
		A = 0
		for x in ans:
			for j in range(k):
				if x[j] != a1[j]:
					A += 1
		print(A)
	except:
		pass
M = 998244353
P = 1000000007
for _ in range(I()):
	main()


def f(l):
	d = {}
	for i in l:
		if i in d:
			d[i] += 1
		else:
			d[i] = 1
	return len(l) - max(d.values())
import sys
for i in range(int(sys.stdin.readline())):
	(n, k) = map(int, sys.stdin.readline().split())
	s = input()
	print(sum((f(s[j::k] + s[k - 1 - j::k]) for j in range(k // 2))) + sum((f(s[k // 2::k]) for j in range(k % 2))))


import sys
import math
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	s = list(s)
	s.insert(0, 'q')
	arr = [[0 for i in range(27)] for j in range(n + 1)]
	ans = 0
	for i in range(1, k // 2 + 1):
		j1 = i
		jf = n - i - 1
		j2 = k + 1 - i
		for j in range(n // k):
			arr[i][ord(s[j1]) - 97] += 1
			arr[jf][ord(s[j2]) - 97] += 1
			j1 += k
			j2 += k
		mini = 2 * (n // k)
		for j2 in range(27):
			if 2 * n // k - (arr[i][j2] + arr[jf][j2]) < mini:
				mini = 2 * n // k - (arr[i][j2] + arr[jf][j2])
		ans += mini
	if k % 2 != 0:
		j1 = k // 2 + 1
		for j in range(n // k):
			arr[k // 2 + 1][ord(s[j1]) - 97] += 1
			j1 += k
		mini = n // k
		for j2 in range(27):
			if n // k - arr[k // 2 + 1][j2] < mini:
				mini = n // k - arr[k // 2 + 1][j2]
		ans += mini
	print(ans)


from collections import Counter
from sys import stdin, stdout
input = stdin.readline
print = stdout.write
T = int(input())
for _ in range(T):
	(n, k) = map(int, input().split())
	s = input()
	ls = [[] for _ in range((k + 1) // 2)]
	for i in range(0, n, k):
		p = s[i:i + k]
		for j in range((k + 1) // 2):
			if k % 2 and j == k // 2:
				ls[j].append(p[j])
			else:
				ls[j].append(p[j])
				ls[j].append(p[-(j + 1)])
	cc = 0
	for ts in ls:
		cc += len(ts) - max(Counter(ts).values())
	print(str(cc) + '\n')


import collections

def min_k_complete(s, n, k):

	def find(i):
		return min(i % k, (n - 1 - i) % k)
	d = collections.defaultdict(list)
	for (i, c) in enumerate(s):
		d[find(i)] += (c,)
	res = 0
	for (k, arr) in d.items():
		res += len(arr) - max(collections.Counter(arr).values())
	return res
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	print(min_k_complete(s, n, k))


def check(s, k):
	string = s[0:k]
	if s == s[::-1]:
		for i in range(len(s) // k):
			if s[i * k:(i + 1) * k] != string:
				return False
		return True
	else:
		return False

def calc(s):
	options = {}
	(i, j, count) = (0, len(s) - 1, 0)
	while i <= j:
		if s[i] != s[j]:
			count += 1
		if i != j:
			options[i] = [s[i], s[j]]
		else:
			options[i] = [s[i]]
		i += 1
		j -= 1
	return (options, count)
for nt in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	if check(s, k):
		print(0)
	else:
		freq = {}
		ans = 0
		for i in range(n // k):
			(d, ans) = calc(s[i * k:k * (i + 1)])
			for j in d:
				for m in d[j]:
					if j in freq:
						if m in freq[j]:
							freq[j][m] += 1
						else:
							freq[j][m] = 1
					else:
						freq[j] = {m: 1}
		ans = 0
		for i in freq:
			s = 0
			m = 0
			for j in freq[i]:
				s += freq[i][j]
				if freq[i][j] > m:
					m = freq[i][j]
			ans += s - m
		print(ans)


import sys
input = sys.stdin.readline
t = int(input())
while t:
	t -= 1
	(n, k) = map(int, input().split())
	a = input()
	r1 = [[] for i in range(k)]
	for i in range(n):
		r1[i % k].append(a[i])
	s = 0
	l = len(r1)
	for i in range(l // 2):
		f = [0 for k in range(26)]
		for j in r1[i]:
			f[ord(j) - 97] += 1
		for j in r1[-1 - i]:
			f[ord(j) - 97] += 1
		z = max(f)
		s += sum(f) - z
	if l % 2:
		f = [0 for k in range(26)]
		for j in r1[l // 2]:
			f[ord(j) - 97] += 1
		z = max(f)
		s += sum(f) - z
	print(s)


for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	s = input()
	z = n // k
	ans = 0
	for i in range(k // 2):
		(d, c) = ({}, 0)
		for j in range(0, n, k):
			(x, y) = (i + j, k - 1 + j - i)
			if s[x] in d:
				d[s[x]] += 1
			else:
				d[s[x]] = 1
			if s[y] in d:
				d[s[y]] += 1
			else:
				d[s[y]] = 1
			if d[s[x]] > c:
				c = d[s[x]]
			if d[s[y]] > c:
				c = d[s[y]]
		ans += 2 * z - c
	if k % 2 != 0:
		i = k // 2
		(d, c) = ({}, 0)
		for j in range(0, n, k):
			x = i + j
			if s[x] in d:
				d[s[x]] += 1
			else:
				d[s[x]] = 1
			if d[s[x]] > c:
				c = d[s[x]]
		ans += z - c
	print(ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	ans = 0
	for i in range(k // 2):
		l = [0] * 26
		c = 0
		for j in range(i, n, k):
			c += 1
			val = ord(s[j]) - ord('a')
			l[val] += 1
		for j in range(k - i - 1, n, k):
			c += 1
			val = ord(s[j]) - ord('a')
			l[val] += 1
		ans += c - max(l)
	if k % 2 != 0:
		c = 0
		l = [0] * 26
		for j in range(k // 2, n, k):
			c += 1
			val = ord(s[j]) - ord('a')
			l[val] += 1
		ans += c - max(l)
	print(ans)


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	st = input()
	v = k // 2 + k % 2
	fin = [[0 for l in range(26)] for j in range(v)]
	j = 0
	while j < n:
		ind = 0
		for l in range(k // 2):
			fin[l][ord(st[j + ind]) - 97] += 1
			ind += 1
		if k % 2 != 0:
			fin[k // 2][ord(st[j + ind]) - 97] += 1
			ind += 1
		for l in range(k // 2):
			fin[k // 2 - l - 1][ord(st[j + ind]) - 97] += 1
			ind += 1
		j += ind
	tot = 0
	for j in range(v):
		tot += max(fin[j])
	print(n - tot)


import math
t = int(input())
for case in range(t):
	(n, k) = map(int, input().split())
	s = input()
	c = 0
	for i in range(math.ceil(k / 2)):
		d = set()
		for j in range(0, n + 1, k):
			if j - i - 1 >= 0:
				d.add(j - i - 1)
			if j + i < n:
				d.add(j + i)
		g = {}
		for x in d:
			if s[x] in g:
				g[s[x]] += 1
			else:
				g[s[x]] = 1
		c += len(d) - max(g.values())
	print(c)


t = int(input())
while t > 0:
	(n, k) = map(int, input().split())
	s = input()
	count = 0
	no = n // k
	for i in range(0, k // 2):
		l = []
		for x in range(26):
			l.append(0)
		for j in range(no):
			l[ord(s[j * k + i]) - 97] += 1
			l[ord(s[k * (j + 1) - 1 - i]) - 97] += 1
		ns = max(l)
		count = count + 2 * no - ns
	if k % 2 == 1:
		i = k // 2
		l = []
		for x in range(26):
			l.append(0)
		for j in range(no):
			l[ord(s[j * k + i]) - 97] += 1
		ns = max(l)
		count = count + no - ns
	print(count)
	t -= 1


oa = ord('a')
st = lambda x: ord(x) - oa
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = list(map(st, list(input())))
	l = [s[i:i + k] for i in range(0, n, k)]
	c = n // k
	ans = n
	for i in range(k // 2):
		d = [0] * 30
		for x in l:
			d[x[i]] += 1
			d[x[k - i - 1]] += 1
		ans -= max(d)
	if k % 2:
		d = [0] * 30
		for x in l:
			d[x[k // 2]] += 1
		ans -= max(d)
	print(ans)


import collections

def solve(N, K, arr):
	res = 0
	for i in range(K // 2):
		sm = 0
		d = collections.Counter()
		for j in range(N // K):
			d[arr[j * K + i]] += 1
			d[arr[j * K + K - i - 1]] += 1
			sm += 2
		res += sm - max(d.values())
	if K % 2 == 1:
		sm = 0
		d = collections.Counter()
		for j in range(N // K):
			d[arr[j * K + (K - 1) // 2]] += 1
			sm += 1
		res += sm - max(d.values())
	print(res)
T = int(input())
for ti in range(1, T + 1):
	(N, K) = map(int, input().split(' '))
	arr = input()
	solve(N, K, arr)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	aa = [[0] * 26 for i in range(k)]
	ans = 0
	for i in range(k):
		for j in range(i, n, k):
			a = 1
			aa[i][ord(s[j]) - ord('a')] += 1
	grp = [0] * k
	for i in range(n // 2):
		if grp[i % k] == 1:
			continue
		if i % k == (n - 1 - i) % k:
			ans += sum(aa[i % k]) - max(aa[i % k])
			grp[i % k] = 1
		else:
			ab = sum(aa[i % k])
			ac = sum(aa[(n - i - 1) % k])
			m = 0
			for j in range(26):
				m = max(m, aa[i % k][j] + aa[(n - i - 1) % k][j])
			grp[i % k] = 1
			grp[(n - i - 1) % k] = 1
			ans += ab + ac - m
	print(ans)


from collections import defaultdict as dd
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(input())
	ans = 0
	for i in range(k // 2):
		s = dd(int)
		x = 0
		while i + x < n:
			s[a[i + x]] += 1
			x += k
		x = 0
		while n - i - 1 - x > 0:
			s[a[n - i - 1 - x]] += 1
			x += k
		ans += 2 * (n // k) - max(s.values())
	if k % 2:
		i = k // 2
		x = 0
		s = dd(int)
		while i + x < n:
			s[a[i + x]] += 1
			x += k
		ans += n // k - max(s.values())
	print(ans)


t = int(input())
sol = []
while t:
	t -= 1
	pas = 0
	(n, k) = tuple(map(int, input().split()))
	s = input()
	d = [i for i in range(k)]
	subu = [set([i]) for i in range(k)]
	dic = [{} for j in range(k)]
	i = 0
	j = k - 1
	poz1 = 0
	poz2 = n - 1
	while poz1 < poz2:
		subu[i].add(j)
		subu[j].add(i)
		i += 1
		j -= 1
		if i == k:
			i = 0
		if j == -1:
			j = k - 1
		poz1 += 1
		poz2 -= 1
	pr = 0
	vis = [True for i in range(k)]
	for i in s:
		if not dic[pr].get(i):
			dic[pr][i] = 0
		dic[pr][i] += 1
		pr += 1
		if pr == k:
			pr = 0
	h = 0
	for i in range(k):
		if vis[i]:
			nr = 0
			g = {}
			for j in subu[i]:
				vis[j] = False
				nr += 1
				for a in dic[j].keys():
					if g.get(a):
						g[a] += dic[j][a]
					else:
						g[a] = dic[j][a]
			mini = 0
			for a in g.keys():
				mini = max(g[a], mini)
			h += n // k * nr - mini
	sol.append(h)
for i in sol:
	print(i)


from collections import defaultdict
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = list(input())
	tp = n // k
	ans = 0
	dp = [[0] * 26 for _ in range((k + 1) // 2)]
	for i in range(n):
		dp[min(i % k, k - 1 - i % k)][ord(s[i]) - ord('a')] += 1
	for i in range(k // 2):
		ans += 2 * tp - max(dp[i])
	if k % 2 == 1:
		ans += tp - max(dp[k // 2])
	print(ans)


from sys import stdin
for _ in range(int(stdin.readline())):
	(n, k) = list(map(int, stdin.readline().split()))
	s = stdin.readline().strip()
	group = n // k
	res = 0
	for i in range(k // 2):
		max_appear = 0
		max_appear_char = ''
		d = {}
		for j in range(group):
			idxes = [j * k + i, j * k + (k - i - 1)]
			for idx in idxes:
				if s[idx] not in d:
					d[s[idx]] = 1
				else:
					d[s[idx]] += 1
				if d[s[idx]] > max_appear:
					max_appear = d[s[idx]]
					max_appear_char = s[idx]
		for (char, val) in d.items():
			if char != max_appear_char:
				res += val
	if k % 2 == 1:
		t = k // 2
		d = {}
		max_appear = 0
		max_appear_char = ''
		for i in range(t, n, k):
			c = s[i]
			if c not in d:
				d[c] = 1
			else:
				d[c] += 1
			if d[c] > max_appear:
				max_appear = d[c]
				max_appear_char = c
		for (char, val) in d.items():
			if char != max_appear_char:
				res += val
	print(res)


from collections import *
I = input
for _ in [0] * int(I()):
	(n, k) = map(int, I().split())
	s = I()
	s += s[::-1]
	print(n - sum((max(Counter(s[i::k]).values()) for i in range(k))) // 2)


from collections import Counter
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 main():
	for _ in range(II()):
		(n, k) = MI()
		aa = [ord(c) for c in SI()]
		fin = [False] * k
		ans = 0
		for m1 in range(k):
			if fin[m1]:
				continue
			m2 = (n - m1 - 1) % k
			if m1 == m2:
				cnt = Counter(aa[m1::k])
				fin[m1] = True
				ans += n // k - max(cnt.values())
			else:
				cnt = Counter(aa[m1::k] + aa[m2::k])
				fin[m1] = fin[m2] = True
				ans += 2 * n // k - max(cnt.values())
		print(ans)
main()


import sys
input = lambda : sys.stdin.readline().rstrip()
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	S = input()
	ans = 0
	for i in range((k + 1) // 2):
		A = [0] * 26
		for j in range(n // k):
			A[ord(S[j * k + i]) - 97] += 1
			if 2 * i + 1 != k:
				A[ord(S[(j + 1) * k - i - 1]) - 97] += 1
		ans += sum(A) - max(A)
	print(ans)


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']
t = int(input())
for j in range(t):
	(n, k) = map(int, input().split())
	ans = n
	s = str(input())
	if k % 2 == 0:
		for i in range(k // 2):
			b = [0] * 26
			for w in range(n // k):
				b[x.index(s[i + w * k])] += 1
				b[x.index(s[k - i - 1 + w * k])] += 1
			ans -= max(b)
		print(ans)
	else:
		for i in range(k // 2):
			b = [0] * 26
			for w in range(n // k):
				b[x.index(s[i + w * k])] += 1
				b[x.index(s[k - i - 1 + w * k])] += 1
			ans -= max(b)
		b1 = [0] * 26
		for w1 in range(n // k):
			b1[x.index(s[k // 2 + w1 * k])] += 1
		ans -= max(b1)
		print(ans)


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

def differ(u, cnt):
	ret = mx = 0
	for i in range(26):
		ret += cnt[u][i]
		mx = max(mx, cnt[u][i])
	return ret - mx
for _ in range(t):
	(n, k) = map(int, sys.stdin.readline().split())
	s = sys.stdin.readline()
	cnt = [[0 for i in range(26)] for j in range((k + 1) // 2)]
	for i in range(n):
		cnt[min(i % k, k - 1 - i % k)][ord(s[i]) - ord('a')] += 1
	ans = 0
	for i in range(math.ceil(k / 2)):
		ans += differ(i, cnt)
	print(ans)


import math
import sys
input = sys.stdin.readline
t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	s = list(input())
	if s[-1] == '\n':
		s.pop()
	arr = []
	for j in range(k):
		arr.append([])
	for j in range(n):
		x = j % k
		arr[x].append(s[j])
	for j in range(k // 2):
		arr[j].extend(arr[k - j - 1])
	ans = 0
	if k % 2 == 0:
		for j in range(k // 2):
			x = max(set(arr[j]), key=arr[j].count)
			cnt = arr[j].count(x)
			ans += len(arr[j]) - cnt
	else:
		for j in range(k // 2 + 1):
			x = max(set(arr[j]), key=arr[j].count)
			cnt = arr[j].count(x)
			ans += len(arr[j]) - cnt
	print(ans)


from sys import stdin
from collections import defaultdict, Counter
from math import ceil
t = int(input())
for _ in range(t):
	(n, k) = map(int, stdin.readline().split())
	S = stdin.readline().strip()
	count = 0
	sub_k = []
	for i in range(n // k):
		sub_k.append(S[k * i:k * i + k])
	for i in range(int(ceil(k / 2))):
		c = Counter()
		for j in range(n // k):
			if i != k - 1 - i:
				c[sub_k[j][i]] += 1
				c[sub_k[j][k - 1 - i]] += 1
			else:
				c[sub_k[j][i]] += 1
		M = max(c.values())
		if i != k - 1 - i:
			count += 2 * (n // k) - M
		else:
			count += n // k - M
	print(count)


from math import ceil, sqrt, log, gcd

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

def si():
	return input()

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

def li():
	return list(mi())
for _ in range(ii()):
	(n, k) = mi()
	s = si()
	ans = 0
	for i in range(ceil(k / 2)):
		x = [0] * 26
		for j in range(i, n, k):
			x[ord(s[j]) - ord('a')] += 1
		if i != k - i - 1:
			for j in range(k - i - 1, n, k):
				x[ord(s[j]) - ord('a')] += 1
		s1 = sum(x)
		n1 = max(x)
		ans += s1 - n1
	print(ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = [el for el in input()]
	ans = 0
	for i in range((k + 1) // 2):
		start = i
		end = k - i - 1
		o = 0
		seen = {}
		ind = set()
		while start < n:
			if s[start] not in seen:
				seen[s[start]] = 0
			if start not in ind:
				seen[s[start]] += 1
				ind.add(start)
			start += k
		while end < n:
			if s[end] not in seen:
				seen[s[end]] = 0
			if end not in ind:
				seen[s[end]] += 1
				ind.add(end)
			end += k
		m = max(seen.values())
		ans += len(ind) - m
	print(ans)


import sys
from math import log2, ceil
input = sys.stdin.readline

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

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

def primefs(n):
	if n == 1:
		return 1
	primes = {}
	while n % 2 == 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	while x != link[x]:
		x = link[x]
	return x

def union(x, y, size, link):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def int_array():
	return list(map(int, input().split()))
for __ in range(int(input())):
	(n, k) = int_array()
	string = input()
	a = [[0 for _ in range(26)] for _ in range(k)]
	for i in range(n):
		a[i % k][ord(string[i]) - ord('a')] += 1
	ans = 0
	for i in range(ceil(k / 2)):
		this = [0] * 26
		for j in range(26):
			if k % 2 != 0 and i == k // 2:
				this[j] = a[i][j]
			else:
				this[j] = a[i][j] + a[k - i - 1][j]
		ans += sum(this) - max(this)
	print(ans)


import sys
from functools import lru_cache
from heapq import merge, heapify, heappop, heappush
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf
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
mod = pow(10, 9) + 7
mod2 = 998244353

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

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

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

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)]
for _ in range(int(data())):
	(n, k) = sp()
	s = list(data())
	dp = dd(list)
	for i in range(n):
		dp[i % k].append(s[i])
	answer = 0
	for i in range(0, k // 2):
		c1 = c(dp[i])
		c2 = c(dp[k - i - 1])
		m = mod
		for j in c1.keys():
			if j in c2.keys():
				a = len(dp[i]) + len(dp[k - i - 1]) - c1[j] - c2[j]
				del c2[j]
			else:
				a = len(dp[i]) + len(dp[k - i - 1]) - c1[j]
			m = min(a, m)
		for j in c2.keys():
			m = min(m, len(dp[i]) + len(dp[k - i - 1]) - c2[j])
		answer += m
	if k & 1:
		answer += len(dp[k // 2]) - max(c(dp[k // 2]).values())
	outln(answer)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	cnt_arr = [[0 for i in range(26)] for j in range((k + 1) // 2)]
	for i in range(n):
		cnt_arr[min(i % k, k - i % k - 1)][ord(s[i]) - ord('a')] += 1
	ans = 0
	for i in range(k // 2):
		ans += 2 * n // k - max(cnt_arr[i])
	if k & 1:
		ans += n // k - max(cnt_arr[k // 2])
	print(ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(str(input()))
	l1 = [0] * n
	i = 0
	c = 0
	if n == 2:
		if s[0] == s[1]:
			print(0)
		else:
			print(1)
		continue
	while i <= n // 2:
		l2 = []
		for j in range(i, n, k):
			if l1[j] == 0:
				l2.append(s[j])
				l1[j] = 1
			if l1[n - j - 1] == 0:
				l2.append(s[n - j - 1])
				l1[n - j - 1] = 1
		dict = {}
		(count, itm) = (0, '')
		for item in reversed(l2):
			dict[item] = dict.get(item, 0) + 1
			if dict[item] >= count:
				(count, itm) = (dict[item], item)
		aa = count
		c += len(l2) - count
		for j in range(i, n):
			if l1[j] == 1:
				i += 1
				if i > n // 2:
					break
			else:
				break
	print(c)


testcases = int(input())
for testcase in range(testcases):
	temp = input()
	temp = temp.split()
	n = int(temp[0])
	nlen = int(temp[1])
	x = input()
	group = []
	(chunks, chunk_size) = (n, nlen)
	group = [x[i:i + chunk_size] for i in range(0, chunks, chunk_size)]
	dicts = {}
	for i in group:
		index = 0
		for j in i:
			if index not in dicts:
				dicts[index] = []
			dicts[index].append(j)
			index += 1
	left = 0
	right = len(dicts) - 1
	while left < right:
		tempindex = dicts[right]
		for i in tempindex:
			dicts[left].append(i)
		del dicts[right]
		left += 1
		right -= 1
	total = 0
	for (key, value) in dicts.items():
		needsame = len(value)
		needadd = len(value)
		tdicts = {}
		for i in value:
			if i not in tdicts:
				tdicts[i] = 1
			else:
				tdicts[i] += 1
		for (key, value) in tdicts.items():
			diffval = needsame - value
			needadd = min(needadd, diffval)
		total += needadd
	print(total)


import math

class Char:
	ch = ''
	count = 0
t = int(input())
for test in range(0, t):
	s = input()
	a = s.split()
	n = int(a[0])
	k = int(a[1])
	s = input()
	arrs = [[0] * 26 for i in range(math.ceil(k / 2))]
	for i in range(0, n):
		if i % k < k / 2:
			arrs[i % k][ord(s[i]) - ord('a')] += 1
		else:
			arrs[k - 1 - i % k][ord(s[i]) - ord('a')] += 1
	tot = 0
	for i in range(0, math.ceil(k / 2)):
		if k % 2 == 1 and i == math.ceil(k / 2) - 1:
			tot += n / k - max(arrs[i])
		else:
			tot += 2 * (n / k) - max(arrs[i])
	print(int(tot))


import collections

def answer(s, k):
	n = len(s)
	if n == 2:
		if s[0] != s[1]:
			return 1
	minimum = 0
	s = [c for c in s]
	if k == 1:
		dic = collections.Counter(s)
		_max = dic.most_common()[0][1]
		minimum = n - _max
		return minimum
	for i in range(n - k):
		if s[i] == -1:
			continue
		periodArr = []
		palindromeArr = []
		j = i
		while n > j:
			pali = n - j - 1
			if s[j] != -1:
				periodArr.append(s[j])
				s[j] = -1
			if j != pali and s[pali] != -1:
				palindromeArr.append(s[pali])
				s[pali] = -1
			j = j + k
		arr = periodArr + palindromeArr
		dic = collections.Counter(arr)
		_max = dic.most_common()[0][1]
		diff = len(arr) - _max
		minimum += diff
	return minimum
testCount = int(input())
for _ in range(testCount):
	(n, k) = map(int, input().split())
	s = input().rstrip()
	print(answer(s, k))


import sys
from collections import Counter

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 ** 18
MOD = 10 ** 9 + 7
for _ in range(INT()):
	(N, K) = MAP()
	S = input()
	C = [Counter() for i in range(ceil(K, 2))]
	for i in range(0, N, K):
		for j in range(K // 2):
			(a, b) = (S[i + j], S[i + K - j - 1])
			C[j][a] += 1
			C[j][b] += 1
		if K % 2 == 1:
			a = S[i + K // 2]
			C[K // 2][a] += 1
	ans = 0
	for c in C:
		ans += sum(c.values()) - max(c.values())
	print(ans)


from sys import stdin
from collections import defaultdict
import math
t = int(stdin.readline())
for _ in range(t):
	(n, k) = map(int, stdin.readline().rstrip().split(' '))
	s = stdin.readline().rstrip()
	l = []
	for i in range(math.ceil(k / 2)):
		d = defaultdict(lambda : 0)
		for j in range(n // k):
			d[s[j * k + i]] += 1
			if k - (i + 1) != i:
				d[s[(j + 1) * k - (i + 1)]] += 1
		l.append(d)
	answer = 0
	for d in l:
		ds = sum(d.values())
		dm = max(d.values())
		answer += ds - dm
	print(answer)


def main():
	import sys
	input = sys.stdin.readline
	for _ in range(int(input())):
		(N, K) = map(int, input().split())
		S = input().rstrip('\n')
		cnt = [[0] * K for _ in range(26)]
		for (i, s) in enumerate(S):
			j = ord(s) - 97
			cnt[j][i % K] += 1
		ans = 0
		L = N // K * 2
		for i in range(K // 2):
			tmp = N + 1
			for j in range(26):
				tmp = min(tmp, L - (cnt[j][i] + cnt[j][K - i - 1]))
			ans += tmp
		if K & 1:
			tmp = N + 1
			for j in range(26):
				tmp = min(tmp, N // K - cnt[j][K // 2])
			ans += tmp
		print(ans)
main()


def bfs(s):
	visited[s] = 1
	ch = [0] * 26
	c = 1
	ch[ord(a[s]) - 97] += 1
	frontier = [s]
	while frontier:
		next = []
		for u in frontier:
			for v in adj[u]:
				if v not in visited:
					visited[v] = 1
					ch[ord(a[v]) - 97] += 1
					c += 1
					next.append(v)
		frontier = next
	return c - max(ch)
t = int(input())
for w in range(t):
	(n, k) = map(int, input().split())
	a = list(input())
	adj = {}
	for i in range(n):
		if i + k < n:
			if i not in adj:
				adj[i] = [i + k]
			else:
				adj[i].append(i + k)
		if i not in adj:
			adj[i] = [n - 1 - i]
		else:
			adj[i].append(n - 1 - i)
	visited = {}
	res = 0
	for i in range(n):
		if i not in visited:
			res += bfs(i)
	print(res)


from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush, heappop
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')]

def add2(a, b):
	for i in range(26):
		a[i] += b[i]
	return a[:]
for _ in range(val()):
	(n, k) = li()
	l = st()
	ans = 0
	for i in range(math.ceil(k / 2)):
		cnt = Counter()
		for j in range(i, n, k):
			cnt[l[j]] += 1
		if i != k - i - 1:
			for j in range(k - i - 1, n, k):
				cnt[l[j]] += 1
		ans += sum(cnt.values()) - max(cnt.values())
	print(ans)


t = int(input())
while t != 0:
	(n, k) = map(int, input().split())
	list1 = list(input())
	ind = 0
	ans = 0
	for i in range(k // 2):
		count = 0
		arr = [0] * 26
		for j in range(n // k):
			p = list1[count + ind]
			count += k
			arr[ord(p) - ord('a')] += 1
		count = 0
		for j in range(n // k):
			p = list1[count + k - ind - 1]
			count += k
			arr[ord(p) - ord('a')] += 1
		ind += 1
		ans += n // k + n // k - max(arr)
	if k % 2 == 1:
		count = 0
		ind = k // 2
		arr = [0] * 26
		for i in range(n // k):
			p = list1[count + ind]
			count += k
			arr[ord(p) - ord('a')] += 1
		ans += n // k - max(arr)
	print(ans)
	t -= 1


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	ans = 0
	for i in range((k + 1) // 2):
		d = {}
		mx = -1
		sec = k - i - 1
		for j in range(i, n, k):
			d[s[sec]] = d.get(s[sec], 0) + 1
			if j != sec:
				d[s[j]] = d.get(s[j], 0) + 1
			sec += k
		sm = 0
		for p in d:
			mx = max(mx, d[p])
			sm += d[p]
		ans += sm - mx
	print(ans)


from collections import Counter
from collections import defaultdict
import math
t = int(input())
for _ in range(0, t):
	(n, k) = list(map(int, input().split()))
	s = list(input())
	a = list()
	p = n // k
	for i in range(0, n):
		a.append([0] * 26)
	for i in range(0, n):
		a[i % k][ord(s[i]) - ord('a')] += 1
	ans = 0
	f = 1
	for i in range(0, k):
		mx = 0
		if i > k - 1 - i:
			f = 0
			break
		if i == k - 1 - i:
			ans = ans + p - max(a[i])
			f = 0
			break
		if f == 0:
			break
		for j in range(0, 26):
			mx = max(mx, a[i][j] + a[k - 1 - i][j])
		ans = ans + 2 * p - mx
	print(ans)


for w in range(int(input())):
	(n, k) = tuple(map(int, input().split()))
	s = str(input())
	vis = [0] * n
	a = []
	ans = 0
	for i in range(n):
		if vis[i] == 0:
			if i != n - 1 - i:
				l = [i, n - 1 - i]
				vis[i] = 1
				vis[n - 1 - i] = 1
			else:
				l = [i]
				vis[i] = 1
			for j in range(i + k, n, k):
				if vis[j] == 0:
					if j != n - 1 - j:
						l.append(j)
						l.append(n - 1 - j)
						vis[j] = 1
						vis[n - 1 - j] = 1
					else:
						l.append(j)
						vis[j] = 1
			a.append(l)
	for i in range(len(a)):
		d = {}
		x = len(a[i])
		for j in range(x):
			if s[a[i][j]] not in d:
				d[s[a[i][j]]] = 1
			else:
				d[s[a[i][j]]] += 1
		m = 0
		for j in d:
			m = max(m, d[j])
		ans += x - m
	print(ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	ans = 0
	for i in range(0, k):
		alphaList = [0] * 26
		for j in range(i, n, k):
			alphaList[ord(s[j]) - 97] += 1
			alphaList[ord(s[k - 1 - j]) - 97] += 1
		ans += sum(alphaList) - max(alphaList)
	print(ans // 2)


from collections import defaultdict
for _ in [0] * int(input()):
	(n, k) = map(int, input().split())
	S = input()
	m = (k + 1) // 2
	to_g = [i if i < m else k - 1 - i for i in range(k)] * (n // k)
	count = [defaultdict(int) for _ in range(m)]
	for (i, s) in enumerate(S):
		count[to_g[i]][s] += 1
	ans = n
	for c in count:
		ans -= max(c.values())
	print(ans)


from sys import stdin, stdout
from collections import Counter
from math import ceil

def find(wr, k):
	N = len(wr) // (k * 2)
	M = len(wr) // k
	arr = [Counter() for _ in range(ceil(k / 2))]
	for i in range(ceil(k / 2)):
		for j in range(len(wr) // k):
			if i == k // 2:
				arr[i][wr[k * j + i]] += 1
			else:
				arr[i][wr[k * j + i]] += 1
				arr[i][wr[k * j + k - i - 1]] += 1
	c = 0
	for i in arr:
		l = i.values()
		c += sum(l) - max(l)
	return c

def main():
	for _ in range(int(input())):
		(n, k) = list(map(int, stdin.readline().strip().split()))
		wr = stdin.readline().strip()
		stdout.write(str(find(wr, k)) + '\n')
main()


t = int(input())
for k in range(t):
	(n, k) = map(int, input().split())
	s = input()
	sum1 = 0
	for i in range(k):
		pos = [0] * 26
		for j in range(i, n, k):
			pos[ord(s[j]) - ord('a')] += 1
			pos[ord(s[k - 1 - j]) - ord('a')] += 1
		sum1 += sum(pos) - max(pos)
	print(sum1 // 2)


for t in range(int(input())):
	(n, k) = [int(i) for i in input().split()]
	p = n // k
	s = input()
	a = [[0] * 26 for i in range(k)]
	for i in range(n):
		a[i % k][ord(s[i]) - ord('a')] += 1
		a[i % k][ord(s[n - 1 - i]) - ord('a')] += 1
	total = 0
	for i in a:
		total += 2 * p - max(i)
	print(total // 2)


from collections import Counter
t = int(input())
for i in range(t):
	(n, k) = [int(v) for v in input().split()]
	s = input()
	c = n // k
	res = 0
	d = 0
	while d < k // 2 + k % 2:
		iota = []
		for j in range(c):
			if d != k - 1 - d:
				iota.append(s[j * k + d])
				iota.append(s[j * k + k - 1 - d])
			else:
				iota.append(s[j * k + d])
		q = Counter(iota)
		if iota:
			res += len(iota) - max(list(q.values()))
		d += 1
	print(res)


testcases = int(input())
for j in range(testcases):
	values = list(map(int, input().split()))
	n = values[0]
	k = values[1]
	words = [ord(k) - 96 for k in input()]
	moves = 0
	if k % 2 == 0:
		for s in range(k // 2):
			store1 = [0 for i in range(26)]
			start = s
			inc = k
			cv = 0
			counter = 0
			while start + cv * inc <= n - 1:
				store1[words[start + cv * inc] - 1] += 1
				store1[words[k - 1 - start + cv * inc] - 1] += 1
				cv += 1
			a1 = 2 * n // k - max(store1)
			moves += a1
	elif k % 2 == 1:
		for s in range(k // 2 + 1):
			store1 = [0 for i in range(26)]
			start = s
			inc = k
			cv = 0
			counter = 0
			if start != k // 2:
				while start + cv * inc <= n - 1:
					store1[words[start + cv * inc] - 1] += 1
					store1[words[k - 1 - start + cv * inc] - 1] += 1
					cv += 1
				a1 = 2 * n // k - max(store1)
				moves += a1
			elif start == k // 2:
				while start + cv * inc <= n - 1:
					store1[words[start + cv * inc] - 1] += 1
					cv += 1
				a1 = n // k - max(store1)
				moves += a1
	print(moves)


from sys import stdin
input = stdin.readline
from collections import defaultdict as di, deque as dq

def f(s, k):
	cost = 0
	n = len(s)
	found = [0] * n
	for i in range(n):
		if found[i]:
			continue
		counter = di(int)
		bfs = dq()
		bfs.append(i)
		while bfs:
			node = bfs.popleft()
			if node < 0 or node >= n or found[node]:
				continue
			found[node] = 1
			counter[s[node]] += 1
			bfs.append(n - node - 1)
			bfs.append(node + k)
			bfs.append(node - k)
		t = 0
		mx = 0
		for char in counter:
			t += counter[char]
			mx = max(mx, counter[char])
		cost += t - mx
	return cost
for i in range(int(input())):
	(a, b) = map(int, input().strip().split())
	s = list(input().strip())
	print(f(s, b))


from collections import Counter
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input().rstrip()
	ls = [[] for i in range((k + 1) // 2)]
	for i in range(n):
		x = ord(s[i])
		i %= k
		ls[min(i, k - i - 1)].append(x)
	ans = 0
	for i in ls:
		ans += len(i) - Counter(i).most_common()[0][1]
	print(ans)


import math, sys
from collections import Counter

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

def ls():
	return list(map(int, list(input())))

def la():
	return list(input())

def i():
	return int(input())

def isPrime(n):
	i = 2
	if n == 1:
		return False
	while i <= int(math.sqrt(n)):
		if n % i == 0:
			return False
		i = i + 1
	return True

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)
t = i()
for _ in range(t):
	(n, k) = li()
	a = la()
	b = []
	for i in range(math.ceil(k / 2)):
		c = []
		j = 0
		while j < n:
			if j + i != j + k - i - 1:
				c.append(a[j + i])
				c.append(a[j + k - i - 1])
			else:
				c.append(a[j + i])
			j = j + k
		c = len(c) - max(Counter(c).values())
		b.append(c)
	print(sum(b))


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	l = input()
	ans = [[0 for i in range(26)] for i in range((k + 1) // 2)]
	t = 0
	for i in range(n):
		ans[min(i % k, k - i % k - 1)][ord(l[i]) - ord('a')] += 1
	for i in range(k // 2):
		t += 2 * n // k - max(ans[i])
	if k % 2 == 1:
		t += n // k - max(ans[k // 2])
	print(t)


from collections import defaultdict

def f(d):
	mm = max(d.values())
	ss = sum(d.values())
	return ss - mm
for _ in range(int(input())):
	(n, k) = [int(x) for x in input().split()]
	s = input()
	r = 0
	for i in range(k // 2):
		d = defaultdict(int)
		for m in range(n // k):
			a = i + k * m
			b = k - 1 - i + k * m
			d[s[a]] += 1
			d[s[b]] += 1
		r += f(d)
	if k % 2 == 1:
		d = defaultdict(int)
		for m in range(n // k):
			c = k // 2 + k * m
			d[s[c]] += 1
		r += f(d)
	print(r)


import collections

def get(t=int):
	lis = input().strip().split()
	return list(map(t, lis))
__t = get()[0]

def getseed(s, k):
	if k == 1:
		return len(s) - collections.Counter(s).most_common(1)[0][1]
	l = k
	counters = ['' for i in range(k)]
	for i in range(k):
		temp = ''
		for j in range(len(s) // k):
			if j * k + i < len(s):
				temp += s[j * k + i]
			else:
				break
		counters[i] = temp
	cnt = 0
	for i in range(l):
		if i <= l - i - 1:
			sbs = counters[i] + counters[l - i - 1]
			sbc = collections.Counter(sbs)
			if i != l - i - 1:
				cnt += 2 * (len(s) // k) - sbc.most_common(1)[0][1]
			else:
				cnt += len(s) // k - sbc.most_common(1)[0][1] // 2
		else:
			break
	return cnt
for __i in range(__t):
	(n, k) = get()
	s = get(str)[0]
	if len(s) == 2 and k == 1:
		if s[0] == s[1]:
			print(0)
		else:
			print(1)
	else:
		seed = getseed(s, k)
		print(seed)


import sys
input = sys.stdin.readline
Q = int(input())
Query = []
for _ in range(Q):
	(N, K) = map(int, input().split())
	S = list(input().rstrip())
	Query.append((N, K, S))
for (N, K, S) in Query:
	ans = 0
	L = N // K
	for r in range(K):
		if K % 2 == 0 and r >= K // 2:
			break
		if K % 2 == 1 and r == K // 2:
			dic = {}
			for i in range(L):
				s = S[i * K + r]
				if s in dic:
					dic[s] += 1
				else:
					dic[s] = 1
			ans += L - max(list(dic.values()))
			break
		else:
			dic = {}
			for i in range(L):
				Ss = [S[i * K + r], S[i * K + (K - r - 1)]]
				for s in Ss:
					if s in dic:
						dic[s] += 1
					else:
						dic[s] = 1
			ans += 2 * L - max(list(dic.values()))
	print(ans)


def find_root(g, x):
	while x != g[x]:
		x = g[x]
	return x

def find_condition(n, k):
	start = {i: i for i in range(k)}
	for j in range(n):
		i1 = j % k
		i2 = (n - 1 - j) % k
		I1 = find_root(start, i1)
		I2 = find_root(start, i2)
		start[I1] = min(I1, I2)
		start[I2] = min(I1, I2)
	answer = {}
	for x in start:
		answer[x] = find_root(start, x)
	return answer

def process(S, n, k):
	a1 = find_condition(n, k)
	counting = {}
	for i in range(n):
		I = a1[i % k]
		if I not in counting:
			counting[I] = [0, {}, 0]
		counting[I][0] += 1
		c = S[i]
		if c not in counting[I][1]:
			counting[I][1][c] = 0
		counting[I][1][c] += 1
		counting[I][2] = max(counting[I][2], counting[I][1][c])
	answer = 0
	for x in counting:
		answer += counting[x][0] - counting[x][2]
	return answer
t = int(input())
for i in range(t):
	(n, k) = [int(x) for x in input().split()]
	S = input()
	print(process(S, n, k))


import sys
from math import log2, ceil
input = sys.stdin.readline

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

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

def primefs(n):
	if n == 1:
		return 1
	primes = {}
	while n % 2 == 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	while x != link[x]:
		x = link[x]
	return x

def union(x, y, size, link):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def int_array():
	return list(map(int, input().split()))
for _ in range(int(input())):
	(n, k) = int_array()
	string = input()
	link = [i for i in range(n)]
	size = [1] * n
	for i in range(n // 2):
		x = i
		y = n - 1 - i
		union(x, y, size, link)
	for i in range(n - k):
		if i + k < n:
			x = i
			y = i + k
			union(x, y, size, link)
	final = []
	for i in link:
		final.append(find(i, link))
	dick = {i: {} for i in range(len(set(final)))}
	for i in range(n):
		f = final[i]
		d = dick[f]
		d[string[i]] = d.get(string[i], 0) + 1
	ans = 0
	for i in dick:
		total = 0
		maxi = -1
		for j in dick[i]:
			total += dick[i][j]
			maxi = max(maxi, dick[i][j])
		ans += total - maxi
	print(ans)


import math
t = int(input())
for _ in range(t):
	(n, k) = list(map(int, input().split()))
	a = input()
	b = [[0] * 26 for i in range(math.ceil(k / 2))]
	if k == 1:
		z = [0] * 26
		for i in range(n):
			z[ord(a[i]) - 97] += 1
		p = sum(z)
		q = max(z)
		print(p - q)
	else:
		i = 0
		while i < n:
			p = i
			q = i + k - 1
			r = k // 2
			x = 0
			while x < r:
				b[x][ord(a[p + x]) - 97] += 1
				b[x][ord(a[q - x]) - 97] += 1
				x = x + 1
			if k % 2 == 1:
				b[x][ord(a[p + x]) - 97] += 1
			i = i + k
		l = 0
		for i in b:
			p = sum(i)
			q = max(i)
			l += p - q
		print(l)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	p = n // k
	s = '0' + input()
	count = 0
	for i in range(1, k // 2 + 1):
		dict = {}
		maxx = 0
		for j in range(p):
			if s[j * k + i] in dict:
				dict[s[j * k + i]] += 1
			else:
				dict[s[j * k + i]] = 1
			maxx = max(maxx, dict[s[j * k + i]])
		for j in range(1, p + 1):
			if s[j * k - i + 1] in dict:
				dict[s[j * k - i + 1]] += 1
			else:
				dict[s[j * k - i + 1]] = 1
			maxx = max(maxx, dict[s[j * k - i + 1]])
		count += 2 * p - maxx
	if k % 2 != 0:
		maxx = 0
		dict = {}
		i = k // 2 + 1
		for j in range(p):
			if s[j * k + i] in dict:
				dict[s[j * k + i]] += 1
			else:
				dict[s[j * k + i]] = 1
			maxx = max(maxx, dict[s[j * k + i]])
		count += p - maxx
	print(count)


for _ in range(int(input())):
	(n, k) = [int(c) for c in input().split()]
	s = [i for i in input()]
	ans = 0
	dic = [0 for i in range(30)]
	h = k // 2 if k % 2 == 0 else k // 2 + 1
	for i in range(h):
		mx = 0
		m = n // k
		dic = [0 for o in range(30)]
		for j in range(i, n, k):
			dic[ord(s[j]) - 97] += 1
		if k - 1 - i != i:
			l = k - 1 - i
			m = 2 * (n // k)
			for j in range(l, n, k):
				dic[ord(s[j]) - 97] += 1
		mx = max(dic)
		ans += m - mx
	print(ans)


import collections
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = list(input())
	cnt = 0
	para = []
	for i in range(k):
		para.append([])
	for i in range(n):
		para[i % k].append(s[i])
	if k % 2 == 0:
		for i in range(k // 2):
			hind = collections.Counter(para[i] + para[k - i - 1])
			nax = max(list(hind.values()))
			cnt += n // k * 2 - nax
	else:
		for i in range(k // 2):
			hind = collections.Counter(para[i] + para[k - i - 1])
			nax = max(list(hind.values()))
			cnt += n // k * 2 - nax
		hind = collections.Counter(para[k // 2])
		nax = max(list(hind.values()))
		cnt += n // k - nax
	print(cnt)


import math
T = int(input())
for _ in range(0, T):
	(n, k) = map(int, input().split())
	s = input()
	L = []
	for i in range(k):
		h = []
		j = i
		while j < len(s):
			h.append(s[j])
			j += k
		L.append(h)
	for i in range(k // 2):
		L[i] += L[k - i - 1]
	ans = 0
	ln = k // 2
	if k % 2 != 0:
		ln += 1
	for i in range(0, ln):
		pos = [0] * 26
		for j in range(0, len(L[i])):
			pos[ord(L[i][j]) - 97] += 1
		mx = max(pos)
		for i in range(26):
			ans += pos[i]
		ans -= mx
	print(ans)


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input().rstrip()
	chk = [0] * n
	q = [[0] * 26]
	now = 1
	cnt = 0
	for x in range(n // 2):
		if chk[x] != 0:
			continue
		chk[x] = now
		q[now - 1][ord(s[x]) - ord('a')] += 1
		for y in range(x, n, k):
			if chk[y] == 0:
				chk[y] = now
				q[now - 1][ord(s[y]) - ord('a')] += 1
			if n - y - 1 >= 0 and chk[n - y - 1] == 0:
				chk[n - y - 1] = now
				q[now - 1][ord(s[n - y - 1]) - ord('a')] += 1
		now += 1
		q.append([0] * 26)
	del q[-1]
	for x in q:
		cnt += sum(x) - max(x)
	print(cnt)


import sys
input = lambda : sys.stdin.readline().strip()
t = int(input())
while t:
	t -= 1
	(n, k) = map(int, input().split())
	s = list(input())
	d = [[0] * 26 for _ in range(k)]
	for i in range(n):
		d[i % k][ord(s[i]) - ord('a')] += 1
	ans = 0
	i = 0
	j = k - 1
	while i <= j:
		if i == j:
			ans += n // k - max(d[i])
			i += 1
			j -= 1
			continue
		for l in range(26):
			d[i][l] += d[j][l]
		ans += 2 * (n // k) - max(d[i])
		i += 1
		j -= 1
	print(ans)


import math
a = int(input())
for i in range(a):
	(n, k) = map(int, input().split())
	s = input()
	ans = [[0 for i in range(26)] for i in range(k)]
	for j in range(k):
		for i in range(n // k):
			ans[j][ord(s[k * i + j]) - 97] += 1
	total = 0
	count = 0
	M = []
	for i in range(k):
		total = 0
		mini = math.inf
		for j in range(26):
			total = n // k - ans[i][j] + n // k - ans[k - i - 1][j]
			if total < mini:
				mini = total
		M.append(mini)
	print(sum(M) // 2)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	ans = []
	if k % 2 == 1:
		ran = k // 2 + 1
	else:
		ran = k // 2
	for i in range(ran):
		temp = []
		start = i
		end = k - i - 1
		for j in range(n // k):
			if i != k // 2:
				temp.append(s[start + (j - 1) * k])
			temp.append(s[end + (j - 1) * k])
		ans.append(temp)
	res = 0
	for i in range(len(ans)):
		some = {}
		for j in range(len(ans[i])):
			if some.get(ans[i][j]) == None:
				some[ans[i][j]] = 1
			else:
				some[ans[i][j]] += 1
		res += len(ans[i]) - max(some.values())
	print(res)


t = int(input())
for i in range(t):
	(n, k) = [int(i) for i in input().split()]
	s = input()
	ans = 0
	for i in range(k // 2):
		cnt = [0 for i in range(26)]
		l = k - i - 1
		for j in range(i, len(s), k):
			cnt[ord(s[j]) - ord('a')] += 1
		for j in range(l, len(s), k):
			cnt[ord(s[j]) - ord('a')] += 1
		ans += sum(cnt) - max(cnt)
	if k % 2 == 1:
		i = k // 2
		cnt = [0 for i in range(26)]
		for j in range(i, len(s), k):
			cnt[ord(s[j]) - ord('a')] += 1
		ans += sum(cnt) - max(cnt)
	print(ans)


t = int(input())
while t != 0:
	(n, k) = map(int, input().split())
	s = input()
	x = 0
	for i in range(k // 2):
		al = [0] * 26
		for j in range(i, n, k):
			al[ord(s[j]) - 97] += 1
			al[ord(s[(j // k + 1) * k - j % k - 1]) - 97] += 1
		x += sum(al) - max(al)
	if k % 2:
		al = [0] * 26
		for j in range(k // 2, n, k):
			al[ord(s[j]) - 97] += 1
		x += sum(al) - max(al)
	print(x)
	t -= 1


from collections import defaultdict, Counter

def test_case():
	(n, k) = map(int, input().split())
	s = input()
	groups = [[0] * 26 for _ in range(k)]
	a = ord('a')
	for (i, ch) in enumerate(s):
		groups[i % k][ord(ch) - a] += 1
	ngroups = n // k
	ans = 0
	for i in range(k // 2):
		front = groups[i]
		back = groups[k - i - 1]
		cnt = 0
		for (f, b) in zip(front, back):
			cnt = max(cnt, f + b)
		ans += 2 * ngroups - cnt
	if k % 2 == 1:
		cnt = max(groups[k // 2])
		ans += ngroups - cnt
	print(ans)
T = int(input())
for _ in range(T):
	test_case()


from collections import Counter
R = lambda : map(int, input().split())
for _ in range(int(input())):
	(n, k) = R()
	s = input()
	cnt = 0
	for i in range(k // 2):
		cnt += 2 * n // k - max(Counter(s[i::k] + s[k - i - 1::k]).values())
	if k & 1:
		cnt += n // k - max(Counter(s[k // 2::k]).values())
	print(cnt)


def pet(s):
	c = [0] * 26
	n = len(s)
	for i in range(len(s)):
		c[ord(s[i]) - 97] += 1
	return n - max(c)
import math
import sys
for _ in range(int(input())):
	(n, k) = map(int, sys.stdin.readline().split())
	q = n // k
	s = input()
	ans = 0
	if k == 1:
		print(pet(s))
	else:
		a = ['' for i in range(math.ceil(k / 2))]
		for i in range(q):
			st = s[i * k:i * k + k]
			for j in range(math.ceil(k / 2)):
				if j == k - j - 1:
					a[j] += st[j]
				else:
					a[j] += st[j] + st[k - j - 1]
		for i in range(math.ceil(k / 2)):
			l = list(a[i])
			ans += pet(a[i])
		print(ans)


import math as m
import string

def f1():
	T = int(input())
	while T != 0:
		T = T - 1
		ARRAY1 = [int(i) for i in input().split(' ')]
		ARRAY2 = [int(i) for i in input().split(' ')]
		(L, R, D, U) = (ARRAY1[0], ARRAY1[1], ARRAY1[2], ARRAY1[3])
		(x, y) = (ARRAY2[0], ARRAY2[1])
		(x1, y1) = (ARRAY2[2], ARRAY2[3])
		(x2, y2) = (ARRAY2[4], ARRAY2[5])
		Flag1 = False
		Flag2 = False
		if y1 <= y - D + U and y - D + U <= y2 and (y1 < y2 or D + U == 0):
			Flag1 = True
		if x1 <= x - L + R and x - L + R <= x2 and (x1 < x2 or L + R == 0):
			Flag2 = True
		if Flag1 == True and Flag2 == True:
			print('YES')
		else:
			print('NO')

def f2():
	T = int(input())
	while T != 0:
		T = T - 1
		N = int(input())
		ARRAY = [int(i) for i in input().split(' ')]
		ARR = [-1 for i in range(0, len(ARRAY))]
		CHECKER = [False] * N
		MAXELEMENT = max(ARRAY)
		Counter = len(ARRAY)
		Primes = GetPrimes(MAXELEMENT)
		Colour = 0
		Flag = False
		for i in range(2, len(Primes)):
			if Counter == 0:
				break
			if Primes[i] == True:
				Prime = i
				Flag = False
				Colour = Colour + 1
				for j in range(0, len(ARRAY)):
					if CHECKER[j] == True:
						continue
					if ARRAY[j] % Prime == 0:
						Flag = True
						ARR[j] = Colour
						CHECKER[j] = True
						Counter = Counter - 1
				if Flag == False:
					Colour = Colour - 1
		print(Colour)
		for i in range(0, len(ARR)):
			print(ARR[i], end=' ')
		print()

def GetPrimes(MAXELEMENT):
	ARRAY = [-1 for i in range(0, MAXELEMENT)]
	for i in range(0, MAXELEMENT):
		Number = i
		if Number == 1 or Number == 0:
			continue
		if ARRAY[i] == False:
			continue
		ARRAY[i] = True
		j = 2
		X = Number * j
		while X < MAXELEMENT:
			ARRAY[X] = False
			j = j + 1
			X = Number * j
	return ARRAY

def GCD(x, y):
	if x == 0:
		return y
	else:
		ans = GCD(y % x, x)
		return ans

def f3():
	T = int(input())
	while T != 0:
		T = T - 1
		(N, K) = map(int, input().split())
		S = str(input())
		min_length_palindrome = K
		f = 0
		e = min_length_palindrome - 1
		Answer = 0
		while f < e:
			fchar = f
			echar = e
			times = 0
			Alphabets = [0] * 26
			maxindex = -11
			maxval = -11
			Total = 0
			while echar < len(S):
				index1 = ord(S[fchar]) - ord('a')
				index2 = ord(S[echar]) - ord('a')
				Total = Total + 2
				Alphabets[index1] += 1
				if Alphabets[index1] > maxval:
					maxval = Alphabets[index1]
					maxindex = index1
				Alphabets[index2] += 1
				if Alphabets[index2] > maxval:
					maxval = Alphabets[index2]
					maxindex = index2
				fchar = fchar + min_length_palindrome
				echar = echar + min_length_palindrome
			Answer = Answer + (Total - maxval)
			f = f + 1
			e = e - 1
		if K % 2 != 0:
			i = f
			maxv = -11
			maxind = -11
			Total = 0
			Alphabets = [0] * 26
			while f < len(S):
				index = ord(S[f]) - ord('a')
				Total = Total + 1
				Alphabets[index] += 1
				if Alphabets[index] > maxv:
					maxv = Alphabets[index]
					maxind = index
				f = f + min_length_palindrome
			Answer = Answer + (Total - maxv)
		print(Answer)
f3()


from sys import stdin, stdout
import collections as clcns
for _ in range(int(stdin.readline())):
	(n, k) = map(int, stdin.readline().split())
	d = [[0 for _ in range(26)] for _ in range(k)]
	vis = [0] * (n + 1)
	s = stdin.readline()
	for i in range(k):
		for p in range(i, n, k):
			if not vis[p] and (not vis[n - p - 1]):
				vis[p] = vis[n - p - 1] = 1
				d[i][ord(s[p]) - 97] += 1
				if p != n - p - 1:
					d[i][ord(s[n - p - 1]) - 97] += 1
	s = 0
	for ind in range(k):
		total = freq = 0
		for i in range(26):
			if d[ind][i] != 0:
				total += d[ind][i]
				freq = max(freq, d[ind][i])
		s += total - freq
	stdout.write(str(s) + '\n')


from collections import defaultdict
from sys import stdin
input = stdin.readline

def solve():
	(n, k) = [int(a) for a in input().split()]
	s = [ord(a) - 97 for a in input()]
	total = 0
	done = [False] * k
	for i in range(k):
		if done[i]:
			break
		d = [0] * 26
		for ii in range(i, n, k):
			d[s[ii]] += 1
			if ii % k != (n - ii - 1) % k:
				d[s[n - ii - 1]] += 1
		done[i] = True
		done[k - i - 1] = True
		total += sum(sorted(d)[:-1])
	print(total)
t = int(input())
for i in range(t):
	solve()


import sys
from collections import defaultdict
input = lambda : sys.stdin.readline().strip()
t = int(input())
while t:
	t -= 1
	(n, k) = map(int, input().split())
	s = list(input())
	ans = 0
	i = 0
	j = k - 1
	while i <= j:
		f = defaultdict(int)
		cnt = 0
		for idx in range(i, n, k):
			f[s[idx]] += 1
			cnt += 1
		if j != i:
			for idx in range(j, n, k):
				f[s[idx]] += 1
				cnt += 1
		cur = cnt
		for it in f:
			cur = min(cur, cnt - f[it])
		ans += cur
		i += 1
		j -= 1
	print(ans)


from sys import stdin, stdout
input = stdin.readline
cases = int(input())
for t in range(cases):
	(n, k) = list(map(int, input().split()))
	s = input()
	output = 0
	for i in range(k):
		d = [0] * 26
		for j in range(i, n, k):
			d[ord(s[j]) - 97] += 1
			d[ord(s[n - j - 1]) - 97] += 1
		output += sum(d) - max(d)
	stdout.write(str(output // 2) + '\n')


from sys import stdin
from collections import deque
mod = 10 ** 9 + 7
import sys
import random
sys.setrecursionlimit(10 ** 6)
from queue import PriorityQueue
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
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	i = 0
	j = n - 1
	seti = set()
	ans = 0
	ha = 1
	ka = n - 1
	for x in range(n):
		hash = defaultdict(int)
		maxi = 0
		i = x
		j = ka
		ka -= 1
		if i not in seti:
			while i < n:
				if i not in seti:
					hash[s[i]] += 1
				if hash[s[i]] > maxi:
					maxi = hash[s[i]]
				seti.add(i)
				i += k
		if j not in seti:
			while j >= 0:
				if j not in seti:
					hash[s[j]] += 1
				if hash[s[j]] > maxi:
					maxi = hash[s[j]]
				seti.add(j)
				j -= k
		len = 0
		for v in hash:
			len += hash[v]
		ans += len - maxi
	print(ans)


from sys import stdin, stdout
T = int(stdin.readline())

def func(s, k, i):
	a = [0] * 29
	n = len(s)
	for u in i:
		j = u
		while j < n:
			a[ord(s[j]) - ord('a')] += 1
			j = j + k
	sm = 0
	mx = -1
	for j in a:
		if j > mx:
			mx = j
		sm += j
	return sm - mx
for _ in range(T):
	(n, k) = map(int, stdin.readline().split())
	s = input()
	ans = 0
	done = [0] * k
	for i in range(k):
		fr = i
		bk = (n - 1 - i) % k
		dd = []
		if done[i] == 0:
			dd.append(fr)
			done[i] = 1
			if fr != bk:
				done[bk] = 1
				dd.append(bk)
			ans += func(s, k, dd)
	stdout.write(str(ans))
	stdout.write('\n')


from sys import stdout, stdin
input = stdin.readline
print = stdout.write
for t in range(int(input())):
	(n, k) = map(int, input().split())
	a = input().strip()
	b = [[0 for i in range(26)] for j in range(k // 2 + k % 2)]
	for i in range(k // 2 + k % 2):
		for j in range(i, n, k):
			if k % 2 == 1 and i == k // 2 + k % 2 - 1:
				b[i][ord(a[j]) - 97] += 1
			else:
				b[i][ord(a[j]) - 97] += 1
				b[i][ord(a[j + k - 2 * i - 1]) - 97] += 1
	c = 0
	for i in range(k // 2 + k % 2):
		if k % 2 == 1 and i == k // 2 + k % 2 - 1:
			c += n // k - max(b[i])
		else:
			c += 2 * (n // k) - max(b[i])
	print(str(c) + '\n')


a = int(input())
for _ in range(a):
	(b, c) = map(int, input().split())
	d = input()
	ans = 0
	for i in range(c // 2):
		now = i
		data = {}
		while now < b:
			if d[now] not in data:
				data[d[now]] = 0
			data[d[now]] += 1
			now += c
		now = c - i - 1
		while now < b:
			if d[now] not in data:
				data[d[now]] = 0
			data[d[now]] += 1
			now += c
		ans += max(data.values())
	if c % 2 == 1:
		now = c // 2
		data = {}
		while now < b:
			if d[now] not in data:
				data[d[now]] = 0
			data[d[now]] += 1
			now += c
		ans += max(data.values())
	print(b - ans)


T = int(input())

def func(s, k, i):
	a = [0] * 26
	n = len(s)
	for u in i:
		j = u
		while j < n:
			a[ord(s[j]) - ord('a')] += 1
			j = j + k
	sm = 0
	mx = -1
	for j in a:
		if j > mx:
			mx = j
		sm += j
	return sm - mx
for _ in range(T):
	(n, k) = map(int, input().split())
	s = input()
	ans = 0
	done = [0] * k
	for i in range(k):
		fr = i
		bk = (n - 1 - i) % k
		dd = []
		if done[i] == 0:
			dd.append(fr)
			done[i] = 1
			if fr != bk:
				done[bk] = 1
				dd.append(bk)
			ans += func(s, k, dd)
	print(ans)


import sys
from math import ceil
u = -1

def input():
	global u
	u += 1
	return d[u]
d = sys.stdin.readlines()
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	answer = 0
	for i in range(ceil(k / 2)):
		y = dict()
		m = 0
		for j in range(n // k):
			x = s[k * j + i]
			if x in y.keys():
				y[x] += 1
			else:
				y[x] = 1
			if y[x] > m:
				m = y[x]
			if i != k - i - 1:
				x2 = s[k * (j + 1) - i - 1]
				if x2 in y.keys():
					y[x2] += 1
				else:
					y[x2] = 1
				if y[x2] > m:
					m = y[x2]
		if i == k - i - 1:
			answer += n // k - m
		else:
			answer += 2 * n // k - m
	print(answer)


from sys import stdin, stdout

def main():
	t = int(stdin.readline())
	for _ in range(t):
		(n, k) = list(map(int, stdin.readline().split()))
		s = stdin.readline().rstrip()
		cnt = 0
		lp = int(k / 2)
		if k % 2 == 1:
			lp += 1
		for i in range(lp):
			st = set()
			tmp = i
			while tmp < n:
				st.add(tmp)
				st.add(n - tmp - 1)
				tmp += k
			a = [0] * 30
			mx = -1
			for pos in st:
				alpha = s[pos]
				a[ord(alpha) - 97] += 1
				if a[ord(alpha) - 97] > mx:
					mx = a[ord(alpha) - 97]
			cnt += len(st) - mx
		stdout.write(str(cnt) + '\n')
main()


from collections import Counter
t = int(input())
while t != 0:
	(n, k) = map(int, input().split())
	A = str(input())
	res = 0
	r = 0
	j = n // k
	if k == 1:
		if n == 2:
			if A[0] == A[1]:
				print(0)
			else:
				print(1)
		else:
			o = Counter(A)
			res = n - max(o.values())
			print(res)
	elif k % 2 == 0:
		while r < k // 2:
			G = []
			i = 0
			while i < j:
				G.append(A[i * k + r])
				G.append(A[i * k - 1 - r])
				i += 1
			tmp = Counter(G)
			res += 2 * j - max(tmp.values())
			r += 1
		print(res)
	else:
		while r < k // 2:
			G = []
			i = 0
			while i < j:
				G.append(A[i * k + r])
				G.append(A[i * k - 1 - r])
				i += 1
			tmp = Counter(G)
			res += 2 * j - max(tmp.values())
			r += 1
		H = []
		m = (k - 1) // 2
		while m < n:
			H.append(A[m])
			m += k
		temp = Counter(H)
		res += j - max(temp.values())
		print(res)
	t -= 1


import sys
LI = lambda : list(map(int, sys.stdin.readline().strip('\n').split()))
MI = lambda : map(int, sys.stdin.readline().strip('\n').split())
SI = lambda : sys.stdin.readline().strip('\n')
II = lambda : int(sys.stdin.readline().strip('\n'))
for _ in range(II()):
	(n, k) = MI()
	s = SI()
	ans = 0
	for i in range(k):
		cnt = {}
		for j in range(i, n, k):
			cnt[s[j]] = cnt.get(s[j], 0) + 1
		for j in range(1, n // k + 1):
			cnt[s[j * k - i - 1]] = cnt.get(s[j * k - i - 1], 0) + 1
		ans += 2 * (n // k) - max(cnt.values())
	print(ans // 2)


def hamming_dist(a, b):
	n = len(a)
	d = [1 if a[i] != b[i] else 0 for i in range(n)]
	return sum(d)

def letter_frequencies(word, k):
	dict_list = []
	for i in range(k // 2):
		sub_wrd1 = word[i::k]
		sub_wrd2 = word[k - i - 1::k]
		sub_wrd = sub_wrd1 + sub_wrd2
		my_dict = {}
		for c in sub_wrd:
			if c not in my_dict:
				my_dict[c] = 0
			my_dict[c] += 1
		dict_list.append(my_dict)
	if k % 2:
		my_dict = {}
		sub_wrd = word[k // 2::k]
		for c in sub_wrd:
			if c not in my_dict:
				my_dict[c] = 0
			my_dict[c] += 1
		dict_list.append(my_dict)
	return dict_list

def dict_max(dic):
	best_key = None
	for k in dic:
		if not best_key:
			best_key = k
		if dic[k] > dic[best_key]:
			best_key = k
	return (best_key, dic[best_key])
t = int(input())
for _ in range(t):
	(n, k) = list(map(int, input().split()))
	word = list(input())
	dict_list = letter_frequencies(word, k)
	letters = [dict_max(dic) for dic in dict_list]
	unit = list(range(k))
	for i in range(k // 2):
		unit[i] = letters[i][0]
		unit[-i - 1] = letters[i][0]
	if k % 2:
		unit[k // 2] = letters[k // 2][0]
	best_word = ''.join(unit * (n // k))
	print(hamming_dist(best_word, word))


from collections import defaultdict
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split(' '))
	s = input()
	ans = [0] * n
	x = n // k
	count = 0
	for i in range(k // 2):
		arr = []
		d = {}
		d = defaultdict(lambda : 0, d)
		for j in range(x):
			d[s[i + j * k]] += 1
			d[s[(j + 1) * k - 1 - i]] += 1
		for i in d.keys():
			arr.append(d[i])
		arr = sorted(arr)
		count += sum(arr) - arr[-1]
	if k % 2 != 0:
		arr = []
		d = {}
		d = defaultdict(lambda : 0, d)
		for j in range(x):
			d[s[k // 2 + j * k]] += 1
		for i in d.keys():
			arr.append(d[i])
		arr = sorted(arr)
		count += sum(arr) - arr[-1]
	print(count)


def solve(s, k):
	m = [[0] * 26 for _ in range((k + 1) // 2)]
	for (i, e) in enumerate(s):
		r = i % k
		if r >= (k + 1) // 2:
			r = k - 1 - r
		m[r][ord(e) - ord('a')] += 1
	res = 0
	for row in m:
		res += sum(row) - max(row)
	return res
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	print(solve(s, k))


from collections import Counter
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	ans = 0
	for i in range(k // 2 + (1 if k % 2 == 1 else 0)):
		a = [s[j] for j in range(i, n, k)]
		if not (k % 2 == 1 and i == k // 2):
			a.extend((s[j] for j in range(k - i - 1, n, k)))
		b = Counter(a)
		ans += sum(b.values()) - max(b.values())
	print(ans)


import sys
input = lambda : sys.stdin.readline().rstrip()

def calc(n, k, A):
	X = [[0] * 26 for _ in range((k + 1) // 2)]
	for (i, a) in enumerate(A):
		j = i % k
		j = min(j, k - 1 - j)
		X[j][a] += 1
	return sum([sum(x) - max(x) for x in X])
T = int(input())
for _ in range(T):
	(N, K) = list(map(int, input().split()))
	S = [ord(a) - 97 for a in input()]
	print(calc(N, K, S))


t = int(input())
for i in range(0, t, 1):
	(n, k) = [int(x) for x in input().split(' ')]
	s = input()
	total = 0
	for i in range(0, k // 2, 1):
		count = [0] * 123
		maxi = 0
		for j in range(0, n, k):
			s1 = j + i
			s2 = j + (k - 1 - i)
			count[ord(s[s1])] += 1
			count[ord(s[s2])] += 1
		maxi = max(count)
		req = 2 * (n // k)
		total = total + (req - maxi)
	if k % 2 != 0:
		count = [0] * 123
		for i in range(k // 2, n, k):
			count[ord(s[i])] += 1
		maxi = max(count)
		total = total + (n // k - maxi)
	print(total)


t = int(input())
for _ in range(t):
	(n, k) = [int(x) for x in input().split()]
	s = input()
	l = []
	for i in range(k):
		l.append([])
	for i in range(n):
		l[i % k].append(s[i])
	ans = 0
	for i in range(k // 2):
		x = l[i] + l[k - 1 - i]
		dict = {}
		for j in range(len(x)):
			if dict.get(x[j]) == None:
				dict[x[j]] = 0
			dict[x[j]] += 1
		ans += 2 * (n // k) - max(list(dict.values()))
	if k % 2 != 0:
		temp = k
		k = k // 2
		x = l[k]
		dict = {}
		for j in range(len(x)):
			if dict.get(x[j]) is None:
				dict[x[j]] = 0
			dict[x[j]] += 1
		ans += n // temp - max(list(dict.values()))
	print(ans)


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	s = input()
	ans = 0
	for j in range(k // 2):
		d = {}
		ob = 0
		el = j
		while el < n:
			ob += 1
			d[s[el]] = d.get(s[el], 0) + 1
			if ob % 2 == 1:
				el = el + k - 1 - 2 * j
			else:
				el += 1 + 2 * j
		ma = 0
		for (key, val) in d.items():
			ma = max(ma, val)
		ans += ob - ma
	if k % 2 == 1:
		el = k // 2
		d = {}
		ob = 0
		while el < n:
			ob += 1
			d[s[el]] = d.get(s[el], 0) + 1
			el += k
		ma = 0
		for (key, val) in d.items():
			ma = max(ma, val)
		ans += ob - ma
	print(ans)


from collections import defaultdict

def solve(s, n, k):
	div = n // k
	i = 0
	j = k - 1
	ans = 0
	while i <= j:
		map_d = defaultdict(int)
		for d in range(div):
			l = d * k + i
			r = d * k + j
			if l != r:
				map_d[s[l]] += 1
				map_d[s[r]] += 1
			else:
				map_d[s[l]] += 1
		max_so_far = -1
		for key in map_d.keys():
			if map_d[key] > max_so_far:
				max_so_far = map_d[key]
		if i == j:
			ans += div - max_so_far
		else:
			ans += 2 * div - max_so_far
		i += 1
		j -= 1
	return ans

def read():
	t = int(input())
	for i in range(t):
		(n, k) = list(map(int, input().strip().split()))
		s = input()
		ans = solve(s, n, k)
		print(ans)
read()


import math
T = int(input())
for test in range(T):
	(n, k) = map(int, input().split())
	s = input()
	l = [[0 for i in range(26)] for j in range((k + 1) // 2)]
	for i in range(n):
		l[min(i % k, k - i % k - 1)][ord(s[i]) - ord('a')] += 1
	ans = 0
	for i in range(k // 2):
		ans += 2 * n // k - max(l[i])
	if k % 2:
		ans += n // k - max(l[k // 2])
	print(ans)


import sys

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

def maps():
	return [int(i) for i in input().split()]
maxx = 1 << 60

def naive(s, k):
	st = 'wudiduw'
	A = 0
	cnt = 0
	for i in range(0, n, k):
		f = s[i:i + k]
		for j in range(k):
			if f[j] != st[j]:
				A += 1
		cnt += 1
for _ in range(*maps()):
	(n, k) = maps()
	s = input()
	d = {i: [0] * 26 for i in range(k)}
	if n // k % 2:
		x = n // k // 2
		st = x * k
		end = st + k - 1
		tot = n // k
	else:
		st = maxx
		end = maxx
		tot = n // k
	ans = 0
	i = 0
	while i < n:
		d[i % k][ord(s[i]) - 97] += 1
		i += 1
	for i in range(k // 2):
		tt = [i + j for (i, j) in zip(d[i], d[k - i - 1])]
		ans += 2 * tot - max(tt)
	if k % 2:
		ans += tot - max(d[k // 2])
	print(ans)


import os, sys, bisect, copy
from collections import defaultdict, Counter, deque
from functools import lru_cache
if os.path.exists('in.txt'):
	sys.stdin = open('in.txt', 'r')
if os.path.exists('out.txt'):
	sys.stdout = open('out.txt', 'w')

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

def mapi(arg=0):
	return map(int if arg == 0 else str, input().split())
cnt = [[0] * 26 for i in range(200005)]
par = [0] * 200005
size = [0] * 200005

def parent(v):
	if v == par[v]:
		return v
	par[v] = parent(par[v])
	return par[v]

def merge(u, v):
	a = parent(u)
	b = parent(v)
	if a != b:
		if size[a] < size[b]:
			(a, b) = (b, a)
		par[b] = a
		size[a] += size[b]
		for i in range(26):
			cnt[a][i] += cnt[b][i]
for _ in range(int(input())):
	(n, k) = mapi()
	a = list(input().strip())
	cnt = [[0] * 26 for i in range(n + 1)]
	par = [0] * (n + 1)
	size = [0] * (n + 1)
	for i in range(n):
		cnt[i][ord(a[i]) - ord('a')] += 1
		par[i] = i
		size[i] = 1
	for i in range(n):
		if i < n - i - 1:
			merge(i, n - i - 1)
		if i + k < n:
			merge(i, i + k)
	res = 0
	for i in range(n):
		if par[i] == i:
			mx = -1
			for j in range(26):
				mx = max(mx, cnt[i][j])
			res += size[i] - mx
	print(res)


t = int(input())
for z in range(0, t):
	(n, k) = map(int, input().split())
	word = []
	ip = list(input())
	temp = []
	num = int(n / k)
	count = 0
	for i in range(0, n):
		temp.append(ip[i])
		count = count + 1
		if count % k == 0:
			word.append(temp)
			temp = []
	count = 0
	ppp = k
	if k % 2 != 0:
		ppp = k + 1
	ppp = int(ppp / 2)
	for i in range(0, ppp):
		dict = {}
		indexfirst = i
		indexlast = k - 1 - i
		if indexfirst != indexlast:
			for j in range(0, num):
				aa = word[j][indexfirst]
				bb = word[j][indexlast]
				if aa not in dict:
					dict[aa] = 1
				else:
					dict[aa] = dict[aa] + 1
				if bb not in dict:
					dict[bb] = 1
				else:
					dict[bb] = dict[bb] + 1
			max_key = max(dict, key=dict.get)
			val = dict[max_key]
			num_replace = 2 * num - val
			count = count + num_replace
		else:
			for j in range(0, num):
				aa = word[j][indexfirst]
				if aa not in dict:
					dict[aa] = 1
				else:
					dict[aa] = dict[aa] + 1
			max_key = max(dict, key=dict.get)
			val = dict[max_key]
			num_replace = num - val
			count = count + num_replace
	print(count)


import sys
input = sys.stdin.readline
from collections import defaultdict as d
t = int(input())
for _ in range(t):
	(n, k) = [int(x) for x in input().split()]
	s = input()[:-1]
	indexGroups = []
	visited = [False for _a in range(n)]
	for i in range(n):
		if visited[i] == False:
			group = []
			for j in range(i, n, k):
				visited[j] = True
				group.append(j)
			for j in range(n - 1 - i, -1, -k):
				if visited[j] == False:
					visited[j] = True
					group.append(j)
			indexGroups.append(group)
	ans = 0
	for group in indexGroups:
		cnts = d(lambda : 0)
		for idx in group:
			cnts[s[idx]] += 1
		ans += len(group) - max(cnts.values())
	print(ans)


from collections import defaultdict

def fun(c):
	return ord(c) - ord('a')
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	mp = False
	if n // k % 2 == 1:
		mp = True
	ans = [0] * k
	fans = 0
	size = [0 for i in range(n // k + n // k % 2)]
	dlist = []
	for x in range(k):
		ind = x
		if x < k // 2 + k % 2:
			ind = x
			dlist.append([0] * 27)
		else:
			ind = k - 1 - x
		for st in range(0, n, k):
			ix = fun(s[st + x])
			dlist[ind][ix] += 1
	fans = 0
	for dd in dlist:
		s = 0
		mv = 0
		for i in dd:
			s += i
			mv = max(mv, i)
		fans += s - mv
	print(fans)


for _ in range(int(input())):
	(n, k) = tuple(map(int, input().split()))
	s = input()
	dicts = [{} for i in range((k - 1) // 2 + 1)]
	for (num, mydict) in enumerate(dicts):
		for i in range(num, len(s), k):
			if s[i] not in mydict:
				mydict[s[i]] = 0
			mydict[s[i]] += 1
		if num == len(dicts) - 1 and k % 2 == 1:
			break
		for i in range(k - num - 1, len(s), k):
			if s[i] not in mydict:
				mydict[s[i]] = 0
			mydict[s[i]] += 1
	ans = 0
	for mydict in dicts:
		values = list(mydict.values())
		ans += sum(values) - max(values)
	print(ans)


from sys import stdin
input = lambda : stdin.readline().strip()
from math import ceil, sqrt, gcd
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	count = 0
	for i in range(ceil(k / 2)):
		d = {}
		a = 1
		for j in range(i, n, k):
			if s[j] in d:
				d[s[j]] += 1
			else:
				d[s[j]] = 1
			if a * k - i - 1 != j:
				if s[a * k - i - 1] in d:
					d[s[a * k - i - 1]] += 1
				else:
					d[s[a * k - i - 1]] = 1
			a += 1
		su = 0
		m = -10
		for j in d:
			m = max(m, d[j])
			su += d[j]
		count += su - m
	print(count)


from collections import Counter
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	r = 0
	for i in range(k // 2):
		d = {}
		for j in range(i, n, k):
			d[s[j]] = d.get(s[j], 0) + 1
			d[s[k - 1 - j]] = d.get(s[k - 1 - j], 0) + 1
		c = max(d.values())
		r -= c
	r += 2 * (n // k) * (k // 2)
	if k % 2:
		d = {}
		for j in range(k // 2, n, k):
			d[s[j]] = d.get(s[j], 0) + 1
		c = max(d.values())
		r += n // k - c
	print(r)


import sys
input = sys.stdin.readline
MOD = 1000000007
MOD2 = 998244353
ii = lambda : int(input().strip('\n'))
si = lambda : input()
dgl = lambda : list(map(int, input().strip('\n')))
f = lambda : map(int, input().strip('\n').split())
il = lambda : list(map(int, input().strip('\n').split()))
ls = lambda : list(input().strip('\n'))
let = 'abcdefghijklmnopqrstuvwxyz'
from collections import Counter
for _ in range(ii()):
	(n, k) = f()
	s = si()
	l = [[] for i in range((k + 1) // 2)]
	for i in range(n):
		if i % k < (k + 1) // 2:
			l[i % k].append(s[i])
		else:
			l[k - i % k - 1].append(s[i])
	ans = 0
	for i in l:
		c = Counter(i)
		ans += len(i) - c.most_common(1)[0][1]
	print(ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	ans = 0
	c = [{} for _ in range(k)]
	for i in range(n):
		x = i % k
		y = k - 1 - x
		x = min(x, y)
		c[x][s[i]] = c[x].get(s[i], 0) + 1
	for i in range(k):
		if c[i]:
			x = sum(c[i].values())
			y = max(c[i].values())
			ans += x - y
	print(ans)


import operator
t = int(input())
estado = 'complete'
igual = 'idle'
decir = []
for i in range(0, t):
	b = input()
	n = int(b.split(' ')[0])
	k = int(b.split(' ')[1])
	word = list(input())
	resultado = 0
	if k % 2 == 0:
		for i in range(0, int(k / 2)):
			l = {}
			dif = []
			estado = 'SI'
			for j in range(0, n, k):
				for g in word[i + j]:
					if g not in dif:
						dif.append(g)
						l[g] = 0
				for g in word[n - 1 - i - j]:
					if g not in dif:
						dif.append(g)
						l[g] = 0
				if word[i + j] in dif:
					l[word[i + j]] += 1
				if i + j == n - 1 - i:
					estado = 'NO'
				if estado != 'NO':
					if i + j != n - 1 - i - k:
						if word[n - 1 - i - j] in dif:
							l[word[n - 1 - i - j]] += 1
			sortL = sorted(l.items(), key=lambda t: t[1])
			for i in range(0, len(sortL) - 1):
				resultado += int(sortL[i][1])
	else:
		for i in range(0, int((k + 1) / 2)):
			l = {}
			dif = []
			estado = 'SI'
			for j in range(n - k, -k, -k):
				for g in word[i + j]:
					if g not in dif:
						dif.append(g)
						l[g] = 0
				for g in word[n - 1 - i - j]:
					if g not in dif:
						dif.append(g)
						l[g] = 0
				if word[i + j] in dif:
					l[word[i + j]] += 1
				if i + j == n - 1 - i:
					estado = 'NO'
				if estado != 'NO':
					if i + j != n - 1 - i - k:
						if word[n - 1 - i - j] in dif:
							l[word[n - 1 - i - j]] += 1
			sortL = sorted(l.items(), key=lambda t: t[1])
			for i in range(0, len(sortL) - 1):
				resultado += int(sortL[i][1])
	decir.append(resultado)
for i in range(0, t):
	print(decir[i])


t = int(input())
for _ in range(t):
	count = 0
	(n, k) = map(int, input().split())
	l = input()
	s = list(l)
	for i in range(k):
		d = {}
		j = 0
		while i + j * k < n:
			if s[i + j * k] in d:
				d[s[i + j * k]] += 1
			else:
				d[s[i + j * k]] = 1
			z = k - i - 1
			if s[z + j * k] in d:
				d[s[z + j * k]] += 1
			else:
				d[s[z + j * k]] = 1
			j += 1
		p = max(d, key=d.get)
		j = 0
		while i + j * k < n:
			if s[i + j * k] != p:
				s[i + j * k] = p
				count += 1
			z = k - i - 1
			if s[z + j * k] != p:
				s[z + j * k] = p
				count += 1
			j += 1
	m = ''.join(s)
	print(count)


from sys import stdin, stdout
an = []
for h in range(int(stdin.readline())):
	(n, k) = map(int, stdin.readline().strip().split())
	s = stdin.readline()
	dp = []
	c = 1
	if k == 1:
		dp = [1 for i in range(n)]
	else:
		dp = [0 for i in range(n)]
	for i in range(k - 1):
		if dp[i] == 0:
			for j in range(i, n, k):
				dp[j] = dp[n - 1 - j] = c
			c += 1
	dicti = [[0, [0 for i in range(26)]] for i in range(c)]
	for i in range(n):
		dicti[dp[i] - 1][0] += 1
		dicti[dp[i] - 1][1][ord(s[i]) - ord('a')] += 1
	ans = 0
	for i in range(c):
		ans += dicti[i][0] - max(dicti[i][1])
	an.append(str(ans))
stdout.write('\n'.join(an))


import sys
input = sys.stdin.readline
from collections import Counter
P = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
t = int(input())
for tests in range(t):
	(n, k) = map(int, input().split())
	S = input().strip()
	A = [Counter() for i in range(k)]
	for i in range(n):
		A[min(i % k, (n - 1 - i) % k)][S[i]] += 1
	ANS = 0
	for a in A:
		if len(a) > 1:
			ANS += sum(a.values()) - max(a.values())
	print(ANS)


from collections import defaultdict
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	ans = 0
	st = []
	for i in range(n // k):
		st.append(s[i * k:i * k + k])
	for i in range(k // 2):
		al = defaultdict(int)
		for g in st:
			al[g[i]] += 1
			al[g[-1 - i]] += 1
		ans += 2 * (n // k) - max(al.values())
	if k % 2 == 1:
		al = defaultdict(int)
		for g in st:
			al[g[k // 2]] += 1
		ans += n // k - max(al.values())
	print(ans)


def mostFrequent(arr, n):
	arr.sort()
	max_count = 1
	res = arr[0]
	curr_count = 1
	for i in range(1, n):
		if arr[i] == arr[i - 1]:
			curr_count += 1
		else:
			if curr_count > max_count:
				max_count = curr_count
				res = arr[i - 1]
			curr_count = 1
	if curr_count > max_count:
		max_count = curr_count
	return max_count
testCases = int(input())
for t in range(testCases):
	parameters = list(map(int, input().split()))
	word = input()
	wordSplit = []
	for i1 in range(parameters[1] // 2):
		temp = []
		for i2 in range(parameters[0] // parameters[1]):
			temp.append(word[i2 * parameters[1] + i1])
			temp.append(word[i2 * parameters[1] + (parameters[1] - 1 - i1)])
		wordSplit.append(temp)
	if parameters[1] % 2 == 1:
		middleLetters = []
		for i1 in range(parameters[0] // parameters[1]):
			middleLetters.append(word[i1 * parameters[1] + parameters[1] // 2])
		wordSplit.append(middleLetters)
	sum_unchanged = 0
	for i1 in wordSplit:
		sum_unchanged += mostFrequent(i1, len(i1))
	print(len(word) - sum_unchanged)


from sys import stdin
input = stdin.readline

def A():
	t = int(input())
	for _ in range(t):
		(a, b, c, d) = map(int, input().split())
		(x, y, x1, y1, x2, y2) = map(int, input().split())
		netr = b - a
		netu = d - c
		if x1 == x2 and (a > 0 or b > 0):
			print('No')
			continue
		if y1 == y2 and (c > 0 or d > 0):
			print('No')
			continue
		if netr + x < x1 or netr + x > x2:
			print('No')
			continue
		if netu + y < y1 or netu + y > y2:
			print('No')
			continue
		print('Yes')

def B():
	t = int(input())
	for _ in range(t):
		m = int(input())
		a = list(map(int, input().split()))
		c = [0] * m
		p = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
		used = [False] * 11
		for i in range(m):
			j = 0
			while a[i] % p[j] != 0:
				j += 1
			used[j] = True
			c[i] = j
		used[0] = int(used[0])
		for i in range(1, 11):
			used[i] = int(used[i]) + used[i - 1]
		for i in range(m):
			c[i] = used[c[i]]
		print(max(used))
		print(*c)

def C():
	t = int(input())
	for _ in range(t):
		(n, k) = map(int, input().split())
		s = [ord(x) - 97 for x in input().rstrip()]
		l = [[0 for i in range(26)] for j in range(k)]
		for i in range(n):
			l[i % k][s[i]] += 1
		for i in range(k // 2):
			for j in range(26):
				l[i][j] += l[k - i - 1][j]
		for i in range((k + 1) // 2):
			n -= max(l[i])
		print(n)
C()


from math import sqrt, ceil

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

def li():
	return list(mi())

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

def si():
	return input()
rank = []

def find(x, parent):
	if x == parent[x]:
		return x
	parent[x] = find(parent[x], parent)
	return parent[x]

def union(x, y, parent):
	x1 = find(x, parent)
	y1 = find(y, parent)
	if x1 == y1:
		return
	if rank[x1] >= rank[y1]:
		parent[y] = x1
		rank[x1] += 1
	else:
		parent[x] = y1
		rank[y1] += 1

def main():
	for _ in range(ii()):
		(n, k) = mi()
		s = si()
		parent = [i for i in range(n)]
		global rank
		for i in range(n):
			rank.append(0)
		for i in range(k):
			rank[i] += 1
			for j in range(i + k, n, k):
				union(i, j, parent)
		for i in range(n // 2):
			union(i, n - i - 1, parent)
		d = {}
		for i in range(ceil(k / 2)):
			d[i] = []
		for i in range(n):
			d[parent[i]].append(i)
		ans = 0
		for i in d.keys():
			x = [0] * 26
			for j in d[i]:
				x[ord(s[j]) - ord('a')] += 1
			ans += sum(x) - max(x)
		print(ans)
main()


for _ in range(int(input())):
	(n, k) = [int(s) for s in input().split()]
	s = input()
	arr = [[0 for i in range(k)] for j in range(n // k)]
	for i in range(n // k):
		for j in range(k):
			arr[i][j] = s[i * k + j]
	sum = 0
	for i in range((k + 1) // 2):
		freq = [0] * 26
		maxi = 0
		for j in range(n // k):
			x = ord(arr[j][i]) - ord('a')
			freq[x] += 1
			if i != k - 1 - i:
				y = ord(arr[j][k - i - 1]) - ord('a')
				freq[y] += 1
				maxi = max(maxi, freq[y])
			maxi = max(maxi, freq[x])
		if i == k - i - 1:
			sum += n // k - maxi
		else:
			sum = sum + (2 * (n // k) - maxi)
	print(sum)


from statistics import mode
t = int(input())
import string
string.ascii_lowercase
import random
for i in range(t):
	ctr = 0
	(n, k) = map(int, input().split())
	s = input()
	k1 = k // 2
	for l in range(k1):
		ls = []
		for j in range(n // k):
			ls.append(s[k * j + l])
			ls.append(s[-(k * j + l + 1)])
		try:
			md = mode(ls)
		except:
			md = ls[0]
		if l <= k1:
			for j in range(n // k):
				if s[k * j + l] != md:
					ctr += 1
				if s[-(k * j + l + 1)] != md:
					ctr += 1
	if k % 2 != 0:
		ls = []
		for j in range(n // k):
			ls.append(s[k * j + k // 2])
		try:
			md = mode(ls)
		except:
			md = ls[0]
		for j in range(n // k):
			if ls[j] != md:
				ctr += 1
	print(ctr)


def CountFrequency(my_list):
	freq = {}
	for item in my_list:
		if item in freq:
			freq[item] += 1
		else:
			freq[item] = 1
	return freq
t = int(input())
for test in range(t):
	(n, k) = [int(i) for i in input().split()]
	s = input()
	moves = 0
	for index in range(k // 2):
		ilst = [i * k + index for i in range(n // k)] + [(i + 1) * k - 1 - index for i in range(n // k)]
		letters = [s[i] for i in ilst]
		ilfreq = CountFrequency(letters)
		moves += len(letters) - ilfreq[max(ilfreq, key=ilfreq.get)]
	if k % 2 == 1:
		ilst = [i * k + k // 2 for i in range(n // k)]
		letters = [s[i] for i in ilst]
		ilfreq = CountFrequency(letters)
		moves += len(letters) - ilfreq[max(ilfreq, key=ilfreq.get)]
	print(moves)


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

def solve():
	(N, K) = getInts()
	word = input()
	changes = 0
	for i in range(math.ceil(K / 2)):
		counts = col.defaultdict(int)
		total_count = 0
		for j in range(N // K):
			letter = word[j * K + i]
			counts[letter] += 1
			total_count += 1
			if (j + 1) * K - i - 1 != j * K + i:
				letter = word[(j + 1) * K - 1 - i]
				counts[letter] += 1
				total_count += 1
		best = 0
		for letter in counts:
			best = max(best, counts[letter])
		changes += total_count - best
	return changes
T = getInt()
for t in range(T):
	print(solve())


def primeFacorize(x):
	p = [7, 11, 13, 17, 19, 23, 29, 31, 37]
	ans = []
	t = 0
	for i in p:
		if x % i == 0:
			t = i
			break
	return [t, x // t]
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	h = 0
	if k % 2 == 0:
		h = k // 2 - 1
	else:
		h = k // 2
	ans = 0
	for i in range(h + 1):
		D = {}
		maxV = 0
		for j in range(0, n, k):
			if s[j + i] not in D:
				D[s[j + i]] = 1
			else:
				D[s[j + i]] += 1
			if D[s[j + i]] > maxV:
				maxV = D[s[j + i]]
			if j + i != j + k - 1 - i:
				if s[j + k - 1 - i] not in D:
					D[s[j + k - 1 - i]] = 1
				else:
					D[s[j + k - 1 - i]] += 1
				if D[s[j + k - 1 - i]] > maxV:
					maxV = D[s[j + k - 1 - i]]
		if i == h and k % 2 != 0:
			ans += n // k - maxV
		else:
			ans += n // k * 2 - maxV
	print(ans)


import sys
import math
from collections import defaultdict
import bisect
import random

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

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

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 Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 1000000000 + 7
for _ in range(int(input())):
	(n, k) = ilele()
	S = input()
	A = {}
	for i in range(k):
		mark = {}
		for j in range(i, len(S), k):
			mark[S[j]] = mark.get(S[j], 0) + 1
		A[i] = mark.copy()
	Ans = {}
	for i in range(math.ceil(k / 2)):
		a = i
		b = k - i - 1
		if a != b:
			maxi = 0
			g = ''
			for (l, m) in A[a].items():
				if m > maxi:
					g = l
					maxi = m
			for (l, m) in A[b].items():
				if m + A[a].get(l, 0) > maxi:
					g = l
					maxi = m + A[a].get(l, 0)
		else:
			maxi = 0
			g = ''
			for (l, m) in A[a].items():
				if m > maxi:
					g = l
					maxi = m
		Ans[a] = g
		Ans[b] = g
	count = 0
	for i in range(len(S)):
		if S[i] != Ans.get(i % k, -1):
			count += 1
	print(count)


from collections import Counter
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	ans = 0
	for i in range(k):
		ans += len(s[i::k]) + len(s[n - i - 1::-k]) - max(Counter(s[i::k] + s[n - i - 1::-k]).values())
	print(ans // 2)


for i in range(int(input())):
	(n, k) = map(int, input().split())
	t = input()
	ans = 0
	for i in range(k // 2):
		b = {}
		h = i
		while h < n:
			if t[h] not in b:
				b[t[h]] = 1
			else:
				b[t[h]] += 1
			if t[k - h - 1] not in b:
				b[t[k - h - 1]] = 1
			else:
				b[t[k - h - 1]] += 1
			h += k
		b = list(b.values())
		ans += sum(b) - max(b)
	if k % 2:
		u = {}
		i = k // 2
		while i < n:
			if t[i] not in u:
				u[t[i]] = 1
			else:
				u[t[i]] += 1
			i += k
		h = list(u.values())
		ans += sum(h) - max(h)
	print(ans)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	c = 0
	for i in range(0, k // 2):
		d = {}
		g = 0
		for j in range(i, n, k):
			g += 1
			if s[j] not in d:
				d[s[j]] = 0
			d[s[j]] += 1
		for j in range(k - i - 1, n, k):
			g += 1
			if s[j] not in d:
				d[s[j]] = 0
			d[s[j]] += 1
		m = max(d.values())
		m = g - m
		c += m
	if k % 2 != 0:
		re = k // 2
		g = 0
		d = {}
		for i in range(re, n, k):
			g += 1
			if s[i] not in d:
				d[s[i]] = 0
			d[s[i]] += 1
		m = max(d.values())
		m = g - m
		c += m
	print(c)


from sys import stdin, stdout

def read():
	x0 = stdin.readline().rstrip()
	return x0

def __main__():
	t = int(read())
	for t0 in range(t):
		(n, k) = map(int, read().split())
		s = read()
		res = 0
		ind1 = 0
		ind2 = k
		s_list = []
		for j in range(n // k):
			s_list.append(s[ind1:ind2])
			ind1 += k
			ind2 += k
		it = k // 2
		left_i = 0
		right_i = -1
		for i in range(it):
			d1 = {}
			d2 = {}
			for w in s_list:
				left = w[left_i]
				right = w[right_i]
				if left not in d1:
					d1.update({left: 1})
				else:
					d1.update({left: d1.get(left) + 1})
				if right not in d2:
					d2.update({right: 1})
				else:
					d2.update({right: d2.get(right) + 1})
			dr = dict(d1)
			for g in d2:
				if g in dr:
					dr.update({g: dr.get(g) + d2.get(g)})
				else:
					dr.update({g: d2.get(g)})
			max0 = 0
			for r in dr:
				m = dr.get(r)
				if m > max0:
					max0 = m
			summ = n // k * 2
			res += summ - max0
			left_i += 1
			right_i -= 1
		if k % 2 == 1:
			dk = {}
			ind = k // 2
			for w in s_list:
				w0 = w[ind]
				if w0 not in dk:
					dk.update({w0: 1})
				else:
					dk.update({w0: dk.get(w0) + 1})
			m0 = 0
			for h in dk:
				h0 = dk.get(h)
				if h0 > m0:
					m0 = h0
			res += n // k - m0
		nx = str(res) + '\n'
		stdout.write(nx)
__main__()


x = int(input())
abc = 'abcdefghijklmnopqrstuvwxyz'
dic = {}
for i in abc:
	dic[i] = 0
for i in range(x):
	(temp1, temp2) = map(int, input().split())
	temp3 = int(temp1 / temp2)
	st = input()
	ans = 0
	for k in range(temp2 // 2):
		y = dic.copy()
		for j in range(temp3):
			y[st[j * temp2 + k]] += 1
			y[st[(j + 1) * temp2 - k - 1]] += 1
		ans += 2 * temp3 - max(y.values())
	if temp2 % 2 != 0:
		y = dic.copy()
		for j in range(temp3):
			y[st[j * temp2 + temp2 // 2]] += 1
		ans += temp3 - max(y.values())
	print(ans)


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

def Find(x, par):
	if par[x] < 0:
		return x
	else:
		par[x] = Find(par[x], par)
		return par[x]

def Unite(x, y, par, rank):
	x = Find(x, par)
	y = Find(y, par)
	if x != y:
		if rank[x] < rank[y]:
			par[y] += par[x]
			par[x] = y
		else:
			par[x] += par[y]
			par[y] = x
			if rank[x] == rank[y]:
				rank[x] += 1

def Same(x, y, par):
	return Find(x, par) == Find(y, par)

def Size(x, par):
	return -par[Find(x, par)]
for _ in range(t):
	(n, k) = map(int, input().split())
	s = str(input())
	par = [-1] * n
	rank = [0] * n
	for i in range(n):
		Unite(i, i % k, par, rank)
		Unite(i, n - 1 - i, par, rank)
	d = {}
	j = 0
	for i in range(n):
		if par[i] < 0:
			d[i] = j
			j += 1
	g = len(d.keys())
	L = [{} for _ in range(g)]
	for i in range(n):
		p = Find(i, par)
		id = d[p]
		if s[i] not in L[id]:
			L[id][s[i]] = 1
		else:
			L[id][s[i]] += 1
	ans = 0
	for i in range(g):
		d_ = L[i]
		vs = list(d_.values())
		ans += sum(vs) - max(vs)
	print(ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	(a, p, m, o) = ([[]], 0, n // k, ord('a'))
	for i in range(n):
		a[p].append(s[i])
		if (i + 1) % k == 0:
			a.append([])
			p += 1
	(l, r, res) = (0, k - 1, 0)
	while l <= r:
		cnt = [0] * 36
		for i in range(m):
			cnt[ord(a[i][l]) - o] += 1
			if l != r:
				cnt[ord(a[i][r]) - o] += 1
		t = max(cnt)
		res += 2 * m - t if l != r else m - t
		l += 1
		r -= 1
	print(res)


for test in range(int(input())):
	(n, k) = list(map(int, input().split()))
	s = input()
	ans = 0
	for i in range(k // 2):
		cnt = 0
		d = dict()
		maxn = 0
		for j in range(i, n, k):
			cnt += 2
			if s[j] not in d:
				d[s[j]] = 1
			else:
				d[s[j]] += 1
			if d[s[j]] > maxn:
				maxn = d[s[j]]
			j2 = (j // k + 1) * k - 1 - i
			if s[j2] not in d:
				d[s[j2]] = 1
			else:
				d[s[j2]] += 1
			if d[s[j2]] > maxn:
				maxn = d[s[j2]]
		ans += cnt - maxn
	if k % 2 == 1:
		cnt = 0
		d = dict()
		maxn = 0
		for j in range(k // 2, n, k):
			cnt += 1
			if s[j] not in d:
				d[s[j]] = 1
			else:
				d[s[j]] += 1
			if d[s[j]] > maxn:
				maxn = d[s[j]]
		ans += cnt - maxn
	print(ans)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	l = list(input())
	ls = []
	for i in range(26):
		ls.append(0)
	count = 0
	if k % 2 == 1:
		i = k // 2
		while i < n:
			ls[ord(l[i]) - 97] += 1
			i += k
		maximum = max(ls)
		count = count + n // k - maximum
		z = 2
	else:
		z = 1
	i = k // 2 - 1
	while i >= 0:
		j = i
		for temp in range(26):
			ls[temp] = 0
		while j < n:
			ls[ord(l[j]) - 97] += 1
			j += k
		j = i + z
		while j < n:
			ls[ord(l[j]) - 97] += 1
			j += k
		maximum = max(ls)
		count = count + 2 * n // k - maximum
		i = i - 1
		z = z + 2
	print(count)


def solve(n, k, word):
	counters = [{} for _ in range((k + 1) // 2)]
	for p in range(n):
		i = p % k
		if i > k - i - 1:
			i = k - i - 1
		counters[i][word[p]] = counters[i].get(word[p], 0) + 1
	ans = n - sum((max(c.values()) for c in counters))
	return ans

def test():
	assert solve(6, 2, 'abaaba') == 2
	assert solve(6, 3, 'abaaba') == 0
	assert solve(36, 9, 'hippopotomonstrosesquippedaliophobia') == 23
	assert solve(21, 7, 'wudixiaoxingxingheclp') == 16
test()
t = int(input())
for i in range(t):
	(n, k) = [int(x) for x in input().split()]
	s = input()
	print(solve(n, k, s))


t = int(input())
delay = []
for i in range(t):
	(n, k) = map(int, input().split())
	word = input()
	chunk = []
	for i in range(k // 2):
		a = list(word[i:n:k] + word[k - i - 1:n:k])
		chunk.append(a)
	if k % 2 == 1:
		a = list(word[k // 2:n:k])
		chunk.append(a)
	inv = 0
	num_chunks = len(chunk) - 1
	for (ii, c) in enumerate(chunk):
		num = 0
		let = '!'
		d = {}
		for i in c:
			if i not in d:
				d[i] = 1
			else:
				d[i] += 1
		for key in d:
			if d[key] > num:
				num = d[key]
				let = key
		total = len(c)
		inv += total - num
	delay.append(inv)
for d in delay:
	print(d)


def most_frequent(List):
	dict = {}
	(count, itm) = (0, '')
	for item in reversed(List):
		dict[item] = dict.get(item, 0) + 1
		if dict[item] >= count:
			(count, itm) = (dict[item], item)
	return count
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = str(input())
	nos = n // k
	if k % 2 != 0:
		nolist = k // 2 + 1
	else:
		nolist = k // 2
	ans = 0
	for i in range(nolist):
		a = []
		for j in range(nos):
			if k % 2 == 0:
				a.append(s[j * k + i])
				a.append(s[j * k + k - i - 1])
			elif i == nolist - 1:
				a.append(s[j * k + i])
			else:
				a.append(s[j * k + i])
				a.append(s[j * k - i + k - 1])
		sub = int(most_frequent(a))
		ans += len(a) - sub
	print(ans)


for t in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	alp = 'abcdefghijklmnopqrstuvwxyz'
	arr = [[0 for i in range(26)] for j in range(k)]
	for i in range(n):
		arr[i % k][alp.index(s[i])] += 1
	ans = 0
	k1 = k
	if k % 2 == 1:
		k1 += 1
	for i1 in range(1, k1 // 2 + 1):
		i = i1 - 1
		j = k - i - 1
		if i != j:
			(mx, total) = (0, 0)
			for kk in range(26):
				total += arr[i][kk] + arr[j][kk]
				mx = max(mx, arr[i][kk] + arr[j][kk])
			ans += total - mx
		else:
			(mx, total) = (0, 0)
			for kk in range(26):
				total += arr[i][kk]
				mx = max(mx, arr[i][kk])
			ans += total - mx
	print(ans)


from sys import stdin

def solve(n, k, s):
	ans = 0
	for i in range(k // 2):
		d = {}
		for j in range(n // k):
			if s[i + k * j] in d:
				d[s[i + k * j]] += 1
			else:
				d[s[i + k * j]] = 1
			if s[k * j - 1 - i] in d:
				d[s[k * j - 1 - i]] += 1
			else:
				d[s[k * j - 1 - i]] = 1
		m = 0
		for u in d.keys():
			m = max(m, d[u])
		ans += n // k * 2 - m
	if k % 2 == 1:
		d = {}
		p = k // 2
		for j in range(n // k):
			if s[p + k * j] in d:
				d[s[p + k * j]] += 1
			else:
				d[s[p + k * j]] = 1
		m = 0
		for u in d.keys():
			m = max(m, d[u])
		ans += n // k - m
	print(ans)

def main():
	input = stdin.readline
	for _ in range(int(input())):
		(n, k) = map(int, input().split())
		s = input().strip()
		solve(n, k, s)
main()


import math
t = int(input())
for test in range(t):
	(n, k) = [int(x) for x in input().split()]
	s = list(input())
	x = []
	for i in range(1, k + 1):
		x.append(set())
		for j in range(i, n + 1, k):
			x[i - 1].add(j)
	ans = 0
	for i in range(int(math.ceil(len(x) / 2))):
		a = list(x[i])
		b = list(x[len(x) - i - 1])
		maxc = 0
		ch = [0] * 26
		for j in range(len(a)):
			ch[ord(s[a[j] - 1]) - 97] += 1
		f = 0
		if a != b:
			f = 1
			for j in range(len(b)):
				ch[ord(s[b[j] - 1]) - 97] += 1
		dd = max(ch)
		if f == 1:
			oo = len(a) + len(b)
			ans += oo - dd
		else:
			ans += len(a) - dd
	print(ans)


from collections import Counter
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	changes = 0
	for j in range(int((k + 1) / 2)):
		c = Counter()
		for t in range(int(n / k)):
			c[s[k * t + j]] += 1
			if 2 * j != k - 1:
				c[s[k * (t + 1) - j - 1]] += 1
		changes += sum(c.values()) - max(c.values())
	print(changes)


import sys
input = sys.stdin.readline

def main():
	testNum = int(input())
	for testcase in range(testNum):
		(n, k) = map(int, input().split())
		s = list(input())[:n]
		res = 0
		used = [True] * n
		empty = [True] * k
		for i in range(n):
			if used[i]:
				tank = [i]
				c = [0] * 27
				while tank:
					p = tank.pop()
					if not used[p]:
						continue
					c[ord(s[p]) - 97] += 1
					used[p] = False
					kk = p % k
					if empty[kk]:
						empty[kk] = False
						for j in range(n // k):
							if used[j * k + kk]:
								tank.append(j * k + kk)
					if used[n - 1 - p]:
						tank.append(n - 1 - p)
				res += sum(c) - max(c)
		print(res)
main()


from math import *
from bisect import bisect_left, bisect_right
from sys import stdin, stdout, setrecursionlimit
from collections import Counter
input = lambda : stdin.readline().strip()
print = stdout.write

def most_frequent(List):
	occurence_count = Counter(List)
	return dict(occurence_count)[occurence_count.most_common(1)[0][0]]
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	d = {}
	for i in range(ceil(k / 2)):
		d[i] = []
	for i in range(len(s)):
		d[min(i % k, (k - i - 1) % k)].append(s[i])
	s = 0
	for v in d.values():
		s += len(v) - most_frequent(v)
	print(str(s) + '\n')


from collections import Counter

def count(s1, s2):
	l = len(s1) + len(s2)
	c = Counter(s1 + s2).most_common(1)[0]
	return l - c[1]

def solve(s, n, k):
	res = 0
	tmp = []
	for i in range(k):
		tmp.append(s[i:n:k])
	l = len(tmp)
	for i in range(l // 2):
		res += count(tmp[i], tmp[l - i - 1])
	if l % 2 == 1:
		c = Counter(tmp[l // 2]).most_common(1)[0]
		res += len(tmp[l // 2]) - c[1]
	print(res)
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	solve(s, n, k)


import math
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	no_of_sets = n // k
	iterations = k // 2
	ans = 0
	for i in range(iterations):
		d = {}
		for fr in range(i, n, k):
			if s[fr] in d:
				d[s[fr]] += 1
			else:
				d[s[fr]] = 1
		for bk in range(k - 1 - i, n, k):
			if s[bk] in d:
				d[s[bk]] += 1
			else:
				d[s[bk]] = 1
		ans += 2 * no_of_sets - max(d.values())
	d = {}
	if k % 2 == 1:
		i = k // 2
		for fr in range(i, n, k):
			if s[fr] in d:
				d[s[fr]] += 1
			else:
				d[s[fr]] = 1
		ans += no_of_sets - max(d.values())
	print(ans)


from sys import stdin
t = int(stdin.readline())
output = []
for tc in range(t):
	(n, k) = map(int, stdin.readline().split())
	w = stdin.readline()
	e = [[] for i in range(n)]
	for i in range(n // 2):
		e[i].append(n - i - 1)
		e[n - i - 1].append(i)
	for i in range(n - k):
		e[i].append(i + k)
		e[i + k].append(i)
	sol = 0
	used = [False for i in range(n)]
	for i in range(n):
		if used[i]:
			continue
		sz = 0
		fr = {}
		q = [i]
		while len(q):
			u = q.pop()
			if used[u]:
				continue
			used[u] = True
			sz += 1
			fr[w[u]] = fr.get(w[u], 0) + 1
			for v in e[u]:
				if not used[v]:
					q.append(v)
		sol += sz - max(fr.values())
	output.append(str(sol))
print('\n'.join(output))


import math
n = int(input())
for a in range(n):
	nums = list(map(int, input().split()))
	word = input()
	n = nums[0]
	k = nums[1]
	changes = 0
	for b in range(math.floor(k / 2 + 0.5)):
		current = ''
		for c in range(0, n, k):
			if k % 2 == 1 and b == k // 2:
				current += word[c + b]
			else:
				current += word[c + b] + word[c + k - 1 - b]
		highest = 0
		counted = set()
		for c in current:
			if c not in counted:
				counted.add(c)
				count = current.count(c)
				if count > highest:
					highest = count
		changes += len(current) - highest
	print(changes)


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	(n, k) = map(int, sys.stdin.readline().split())
	s = sys.stdin.readline()[:-1]
	x = n // k
	ans = 0
	(left, right) = (0, k - 1)
	while left < right:
		arr = [0] * 26
		for j in range(x):
			arr[ord(s[left + k * j]) - 97] += 1
			arr[ord(s[right + k * j]) - 97] += 1
		m = max(arr)
		ans += 2 * x - m
		left += 1
		right -= 1
	if left == right:
		arr = [0] * 26
		for j in range(x):
			arr[ord(s[left + k * j]) - 97] += 1
		m = max(arr)
		ans += x - m
	print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(N, K) = map(int, input().split())
	S = list(input())[:-1]
	table = [[0] * 26 for _ in range(K)]
	a = ord('a')
	for i in range(N):
		table[i % K][ord(S[i]) - a] += 1
	smtable = [0] * K
	for k in range(26):
		for i in range(K):
			smtable[i] += table[i][k]
	res = 0
	for i in range(K // 2):
		j = K - i - 1
		tt = 10 ** 6
		for k in range(26):
			x = smtable[i] - table[i][k]
			y = smtable[j] - table[j][k]
			tt = min(tt, x + y)
		res += tt
	if K % 2:
		tt = 10 ** 6
		for k in range(26):
			x = smtable[K // 2] - table[K // 2][k]
			tt = min(tt, x)
		res += tt
	print(res)


from collections import Counter
for lp in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	(q, t) = (0, 1)
	for j in range((k + 1) // 2):
		l = []
		for i in range(j, n, k):
			l.append(s[i])
		if k % 2 == 0 or j != (k + 1) // 2 - 1:
			for i in range(k - j - 1, n, k):
				l.append(s[i])
		else:
			t = 0
		q += n // k * (1 + t) - Counter(l).most_common(1)[0][1]
	print(q)


t = int(input())
run = 0
while run < t:
	(n, k) = map(int, input().split())
	s = input()
	rep = int(n / k)
	units = [s[j * k:(j + 1) * k] for j in range(rep)]
	changes = 0
	if k % 2 == 0:
		for i in range(k // 2):
			arr1 = [units[j][i] for j in range(rep)]
			arr2 = [units[j][k - i - 1] for j in range(rep)]
			arr = arr1 + arr2
			letters = []
			counts = []
			for letter in arr:
				if letter not in letters:
					letters.append(letter)
					counts.append(arr.count(letter))
			changes = changes + len(arr) - max(counts)
		print(changes)
	else:
		for i in range(k // 2 + 1):
			arr1 = [units[j][i] for j in range(rep)]
			arr2 = [units[j][k - i - 1] for j in range(rep)]
			arr = arr1 + arr2
			letters = []
			counts = []
			for letter in arr:
				if letter not in letters:
					letters.append(letter)
					counts.append(arr.count(letter))
			if i == k // 2:
				changes = changes + (len(arr) - max(counts)) / 2
			else:
				changes = changes + len(arr) - max(counts)
		print(int(changes))
	run += 1


import sys
input = lambda : sys.stdin.readline().strip()
ipnut = input
for i in range(int(input())):
	(n, k) = map(int, ipnut().split())
	s = list(ipnut())
	l = n // k
	lol = [[] for _ in range(k)]
	lo = [[] for _ in range((k + 1) // 2)]
	for i in range(n):
		lol[i % k].append(s[i])
	for i in range(k // 2):
		lo[i] = lol[i] + lol[-i - 1]
	if k % 2:
		lo[k // 2] = lol[k // 2]
	ans = 0
	for i in lo:
		s = {}
		for j in i:
			s[j] = s.get(j, 0) + 1
		ans += len(i) - max(s.values())
	print(ans)


import sys
from heapq import *
from collections import defaultdict as dd
input = lambda : sys.stdin.readline().strip()
inp = lambda : list(map(int, input().split()))
for _ in range(int(input())):
	(n, k) = inp()
	a = str(input())
	t = n // k
	ans = 0
	for i in range(k // 2):
		d = dd(lambda : 0)
		for j in range(i, n, k):
			d[a[j]] += 1
		for j in range(k - 1 - i, n, k):
			d[a[j]] += 1
		ans += 2 * t - max(d.values())
	if k % 2 == 1:
		for kk in range(1):
			i = k // 2
			d = dd(lambda : 0)
			for j in range(i, n, k):
				d[a[j]] += 1
			ans += t - max(d.values())
	print(ans)


def main():
	(n, k) = map(int, input().split())
	string = str(input())
	a = list(string)
	ans = 0
	for i in range(k):
		dp = [0 for _ in range(26)]
		for j in range(i, n, k):
			dp[ord(a[j]) - ord('a')] += 1
			dp[ord(a[n - j - 1]) - ord('a')] += 1
		mx = -1
		index = -1
		for j in range(26):
			if mx < dp[j]:
				mx = dp[j]
				index = j
		ch = chr(index + ord('a'))
		for j in range(i, n, k):
			if a[j] != ch:
				a[j] = ch
				ans += 1
			if a[n - j - 1] != ch:
				a[n - j - 1] = ch
				ans += 1
	print(ans)
	return

def test():
	t = int(input())
	while t:
		main()
		t -= 1
test()


import sys
inp = sys.stdin.readline

def input():
	return inp().strip()

def iin():
	return int(input())

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

def main():
	from collections import defaultdict as dd
	T = iin()
	while T:
		T -= 1
		(n, k) = lin()
		a = list(input())
		a1 = [dd(int) for i in range(k)]
		for i in range(n):
			a1[i % k][a[i]] += 1
		ans = 0
		for i in range(k // 2):
			c1 = dd(int)
			for j in a1[i]:
				c1[j] += a1[i][j]
			for j in a1[k - i - 1]:
				c1[j] += a1[k - i - 1][j]
			c1 = list(c1.values())
			ans += sum(c1) - max(c1)
		if k % 2 == 1:
			c1 = list(a1[k // 2].values())
			ans += sum(c1) - max(c1)
		print(ans)
main()


t = int(input())
from sys import stdin, stdout
from collections import Counter, defaultdict
for i in range(t):
	(n, k) = map(int, input().split())
	s = stdin.readline().rstrip('\n')
	l = list(s)
	c = Counter(l)
	ans = 0
	d = defaultdict(int)
	l1 = []
	l2 = []
	for j in range(k // 2 + 1):
		d1 = d.copy()
		mx = 0
		for p in range(n // k):
			d1[s[p * k + j]] += 1
			d1[s[p * k + k - 1 - j]] += 1
			mx = max(mx, d1[s[p * k + j]], d1[s[p * k + k - 1 - j]])
		l1.append(d1)
		l2.append(mx)
	ls = [''] * k
	for j in range(k // 2 + 1):
		for p in l1[j]:
			if l1[j][p] == l2[j]:
				ls[j] = p
				ls[k - 1 - j] = p
				break
	for j in range(n):
		if l[j] != ls[j % k]:
			ans += 1
	print(ans)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input().rstrip()
	ans = 0
	for i in range(k // 2):
		alph = [0] * 26
		for j in range(n // k):
			alph[ord(s[j * k + i]) - ord('a')] += 1
			alph[ord(s[j * k + k - 1 - i]) - ord('a')] += 1
		ans += sum(alph) - max(alph)
	if k % 2:
		alph = [0] * 26
		for j in range(n // k):
			alph[ord(s[j * k + k // 2]) - ord('a')] += 1
		ans += sum(alph) - max(alph)
	print(ans)


import sys
input = sys.stdin.readline
(t,) = map(int, input().split())
for _ in range(t):
	(a, b) = map(int, input().split())
	s = input().strip()
	r = 0
	if 1:
		for i in range(b // 2):
			d = dict()
			for j in range(a // b):
				c = s[i + j * b]
				if c not in d:
					d[c] = 0
				d[c] += 1
				c = s[b - i - 1 + j * b]
				if c not in d:
					d[c] = 0
				d[c] += 1
			mx = 0
			for c in 'abcdefghijklmnopqrstuvwxyz':
				if c in d:
					mx = max(mx, d[c])
			r += a // b * 2 - mx
		if b % 2:
			d = dict()
			for j in range(a // b):
				c = s[b // 2 + j * b]
				if c not in d:
					d[c] = 0
				d[c] += 1
			mx = 0
			for c in 'abcdefghijklmnopqrstuvwxyz':
				if c in d:
					mx = max(mx, d[c])
			r += a // b - mx
	print(r)


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
import threading
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))

def input():
	return sys.stdin.readline().rstrip('\r\n')
t = int(input())
while t != 0:
	t -= 1
	(n, k) = map(int, input().split())
	s = input()
	ans = 0
	x = k // 2
	if k % 2 != 0:
		x += 1
	lst = [0] * 26
	for i in range(x):
		lst.clear()
		lst = [0] * 26
		j = i
		while j < n:
			lst[ord(s[j]) - 97] += 1
			j += k
		j = k - i - 1
		if j != i:
			while j < n:
				lst[ord(s[j]) - 97] += 1
				j += k
		ans += max(lst)
	ans = n - ans
	print(ans)


T = int(input())
for test in range(T):
	(N, K) = list(map(int, input().split()))
	word = input()
	if K == 1:
		bins = [0 for i in range(128)]
		for c in word:
			bins[ord(c)] += 1
		print(N - max(bins[97:]))
		continue
	MDcount = N // K
	changes = 0
	for pos in range(K // 2):
		bins = [0 for i in range(128)]
		indices = [i * K + pos for i in range(MDcount)] + [(i + 1) * K - 1 - pos for i in range(MDcount)]
		for index in indices:
			bins[ord(word[index])] += 1
		changes += 2 * MDcount - max(bins[97:])
	if K % 2 == 1:
		bins = [0 for i in range(128)]
		indices = [(K - 1) // 2 + i * K for i in range(MDcount)]
		for index in indices:
			bins[ord(word[index])] += 1
		changes += MDcount - max(bins[97:])
	print(changes)


t = int(input())
res = []
for i in range(t):
	summ_all = 0
	(n, k) = map(int, input().split())
	s = input()
	ans = [[0] * 26 for j in range((k + 1) // 2)]
	for j in range(n):
		ans[min(j % k, k - j % k - 1)][ord(s[j]) - 97] += 1
	for j in ans:
		summ = 0
		maximum = 0
		for k in j:
			summ += k
			maximum = max(maximum, k)
		summ_all += summ - maximum
	res.append(summ_all)
for i in res:
	print(i)


import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
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 value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
for _ in range(Int()):
	(n, k) = value()
	s = input()
	votes = defaultdict(lambda : defaultdict(int))
	ans = defaultdict(lambda : ['', -1])
	for i in range(0, n, k):
		l = i
		r = i + k - 1
		index = 1
		while l <= r:
			if r != l:
				votes[index][s[l]] += 1
				votes[index][s[r]] += 1
			if r == l:
				votes[index][s[l]] += 1
			if votes[index][s[l]] > ans[index][1]:
				ans[index][0] = s[l]
				ans[index][1] = votes[index][s[l]]
			if votes[index][s[r]] > ans[index][1]:
				ans[index][0] = s[r]
				ans[index][1] = votes[index][s[r]]
			l += 1
			r -= 1
			index += 1
	count = 0
	for i in range(0, n, k):
		l = i
		r = i + k - 1
		index = 1
		while l <= r:
			if l != r:
				if s[l] != ans[index][0]:
					count += 1
				if s[r] != ans[index][0]:
					count += 1
			elif s[l] != ans[index][0]:
				count += 1
			l += 1
			r -= 1
			index += 1
	print(count)


t = int(input())
for _ in range(t):
	(n, k) = [int(x) for x in input().split()]
	s = input()
	changes = 0
	counts = dict()
	for i in range((k + 1) // 2):
		counts.clear()
		for j in range(i, n, k):
			counts[s[j]] = counts.get(s[j], 0) + 1
		if i != k // 2:
			for j in range(k - i - 1, n, k):
				counts[s[j]] = counts.get(s[j], 0) + 1
		changes += sum(counts.values()) - max(counts.values())
	print(changes)


from collections import defaultdict

def main():
	for _ in range(int(input())):
		(n, k) = list(map(int, input().split()))
		s = input()
		ans = 0
		for i in range(k // 2):
			mx = 0
			mp = defaultdict(lambda : 0)
			for j in range(0, len(s), k):
				mp[s[i + j]] += 1
				mp[s[j + (k - i - 1)]] += 1
				mx = max(mx, mp[s[i + j]], mp[s[j + (k - i - 1)]])
			ans += 2 * (n // k) - mx
		if k % 2 == 1:
			mx = 0
			mp = defaultdict(lambda : 0)
			for i in range(k // 2, n, k):
				mp[s[i]] += 1
				mx = max(mx, mp[s[i]])
			ans += n // k - mx
		print(ans)
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, k) = LI()
		s = S()
		r = 0
		for i in range((k + 1) // 2):
			t = set()
			for j in range(n // k):
				t.add(j * k + i)
				t.add(j * k + k - i - 1)
			c = collections.Counter((s[j] for j in t))
			r += len(t) - max(c.values())
		rr.append(r)
	return JA(rr, '\n')
print(main())


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	count = 0
	ls = []
	i = 0
	for j in range(n // k):
		ls.append(s[i:i + k])
		i += k
	for i in range((k + 1) // 2):
		dct = {}
		mx = 1
		res = ls[0][i]
		for st in ls:
			if st[i] not in dct:
				dct[st[i]] = 1
			else:
				dct[st[i]] += 1
				if dct[st[i]] > mx:
					mx = dct[st[i]]
					res = st[i]
			if i != k - i - 1:
				if st[k - i - 1] not in dct:
					dct[st[k - i - 1]] = 1
				else:
					dct[st[k - i - 1]] += 1
					if dct[st[k - i - 1]] > mx:
						mx = dct[st[k - i - 1]]
						res = st[k - i - 1]
		for key in dct.keys():
			if key != res:
				count += dct[key]
	print(count)


from sys import stdin
from collections import defaultdict
for _ in range(int(stdin.readline())):
	(n, k) = map(int, stdin.readline().split())
	s = list(stdin.readline())
	s.pop()
	r = n // k
	ans = 0
	for j in range(k // 2):
		freq = {}
		t = 0
		for i in range(n // k):
			x = i * k + j
			y = k - 1 - x
			freq[s[x]] = freq.get(s[x], 0) + 1
			t = max(t, freq[s[x]])
			freq[s[y]] = freq.get(s[y], 0) + 1
			t = max(t, freq[s[y]])
		ans += 2 * (n // k) - t
	if k % 2 != 0:
		j = k // 2
		freq = {}
		t = 0
		for i in range(n // k):
			x = i * k + j
			freq[s[x]] = freq.get(s[x], 0) + 1
			t = max(t, freq[s[x]])
		ans += n // k - t
	print(ans)


input = __import__('sys').stdin.readline
print = __import__('sys').stdout.write
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	word = input()
	r = n // k
	ans = 0
	tmp = [[0 for _ in range(26)] for i in range(k)]
	for i in range(k):
		j = i
		while j < n:
			tmp[i][ord(word[j]) - 97] += 1
			j += k
	until = k // 2
	for i in range(until):
		for j in range(26):
			tmp[i][j] += tmp[k - i - 1][j]
		ans += r * 2 - max(tmp[i])
	if k % 2 == 1:
		ans += r - max(tmp[until])
	print(f'{ans}\n')


import sys, math, cmath
input = sys.stdin.readline

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

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

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

def invr():
	return map(int, input().split())
t = 1
t = inp()
for tt in range(t):
	(n, k) = invr()
	s = insr()
	q = 0
	w = n // k
	if k % 2 == 1:
		qw = [0] * 26
		for j in range(k // 2, n, k):
			qw[ord(s[j]) - 97] = qw[ord(s[j]) - 97] + 1
		qw.sort()
		q = q + w - max(qw)
	for i in range(k // 2):
		qw = [0] * 26
		for j in range(i, n, k):
			qw[ord(s[j]) - 97] = qw[ord(s[j]) - 97] + 1
		for j in range(k - i - 1, n, k):
			qw[ord(s[j]) - 97] = qw[ord(s[j]) - 97] + 1
		q = q + 2 * w - max(qw)
	print(q)


import math
import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	groups = n // k
	string = input()
	ans = 0
	for i in range(math.ceil(k / 2)):
		lis = [0] * 26
		freq = 0
		for j in range(groups):
			if k * j + i != k * (j + 1) - 1 - i:
				freq = 2 * groups
				lis[ord(string[k * j + i]) - 97] += 1
				lis[ord(string[k * (j + 1) - 1 - i]) - 97] += 1
			else:
				freq = groups
				lis[ord(string[k * j + i]) - 97] += 1
		ans = ans + freq - max(lis)
	print(ans)


for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	a = list(input())
	ans = 0
	if k % 2 == 0:
		for i in range(k // 2):
			ar = dict()
			for r in range(i, n, k):
				if a[r] not in ar:
					ar[a[r]] = 1
				else:
					ar[a[r]] += 1
			for r in range(n - 1 - i, -1, -k):
				if a[r] not in ar:
					ar[a[r]] = 1
				else:
					ar[a[r]] += 1
			m = 0
			for elem in ar:
				if ar[elem] > m:
					m = ar[elem]
			ans += 2 * (n // k) - m
		print(ans)
	else:
		for i in range(k // 2):
			ar = dict()
			for r in range(i, n, k):
				if a[r] not in ar:
					ar[a[r]] = 1
				else:
					ar[a[r]] += 1
			for r in range(n - 1 - i, -1, -k):
				if a[r] not in ar:
					ar[a[r]] = 1
				else:
					ar[a[r]] += 1
			m = 0
			for elem in ar:
				if ar[elem] > m:
					m = ar[elem]
			ans += 2 * (n // k) - m
		ar = dict()
		for i in range(k // 2, n, k):
			if a[i] not in ar:
				ar[a[i]] = 1
			else:
				ar[a[i]] += 1
		m = 0
		for elem in ar:
			if ar[elem] > m:
				m = ar[elem]
		ans += n // k - m
		print(ans)


from sys import stdin
inp = stdin.readline
t = int(inp().strip())
for c in range(t):
	(n, k) = [int(x) for x in inp().strip().split()]
	array = list(inp().strip())
	d1 = {}
	for i in range(n):
		if i % k < k / 2:
			dIndex = k - 1 - i % k
		else:
			dIndex = i % k
		if not d1.get(dIndex, 0):
			d1[dIndex] = {array[i]: 1}
		elif not d1[dIndex].get(array[i], 0):
			d1[dIndex][array[i]] = 1
		else:
			d1[dIndex][array[i]] += 1
	number = n // k
	numberOfMoves = 0
	for i in d1.values():
		maximum = 0
		for j in i.values():
			if j > maximum:
				maximum = j
		numberOfMoves += 2 * number - maximum
	if k % 2:
		numberOfMoves -= number
	print(numberOfMoves)


t = int(input())
for i in range(t):
	inp1 = list(map(int, input().split()))
	n = inp1[0]
	k = inp1[1]
	s = input()
	anss = 0
	for p in range(int(k / 2)):
		aplha = ''
		for j in range(int(n / k)):
			aplha = aplha + s[j * k + p] + s[(j + 1) * k - 1 - p]
		dicti = [0] * 28
		for j in aplha:
			dicti[ord(j) - 96] += 1
		maxfre = max(dicti)
		anss += 2 * int(n / k) - maxfre
	if k & 1:
		p = int(k / 2 + 1)
		aplha = ''
		for j in range(int(n / k)):
			aplha = aplha + s[j * k + p - 1]
		dicti = [0] * 28
		for j in aplha:
			dicti[ord(j) - 96] += 1
		maxfre = max(dicti)
		anss += int(n / k) - maxfre
	print(int(anss))


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

def solve(N, L, S):
	repeat = N // L
	posToCounts = defaultdict(Counter)
	for k in range(repeat):
		for l in range(L):
			i = k * L + l
			posToCounts[min(l, L - 1 - l)][S[i]] += 1
	cost = 0
	for (pos, counts) in posToCounts.items():
		mostFreq = max(counts.values())
		if L % 2 == 1 and pos == L // 2:
			cost += repeat - mostFreq
		else:
			cost += 2 * repeat - mostFreq
	return cost
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
T = int(input())
for t in range(T):
	(N, K) = [int(x) for x in input().split()]
	S = input().decode().rstrip()
	ans = solve(N, K, S)
	print(ans)


import sys
import math
import heapq
import collections

def inputnum():
	return int(input())

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

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

def inputstring():
	return [x for x in input()]

def inputmatrixchar(rows):
	arr2d = [[j for j in input().strip()] for i in range(rows)]
	return arr2d

def inputmatrixint(rows):
	arr2d = []
	for _ in range(rows):
		arr2d.append([int(i) for i in input().split()])
	return arr2d
t = int(input())
for q in range(t):
	(n, k) = inputnums()
	s = inputstring()
	ans = 0
	for i in range(k // 2):
		cnt = [0] * 27
		j = 0
		while j < n:
			cnt[ord(s[j + i]) % 32] += 1
			cnt[ord(s[j + k - i - 1]) % 32] += 1
			j += k
		cnt.sort()
		ans += 2 * (n // k) - cnt[26]
	if k % 2 == 1:
		cnt = [0] * 27
		j = k // 2
		while j < n:
			cnt[ord(s[j]) % 32] += 1
			j += k
		cnt.sort()
		ans += n // k - cnt[26]
	print(ans)


for xyz in range(0, int(input())):
	(n, k) = map(int, input().split())
	s = input()
	c = 0
	for i in range(0, k):
		ac = [0] * 26
		r = i
		while r < n:
			ac[ord(s[r]) - 97] += 1
			r += k
		r = k - i - 1
		while r < n:
			ac[ord(s[r]) - 97] += 1
			r += k
		c += sum(ac) - max(ac)
	print(c // 2)


import heapq
t = int(input())
for case in range(t):
	(n, k) = [int(x) for x in input().split(' ')]
	s = input()
	h = [i for i in range(n)]
	v = set()
	heapq.heapify(h)
	ans = 0
	while h:
		x = heapq.heappop(h)
		if x in v:
			pass
		else:
			a_dict = {}
			l = set()
			while x < n:
				v.add(x)
				l.add(x)
				if s[x] in a_dict.keys():
					a_dict[s[x]] += 1
				else:
					a_dict[s[x]] = 1
				x += k
			for y in l:
				v.add(n - 1 - y)
				if n - 1 - y not in l:
					if s[n - 1 - y] in a_dict.keys():
						a_dict[s[n - 1 - y]] += 1
					else:
						a_dict[s[n - 1 - y]] = 1
			ans += sum(a_dict.values()) - max(a_dict.values())
	print(ans)


def makeLetterList(s, i):
	alphabet[ord(s[i]) - 97] += 1
	letterReached[i] = True
	if i + k < n and letterReached[i + k] == False:
		makeLetterList(s, i + k)
	if i - k >= 0 and letterReached[i - k] == False:
		makeLetterList(s, i - k)
	if n - 1 - i >= 0 and letterReached[n - i - 1] == False:
		makeLetterList(s, n - i - 1)
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	letterReached = [False] * n
	count = 0
	for i in range(n):
		if not letterReached[i]:
			alphabet = [0] * 26
			for j in range(i, n, k):
				if not letterReached[j]:
					alphabet[ord(s[j]) - 97] += 1
					letterReached[j] = True
				if not letterReached[n - 1 - j]:
					alphabet[ord(s[n - 1 - j]) - 97] += 1
					letterReached[n - 1 - j] = True
			count += sum(alphabet) - max(alphabet)
	print(count)


from sys import stdin
input = stdin.readline
q = int(input())
for rwerwe in range(q):
	(n, k) = map(int, input().split())
	s = input()
	if k == n:
		print(0)
	else:
		wyn = 0
		for poz in range((k + 1) // 2):
			zaj = [0] * 126
			i = poz
			while i < n:
				zaj[ord(s[i])] += 1
				i += k
			i = k - 1 - poz
			while i < n:
				zaj[ord(s[i])] += 1
				i += k
			cyk = max(zaj)
			if poz * 2 + 1 != k:
				wyn += 2 * n // k - cyk
			else:
				wyn += n // k - cyk // 2
	print(wyn)


from collections import defaultdict
R = lambda : map(int, input().split())
t = int(input())
for _ in range(t):
	(n, k) = R()
	s = input()
	words = [s[k * i:k * (i + 1)] for i in range(n // k)]
	ans = 0
	for i in range(k // 2):
		d = defaultdict(int)
		for word in words:
			d[word[i]] += 1
			d[word[k - 1 - i]] += 1
		ans += 2 * len(words) - max(d.values())
	if k % 2 == 1:
		d = defaultdict(int)
		for word in words:
			d[word[k // 2]] += 1
		ans += len(words) - max(d.values())
	print(ans)


import collections as cl
import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input().strip()
	ans = 0
	for i in range((k + 1) // 2):
		cnt = cl.Counter(s[i::k] + s[k - 1 - i::k])
		v = cnt.most_common(1)[0][0]
		ans += sum((j != v for j in s[i::k]))
		if k % 2 == 0 or i != (k + 1) // 2 - 1:
			ans += sum((j != v for j in s[k - 1 - i::k]))
	print(ans)


def solve(ans):
	(n, k) = [int(x) for x in input().split()]
	s = input()
	occur = [[0] * 26 for i in range((k + 1) // 2)]
	for i in range(0, n, k):
		for j in range((k + 1) // 2):
			occur[j][ord(s[i + j]) - ord('a')] += 1
		for j in range(k // 2):
			occur[j][ord(s[i + k - j - 1]) - ord('a')] += 1
	change = 0
	for j in range(k // 2):
		max_at_pos = max(occur[j])
		change += 2 * (n // k) - max_at_pos
	if k % 2 != 0:
		change += n // k - max(occur[-1])
	ans.append(change)
	return
n = int(input())
ans = []
for i in range(n):
	solve(ans)
for ele in ans:
	print(ele)


t = int(input())
for _ in range(t):
	(n, k) = [int(p) for p in input().split()]
	s = input()
	subStr = []
	for i in range(0, n, k):
		subStr.append(s[i:i + k])
	cnt = 0
	p = k
	if k % 2 != 0:
		p += 1
	for i in range(p // 2):
		letters = [0] * 26
		for sub in subStr:
			letters[ord(sub[i]) - 97] += 1
			letters[ord(sub[k - i - 1]) - 97] += 1
		final = max(letters)
		if k - i - 1 != i:
			cnt += 2 * (n // k) - final
		else:
			cnt += n // k - final // 2
	print(cnt)


from math import *
for zz in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	d = {}
	for i in range(k // 2):
		d[i] = {}
		for j in range(n // k):
			if s[j * k + i] not in d[i]:
				d[i][s[j * k + i]] = 0
			d[i][s[j * k + i]] += 1
	for i in range(k // 2, k):
		if k - i - 1 not in d:
			d[k - i - 1] = {}
		for j in range(n // k):
			if s[j * k + i] not in d[k - i - 1]:
				d[k - i - 1][s[j * k + i]] = 0
			d[k - i - 1][s[j * k + i]] += 1
	ans = 0
	for k in d:
		v = list(d[k].values())
		m = max(v)
		s = sum(v)
		ans += s - m
	print(ans)


from sys import stdin
from collections import Counter
input = stdin.readline
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = list(input().rstrip())
	l = [[] for i in range((k + 1) // 2)]
	for i in range(n):
		m = i % k
		if m >= (k + 1) // 2:
			m = k - 1 - m
		l[m].append(s[i])
	cnt = 0
	for i in range((k + 1) // 2):
		c = Counter(l[i])
		ll = len(l[i])
		cnt += ll - c.most_common()[0][1]
	print(cnt)


DEBUG = 0

def least_cost(alpha_cnt, k_clusters, i, mid):
	for k_wrd in k_clusters:
		ind_s = ord(k_wrd[i]) - 97
		alpha_cnt[ind_s] += 1
		if DEBUG:
			print(k_wrd, k_wrd[i], k_wrd[-i - 1])
			print(ind_s)
		if not mid:
			ind_e = ord(k_wrd[-i - 1]) - 97
			alpha_cnt[ind_e] += 1
			if DEBUG:
				print(ind_e)
	if DEBUG:
		print('ind: ', i, alpha_cnt)
	max_ind = alpha_cnt.index(max(alpha_cnt))
	cost = 0
	for i in range(26):
		if i == max_ind:
			continue
		cost += alpha_cnt[i]
	return cost

def main():
	t = int(input())
	test_no = 0
	alpha_cnt = [0 for x in range(26)]
	while test_no < t:
		(n, k) = map(int, input().split())
		s = input()
		k_clusters = []
		for i in range(n // k):
			k_clusters.append(s[i * k:(i + 1) * k])
		if DEBUG:
			print(k_clusters)
		cost = 0
		for i in range(k // 2):
			lc = least_cost(alpha_cnt.copy(), k_clusters, i, 0)
			cost += lc
		if k % 2 == 1:
			cost += least_cost(alpha_cnt.copy(), k_clusters, k // 2, 1)
		print(cost)
		test_no += 1
main()


t = int(input())
for q in range(t):
	(n, k) = map(int, input().split(' '))
	s = input()
	cnt = [[0 for i in range(26)] for j in range((k + 1) // 2)]
	for i in range(n):
		cnt[min(i % k, k - i % k - 1)][ord(s[i]) - ord('a')] += 1
	ans = 0
	for i in range(k // 2):
		ans += 2 * n // k - max(cnt[i])
	if k % 2 == 1:
		ans += n // k - max(cnt[k // 2])
	print(ans)


import sys
ne = [[]]
word = []

def dfs(v, fd):
	dic = [0] * 26
	st = [v]
	fd[v] = 1
	dic[word[v]] = 1
	while st:
		h = st.pop()
		for nei in ne[h]:
			if not fd[nei]:
				fd[nei] = 1
				dic[word[nei]] += 1
				st.append(nei)
	return sum(dic) - max(dic)
for q in range(int(sys.stdin.readline())):
	(n, k) = [int(i) for i in sys.stdin.readline().split()]
	oa = ord('a')
	word = [ord(o) - oa for o in sys.stdin.readline().strip()]
	ne = [[] for i in range(n)]
	for i in range(n):
		if i >= k:
			ne[i].append(i - k)
		if i + k < n:
			ne[i].append(i + k)
		if 2 * i + 1 != n:
			ne[i].append(n - 1 - i)
	fnd = [0] * n
	ans = 0
	for i in range(n):
		if not fnd[i]:
			ans += dfs(i, fnd)
	sys.stdout.write(str(ans) + '\n')


for test_i in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	ans = 0
	for ki in range(k // 2):
		chars = {}
		for i in range(n // k):
			if s[ki + i * k] in chars:
				chars[s[ki + i * k]] += 1
			else:
				chars[s[ki + i * k]] = 1
		for i in range(n // k):
			if s[-ki - 1 + i * k] in chars:
				chars[s[-ki - 1 + i * k]] += 1
			else:
				chars[s[-ki - 1 + i * k]] = 1
		ans += 2 * n // k - max(chars.values())
	if k % 2 == 1:
		chars = {}
		for i in range(n // k):
			if s[k // 2 + i * k] in chars:
				chars[s[k // 2 + i * k]] += 1
			else:
				chars[s[k // 2 + i * k]] = 1
		ans += n // k - max(chars.values())
	print(ans)


import sys
from collections import Counter
input = sys.stdin.readline
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input().rstrip()
	ans = 0
	for i in range((k + 1) // 2):
		cnt = Counter(s[i::k])
		total = n // k
		j = k - 1 - i
		if i != j:
			cnt.update(s[j::k])
			total *= 2
		ans += total - cnt.most_common(1)[0][1]
	print(ans)


DEBUG = False
if DEBUG:
	import sys
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')

def solve():
	(n, k) = map(int, input().split())
	num = n // k
	s = input()
	ans = 0
	for i in range(k // 2):
		d = [0] * 26
		for j in range(i, n, k):
			d[ord(s[j]) - ord('a')] += 1
		for j in range(k - i - 1, n, k):
			d[ord(s[j]) - ord('a')] += 1
		ans += 2 * num - max(d)
	if k % 2 == 1:
		d = [0] * 26
		for j in range(k // 2, n, k):
			d[ord(s[j]) - ord('a')] += 1
		ans += num - max(d)
	return ans
t = int(input())
for _ in range(t):
	print(solve())


for t in range(int(input())):
	(n, k) = [int(i) for i in input().split()]
	s = input()
	ss = 0
	mm = []
	for i in range(0, k):
		kk = [0] * 26
		for j in range(i, n, k):
			kk[ord(s[j]) - ord('a')] += 1
		mm.append(kk)
	cc = []
	for i in range(0, k // 2):
		tt = [mm[i][j] + mm[k - i - 1][j] for j in range(0, 26)]
		ss += sum(tt) - max(tt)
	if k % 2:
		tt = [mm[k // 2][j] for j in range(0, 26)]
		ss += sum(tt) - max(tt)
	print(ss)


for krya in range(int(input())):
	(n, k) = map(int, input().split())
	st = input()
	ans = 0
	stn = []
	for i in range(k):
		stn.append(st[i::k])
	newlst = []
	for i in range(k // 2):
		newlst.append(stn[i] + stn[-1 - i])
	if k % 2 == 1:
		newlst.append(stn[k // 2])
	for i in newlst:
		bukvi = [0] * 26
		num = 0
		for j in list(i):
			num += 1
			bukvi[ord(str(j)) - ord('a')] += 1
		ans += num - max(bukvi)
	print(ans)


import sys
input = sys.stdin.readline
from collections import Counter
P = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
import math
t = int(input())
for tests in range(t):
	(n, k) = map(int, input().split())
	s = [i for i in input() if i != '\n']
	counter = [[0 for _ in range(26)] for _ in range(math.ceil(k / 2))]
	for i in range(len(s)):
		if k % 2 == 1:
			if i % k <= k // 2:
				counter[i % k][ord(s[i]) - 97] += 1
		elif i % k < k // 2:
			counter[i % k][ord(s[i]) - 97] += 1
	s.reverse()
	for i in range(len(s)):
		if i % k < k // 2:
			counter[i % k][ord(s[i]) - 97] += 1
	ans = 0
	for i in range(len(counter)):
		ans += sum(counter[i]) - max(counter[i])
	sys.stdout.write(str(ans) + '\n')


tt = int(input())
while tt != 0:
	(n, k) = map(int, input().split())
	s = list(input())
	ans = 0
	p = n // k
	t = -1
	if k % 2 == 0:
		t = k // 2
	else:
		t = k // 2 + 1
	for i in range(t):
		temp = [0] * 26
		for j in range(n // k):
			if s[j * k + i] == s[j * k + k - i - 1]:
				if j * k + i == j * k + k - i - 1:
					temp[ord(s[j * k + i]) - ord('a')] += 1
				else:
					temp[ord(s[j * k + i]) - ord('a')] += 2
			else:
				temp[ord(s[j * k + i]) - ord('a')] += 1
				temp[ord(s[j * k + k - i - 1]) - ord('a')] += 1
		ans += sum(temp) - max(temp)
	print(ans)
	tt -= 1


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	(s, p, k) = (input(), n, n // k)
	for i in range(n // k // 2):
		(d, m) = (dict(), 0)
		for j in range(k):
			d[s[j * (n // k) + i]] = d.get(s[j * (n // k) + i], 0) + 1
			d[s[(j + 1) * (n // k) - i - 1]] = d.get(s[(j + 1) * (n // k) - i - 1], 0) + 1
		for i in d:
			if m < d[i]:
				m = d[i]
		p = p - m
	if n // k % 2 == 1:
		(d, m) = (dict(), 0)
		for j in range(k):
			d[s[j * (n // k) + n // k // 2]] = d.get(s[j * (n // k) + n // k // 2], 0) + 1
		for i in d:
			if m < d[i]:
				m = d[i]
		p = p - m
	print(p)


def dfs(node, edges, s, visited):
	letters = [0] * 26
	stack = [node]
	while stack:
		curr = stack.pop()
		if curr not in visited:
			visited.add(curr)
			letters[ord(s[curr - 1]) - ord('a')] += 1
			for kid in edges[curr]:
				stack.append(kid)
	letters.sort()
	letters.pop()
	return sum(letters)

def solve(s, n, k, ans):
	edges = {}
	for i in range(1, n + 1):
		edges[i] = set()
	for i in range(1, n + 1):
		if i + k <= n:
			edges[i].add(i + k)
			edges[i + k].add(i)
		if i - k >= 1:
			edges[i].add(i - k)
			edges[i - k].add(i)
		edges[i].add(n + 1 - i)
		edges[n + 1 - i].add(i)
	visited = set()
	count = 0
	for i in range(1, n + 1):
		if i not in visited:
			count += dfs(i, edges, s, visited)
	ans.append(count)

def main():
	t = int(input())
	ans = []
	for i in range(t):
		(n, k) = map(int, input().split())
		s = input()
		solve(s, n, k, ans)
	for i in ans:
		print(i)
main()


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	temp = []
	b = []
	j = 0
	while j < k:
		for i in range(j, n, k):
			temp += [s[i]]
		j += 1
		b += [temp]
		temp = []
	te = []
	for i in range(k // 2):
		te += [b[i] + b[k - i - 1]]
	if k % 2 == 1:
		te += [b[k // 2]]
	l2 = []
	lol = 0
	for i in range(len(te)):
		d = dict()
		for j in range(len(te[i])):
			if te[i][j] in d:
				d[te[i][j]] += 1
			else:
				d[te[i][j]] = 1
			lol += 1
		l1 = list(d.values())
		l2 += [len(te[i]) - max(l1)]
	print(sum(l2))


t = int(input())
for i in range(1, t + 1):
	(n, k) = map(int, input().split())
	w = [x for x in input()]
	answer = 0
	for i in range((k - 1) // 2 + 1):
		letters = {}
		if i == k // 2:
			length = n // k
			for j in range(n // k):
				lettersIdxs = [j * k + i]
				for idx in lettersIdxs:
					letter = w[idx]
					if letter not in letters:
						letters[letter] = 0
					letters[letter] += 1
		else:
			length = n // k * 2
			for j in range(n // k):
				lettersIdxs = [j * k + i, n - j * k - i - 1]
				for idx in lettersIdxs:
					letter = w[idx]
					if letter not in letters:
						letters[letter] = 0
					letters[letter] += 1
		cur_max = 0
		for o in letters.values():
			cur_max = max(cur_max, o)
		answer += length - cur_max
	print(answer)


t = int(input())
for q in range(t):
	(n, k) = map(int, input().split())
	n //= k
	longword = input()
	lst = [longword[i * k:(i + 1) * k] for i in range(n)]
	ans = 0
	for j in range(k // 2):
		temp = dict()
		maxc = 0
		for i in range(n):
			if lst[i][j] in temp:
				temp[lst[i][j]] += 1
			else:
				temp[lst[i][j]] = 1
			if temp[lst[i][j]] > maxc:
				maxc = temp[lst[i][j]]
				letter = lst[i][j]
			if lst[i][k - j - 1] in temp:
				temp[lst[i][k - j - 1]] += 1
			else:
				temp[lst[i][k - j - 1]] = 1
			if temp[lst[i][k - j - 1]] > maxc:
				maxc = temp[lst[i][k - j - 1]]
				letter = lst[i][k - j - 1]
		for i in range(n):
			if lst[i][j] != letter:
				ans += 1
			if lst[i][k - j - 1] != letter:
				ans += 1
	if k % 2 == 1:
		temp = dict()
		maxc = 0
		for i in range(n):
			if lst[i][k // 2] in temp:
				temp[lst[i][k // 2]] += 1
			else:
				temp[lst[i][k // 2]] = 1
			if temp[lst[i][k // 2]] > maxc:
				maxc = temp[lst[i][k // 2]]
				letter = lst[i][k // 2]
		for i in range(n):
			if lst[i][k // 2] != letter:
				ans += 1
	print(ans)


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

def find(no):
	global par
	if par[no] == no:
		return no
	par[no] = find(par[no])
	return par[no]
for _ in range(t):
	(n, k) = map(int, input().split())
	s = [ord(i) - 97 for i in input()]
	par = [i for i in range(n)]
	vis = [0 for i in range(n)]
	for i in range(k):
		for j in range(i, n, k):
			vis[j] = 1
			x = n - j - 1
			aa = find(x)
			bb = find(j)
			par[bb] = aa
			if j > i:
				aa = find(j - k)
				bb = find(j)
				par[bb] = aa
	tot = n
	aa = set(par)
	co = [[0 for i in range(26)] for j in range(n)]
	for i in range(n):
		co[par[i]][s[i]] += 1
	tot -= sum([max(i) for i in co])
	print(tot)


import sys
input = lambda : sys.stdin.readline().rstrip()
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = [ord(i) - 97 for i in input()]
	d = [[0] * 26 for i in range((k + 1) // 2)]
	ans = 0
	for i in range(len(a)):
		d[min(i % k, k - i % k - 1)][a[i]] += 1
	for i in d:
		ans += sum(i) - max(i)
	print(ans)


from collections import defaultdict
from sys import stdin, stdout
t = int(input())
for _ in range(t):
	(n, k) = list(map(int, input().split()))
	s = stdin.readline()[:-1]
	mid = (k - 1) // 2
	d = {i: defaultdict(int) for i in range(mid + 1)}
	for (i, letter) in enumerate(s):
		rem = i % k
		if rem > mid:
			rem = k - 1 - rem
		d[rem][letter] += 1
	result = 0
	for each in d.values():
		maxx = max(each.values())
		result += sum(each.values()) - maxx
	print(result)


ans_ = []
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	for i in range(n):
		s[i] = ord(s[i]) - ord('a')
	ans = 0
	for p in range(k // 2):
		abc = [0] * 26
		for i in range(p, n, k):
			abc[s[i]] += 1
			abc[s[i - 1 - p * 2]] += 1
			s[i] = '+'
			s[i - 1 - p * 2] = '+'
		ans += sum(abc) - max(abc)
	if k % 2 == 1:
		abc = [0] * 26
		for i in range(k // 2, n, k):
			abc[s[i]] += 1
			s[i] = '+'
		ans += sum(abc) - max(abc)
	ans_.append(ans)
print('\n'.join(map(str, ans_)))


from sys import stderr

def count_min_to_equalize(cs):
	c_counts = [0 for _ in range(ord('z') - ord('a') + 1)]
	for c in cs:
		c_counts[ord(c) - ord('a')] += 1
	return len(cs) - max(c_counts)
for _ in range(int(input())):
	(n, k) = map(int, input().split(' '))
	s = input()
	n_min = 0
	s_new = [None for c in s]
	for i in range(k // 2):
		cs = [s[i + j * k] for j in range(0, n // k)]
		cs += [s[(j + 1) * k - (i + 1)] for j in range(0, n // k)]
		n_min += count_min_to_equalize(cs)
	if k % 2 == 1:
		n_min += count_min_to_equalize([s[k // 2 + i * k] for i in range(n // k)])
	print(n_min)


def NO():
	return print('NO')

def YES():
	return print('YES')

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

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

def STR():
	return input()

def MAP():
	return map(int, input().split())
for _ in range(INT()):
	(n, k) = MAP()
	s = STR()
	a = []
	for j in range(k):
		b = []
		for _ in range(26):
			b.append([0])
		a.append(b)
	if k % 2 == 0:
		for j in range(n):
			t = j % k
			if t < k // 2:
				a[t][ord(s[j]) - 97][0] += 1
			else:
				a[k - t - 1][ord(s[j]) - 97][0] += 1
	else:
		for j in range(n):
			t = j % k
			if t <= k // 2:
				a[t][ord(s[j]) - 97][0] += 1
			else:
				a[k - t - 1][ord(s[j]) - 97][0] += 1
	ans = 0
	for j in range(k):
		p = -1
		f = 0
		for i in range(26):
			f += a[j][i][0]
			p = max(p, a[j][i][0])
		ans += f - p
	print(ans)


import sys
from collections import Counter
sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2 ** 62 - 1

def read_int():
	return int(input())

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

def read_float():
	return float(input())

def read_float_n():
	return list(map(float, input().split()))

def read_str():
	return input().strip()

def read_str_n():
	return list(map(str, input().split()))

def error_print(*args):
	print(*args, file=sys.stderr)

def mt(f):
	import time

	def wrap(*args, **kwargs):
		s = time.time()
		ret = f(*args, **kwargs)
		e = time.time()
		error_print(e - s, 'sec')
		return ret
	return wrap

def slv(N, K, S):
	h = (K + 1) // 2
	c = [Counter() for _ in range(h)]
	e = [0] * h
	for i in range(h):
		for j in range(N // K):
			c[i][S[i + j * K]] += 1
			e[i] += 1
	for i in range(h, K):
		k = 2 * h - 1 - i
		if K % 2 != 0:
			k -= 1
		for j in range(N // K):
			c[k][S[i + j * K]] += 1
			e[k] += 1
	ans = 0
	for (i, d) in enumerate(c):
		ans += e[i] - d.most_common(1)[0][1]
	return ans

def main():
	T = read_int()
	for _ in range(T):
		(N, K) = read_int_n()
		S = read_str()
		print(slv(N, K, S))
main()


import sys

def answer(n, k, s):
	odd = False
	if k % 2 == 1:
		l = k // 2 + 1
		odd = True
	else:
		l = k // 2
	base = ord('a')
	cnt = [[0 for j in range(26)] for i in range(l)]
	for i in range(n):
		c_num = i % k
		if c_num >= l:
			c_num = k - (c_num + 1)
		offset = ord(s[i]) - base
		cnt[c_num][offset] += 1
	score = 0
	num_times = n // k
	for i in range(l):
		if odd and i == l - 1:
			score += num_times - max(cnt[i])
		else:
			score += 2 * num_times - max(cnt[i])
	return score

def main():
	t = int(sys.stdin.readline())
	while t:
		(n, k) = map(int, sys.stdin.readline().split())
		s = sys.stdin.readline().rstrip()
		print(answer(n, k, s))
		t -= 1
	return
main()


import sys
input = sys.stdin.readline
from collections import defaultdict

class Unionfind:

	def __init__(self, n):
		self.par = [-1] * n
		self.rank = [1] * n

	def root(self, x):
		r = x
		while not self.par[r] < 0:
			r = self.par[r]
		t = x
		while t != r:
			tmp = t
			t = self.par[t]
			self.par[tmp] = r
		return r

	def unite(self, x, y):
		rx = self.root(x)
		ry = self.root(y)
		if rx == ry:
			return
		if self.rank[rx] <= self.rank[ry]:
			self.par[ry] += self.par[rx]
			self.par[rx] = ry
			if self.rank[rx] == self.rank[ry]:
				self.rank[ry] += 1
		else:
			self.par[rx] += self.par[ry]
			self.par[ry] = rx

	def is_same(self, x, y):
		return self.root(x) == self.root(y)

	def count(self, x):
		return -self.par[self.root(x)]
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()[:-1]
	uf = Unionfind(n)
	for i in range(n // 2):
		uf.unite(i, n - 1 - i)
	for i in range(n - k):
		uf.unite(i, i + k)
	d = defaultdict(dict)
	for i in range(n):
		if s[i] not in d[uf.root(i)]:
			d[uf.root(i)][s[i]] = 1
		else:
			d[uf.root(i)][s[i]] += 1
	rs = set((uf.root(i) for i in range(n)))
	ans = 0
	for r in rs:
		ans += uf.count(r) - max(list(d[r].values()))
	print(ans)


def to_list(s):
	return list(map(lambda x: int(x), s.split(' ')))

def get_replace_count(item):
	moda_count = get_moda_count(item)
	return len(item) - moda_count

def get_moda_count(item):
	sorted_item = sorted(item)
	len_ = 1
	max_len = 1
	curr_letter = sorted_item[0]
	prev_letter = sorted_item[0]
	for i in range(1, len(item)):
		curr_letter = sorted_item[i]
		if curr_letter != prev_letter:
			if len_ > max_len:
				max_len = len_
			len_ = 1
		else:
			len_ += 1
		prev_letter = curr_letter
	if len_ > max_len:
		return len_
	return max_len

def solve(s, k):
	if k == 1:
		return str(len(s) - get_moda_count(s))
	arr = []
	for i in range(0, len(s), k):
		arr.append(s[i:i + k])
	dict_ = {}
	for j in range(int(k / 2)):
		dict_[j] = ''
	if k % 2 != 0:
		dict_[int(k / 2)] = ''
	for item in arr:
		for i in range(int(k / 2)):
			dict_[i] += item[i]
			dict_[i] += item[-i - 1]
		if k % 2 != 0:
			dict_[int(k / 2)] += item[int(k / 2)]
	replace_count = 0
	for item in list(dict_.values()):
		replace_count += get_replace_count(item)
	return str(replace_count)
answers = []
t = int(input())
for i in range(t):
	(n, k) = to_list(input())
	s = input()
	answers.append(solve(s, k))
print('\n'.join(answers))


tt = int(input())
for qwerty in range(tt):
	(n, k) = map(int, input().split())
	s = input()
	arr = [[0 for i in range(26)] for j in range((k + 1) // 2)]
	for i in range(n):
		v1 = min(i % k, k - i % k - 1)
		arr[v1][ord(s[i]) - ord('a')] += 1
	result = 0
	for i in range(k // 2):
		result += 2 * n // k - max(arr[i])
	if k % 2 != 0:
		result += n // k - max(arr[k // 2])
	print(result)


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()[:-1]
	cnt = [[0] * 26 for _ in range((k + 1) // 2)]
	for i in range(n):
		cnt[min(i % k, k - 1 - i % k)][ord(s[i]) - ord('a')] += 1
	ans = 0
	for i in range((k + 1) // 2):
		ans += sum(cnt[i]) - max(cnt[i])
	print(ans)


import sys
input = iter(sys.stdin.readlines()).__next__
import collections
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	prefix_len = k // 2 + (k & 1)
	counts = [collections.defaultdict(int) for _ in range(prefix_len)]
	for (i, c) in enumerate(s):
		j = i % k
		if j >= prefix_len:
			j = k - 1 - j
		counts[j][c] += 1
	score = n
	for count in counts:
		score -= max(count.values())
	print(score)


from sys import stdin
t = int(input())
for _ in range(t):
	(n, k) = map(int, stdin.readline().split())
	s = str(stdin.readline())
	check = [0] * (n + 1)
	ans = 0
	for i in range(k):
		ma = 0
		(v, count) = ([], [0] * 26)
		for i2 in range(i, n, k):
			if check[i2] == 0:
				check[i2] = 1
				v.append(s[i2])
		for i2 in range(n - 1 - i, -1, -k):
			if check[i2] == 0:
				check[i2] = 1
				v.append(s[i2])
		for i2 in v:
			count[ord(i2) - 97] += 1
			ma = max(ma, count[ord(i2) - 97])
		ans += len(v) - ma
	print(ans)


printn = lambda x: print(x, end='')
inn = lambda : int(input())
inl = lambda : list(map(int, input().split()))
inm = lambda : map(int, input().split())
ins = lambda : input().strip()
DBG = True
BIG = 10 ** 18
R = 10 ** 9 + 7

def ddprint(x):
	if DBG:
		print(x)

def foo(s, n, k):
	ccnt = [[0] * 26 for i in range((k + 1) // 2)]
	for i in range(n):
		r = i % k
		j = r if r < (k + 1) // 2 else k - 1 - r
		ccnt[j][ord(s[i]) - ord('a')] += 1
	sm = 0
	for j in range((k + 1) // 2):
		sm += max(ccnt[j])
	return n - sm
t = inn()
for tt in range(t):
	(n, k) = inm()
	s = ins()
	print(foo(s, n, k))


t = int(input(''))
import math
for _ in range(t):
	(n, k) = list(map(int, input('').split(' ')))
	s = input('')
	ele = n // k
	ans = 0
	for i in range(math.ceil(k / 2)):
		d = {}
		j = i
		while j < n:
			if s[j] not in d:
				d[s[j]] = 0
			d[s[j]] = d[s[j]] + 1
			j = j + k
		if k - 1 - i != i:
			j = k - 1 - i
			while j < n:
				if s[j] not in d:
					d[s[j]] = 0
				d[s[j]] = d[s[j]] + 1
				j = j + k
		mx = 0
		for c in d:
			if d[c] > mx:
				mx = d[c]
		if k - 1 - i == i:
			ans += ele - mx
			continue
		ans += 2 * ele - mx
	print(ans)


cases = int(input())
for t in range(cases):
	(n, k) = list(map(int, input().split()))
	s = input()
	output = 0
	for i in range(k):
		d = [0] * 26
		for j in range(i, n, k):
			d[ord(s[j]) - 97] += 1
			d[ord(s[n - j - 1]) - 97] += 1
		output += sum(d) - max(d)
	print(output // 2)


import os
import sys
from io import BytesIO, IOBase
import threading
from collections import Counter

def main():
	for _ in range(int(input())):
		(n, k) = map(int, input().split())
		arr = [c for c in input()]
		clc = [[] for i in range(k)]
		for (i, ch) in enumerate(arr):
			clc[i % k].append(ch)
		ans = 0
		for i in range(k // 2):
			li = clc[i] + clc[k - 1 - i]
			ct = Counter(li)
			ans += len(li) - max(list(ct.values()))
		if k % 2 == 1:
			li = clc[k // 2]
			ct = Counter(li)
			ans += len(li) - max(list(ct.values()))
		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()


from collections import defaultdict as dd
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	arr = list(map(str, input().strip()))
	d = dict()
	x = 0
	if k % 2 == 1:
		x = 1
	for i in range(k // 2 + x):
		d[i] = dd(int)
	for i in range(len(arr)):
		d[min(i % k, k - 1 - i % k)][arr[i]] += 1
	ans = 0
	for i in range(k // 2 + x):
		maxi = 0
		summ = 0
		for k in d[i]:
			summ += d[i][k]
			if maxi < d[i][k]:
				maxi = d[i][k]
		ans += summ - maxi
	print(ans)


import math, string, itertools, fractions, heapq, collections, re, array, bisect

def chunks(l, n):
	for i in range(0, len(l), n):
		yield l[i:i + n]
cases = int(input().strip())
for case in range(cases):
	(n, k) = list(map(int, input().strip().split(' ')))
	s = list(input().strip())
	s = list(chunks(s, k))
	ans = 0
	for i in range(int((k + 1) / 2)):
		d = collections.defaultdict(int)
		most_common = 0
		total = 0
		for j in range(len(s)):
			d[s[j][i]] += 1
			most_common = max(most_common, d[s[j][i]])
			total += 1
			if i < k - i - 1:
				d[s[j][k - i - 1]] += 1
				most_common = max(most_common, d[s[j][k - i - 1]])
				total += 1
		ans += total - most_common
	print(int(ans))


import atexit
import io
import sys
_INPUT_LINES = sys.stdin.read().splitlines()
input = iter(_INPUT_LINES).__next__
_OUTPUT_BUFFER = io.StringIO()
sys.stdout = _OUTPUT_BUFFER

@atexit.register
def write():
	sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
n = int(input())
for _ in range(n):
	(n, k) = map(int, input().split())
	s = input()
	ans = 0
	for offset in range((k + 1) // 2):
		counter = [0] * 26
		left = offset
		right = k - offset - 1
		for jump in range(0, n, k):
			counter[ord(s[jump + left]) - ord('a')] += 1
			if k % 2 == 1 and offset == k // 2:
				continue
			counter[ord(s[jump + right]) - ord('a')] += 1
		ans += sum(counter) - max(counter)
	print(ans)


for t in range(int(input())):
	(n, l) = map(int, input().split())
	s = input()
	d = n // l
	ans = 0
	for k in range(0, l // 2):
		a = [0 for o in range(26)]
		b = [0 for o in range(26)]
		for p in range(k, n, l):
			a[ord(s[p]) - 97] += 1
			b[ord(s[l - p - 1]) - 97] += 1
		minn = 10 ** 9
		for p in range(26):
			minn = min(minn, 2 * d - a[p] - b[p])
		ans += minn
	if l % 2 == 1:
		k = l // 2
		minn = 10 ** 9
		a = [0 for o in range(26)]
		for p in range(k, n, l):
			a[ord(s[p]) - 97] += 1
		for p in range(26):
			minn = min(minn, d - a[p])
		ans += minn
	print(ans)


from sys import stdin
t = int(stdin.readline())
for _ in range(t):
	(n, k) = map(int, stdin.readline().split())
	s = stdin.readline()
	su = 0
	for x in range(k // 2):
		d = {}
		for i in range(n // k):
			if s[x + k * i] in d:
				d[s[x + k * i]] += 1
			else:
				d[s[x + k * i]] = 1
			if s[k - x - 1 + k * i] in d:
				d[s[k - x - 1 + k * i]] += 1
			else:
				d[s[k - x - 1 + k * i]] = 1
		u = max(d, key=d.get)
		su += int(d.get(u))
	if k % 2 != 0:
		d = {}
		for i in range(n // k):
			if s[k // 2 + k * i] in d:
				d[s[k // 2 + k * i]] += 1
			else:
				d[s[k // 2 + k * i]] = 1
		u = max(d, key=d.get)
		su += int(d.get(u))
	print(n - su)


for i in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	ans = 0
	for j in range(k // 2):
		it = j
		cnt = [0] * 26
		while it < n:
			cnt[ord(s[it]) - ord('a')] += 1
			it += k
		it = k - j - 1
		while it < n:
			cnt[ord(s[it]) - ord('a')] += 1
			it += k
		ans += sum(cnt) - max(cnt)
	if k % 2 != 0:
		it = k // 2
		cnt = [0] * 26
		while it < n:
			cnt[ord(s[it]) - ord('a')] += 1
			it += k
		ans += sum(cnt) - max(cnt)
	print(ans)


def canwalk(a, b, c, d, x, y, x1, y1, x2, y2):
	if x1 == x2 and (a > 0 or b > 0):
		return 'NO'
	if y1 == y2 and (c > 0 or d > 0):
		return 'NO'
	if b + abs(x - x1) >= a and a + abs(x - x2) >= b:
		if d + abs(y - y1) >= c and c + abs(y - y2) >= d:
			return 'YES'
	return 'NO'

def length(x):
	return len(x)
colors = [0, 0, 1, 0, 1, 0, 1, 2, 1, 0, 1, 0, 1, 2, 1, 0, 1, 0, 1, 2, 1, 0, 1, 3, 1, 2, 1, 0, 1, 0, 1, 2, 1, 3, 1, 0, 1, 2, 1, 0, 1, 0, 1, 2, 1, 0, 1, 4, 1, 2, 1, 0, 1, 3, 1, 2, 1, 0, 1, 0, 1, 2, 1, 3, 1, 0, 1, 2, 1, 0, 1, 0, 1, 2, 1, 4, 1, 0, 1, 2, 1, 0, 1, 3, 1, 2, 1, 0, 1, 4, 1, 2, 1, 3, 1, 0, 1, 2, 1, 0, 1, 0, 1, 2, 1, 0, 1, 0, 1, 2, 1, 0, 1, 3, 1, 2, 1, 4, 1, 5, 1, 2, 1, 3, 1, 0, 1, 2, 1, 0, 1, 4, 1, 2, 1, 0, 1, 0, 1, 2, 1, 5, 1, 3, 1, 2, 1, 0, 1, 0, 1, 2, 1, 3, 1, 0, 1, 2, 1, 4, 1, 0, 1, 2, 1, 0, 1, 6, 1, 2, 1, 0, 1, 3, 1, 2, 1, 0, 1, 0, 1, 2, 1, 3, 1, 5, 1, 2, 1, 0, 1, 0, 1, 2, 1, 0, 1, 0, 1, 2, 1, 4, 1, 3, 1, 2, 1, 5, 1, 0, 1, 2, 1, 3, 1, 4, 1, 2, 1, 6, 1, 0, 1, 2, 1, 0, 1, 0, 1, 2, 1, 0, 1, 3, 1, 2, 1, 0, 1, 0, 1, 2, 1, 3, 1, 6, 1, 2, 1, 0, 1, 5, 1, 2, 1, 0, 1, 4, 1, 2, 1, 0, 1, 3, 1, 2, 1, 0, 1, 0, 1, 2, 1, 3, 1, 0, 1, 2, 1, 0, 1, 0, 1, 2, 1, 4, 1, 7, 1, 2, 1, 0, 1, 3, 1, 2, 1, 6, 1, 4, 1, 2, 1, 3, 1, 0, 1, 2, 1, 0, 1, 0, 1, 2, 1, 0, 1, 5, 1, 2, 1, 7, 1, 3, 1, 2, 1, 4, 1, 0, 1, 2, 1, 3, 1, 0, 1, 2, 1, 5, 1, 4, 1, 2, 1, 0, 1, 0, 1, 2, 1, 0, 1, 3, 1, 2, 1, 0, 1, 8, 1, 2, 1, 3, 1, 0, 1, 2, 1, 4, 1, 0, 1, 2, 1, 6, 1, 0, 1, 2, 1, 0, 1, 3, 1, 2, 1, 0, 1, 7, 1, 2, 1, 3, 1, 0, 1, 2, 1, 0, 1, 6, 1, 2, 1, 5, 1, 0, 1, 2, 1, 4, 1, 3, 1, 2, 1, 0, 1, 0, 1, 2, 1, 3, 1, 4, 1, 2, 1, 0, 1, 0, 1, 2, 1, 8, 1, 0, 1, 2, 1, 0, 1, 3, 1, 2, 1, 0, 1, 5, 1, 2, 1, 3, 1, 0, 1, 2, 1, 0, 1, 0, 1, 2, 1, 0, 1, 4, 1, 2, 1, 5, 1, 3, 1, 2, 1, 0, 1, 6, 1, 2, 1, 3, 1, 0, 1, 2, 1, 0, 1, 7, 1, 2, 1, 4, 1, 0, 1, 2, 1, 0, 1, 3, 1, 2, 1, 0, 1, 4, 1, 2, 1, 3, 1, 5, 1, 2, 1, 0, 1, 0, 1, 2, 1, 7, 1, 9, 1, 2, 1, 6, 1, 3, 1, 2, 1, 4, 1, 0, 1, 2, 1, 3, 1, 0, 1, 2, 1, 8, 1, 4, 1, 2, 1, 0, 1, 6, 1, 2, 1, 0, 1, 3, 1, 2, 1, 0, 1, 0, 1, 2, 1, 3, 1, 0, 1, 2, 1, 4, 1, 5, 1, 2, 1, 0, 1, 8, 1, 2, 1, 0, 1, 3, 1, 2, 1, 0, 1, 0, 1, 2, 1, 3, 1, 0, 1, 2, 1, 6, 1, 0, 1, 2, 1, 0, 1, 0, 1, 2, 1, 4, 1, 3, 1, 2, 1, 7, 1, 0, 1, 2, 1, 3, 1, 4, 1, 2, 1, 0, 1, 0, 1, 2, 1, 0, 1, 5, 1, 2, 1, 0, 1, 3, 1, 2, 1, 0, 1, 0, 1, 2, 1, 3, 1, 9, 1, 2, 1, 5, 1, 0, 1, 2, 1, 0, 1, 4, 1, 2, 1, 0, 1, 3, 1, 2, 1, 6, 1, 0, 1, 2, 1, 3, 1, 7, 1, 2, 1, 0, 1, 8, 1, 2, 1, 4, 1, 0, 1, 2, 1, 9, 1, 3, 1, 2, 1, 0, 1, 4, 1, 2, 1, 3, 1, 0, 1, 2, 1, 7, 1, 0, 1, 2, 1, 5, 1, 0, 1, 2, 1, 0, 1, 3, 1, 2, 1, 4, 1, 0, 1, 2, 1, 3, 1, 0, 1, 2, 1, 0, 1, 4, 1, 2, 1, 6, 1, 0, 1, 2, 1, 0, 1, 3, 1, 2, 1, 8, 1, 5, 1, 2, 1, 3, 1, 0, 1, 2, 1, 4, 1, 6, 1, 2, 1, 0, 1, 7, 1, 2, 1, 5, 1, 3, 1, 2, 1, 0, 1, 0, 1, 2, 1, 3, 1, 8, 1, 2, 1, 0, 1, 0, 1, 2, 1, 0, 1, 0, 1, 2, 1, 4, 1, 3, 1, 2, 1, 0, 1, 10, 1, 2, 1, 3, 1, 4, 1, 2, 1, 9, 1, 0, 1, 2, 1, 0, 1, 0, 1, 2, 1, 0, 1, 3, 1, 2, 1, 5, 1, 6, 1, 2, 1, 3, 1, 0, 1, 2, 1, 0, 1, 0, 1, 2, 1, 0, 1, 4, 1, 2, 1, 8, 1, 3, 1, 2, 1, 10, 1, 7, 1, 2, 1, 3, 1, 0, 1, 2, 1, 0, 1, 5, 1, 2, 1, 4, 1, 0, 1, 2, 1, 6, 1, 3, 1, 2, 1, 0, 1, 4, 1, 2, 1, 3, 1, 0, 1, 2, 1, 0, 1, 9, 1, 2, 1, 0, 1, 6, 1, 2, 1, 0, 1, 3, 1, 2, 1, 4, 1, 11, 1, 2, 1, 3, 1, 0, 1, 2, 1, 0, 1, 4, 1, 2, 1, 0, 1, 5, 1, 2, 1, 0, 1, 3, 1, 2, 1, 9, 1, 0, 1, 2, 1, 3, 1, 0, 1, 2, 1]

def compositecoloring(x):
	ccolors = []
	for i in range(len(x)):
		ccolors.append(colors[x[i] - 2])
	for i in range(1, max(ccolors)):
		s = 0
		for j in range(len(ccolors)):
			if ccolors[j] == i:
				s += 1
		if s == 0:
			minc = 12
			for j in range(len(ccolors)):
				if ccolors[j] > i and minc > ccolors[j]:
					minc = ccolors[j]
			if minc == 12:
				break
			for j in range(len(ccolors)):
				if ccolors[j] == minc:
					ccolors[j] = i
	return ccolors

def make_komplete(x, k):
	y = list(x)
	s = 0
	for i in range((k - 1) // 2 + 1):
		if k % 2 == 0:
			l = y[i::k] + y[k - i - 1::k]
		elif i == k // 2:
			l = y[i::k]
		else:
			l = y[i::k] + y[k - i - 1::k]
		counts = [0] * 26
		for i in range(len(l)):
			counts[ord(l[i]) - 97] += 1
		s += len(l) - max(counts)
	return s
cases = int(input())
for _ in range(cases):
	(l, k) = list(map(int, input().split()))
	x = input()
	if x[-1] == '\r':
		x = x[:-1]
	print(make_komplete(x, k))


from collections import Counter
t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	s = input()
	a = []
	for j in range(k):
		b = []
		for i in range(26):
			b.append([0])
		a.append(b)
	if k % 2 == 0:
		for j in range(n):
			t = j % k
			if t < k // 2:
				a[t][ord(s[j]) - 97][0] += 1
			else:
				a[k - t - 1][ord(s[j]) - 97][0] += 1
	else:
		for j in range(n):
			t = j % k
			if t <= k // 2:
				a[t][ord(s[j]) - 97][0] += 1
			else:
				a[k - t - 1][ord(s[j]) - 97][0] += 1
	c = 0
	for j in range(k):
		p = -1
		f = 0
		for i in range(26):
			f += a[j][i][0]
			p = max(p, a[j][i][0])
		c += f - p
	print(c)


t = int(input())

def most_f(List):
	return max(set(List), key=List.count)
for test in range(t):
	(n, k) = [int(x) for x in input().split()]
	arr = list(input())
	allsame = True
	count = 0
	end = k
	ev = 0
	if n % 2 == 0:
		ev = 1
	for i in range(0, k // 2 + ev):
		valk = []
		end -= 1
		valk = [arr[x] for x in range(i, n, k)]
		valm = [arr[x] for x in range(end, n, k)]
		val = valk[:] + valm[:]
		most = most_f(val)
		endwr = end
		for j in range(i, n, k):
			if arr[j] != most:
				arr[j] = most
				count += 1
			if arr[endwr] != most:
				arr[endwr] = most
				count += 1
			endwr += k
	if n % 2 == 1:
		val = [arr[x] for x in range(k // 2, n, k)]
		most = most_f(val)
		for i in range(k // 2, n, k):
			if arr[i] != most:
				arr[i] = most
				count += 1
	print(count)


for t in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	strings = [s[i:i + k] for i in range(0, n, k)]
	ans = 0
	for i in range(k // 2 + k % 2):
		arr = []
		freq = {}
		for j in range(n // k):
			arr.append(strings[j][i])
			freq[strings[j][i]] = freq.get(strings[j][i], 0) + 1
			if i != k // 2:
				arr.append(strings[j][k - 1 - i])
				freq[strings[j][k - 1 - i]] = freq.get(strings[j][k - 1 - i], 0) + 1
		freq = freq.values()
		freq = (1 + (i != k // 2)) * (n // k) - max(freq)
		ans += freq
	print(ans)


from sys import stdin, stdout
import collections, heapq, bisect, math
input = stdin.readline

def rint():
	return int(input())

def rstr():
	return input().strip()

def rlstr():

	def f(s):
		return s.strip()
	return list(map(f, input().split()))

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

def main():
	(n, k) = rlint()
	s = rstr()
	cnt = [[0] * 26 for _ in range((k + 1) // 2)]
	for i in range(n):
		cnt[min(i % k, k - i % k - 1)][ord(s[i]) - ord('a')] += 1
	res = 0
	for i in range(k // 2):
		res += 2 * n // k - max(cnt[i])
	if k % 2 == 1:
		res += n // k - max(cnt[k // 2])
	print(res)
for _ in range(rint()):
	main()


from collections import Counter

def answer():
	(n, k) = map(int, input().split(' '))
	s = input()
	bunkatu_s_list = [s[i:i + k] for i in range(0, n, k)]
	bunkatu_n = n // k
	half_s_list = []
	out = 0
	if k % 2 == 0:
		half_k = k // 2
		for i in range(half_k):
			i1 = i
			i2 = k - (i + 1)
			tmp1 = s[i1::k]
			tmp2 = s[i2::k]
			tmp = len(tmp1) + len(tmp2) - Counter(tmp1 + tmp2).most_common(1)[0][1]
			out += tmp
		aidas = None
	else:
		half_k = (k - 1) // 2
		for i in range(half_k):
			i1 = i
			i2 = k - (i + 1)
			tmp1 = s[i1::k]
			tmp2 = s[i2::k]
			tmp = len(tmp1) + len(tmp2) - Counter(tmp1 + tmp2).most_common(1)[0][1]
			out += tmp
		aidas = [bunkatu_s[half_k] for bunkatu_s in bunkatu_s_list]
		tmp = bunkatu_n - max(Counter(aidas).values())
		out += tmp
	print(out)
mm = int(input())
for _ in range(mm):
	answer()


import math
import sys
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = str(input())
	per = 0
	for i in range(k // 2):
		m = {}
		for j in range(n // k):
			if s[i + j * k] not in m:
				m[s[i + j * k]] = 1
			else:
				m[s[i + j * k]] += 1
			if s[k - 1 - i + j * k] not in m:
				m[s[k - 1 - i + j * k]] = 1
			else:
				m[s[k - 1 - i + j * k]] += 1
		mx = 0
		for key in m:
			mx = max(mx, m[key])
		per += 2 * n // k - mx
	if k % 2 == 1:
		m = {}
		for j in range(n // k):
			if s[k // 2 + j * k] not in m:
				m[s[k // 2 + j * k]] = 1
			else:
				m[s[k // 2 + j * k]] += 1
		mx = 0
		for key in m:
			mx = max(mx, m[key])
		per += n // k - mx
	print(per)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	bags = [[0 for i in range(26)] for i in range(k // 2 + 1)]
	maxims = [0 for i in range(k // 2 + 1)]
	for i in range(n):
		bag_number = i % k
		if bag_number >= k // 2:
			bag_number = k - 1 - bag_number
		bags[bag_number][ord(s[i]) - ord('a')] += 1
		if maxims[bag_number] < bags[bag_number][ord(s[i]) - ord('a')]:
			maxims[bag_number] = bags[bag_number][ord(s[i]) - ord('a')]
	print(n - sum(maxims))


import os, sys, bisect, copy
from collections import defaultdict, Counter, deque
from functools import lru_cache
if os.path.exists('in.txt'):
	sys.stdin = open('in.txt', 'r')
if os.path.exists('out.txt'):
	sys.stdout = open('out.txt', 'w')

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

def mapi(arg=0):
	return map(int if arg == 0 else str, input().split())
for _ in range(int(input())):
	(n, k) = mapi()
	a = list(input().strip())
	res = 0
	ls = defaultdict(list)
	freq = [[0] * 26 for i in range(k + 2)]
	res = 0
	for i in range(1, k + 1):
		for j in range(i, n + 1, k):
			ls[i].append(ord(a[j - 1]) - ord('a'))
			freq[i][ord(a[j - 1]) - ord('a')] += 1
	l = 1
	r = k
	while l < r:
		val = 200010
		for i in range(26):
			val = min(val, 2 * n // k - freq[l][i] - freq[r][i])
		res += val
		l += 1
		r -= 1
	if l == r:
		val = 200010
		for i in range(26):
			val = min(val, n // k - freq[l][i])
		res += val
	print(res)


for _ in range(int(input())):
	[n, k] = list(map(int, input().split(' ')))
	s = input()
	ans = 0
	for i in range((k + 1) // 2):
		d = [0 for i in range(26)]
		for j in range(n // k):
			if i != k - 1 - i:
				d[ord(s[i + k * j]) - ord('a')] += 1
				d[ord(s[k - 1 - i + k * j]) - ord('a')] += 1
			else:
				d[ord(s[i + k * j]) - ord('a')] += 1
		if i == k - 1 - i:
			ans += n // k - max(d)
		else:
			ans += 2 * n // k - max(d)
	print(ans)


import sys
input = sys.stdin.buffer.readline

def print(val):
	sys.stdout.write(str(val) + '\n')
from collections import Counter

def prog():
	for _ in range(int(input())):
		(n, k) = map(int, input().split())
		s = input().strip()
		length = n // k
		tot_switch = 0
		to_test = []
		i = 0
		if n % 2 == 1:
			mid = (n + 1) // 2 % k
			tot_switch += length - Counter(s[(mid - 1) % k:n:k]).most_common(1)[0][1]
			for i in range(1, (k - 1) // 2 + 1):
				to_test.append(((mid - i - 1) % k, (mid + i - 1) % k))
		else:
			mid = n // 2 % k
			for i in range(k // 2):
				to_test.append(((mid - i - 1) % k, (mid + i + 1 - 1) % k))
		if n % 2 == 0 and k % 2 == 1:
			tot_switch += length - Counter(s[(mid - i - 1 - 1) % k:n:k]).most_common(1)[0][1]
		for (m1, m2) in to_test:
			a = Counter(s[m1:n:k])
			a.update(s[m2:n:k])
			tot_switch += 2 * length - a.most_common(1)[0][1]
		print(tot_switch)
prog()


from math import *
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	d = []
	for i in range(ceil(n / 2)):
		tmp = []
		for j in range(26):
			tmp.append(0)
		d.append(tmp)
	for i in range(n):
		x = i % k
		if n % 2 == 0:
			if x >= k // 2:
				x = k - x - 1
		elif x > k // 2:
			x = k - x - 1
		pos = ord(s[i]) - 97
		d[x][pos] += 1
	ans = n
	for i in range(len(d)):
		ans -= max(d[i])
	print(ans)


import math
from decimal import *
import random
mod = int(1000000000.0) + 7
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = str(input())
	ans = 0
	for i in range(k // 2):
		mrk = {}
		for j in range(0, n, k):
			if s[j + i] not in mrk:
				mrk[s[j + i]] = 0
			if s[j + k - i - 1] not in mrk:
				mrk[s[j + k - i - 1]] = 0
			mrk[s[j + i]] += 1
			mrk[s[j + k - i - 1]] += 1
		ans += 2 * (n // k) - max(mrk.values())
	if k % 2 == 1:
		mrk = {}
		for i in range(0, n, k):
			if s[i + k // 2] not in mrk:
				mrk[s[i + k // 2]] = 0
			mrk[s[i + k // 2]] += 1
		ans += n // k - max(mrk.values())
	print(ans)


import sys

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

def iinput():
	return int(input())

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

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

def main():

	def pro(a, b, x, x1, x2):
		if (a == -2 * b or x != x1 or x != x2 or (b == 0)) and x1 <= x - a + b <= x2:
			return True
		return False
	(n, k) = rinput()
	s = list(input())
	h = n // k
	res = 0
	f = (k + 1) // 2
	fl = 0
	if k % 2:
		fl = 1
	f = k // 2 + fl
	for i in range(f):
		q = dict()
		w = 0
		for o in range(h):
			if s[k * (o + 1) - i - 1] in q:
				q[s[k * (o + 1) - i - 1]] += 1
			else:
				q[s[k * (o + 1) - i - 1]] = 1
			if s[i + k * o] in q:
				q[s[i + k * o]] += 1
			else:
				q[s[i + k * o]] = 1
		for o in q:
			w = max(w, q[o])
		if fl and i + 1 == f:
			res += h - w // 2
		else:
			res += 2 * h - w
	print(res)
for i in range(iinput()):
	main()


def solve(n, k, s):
	i = 0
	j = k - 1
	ans = 0
	while i <= j:
		fre = [0 for i in range(0, 26)]
		for ind in range(i, len(s), k):
			fre[ord(s[ind]) - 97] += 1
		if i != j:
			for ind in range(j, len(s), k):
				fre[ord(s[ind]) - 97] += 1
		ans += sum(fre) - max(fre)
		i += 1
		j -= 1
	print(ans)
t = int(input())
for i in range(0, t):
	inp = list(map(lambda x: int(x), input().split()))
	s = input()
	solve(inp[0], inp[1], s)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	arr = input()
	s = 0
	for i in range(k):
		d = {}
		count = 0
		for j in range(n // k):
			x = i + j * k
			if arr[x] in d:
				d[arr[x]] += 1
			else:
				d[arr[x]] = 1
			if arr[n - x - 1] in d:
				d[arr[n - x - 1]] += 1
			else:
				d[arr[n - x - 1]] = 1
			count += 2
		max1 = max(d.values())
		s += count - max1
	print(s // 2)


import sys, math
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())
t = inp()
for i in range(t):
	(n, k) = invr()
	str = insr()
	res = 0
	length = math.ceil(k / 2)
	for j in range(length):
		pos_count = {}
		total = 0
		max = 0
		for i in range(0, n, k):
			front_index = i + j
			total += 1
			cur_val = pos_count.get(str[front_index], 0) + 1
			pos_count[str[front_index]] = cur_val
			if cur_val > max:
				max = cur_val
			if k % 2 != 1 or j != length - 1:
				back_index = i + k - j - 1
				total += 1
				cur_val = pos_count.get(str[back_index], 0) + 1
				pos_count[str[back_index]] = cur_val
				if cur_val > max:
					max = cur_val
		res += total - max
	print(res)


from sys import stdin, stdout
from collections import defaultdict, Counter
from functools import lru_cache
from math import gcd, floor, ceil

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

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

def istr():
	return stdin.readline().strip()

def f(s):
	(i, j) = (0, len(s) - 1)
	c = 0
	L = list(s)
	while i < j:
		if s[i] != s[j]:
			c += 1
			L[i] = L[j]
		i += 1
		j -= 1
	return (c, ''.join(L))
t = iint()
for _x in range(t):
	(n, k) = ilist()
	s = istr()
	res = float('inf')
	mem = [[0] * k for _x in range(26)]
	for i in range(0, n, k):
		for j in range(i, i + k):
			c = ord(s[j]) - ord('a')
			mem[c][j % k] += 1
	(i, j) = (0, k - 1)
	ans = 0
	total = n // k
	while i <= j:
		mini = float('inf')
		for x in range(26):
			if i != j:
				mini = min(mini, 2 * total - mem[x][i] - mem[x][j])
			else:
				mini = min(mini, total - mem[x][i])
		ans += mini
		i += 1
		j -= 1
	print(ans)


o = int(input())
for _ in range(o):
	(n, k) = list(map(int, input().split(' ')))
	s = input()
	ans = 0
	count = 2 * n // k
	for i in range(k):
		j = k - 1 - i
		if j <= i:
			break
		else:
			dic = {}
			for t in range(i, n, k):
				x = s[t]
				if x in dic:
					dic[x] += 1
				else:
					dic[x] = 1
			for t in range(j, n, k):
				x = s[t]
				if x in dic:
					dic[x] += 1
				else:
					dic[x] = 1
			MAX = 0
			temp = list(dic.keys())
			for i in temp:
				MAX = max(MAX, dic[i])
			ans += count - MAX
	if k % 2 == 1:
		i = (k - 1) // 2
		dic = {}
		for t in range(i, n, k):
			x = s[t]
			if x in dic:
				dic[x] += 1
			else:
				dic[x] = 1
		MAX = 0
		temp = list(dic.keys())
		for i in temp:
			MAX = max(MAX, dic[i])
		count = n // k
		ans += count - MAX
	print(ans)


import sys
from collections import defaultdict

def solve(n, k, s):

	def find(x):
		if UF[x] != x:
			UF[x] = find(UF[x])
		return UF[x]

	def union(x, y):
		UF[find(y)] = UF[find(x)]
		return
	UF = {x: x for x in range(n)}
	for i in range(n):
		if i + k < n:
			union(i, i + k)
		union(i, n - 1 - i)
	ctr = {}
	for i in range(n):
		p = UF[i]
		if p not in ctr:
			ctr[p] = defaultdict(int)
		ctr[p][s[i]] += 1
	ans = 0
	for p in ctr:
		ans += sum(ctr[p].values()) - max(ctr[p].values())
	return ans
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	print(solve(n, k, s))


import math
t = int(input())
for j in range(t):
	(n, k) = list(map(int, input().split()))
	txt = str(input())
	counts = [[0] * 28 for _ in range(math.ceil(k / 2))]
	BASE = 97
	for (i, s) in enumerate(txt):
		r = min(i % k, k - 1 - i % k)
		counts[r][ord(s) - BASE] += 1
	changes = 0
	for c in counts:
		changes += sum(c) - max(c)
	print(changes)


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

def gift():
	for _ in range(t):
		(n, k) = list(map(int, input().split()))
		string = input()
		res = []
		for i in range(k // 2 + k % 2):
			res.append({})
		for i in range(n // k):
			for j in range(k // 2 + k % 2):
				freq = res[j].get(string[i * k + j], 0)
				res[j][string[i * k + j]] = freq + 1
				if k % 2 != 0 and j == k // 2 + k % 2 - 1:
					continue
				else:
					freq = res[j].get(string[(i + 1) * k - j - 1], 0)
					res[j][string[(i + 1) * k - j - 1]] = freq + 1
		ans = 0
		for i in range(k // 2 + k % 2):
			each = res[i]
			maxval = each[max(each, key=each.get)]
			if k % 2 != 0 and i == k // 2 + k % 2 - 1:
				ans += n // k - maxval
			else:
				ans += n // k * 2 - maxval
		yield ans
t = int(input())
ans = gift()
print(*ans, sep='\n')


from collections import Counter
import sys
input = lambda : sys.stdin.readline().rstrip()

def solve(w, k):
	res = len(w)
	c = [[0 for i in range(26)] for _ in range(k + 2)]
	for i in range(1, 1 + n):
		l = (i - 1) % k + 1
		if k - l + 1 < l:
			l = k - l + 1
		c[l][ord(w[i - 1]) - ord('a')] += 1
	for i in range(1, (k + 1 >> 1) + 1):
		d = 0
		for ch in range(26):
			d = max(d, c[i][ch])
		res -= d
	return res
tests = int(input())
for test in range(1, tests + 1):
	(n, k) = map(int, input().split())
	A = input()
	print(solve(A, k))


def main():
	(n, k) = map(int, input().split())
	s = input()
	dct = dict()
	b = 0
	if k % 2 == 1:
		b = 1
	for i in range(k // 2 + b):
		dct[i] = dict()
	for i in range(0, k // 2 + b):
		for r in range(0, n // k):
			if not s[k * r + i] in dct[i]:
				dct[i][s[k * r + i]] = 1
			else:
				dct[i][s[k * r + i]] += 1
	m = 0
	for i in range(k - 1, k // 2 + b - 1, -1):
		for r in range(0, n // k):
			if not s[k * r + i] in dct[m]:
				dct[m][s[k * r + i]] = 1
			else:
				dct[m][s[k * r + i]] += 1
		m += 1
	sm = 0
	lst = [0 for _ in range(k)]
	for i in dct:
		sm += dct[i][max(dct[i], key=lambda x: dct[i][x])]
	print(n - sm)
t = int(input())
for i in range(t):
	main()


import math

def solve(n: int, k: int, s: str) -> str:
	mini_pal = ''
	for i in range(math.ceil(k / 2)):
		freq = {}
		for j in range(i, n, k):
			if s[j] in freq:
				freq[s[j]] += 1
			else:
				freq[s[j]] = 1
			m = j + k - 1 - 2 * i
			if m < n:
				if s[m] in freq:
					freq[s[m]] += 1
				else:
					freq[s[m]] = 1
		mini_pal += max(freq, key=lambda x: freq[x])
	if k % 2:
		mini_pal += mini_pal[:-1][::-1]
	else:
		mini_pal += mini_pal[::-1]
	ans = 0
	for i in range(n):
		if s[i] != mini_pal[i % k]:
			ans += 1
	return ans
test_cases = int(input())
for _ in range(test_cases):
	(n, k) = list(map(int, input().split()))
	s = input()
	print(solve(n, k, s))


from collections import Counter
import bisect
import sys
input = sys.stdin.readline

def solve(n, k, s):
	ls = list(s)
	l = n // k
	ans = 0
	for i in range(k // 2):
		tmp = ls[i::k]
		tmp = tmp + ls[n - 1 - i::-k]
		cmax = Counter(tmp).most_common(1)[0][1]
		ans += 2 * l - cmax
	if k % 2 == 1:
		tmp = ls[k // 2::k]
		cmax = Counter(tmp).most_common(1)[0][1]
		ans += l - cmax
	print(ans)

def main():
	t = int(input())
	for i in range(t):
		(n, k) = map(int, input().split())
		s = input().rstrip()
		solve(n, k, s)
main()


import sys, os, io
from math import log, gcd, ceil
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop
import math

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

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

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def si():
	return input()

def prefix_sum(arr):
	r = [0] * (len(arr) + 1)
	for (i, el) in enumerate(arr):
		r[i + 1] = r[i] + el
	return r

def divideCeil(n, x):
	if n % x == 0:
		return n // x
	return n // x + 1

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

def li():
	return list(map(int, input().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')
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
t = int(input())
for _ in range(t):
	(n, k) = li()
	s = si()
	done = [0] * n
	res = 0
	for i in range(k):
		if done[i] == 0:
			m = i
			arr = [0] * 26
			while m < n and done[m] == 0:
				arr[ord(s[m]) - 97] += 1
				done[m] = 1
				m += k
			m = n - i - 1
			while m >= 0 and done[m] == 0:
				done[m] = 1
				arr[ord(s[m]) - 97] += 1
				m -= k
			m = max(arr)
			res += sum(arr) - m
	print(res)


from sys import stdin, stdout
from math import *
input = stdin.readline
for __ in range(int(input())):
	(n, m) = map(int, input().split())
	s = input()
	h = [[0] * 26 for i in range(m)]
	for i in range(26):
		for j in range(n):
			if ord(s[j]) - 97 != i:
				h[j % m][i] += 1
	ans = 0
	for i in range(ceil(m / 2)):
		a = inf
		for j in range(26):
			if i == ceil(m / 2) - 1 and m % 2 == 1 and (a > h[i][j]):
				a = h[i][j]
			elif h[i][j] + h[-(i + 1)][j] < a:
				a = h[i][j] + h[-(i + 1)][j]
		ans += a
	print(str(ans))


def solve(string, n, k):
	lenper = n // k
	iterind = k // 2
	ans = 0
	for ind in range(iterind):
		voc = {}
		for i in range(ind, n, k):
			if string[i] not in voc:
				voc[string[i]] = 1
			else:
				voc[string[i]] += 1
		for i in range(k - ind - 1, n, k):
			if string[i] not in voc:
				voc[string[i]] = 1
			else:
				voc[string[i]] += 1
		if len(voc) != 1:
			pop_sym = max(voc.values())
			ans += 2 * lenper - pop_sym
	if k % 2 == 1:
		voc = {}
		for i in range(iterind, n, k):
			if string[i] not in voc:
				voc[string[i]] = 1
			else:
				voc[string[i]] += 1
		if len(voc) != 1:
			pop_sym = max(voc.values())
			ans += lenper - pop_sym
	print(ans)
for _ in range(int(input())):
	(n, k) = [int(x) for x in input().split()]
	s = input()
	solve(s, n, k)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = [i for i in input()]
	mas = [s[i * k:i * k + k] for i in range(n // k)]
	num = [0] * 27
	ans = 0
	for i in range(k):
		for j in range(n // k):
			num[ord(mas[j][i]) - 97] += 1
			num[ord(mas[j][k - 1 - i]) - 97] += 1
		ans += 2 * n // k - max(num)
		num = [0] * 27
	print(ans // 2)


from sys import stdin
t = int(input())
for _ in range(t):
	(n, k) = map(int, stdin.readline().split())
	s = stdin.readline().strip()
	ans = 0
	vis = [False for i in range(n)]
	for i in range(n):
		if vis[i]:
			continue
		check = set()
		for j in range(i, n, k):
			check.add(j)
		new = set()
		for j in check:
			new.add(n - 1 - j)
		for j in new:
			check.add(j)
		cnt = [0 for i in range(26)]
		for j in check:
			cnt[ord(s[j]) - 97] += 1
			vis[j] = True
		ans += len(check) - max(cnt)
	print(ans)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	z = 0
	cnt = [[0] * 26 for i in range(k)]
	for i in range(n):
		c = s[i]
		cnt[i % k][ord(c) - ord('a')] += 1
	cnts = [sum(x) for x in cnt]
	all = 0
	for j in range(k // 2):
		mi = 10 ** 9
		for x in range(26):
			q = cnts[j] - cnt[j][x] + cnts[k - j - 1] - cnt[k - j - 1][x]
			mi = min(mi, q)
		all += mi
	if k % 2:
		mi = 10 ** 9
		for x in range(26):
			q = cnts[k // 2] - cnt[k // 2][x]
			mi = min(mi, q)
		all += mi
	print(all)


from sys import stdin, stdout
from math import *
input = stdin.readline
for __ in range(int(input())):
	(n, m) = map(int, input().split())
	s = input()
	ans = 0
	for i in range(m):
		if i < ceil(m / 2):
			a = [0] * 26
			for al in range(26):
				for j in range(n // m):
					if ord(s[j * m + i]) != al + 97:
						a[al] += 1
					if j * m + i != j * m + (m - i - 1) and ord(s[j * m + (m - i - 1)]) != al + 97:
						a[al] += 1
			ans += min(a)
	print(ans)


from math import ceil
from collections import defaultdict
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	l = ceil(k / 2)
	s = input()
	(list_of_dicts, list_of_maxs) = ([defaultdict(int) for _ in range(l)], [0] * l)
	for i in range(n):
		if i % k <= l - 1:
			list_of_dicts[i % k][s[i]] += 1
			if list_of_dicts[i % k][s[i]] > list_of_maxs[i % k]:
				list_of_maxs[i % k] = list_of_dicts[i % k][s[i]]
		else:
			list_of_dicts[k - 1 - i % k][s[i]] += 1
			if list_of_dicts[k - 1 - i % k][s[i]] > list_of_maxs[k - 1 - i % k]:
				list_of_maxs[k - 1 - i % k] = list_of_dicts[k - 1 - i % k][s[i]]
	result = 0
	for i in range(l):
		if i == l - 1 and k % 2 != 0:
			result += int(n / k) - list_of_maxs[i]
		else:
			result += 2 * int(n / k) - list_of_maxs[i]
	print(result)


from collections import Counter
t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	s = input().strip()
	m = k
	q = 0
	for j in range(m // 2):
		p = []
		c = 0
		for v in range(j, n, k):
			p.append(s[v])
			c = c + 1
		for v in range(m - j - 1, n, k):
			p.append(s[v])
			c = c + 1
		d = Counter(p)
		q = q + c - max(d.values())
	if m % 2 != 0:
		j = m // 2
		p = []
		c = 0
		for v in range(j, n, k):
			p.append(s[v])
			c = c + 1
		d = Counter(p)
		q = q + c - max(d.values())
	print(q)


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	s = list(input())
	ans = 0
	for i in range(k // 2):
		cnt = [0 for i in range(26)]
		st = 0
		while st + k - 1 < n:
			i1 = st + i
			i2 = st + (k - 1 - i)
			cnt[ord(s[i1]) - ord('a')] += 1
			cnt[ord(s[i2]) - ord('a')] += 1
			st += k
		req = 2 * (n // k)
		mx = max(cnt)
		ans += req - mx
	if k % 2 == 1:
		cnt2 = [0 for i in range(26)]
		for i in range(k // 2, n, k):
			cnt2[ord(s[i]) - ord('a')] += 1
		req = n // k
		mx = max(cnt2)
		ans += req - mx
	print(ans)


import sys
import math

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

def idata():
	return [int(x) for x in ii().split()]

def binary_search(array, x):
	(left, right) = (-1, len(array))
	while left + 1 != right:
		middle = (left + right) // 2
		if array[middle] >= x:
			right = middle
		elif array[middle] < x:
			left = middle
	return right

def c_mod(n1, k1, mod1):
	num = den = 1
	for i in range(n1 - k1):
		num = num * (n1 - i) % mod1
		den = den * (i + 1) % mod1
	return num * pow(den, mod1 - 2, mod1) % mod1

def c_k(n1, k1):
	num = den = 1
	for i in range(n1 - k1):
		num = num * (n1 - i)
		den = den * (i + 1)
	return num // den

def v_sistemu(x, k):
	x = int(x)
	z = ''
	while x:
		z += str(x % k)
		x //= k
	return z[::-1]

def iz_sistemi(x, k):
	x = str(x)[::-1]
	ans = 0
	for i in range(len(x)):
		ans += int(x[i]) * pow(k, i)
	return ans

def solve_of_problem():
	(n, k) = idata()
	s = ii()
	count = 0
	z = n // k
	flag = (k + 1) // 2
	for i in range(flag):
		di1 = dict()
		for j in range(z):
			if s[(j + 1) * k - i - 1] not in di1:
				di1[s[(j + 1) * k - i - 1]] = 1
			else:
				di1[s[(j + 1) * k - i - 1]] += 1
			if s[j * k + i] not in di1:
				di1[s[j * k + i]] = 1
			else:
				di1[s[j * k + i]] += 1
		if k % 2 == 0 or flag != i + 1:
			count += 2 * z - max(di1.values())
		else:
			count += z - max(di1.values()) // 2
	print(count)
	return
for ____ in range(int(ii())):
	solve_of_problem()


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	s = input()
	d = {}
	i = 0
	while i < n:
		q = s[i:i + k]
		for j in range(k // 2):
			if j not in d:
				d[j] = {}
			if q[j] not in d[j]:
				d[j][q[j]] = 0
			if q[-j - 1] not in d[j]:
				d[j][q[-j - 1]] = 0
			d[j][q[-j - 1]] = d[j][q[-j - 1]] + 1
			d[j][q[j]] = d[j][q[j]] + 1
		if k % 2 == 1:
			if k // 2 not in d:
				d[k // 2] = {}
			if q[k // 2] not in d[k // 2]:
				d[k // 2][q[k // 2]] = 0
			d[k // 2][q[k // 2]] = d[k // 2][q[k // 2]] + 1
		i = i + k
	ans = 0
	for i in d:
		m = 0
		for s in d[i]:
			if d[i][s] > m:
				m = d[i][s]
			ans = ans + d[i][s]
		ans = ans - m
	print(ans)


t = int(input())
for _ in range(t):
	(n, k) = [int(x) for x in input().strip().split()]
	s = input()
	mat = [[0 for i in range(26)] for j in range(k)]
	i = 0
	while i < n:
		j = 0
		while j < k:
			char = ord(s[i + j]) - 97
			mat[j][char] += 1
			j += 1
		i += k

	def fmax(mat, l, h):
		res = 10 ** 20
		for i in range(26):
			temp = n // k - mat[l][i] + n // k - mat[h][i]
			res = min(res, temp)
		if l == h:
			res = res // 2
		return res
	res = 0
	l = 0
	h = k - 1
	while l <= h:
		if l == h:
			res += fmax(mat, l, l)
			break
		res += fmax(mat, l, h)
		l += 1
		h -= 1
	print(res)


from collections import defaultdict
xxx = 5
for i in range(10000):
	xxx += 1
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	i = 0
	j = n - 1
	seti = set()
	ans = 0
	ha = 1
	ka = n - 1
	for x in range(n):
		hash = defaultdict(int)
		maxi = 0
		i = x
		j = ka
		ka -= 1
		if i not in seti:
			while i < n:
				if i not in seti:
					hash[s[i]] += 1
				if hash[s[i]] > maxi:
					maxi = hash[s[i]]
				seti.add(i)
				i += k
		if j not in seti:
			while j >= 0:
				if j not in seti:
					hash[s[j]] += 1
				if hash[s[j]] > maxi:
					maxi = hash[s[j]]
				seti.add(j)
				j -= k
		len = 0
		for v in hash:
			len += hash[v]
		ans += len - maxi
	print(ans)


debug = 0
if debug:
	print('DEBUG MODE ON')
	f = None

	def new_input():
		global f
		if f == None:
			f = open('input.txt')
		return f.readline().strip()
	input = new_input

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

def readint():
	return int(input())

def readarr():
	return [int(x) for x in input().split()]

def readmat(rows):
	return [readarr() for _ in range(rows)]
from collections import Counter as counter
letters = {chr(x): 0 for x in range(97, 123)}
for _ in readrange():
	(n, size) = readarr()
	words = n // size
	s = input()
	mods = 0
	for offset in range((size + 1) // 2):
		for l in letters:
			letters[l] = 0
		letter = 'a'
		for w in range(words):
			c1 = w * size + offset
			c2 = (w + 1) * size - offset - 1
			letters[s[c1]] += 1
			letters[s[c2]] += 1
			if letters[s[c1]] > letters[letter]:
				letter = s[c1]
			if letters[s[c2]] > letters[letter]:
				letter = s[c2]
		inc = words * 2 - letters[letter]
		mods += inc if offset < size // 2 else inc // 2
	print(mods)


import sys
from collections import Counter
from copy import deepcopy
for t in range(int(sys.stdin.readline().strip())):
	(n, k) = [int(j) for j in sys.stdin.readline().split()]
	s = sys.stdin.readline().strip()
	s = list(s)
	ct = 0
	m = set()
	for i in range(k):
		x = set()
		for j in range(n // k):
			if i + k * j not in m:
				x.add(i + k * j)
				m.add(i + k * j)
			if n - i - k * j - 1 not in m:
				x.add(n - i - k * j - 1)
				m.add(n - i - k * j - 1)
		l = [s[m] for m in x]
		d = Counter(l)
		if len(d):
			mx = max(d.values())
			ct += len(x) - mx
	print(ct)


import sys
from collections import Counter

def input():
	return sys.stdin.readline()[:-1]
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	c = [Counter() for _ in range((k + 1) // 2)]
	for i in range(n):
		c[min(i % k, k - 1 - i % k)][s[i]] += 1
	ans = 0
	for d in c:
		ans += sum(d.values()) - max(d.values())
	print(ans)


t = int(input())
for _ in range(t):
	(n, k) = [int(i) for i in input().split()]
	s = input()
	out = 0
	for i in range(k // 2):
		d = dict()
		for j in range(n // k):
			c1 = s[i + j * k]
			c2 = s[k - i - 1 + j * k]
			d[c1] = d[c1] + 1 if c1 in d else 1
			d[c2] = d[c2] + 1 if c2 in d else 1
		out += 2 * n // k - max(d.values())
	if k % 2 == 1:
		d = dict()
		for j in range(n // k):
			c1 = s[j * k + k // 2]
			d[c1] = d[c1] + 1 if c1 in d else 1
		out += n // k - max(d.values())
	print(out)


import sys
input = sys.stdin.readline
inputi = lambda : map(int, input().split())
from math import *
from bisect import *
from functools import reduce, lru_cache
from collections import Counter, defaultdict
for _ in range(int(input())):
	(n, k) = inputi()
	S = input()
	res = 0
	b = n // k
	k_2 = (k - 1) // 2
	for i in range(k_2 + 1):
		t = 0
		C = [0] * 26
		for j in range(i, n, k):
			C[ord(S[j]) - 97] += 1
			t += 1
		if not i == k_2 or not k % 2:
			for j in range(k - 1 - i, n, k):
				C[ord(S[j]) - 97] += 1
				t += 1
		res += t - max(C)
	print(res)


import string
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	ans = 0
	c = 2 * (n // k)
	for i in range(k // 2 + (k % 2 == 1)):
		dct = dict.fromkeys(string.ascii_lowercase, 0)
		for x in range(n // k):
			dct[s[i + x * k]] += 1
			dct[s[k - 1 - i + x * k]] += 1
			if k - 1 - i == i:
				dct[s[k - 1 - i + x * k]] -= 1
				c = n // k
		ans += c - dct[max(dct, key=dct.get)]
	print(ans)


from sys import stdin, gettrace
if not gettrace():

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

def main():

	def solve():
		(n, k) = map(int, input().split())
		ss = input()
		counts = [[0] * 26 for _ in range((k + 1) // 2)]
		for i in range(0, n, k):
			for j in range((k + 1) // 2):
				counts[j][ord(ss[i + j]) - ord('a')] += 1
			for j in range(k // 2):
				counts[j][ord(ss[i + k - j - 1]) - ord('a')] += 1
		res = 0
		for i in range(k // 2):
			res += 2 * n // k - max(counts[i])
		if k % 2 == 1:
			res += n // k - max(counts[-1])
		print(res)
	q = int(input())
	for _ in range(q):
		solve()
main()


from collections import defaultdict
from math import ceil
from sys import stdin

def solve(st, l, d):
	arr = []
	for i in range(0, l, d):
		arr.append(st[i:i + d])
	nd = ceil(d / 2)
	ans = 0
	for i in range(nd):
		m = defaultdict(int)
		for j in range(l // d):
			m[arr[j][i]] += 1
			if i != d - (i + 1):
				m[arr[j][d - (i + 1)]] += 1
		ans += sum(m.values()) - max(m.values())
	return ans
t = int(stdin.readline())
lans = ''
for l in range(t):
	(l, d) = map(int, stdin.readline().split())
	st = input()
	lans += str(solve(st, l, d))
	lans += '\n'
print(lans)


import sys
input = sys.stdin.readline
from math import *
mod = 10 ** 9 + 7

def bo(i):
	return ord(i) - ord('a')
prime = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
t = int(input())

def find(i, a):
	if i == a[i]:
		return a[i]
	else:
		a[i] = find(a[i], a)
		return a[i]

def union(x, y, a):
	xs = find(x, a)
	ys = find(y, a)
	if xs == ys:
		return
	if rank[ys] > rank[xs]:
		(xs, ys) = (ys, xs)
	a[ys] = xs
	rank[xs] += 1
while t > 0:
	t -= 1
	(n, k) = map(int, input().split())
	s = list(input().rstrip())
	a = [i for i in range(n)]
	rank = [0 for i in range(n)]
	for i in range(k):
		for j in range(i + k, n, k):
			union(i, j, a)
	for i in range(n // 2):
		union(i, n - i - 1, a)
	d = {}
	for i in range(n):
		a[i] = find(i, a)
	d = [[] for i in range(n + 1)]
	for i in range(n):
		d[a[i]].append(i)
	c = 0
	for i in range(len(d)):
		if len(d[i]) == 0:
			continue
		p = [0 for i in range(26)]
		maxi = 0
		for j in d[i]:
			p[bo(s[j])] += 1
			if p[bo(s[j])] > maxi:
				maxi = p[bo(s[j])]
		c += len(d[i]) - maxi
	print(c)


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

def to_arr(s):
	a = [0] * len(s)
	for i in range(len(s)):
		a[i] = alph.index(s[i])
	return a
for _ in range(q):
	(n, k) = map(int, input().split())
	s = to_arr(input())
	ans = 0
	for i in range(k // 2):
		r = [0] * 26
		ml = 1
		r[s[i]] += 1
		r[s[k - i - 1]] += 1
		if r[s[k - i - 1]] > ml:
			ml = r[s[k - i - 1]]
		for m in range(1, n // k):
			r[s[i + m * k]] += 1
			if r[s[i + m * k]] > ml:
				ml = r[s[i + m * k]]
			r[s[k - i - 1 + m * k]] += 1
			if r[s[k - i - 1 + m * k]] > ml:
				ml = r[s[k - i - 1 + m * k]]
		ans += n // k * 2 - ml
	if k % 2:
		r = [0] * 26
		ml = 1
		r[s[k // 2]] += 1
		for m in range(1, n // k):
			r[s[k // 2 + m * k]] += 1
			if r[s[k // 2 + m * k]] > ml:
				ml = r[s[k // 2 + m * k]]
		ans += n // k - ml
	print(ans)


from bisect import bisect_left as bl, bisect_right as br, insort
import sys
import heapq
from math import *
from collections import defaultdict as dd, deque

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

def mdata():
	return map(int, data().split())
for i in range(int(data())):
	(n, k) = mdata()
	s = data()
	ans = 0
	for i in range(k // 2 + k % 2):
		d = dd(int)
		cnt = 0
		for j in range(i, n, k):
			d[s[j]] += 1
			cnt += 1
		if k % 2 == 1 and i == k // 2 + k % 2 - 1:
			m = max(d.values())
			ans += cnt - m
			continue
		for j in range(k - i - 1, n, k):
			d[s[j]] += 1
			cnt += 1
		m = max(d.values())
		ans += cnt - m
	print(ans)


from math import *
from sys import stdin, stdout, setrecursionlimit
from collections import Counter
input = lambda : stdin.readline().strip()
print = stdout.write

def most_frequent(List):
	occurence_count = Counter(List)
	return dict(occurence_count)[occurence_count.most_common(1)[0][0]]
t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	s = input()
	f = []
	for i in range(k):
		x = []
		for j in range(i, len(s), k):
			x.append(s[j])
		f.append(x)
	ans = 0
	m = len(f)
	v = n // k * 2
	en = int(ceil(m / 2))
	for i in range(en):
		u = most_frequent(f[i] + f[m - i - 1])
		if k % 2:
			if i == en - 1:
				ans += (v - u) // 2
			else:
				ans += v - u
		else:
			ans += v - u
	print(str(ans) + '\n')


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	a = input()
	c = 0
	for i in range(k // 2):
		d = dict()
		for j in range(i, n, k):
			if a[j] in d:
				d[a[j]] += 1
			else:
				d[a[j]] = 1
		for j in range(k - i - 1, n, k):
			if a[j] in d:
				d[a[j]] += 1
			else:
				d[a[j]] = 1
		maxi = 0
		for i in d:
			if d[i] > maxi:
				maxi = d[i]
				p = i
		c += maxi
	if k % 2 == 1:
		d = dict()
		for j in range(k // 2, n, k):
			if a[j] in d:
				d[a[j]] += 1
			else:
				d[a[j]] = 1
		maxi = 0
		for i in d:
			if d[i] > maxi:
				maxi = d[i]
				p = i
		c += maxi
	print(n - c)


import sys, bisect
from math import gcd
input = sys.stdin.readline

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

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

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

def invr():
	return map(int, input().rstrip().split())
prime_list = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]
t = inp()
for _ in range(t):
	(n, k) = invr()
	s = input().strip()
	count = dict()
	s_list = []
	cnt = 0
	(start, end) = (0, k)
	while cnt < n // k:
		s_list.append(s[start:end])
		start = end
		end = end + k
		cnt += 1
	ans = ['' for i in range(k)]
	cost = 0
	need = k // 2
	for i in range(need):
		count[i] = dict()
		for l in s_list:
			if l[i] not in count[i]:
				count[i][l[i]] = 1
			else:
				count[i][l[i]] += 1
			if l[k - i - 1] not in count[i]:
				count[i][l[k - i - 1]] = 1
			else:
				count[i][l[k - i - 1]] += 1
		big = 0
		bigl = ''
		for item in count[i]:
			if big < count[i][item]:
				big = count[i][item]
				bigl = item
		ans[i] = bigl
		ans[k - i - 1] = bigl
	if k % 2 != 0:
		count[need] = dict()
		for l in s_list:
			if l[need] not in count[need]:
				count[need][l[need]] = 1
			else:
				count[need][l[need]] += 1
		big = 0
		bigl = ''
		for item in count[need]:
			if big < count[need][item]:
				big = count[need][item]
				bigl = item
		ans[need] = bigl
	for l in s_list:
		for (index, le) in enumerate(l):
			if le != ans[index]:
				cost += 1
	print(cost)


import sys
readline = sys.stdin.readline
T = int(readline())
Ans = [None] * T
als = 26
for qu in range(T):
	(N, K) = map(int, readline().split())
	S = list(map(lambda x: ord(x) - 97, readline().strip()))
	R = N // K
	cost = [[2 * R] * als for _ in range(K // 2)]
	if K & 1:
		cost.append([R] * als)
	for i in range(N):
		pidx = i % K
		pidx = min(pidx, K - 1 - pidx)
		cost[pidx][S[i]] -= 1
	Ans[qu] = sum(map(min, cost))
print('\n'.join(map(str, Ans)))


import sys
input = sys.stdin.readline
from collections import defaultdict as dd
t = int(input())
while t:
	(n, k) = map(int, input().split())
	s = input()
	vis = [0] * n
	res = 0
	if k % 2 == 0:
		hk = k // 2
	else:
		hk = k // 2 + 1
	for i in range(hk):
		kk = 0
		d = dd(int)
		while i + kk < n:
			if vis[i + kk] == 0:
				d[s[i + kk]] += 1
				vis[i + kk] = 1
			else:
				break
			kk += k
		kk = 0
		while k - 1 - i + kk < n:
			if vis[k - 1 - i + kk] == 0:
				d[s[k - 1 - i + kk]] += 1
				vis[k - 1 - i + kk] = 1
			else:
				break
			kk += k
		su = 0
		mx = -1
		for i in d:
			su += d[i]
			mx = max(mx, d[i])
		res += su - mx
	print(res)
	t -= 1


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	s = input()
	count = 0
	for i in range(k):
		d = dict()
		summ = 0
		for j in range((n - i - 1) // k + 1):
			t = s[i + k * j]
			if t in d:
				d[t] += 1
			else:
				d[t] = 1
			summ += 1
		for j in range((n - i - 1) // k + 1):
			t = s[n - 1 - i - k * j]
			if t in d:
				d[t] += 1
			else:
				d[t] = 1
			summ += 1
		count += summ - max(d.values())
	print(count // 2)


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')
for i1 in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	ans = 0
	for i in range(k // 2):
		d = dict()
		for j in range(n // k):
			if s[i + j * k] not in d:
				d.update({s[i + j * k]: 1})
			else:
				d[s[i + j * k]] += 1
			if s[(j + 1) * k - 1 - i] not in d:
				d.update({s[(j + 1) * k - 1 - i]: 1})
			else:
				d[s[(j + 1) * k - 1 - i]] += 1
		ans += 2 * (n // k) - max(d.values())
	if k % 2 == 1:
		d = dict()
		for j in range(n // k):
			if s[k // 2 + j * k] not in d:
				d.update({s[k // 2 + j * k]: 1})
			else:
				d[s[k // 2 + j * k]] += 1
		ans += n // k - max(d.values())
	print(ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	su = 0
	for x in range(k // 2):
		d = {}
		for i in range(n // k):
			if s[x + k * i] in d:
				d[s[x + k * i]] += 1
			else:
				d[s[x + k * i]] = 1
			if s[k - x - 1 + k * i] in d:
				d[s[k - x - 1 + k * i]] += 1
			else:
				d[s[k - x - 1 + k * i]] = 1
		u = max(d, key=d.get)
		su += int(d.get(u))
	if k % 2 != 0:
		d = {}
		for i in range(n // k):
			if s[k // 2 + k * i] in d:
				d[s[k // 2 + k * i]] += 1
			else:
				d[s[k // 2 + k * i]] = 1
		u = max(d, key=d.get)
		su += int(d.get(u))
	print(n - su)


import math as mt
I = lambda : list(map(int, input().split()))
for tc in range(int(input())):
	(n, k) = I()
	s = list(input())
	ans = 0
	for i in range(k // 2):
		ch = [0] * 26
		for j in range(i, n, k):
			ch[ord(s[j]) - 97] += 1
			ch[ord(s[(j // k + 1) * k - j % k - 1]) - 97] += 1
		ans += sum(ch) - max(ch)
	if k % 2:
		ch = [0] * 26
		for i in range(k // 2, n, k):
			ch[ord(s[i]) - 97] += 1
		ans += sum(ch) - max(ch)
	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):
	values = list(map(int, input().split()))
	n = values[0]
	k = values[1]
	words = [ord(k) - 96 for k in input()]
	moves = 0
	if k % 2 == 0:
		for s in range(k // 2):
			store1 = [0 for i in range(26)]
			start = s
			inc = k
			cv = 0
			counter = 0
			while start + cv * inc <= n - 1:
				store1[words[start + cv * inc] - 1] += 1
				store1[words[k - 1 - start + cv * inc] - 1] += 1
				cv += 1
			a1 = 2 * n // k - max(store1)
			moves += a1
	elif k % 2 == 1:
		for s in range(k // 2 + 1):
			store1 = [0 for i in range(26)]
			start = s
			inc = k
			cv = 0
			counter = 0
			if start != k // 2:
				while start + cv * inc <= n - 1:
					store1[words[start + cv * inc] - 1] += 1
					store1[words[k - 1 - start + cv * inc] - 1] += 1
					cv += 1
				a1 = 2 * n // k - max(store1)
				moves += a1
			elif start == k // 2:
				while start + cv * inc <= n - 1:
					store1[words[start + cv * inc] - 1] += 1
					cv += 1
				a1 = n // k - max(store1)
				moves += a1
	print(moves)