#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
char s[maxn];
int res[maxn][26];
char need[maxn];

int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		int n, m;
		scanf("%d %d",&n,&m);
		for(int i = 0 ; i <= m ; i++){
			for(int j = 0 ; j < 26 ; j++)res[i][j] = 0;
		}
		scanf(" %s", s + 1);
		int cnt = n / m;
		for(int i = 0 ; i < cnt ; i++){
			for(int j = 1 ; j <= m ; j++){
				if(j <= (m + 1) / 2){
					res[j][s[i * m + j] - 'a']++;
				}
				else{
					res[m + 1 - j][s[i * m + j] - 'a']++;
				}
			}
		}

		int tmp;
		for(int i = 1 ; i <= m ; i++){
			int maxx = 0, num = 0;
			if(i <= (m + 1) / 2)tmp = i;
			else tmp = m + 1 - i;
			for(int j = 0 ; j < 26 ; j++){
				if(res[tmp][j] > maxx){
					maxx = res[tmp][j];
					num = j;
				}
			}
			need[i] = num + 'a';
		}
		int ans = 0;
		for(int i = 0 ; i < cnt ; i++){
			for(int j = 1 ; j <= m ; j++){
				if(s[i * m + j] != need[j])ans++;
			}
		}
		printf("%d\n",ans);
	}
}


#include<stdio.h>
#include<string>
#include<string.h>
using namespace std;
const int maxn = 2e5 + 5;
char s[maxn];
int res[maxn][26];
char need[maxn];

int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		memset(res, 0, sizeof res);

		int n, m;
		scanf("%d %d",&n,&m);
		scanf(" %s", s + 1);
		int cnt = n / m;
		for(int i = 0 ; i < cnt ; ++i){
			for(int j = 1 ; j <= m ; ++j){
				res[(j <= (m + 1) / 2 ? j : m + 1 - j)][s[i * m + j] - 'a']++;
			}
		}

		int tmp;
		for(int i = 1 ; i <= m ; ++i){
			int maxx = 0, num = 0;
			if(i <= (m + 1) / 2)tmp = i;
			else tmp = m + 1 - i;

			for(int j = 0 ; j < 26 ; ++j){
				if(res[tmp][j] > maxx){
					maxx = res[tmp][j];
					num = j;
				}
			}
			need[i] = num + 'a';
		}
		int ans = 0;
		for(int i = 0 ; i < cnt ; ++i){
			for(int j = 1 ; j <= m ; ++j){
				if(s[i * m + j] != need[j])++ans;
			}
		}
		printf("%d\n",ans);
	}
}


#define CF_630DIV2_C
#ifdef CF_630DIV2_C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXN 1000009
char s[MAXN];
int count[MAXN][30];

//
// <"CF_630DIV2_C.in" >"CF_630DIV2_C.out"
int main()
{
int diff(int p, int q);

int t,n,k,ans;
scanf("%d",&t);
while(t--)
	{
	scanf("%d%d%s",&n,&k,&s);
	//memset(count, 0, sizeof(count));
	for(int i=0; i<n; i++)
		for(int j=0; j<30; j++)
			count[i][j]=0;
	for(int i=0; i<n; i++)
		count[i%k][s[i]-'a']++;

	ans=0;
	for(int p=0,q=k-1;p<=q;p++,q--)
		ans += diff(p,q);
	printf("%d\n",ans);
	}
return 0;
}

int diff(int p, int q)
{
int MAX(int a, int b);
int ret,mx;
ret=mx=0;

if(p != q)
	{
	for(int k=0; k<26; k++)
		{
		ret += count[p][k] + count[q][k];
		mx = MAX(mx, count[p][k]+count[q][k]);
		}
	return ret-mx;
	}
else
	{
	for(int k=0; k<26; k++)
		{
		ret += count[p][k];
		mx = MAX(mx, count[p][k]);
		}
	return ret-mx;
	}
}

int MAX(int a, int b) { return a>b?a:b; }

#endif


#define CF_630DIV2_C
#ifdef CF_630DIV2_C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXN 1000009
char s[MAXN];
int count[MAXN][30];

//
// <"CF_630DIV2_C.in" >"CF_630DIV2_C.out"
int main()
{
int diff(int p, int q);

int t,n,k,ans;
scanf("%d",&t);
while(t--)
	{
	scanf("%d%d%s",&n,&k,&s);
	memset(count, 0, sizeof(count));
	for(int i=0; i<n; i++)
		count[i%k][s[i]-'a']++;

	ans=0;
	int p=0, q=k-1;
	for(;;)
		{
		ans += diff(p,q);
		p++;
		q--;
		if(p>q)
			break;
		}
	printf("%d\n",ans);
	}
return 0;
}

int diff(int p, int q)
{
int MAX(int a, int b);
int ret,mx;
ret=mx=0;

if(p!=q)
	{
	for(int k=0; k<26; k++)
		{
		ret += count[p][k] + count[q][k];
		mx = MAX(mx, count[p][k]+count[q][k]);
		}
	return ret-mx;
	}
else
	{
	for(int k=0; k<26; k++)
		{
		ret += count[p][k];
		mx = MAX(mx, count[p][k]);
		}
	return ret-mx;
	}
}

int MAX(int a, int b) { return a>b?a:b; }

#endif


#define CF_630DIV2_C
#ifdef CF_630DIV2_C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXN 1000009
char s[MAXN];
int count[MAXN][30];

//
// <"CF_630DIV2_C.in" >"CF_630DIV2_C.out"
int main()
{
int diff(int p, int q);

int t,n,k,ans;
scanf("%d",&t);
while(t--)
	{
	scanf("%d%d%s",&n,&k,&s);
	memset(count, 0, sizeof(count));
	for(int i=0; i<n; i++)
		count[i%k][s[i]-'a']++;

	ans=0;
	for(int p=0,q=k-1;p<=q;p++,q--)
		ans += diff(p,q);
	printf("%d\n",ans);
	}
return 0;
}

int diff(int p, int q)
{
int MAX(int a, int b);
int ret,mx;
ret=mx=0;

if(p != q)
	{
	for(int k=0; k<26; k++)
		{
		ret += count[p][k] + count[q][k];
		mx = MAX(mx, count[p][k]+count[q][k]);
		}
	return ret-mx;
	}
else
	{
	for(int k=0; k<26; k++)
		{
		ret += count[p][k];
		mx = MAX(mx, count[p][k]);
		}
	return ret-mx;
	}
}

int MAX(int a, int b) { return a>b?a:b; }

#endif


#define CF_630DIV2_C
#ifdef CF_630DIV2_C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXN 1000009
char s[MAXN];
int count[MAXN][30];

//
// <"CF_630DIV2_C.in" >"CF_630DIV2_C.out"
int main()
{
int diff(int p, int q);

int t,n,k,ans;
scanf("%d",&t);
while(t--)
	{
	scanf("%d%d%s",&n,&k,&s);
	memset(count, 0, sizeof(count));
	for(int i=0; i<n; i++)
		count[i%k][s[i]-'a']++;

	ans=0;
	int p=0, q=k-1;
	for(;;)
		{
		ans += diff(p,q);
		if(p==q || p+1==q)
			break;
		else
			{
			p++;
			q--;
			}
		}
	printf("%d\n",ans);
	}
return 0;
}

int diff(int p, int q)
{
int MAX(int a, int b);
int ret,mx;
ret=mx=0;

if(p!=q)
	{
	for(int k=0; k<26; k++)
		{
		ret += count[p][k] + count[q][k];
		mx = MAX(mx, count[p][k]+count[q][k]);
		}
	return ret-mx;
	}
else
	{
	for(int k=0; k<26; k++)
		{
		ret += count[p][k];
		mx = MAX(mx, count[p][k]);
		}
	return ret-mx;
	}
}

int MAX(int a, int b) { return a>b?a:b; }

#endif


#include<iostream>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<string.h>
using namespace std;
int k,n;
string s;
int sing[26];
int cous[101111][26];
//void addtotrie(const string& ss)
//{
//	int len=ss.size();
//	for(int i=0;i<len;i++)
//	{
//		++cous[i][ss[i]-'a'];
//	}
//}
int main()
{
	cin.tie(0);
    cin.sync_with_stdio(false);
	int T;
	cin>>T;
	while(T--)
	{
		cin>>n>>k;
		string s;
		cin>>s;
		int div=n/k;
		memset(sing,0,sizeof(sing));
		for(int i=0;i<=n;i++)
			for(int j=0;j<26;j++)
				cous[i][j]=0;
//		memset(cous,0,sizeof(cous));
//		root= new node();
		for(int i=1;i<=div;i++)
		{
//			string s1="",s2="";
			for(int j1=(i-1)*k,j2=i*k-1,ii=0;j1<j2;++j1,--j2,++ii)
			{
				++cous[ii][s[j1]-'a'];
				++cous[ii][s[j2]-'a'];
//				s1+=s[j1];
//				s2+=s[j2];
			}
//			cout<<s1<<" "<<s2<<endl;
//			addtotrie(s1);
//			addtotrie(s2);
			if(k%2)
			{
				sing[s[(i-1)*k+k/2]-'a']++;
			}
		}
		int courem=0;
		for(int i=0;i<k/2;i++)
		{
			int max=0,maxj;
			for(int j=0;j<26;j++)
			{
//				cout<<cous[i][j]<<" ";
				if(cous[i][j]>max)
				{
					max=cous[i][j];
					maxj=j;
				}
			}
//			cout<<endl;
			courem+=max;
		}
		if(k%2)
		{
			int max=0,maxj;
			for(int j=0;j<26;j++)
			{
//				cout<<sing[j]<<" ";
				if(sing[j]>max)
				{
					max=sing[j];
				}
			}
//			cout<<endl;
			courem+=max;
		}
		cout<<n-courem<<endl;
	}

}


#include<iostream>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<string.h>
using namespace std;
int k,n;
string s;
int sing[26];
int cous[101111][26];
//void addtotrie(const string& ss)
//{
//	int len=ss.size();
//	for(int i=0;i<len;i++)
//	{
//		++cous[i][ss[i]-'a'];
//	}
//}
int main()
{
	cin.tie(0);
    cin.sync_with_stdio(false);
	int T;
	cin>>T;
	while(T--)
	{
		cin>>n>>k;
		string s;
		cin>>s;
		int div=n/k;
		memset(sing,0,sizeof(sing));
		memset(cous,0,sizeof(cous));
//		root= new node();
		for(int i=1;i<=div;i++)
		{
//			string s1="",s2="";
			for(int j1=(i-1)*k,j2=i*k-1,ii=0;j1<j2;++j1,--j2,++ii)
			{
				++cous[ii][s[j1]-'a'];
				++cous[ii][s[j2]-'a'];
//				s1+=s[j1];
//				s2+=s[j2];
			}
//			cout<<s1<<" "<<s2<<endl;
//			addtotrie(s1);
//			addtotrie(s2);
			if(k%2)
			{
				sing[s[(i-1)*k+k/2]-'a']++;
			}
		}
		int courem=0;
		for(int i=0;i<k/2;i++)
		{
			int max=0,maxj;
			for(int j=0;j<26;j++)
			{
//				cout<<cous[i][j]<<" ";
				if(cous[i][j]>max)
				{
					max=cous[i][j];
					maxj=j;
				}
			}
//			cout<<endl;
			courem+=max;
		}
		if(k%2)
		{
			int max=0,maxj;
			for(int j=0;j<26;j++)
			{
//				cout<<sing[j]<<" ";
				if(sing[j]>max)
				{
					max=sing[j];
				}
			}
//			cout<<endl;
			courem+=max;
		}
		cout<<n-courem<<endl;
	}

}


#include<iostream>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<string.h>
using namespace std;
int k,n;
string s;
int sing[26];
int cous[101111][26];
//void addtotrie(const string& ss)
//{
//	int len=ss.size();
//	for(int i=0;i<len;i++)
//	{
//		++cous[i][ss[i]-'a'];
//	}
//}
int main()
{
	cin.tie(0);
    cin.sync_with_stdio(false);
	int T;
	cin>>T;
	while(T--)
	{
		cin>>n>>k;
		string s;
		cin>>s;
		int div=n/k;
		memset(sing,0,sizeof(sing));
		for(int i=0;i<=n/2;i++)
			for(int j=0;j<26;j++)
				cous[i][j]=0;
//		memset(cous,0,sizeof(cous));
//		root= new node();
		for(int i=1;i<=div;i++)
		{
//			string s1="",s2="";
			for(int j1=(i-1)*k,j2=i*k-1,ii=0;j1<j2;++j1,--j2,++ii)
			{
				++cous[ii][s[j1]-'a'];
				++cous[ii][s[j2]-'a'];
//				s1+=s[j1];
//				s2+=s[j2];
			}
//			cout<<s1<<" "<<s2<<endl;
//			addtotrie(s1);
//			addtotrie(s2);
			if(k%2)
			{
				sing[s[(i-1)*k+k/2]-'a']++;
			}
		}
		int courem=0;
		for(int i=0;i<k/2;i++)
		{
			int max=0,maxj;
			for(int j=0;j<26;j++)
			{
//				cout<<cous[i][j]<<" ";
				if(cous[i][j]>max)
				{
					max=cous[i][j];
					maxj=j;
				}
			}
//			cout<<endl;
			courem+=max;
		}
		if(k%2)
		{
			int max=0,maxj;
			for(int j=0;j<26;j++)
			{
//				cout<<sing[j]<<" ";
				if(sing[j]>max)
				{
					max=sing[j];
				}
			}
//			cout<<endl;
			courem+=max;
		}
		cout<<n-courem<<endl;
	}

}


#include<iostream>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<string.h>
using namespace std;
int k,n;
string s;
int sing[26];
int cous[101111][26];
void addtotrie(const string& ss)
{
	int len=ss.size();
	for(int i=0;i<len;i++)
	{
		++cous[i][ss[i]-'a'];
	}
}
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		cin>>n>>k;
		string s;
		cin>>s;
		int div=n/k;
		memset(sing,0,sizeof(sing));
		memset(cous,0,sizeof(cous));
//		root= new node();
		for(int i=1;i<=div;i++)
		{
			string s1="",s2="";
			for(int j1=(i-1)*k,j2=i*k-1;j1<j2;++j1,--j2)
			{
				s1+=s[j1];
				s2+=s[j2];
			}
//			cout<<s1<<" "<<s2<<endl;
			addtotrie(s1);
			addtotrie(s2);
			if(k%2)
			{
				sing[s[(i-1)*k+k/2]-'a']++;
			}
		}
		int courem=0;
		for(int i=0;i<k/2;i++)
		{
			int max=0,maxj;
			for(int j=0;j<26;j++)
			{
//				cout<<cous[i][j]<<" ";
				if(cous[i][j]>max)
				{
					max=cous[i][j];
					maxj=j;
				}
			}
//			cout<<endl;
			courem+=max;
		}
		if(k%2)
		{
			int max=0,maxj;
			for(int j=0;j<26;j++)
			{
//				cout<<sing[j]<<" ";
				if(sing[j]>max)
				{
					max=sing[j];
				}
			}
//			cout<<endl;
			courem+=max;
		}
		cout<<n-courem<<endl;
	}

}


#include<iostream>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#include<string.h>
using namespace std;
int k,n;
string s;
int sing[26];
int cous[101111][26];
//void addtotrie(const string& ss)
//{
//	int len=ss.size();
//	for(int i=0;i<len;i++)
//	{
//		++cous[i][ss[i]-'a'];
//	}
//}
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		cin>>n>>k;
		string s;
		cin>>s;
		int div=n/k;
		memset(sing,0,sizeof(sing));
		memset(cous,0,sizeof(cous));
//		root= new node();
		for(int i=1;i<=div;i++)
		{
//			string s1="",s2="";
			for(int j1=(i-1)*k,j2=i*k-1,ii=0;j1<j2;++j1,--j2,++ii)
			{
				++cous[ii][s[j1]-'a'];
				++cous[ii][s[j2]-'a'];
//				s1+=s[j1];
//				s2+=s[j2];
			}
//			cout<<s1<<" "<<s2<<endl;
//			addtotrie(s1);
//			addtotrie(s2);
			if(k%2)
			{
				sing[s[(i-1)*k+k/2]-'a']++;
			}
		}
		int courem=0;
		for(int i=0;i<k/2;i++)
		{
			int max=0,maxj;
			for(int j=0;j<26;j++)
			{
//				cout<<cous[i][j]<<" ";
				if(cous[i][j]>max)
				{
					max=cous[i][j];
					maxj=j;
				}
			}
//			cout<<endl;
			courem+=max;
		}
		if(k%2)
		{
			int max=0,maxj;
			for(int j=0;j<26;j++)
			{
//				cout<<sing[j]<<" ";
				if(sing[j]>max)
				{
					max=sing[j];
				}
			}
//			cout<<endl;
			courem+=max;
		}
		cout<<n-courem<<endl;
	}

}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<algorithm>
#include<queue>
#include<map>
#include<cstdio>
#include<bitset>
#include<cmath>
#include<cassert>
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;
const int maxn=2e5+7;
ll dp[maxn][26];
string s;
bool book[maxn];
int sum[maxn][26];
int cnt[26];
void dfs(int index,int n,int k,int id)
{
    if(index>=n||index<0)
        return;
    if(book[index]!=0)
        return;
    book[index]=1;
    sum[id][s[index]-'a']+=1;
//    cout<<index<<" "<<id<<endl;
    cnt[id]+=1;
    dfs(n-index-1,n,k,id);
    dfs(n-index-k-1,n,k,id);
    dfs(index+k,n,k,id);
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    //freopen("C:\\Users\\18378\\Documents\\cb-code\\te\\in.txt","r",stdin);
    int t;
    cin>>t;
    while(t--)
    {
        int n,k;
        cin>>n>>k;
        cin>>s;
        memset(book,0,sizeof(book));
        memset(cnt,0,sizeof(cnt));
        int id=1;
        for(int i=0;i<n-k;i++)
        {
            if(book[i]==0)
            {
                for(int j=0;j<26;j++)
                    sum[id][j]=0;
                dfs(i,n,k,id);
                id+=1;
            }
        }
        int ans=0;
//        for(int j=0;j<26;j++)
//        {
//            cout<<sum[1][j]<<endl;
//        }
        for(int i=1;i<id;i++)
        {
            int tmp=0;
            for(int j=0;j<26;j++)
            {
                tmp=max(tmp,sum[i][j]);
            }
//            cout<<cnt[i]<<" "<<tmp<<endl;
            ans+=cnt[i]-tmp;
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<algorithm>
#include<queue>
#include<map>
#include<cstdio>
#include<bitset>
#include<cmath>
#include<cassert>
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;
const int maxn=2e5+7;
char s[maxn];
bool book[maxn];
int sum[maxn][26];
int cnt[26];
void dfs(int index,int n,int k,int id)
{
    if(index>n||index<=0)
        return;
    if(book[index]!=0)
        return;
    book[index]=1;
    sum[id][s[index]-'a']+=1;
    cnt[id]+=1;
    dfs(n-index+1,n,k,id);
    dfs(n-index-k+1,n,k,id);
    dfs(index+k,n,k,id);
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    //freopen("C:\\Users\\18378\\Documents\\cb-code\\te\\in.txt","r",stdin);
    int t;
    cin>>t;
    while(t--)
    {
        int n,k;
        cin>>n>>k;
        cin>>(s+1);
        memset(book,0,sizeof(book));
        memset(cnt,0,sizeof(cnt));
        int id=1;
        for(int j=0;j<26;j++)
            sum[1][j]=0;
        for(int i=1;i<=n;i++)
        {
            if(book[i]==0)
            {
                dfs(i,n,k,id);
                id+=1;
                for(int j=0;j<26;j++)
                    sum[id][j]=0;
            }
        }
        int ans=0;
        for(int i=1;i<id;i++)
        {
            int tmp=0;
            for(int j=0;j<26;j++)
            {
                tmp=max(tmp,sum[i][j]);
            }
            ans+=cnt[i]-tmp;
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<algorithm>
#include<queue>
#include<map>
#include<cstdio>
#include<bitset>
#include<cmath>
#include<cassert>
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;
const int maxn=2e5+7;
string s;
bool book[maxn];
int sum[maxn][26];
int cnt[26];
void dfs(int index,int n,int k,int id)
{
    if(index>=n||index<0)
        return;
    if(book[index]!=0)
        return;
    book[index]=1;
    sum[id][s[index]-'a']+=1;
    cnt[id]+=1;
    dfs(n-index-1,n,k,id);
    dfs(n-index-k-1,n,k,id);
    dfs(index+k,n,k,id);
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    //freopen("C:\\Users\\18378\\Documents\\cb-code\\te\\in.txt","r",stdin);
    int t;
    cin>>t;
    while(t--)
    {
        int n,k;
        cin>>n>>k;
        s.clear();
        cin>>s;
        memset(book,0,sizeof(book));
        memset(cnt,0,sizeof(cnt));
        int id=1;
        for(int j=0;j<26;j++)
                    sum[1][j]=0;
        for(int i=0;i<n-k;i++)
        {
            if(book[i]==0)
            {
                dfs(i,n,k,id);
                id+=1;
                for(int j=0;j<26;j++)
                    sum[id][j]=0;
            }
        }
        int ans=0;
        for(int i=1;i<id;i++)
        {
            int tmp=0;
            for(int j=0;j<26;j++)
            {
                tmp=max(tmp,sum[i][j]);
            }
            ans+=cnt[i]-tmp;
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<algorithm>
#include<queue>
#include<map>
#include<cstdio>
#include<bitset>
#include<cmath>
#include<cassert>
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;
const int maxn=2e5+7;
char s[maxn];
bool book[maxn];
int sum[maxn][26];
int cnt[maxn];
void dfs(int index,int n,int k,int id)
{
    if(index>n||index<=0)
        return;
    if(book[index]!=0)
        return;
    book[index]=1;
    sum[id][s[index]-'a']+=1;
    cnt[id]+=1;
    dfs(n-index+1,n,k,id);
    dfs(n-index-k+1,n,k,id);
    dfs(index+k,n,k,id);
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    //freopen("C:\\Users\\18378\\Documents\\cb-code\\te\\in.txt","r",stdin);
    int t;
    cin>>t;
    while(t--)
    {
        int n,k;
        cin>>n>>k;
        cin>>(s+1);
        memset(book,0,sizeof(book));
        int id=1;
        for(int j=0;j<26;j++)
            sum[1][j]=0;
        cnt[1]=0;
        for(int i=1;i<=n;i++)
        {
            if(book[i]==0)
            {
                dfs(i,n,k,id);
                id+=1;
                for(int j=0;j<26;j++)
                    sum[id][j]=0;
                cnt[id]=0;
            }
        }
        int ans=0;
        for(int i=1;i<id;i++)
        {
            int tmp=0;
            for(int j=0;j<26;j++)
            {
                tmp=max(tmp,sum[i][j]);
            }
            ans+=cnt[i]-tmp;
        }
        cout<<ans<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    int t;
    cin>>t;
    while(t--){
        int n, m, i, j, k, x=0, y=0, h, p, f, ma, xx=0, yy;
        cin>>n>>k;
        int b[26], d[26];
        memset(d, 0, sizeof(d));
        string s;
        cin>>s;
        int an[k];
        vector<int>vv;
        vector<int>v[k];
        for(i=0; i<k; i++){
            for(j=i; j<s.size(); j+=k){
                m=s[j]-'a';
                d[m]++;
                v[i].push_back(m);
            }
        }

        ma=0;
        for(f=0; f<26; f++){
            if(d[f]>ma){
                ma=d[f];
                y=f;
            }
        }
        for(f=0; f<26; f++){
            if(d[f]==ma){
               vv.push_back(f);
            }
        }
        yy=n-ma;
        for(i=0, j=k -1; i<j; i++, j--){
            memset(b, 0, sizeof(b));
            for(f=0; f<v[i].size(); f++){
                h=v[i][f];
                b[h]++;
            }
            for(f=0; f<v[j].size(); f++){
                h=v[j][f];
                b[h]++;
            }
            ma=0;
            for(f=0; f<26; f++){
                if(b[f]>ma){
                    ma=b[f];
                    an[i]=ma;
                    an[j]=ma;
                }
            }
            xx+=(((n/k) * 2)-ma);
        }
        if(k%2==1){
            memset(b, 0, sizeof(b));
            j=k/2;
            for(f=0; f<v[j].size(); f++){
                h=v[j][f];
                b[h]++;
            }
            ma=0;
            for(f=0; f<26; f++){
                if(b[f]>ma){
                    ma=b[f];
                    an[j]=ma;
                }
            }
            xx+=((n/k)-ma);
        }
        char dd='a'+y;
        if(yy<xx){
            if(n%2==1){
                char cc='a'+y;
                if(s[n/2]!=cc)cout<<yy-1<<endl;
                else if(vv.size()>1)cout<<yy-1<<endl;
                else cout<<yy<<endl;
            }
            else cout<<yy<<endl;
        }
        else{
            cout<<xx<<endl;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    int t;
    cin>>t;
    while(t--){
        int n, m, i, j, k, x=0, y=0, h, p, f, ma, xx=0, yy;
        cin>>n>>k;
        int b[26], d[26];
        memset(d, 0, sizeof(d));
        string s;
        cin>>s;
        int an[k];
        vector<int>vv;
        vector<int>v[k];
        for(i=0; i<k; i++){
            for(j=i; j<s.size(); j+=k){
                m=s[j]-'a';
                d[m]++;
                v[i].push_back(m);
            }
        }

        ma=0;
        for(f=0; f<26; f++){
            if(d[f]>ma){
                ma=d[f];
                y=f;
            }
        }
        for(f=0; f<26; f++){
            if(d[f]==ma){
               vv.push_back(f);
            }
        }
        yy=n-ma;
        for(i=0, j=k -1; i<j; i++, j--){
            memset(b, 0, sizeof(b));
            for(f=0; f<v[i].size(); f++){
                h=v[i][f];
                b[h]++;
            }
            for(f=0; f<v[j].size(); f++){
                h=v[j][f];
                b[h]++;
            }
            ma=0;
            for(f=0; f<26; f++){
                if(b[f]>ma){
                    ma=b[f];
                    an[i]=ma;
                    an[j]=ma;
                }
            }
            xx+=(((n/k) * 2)-ma);
        }
        if(k%2==1){
            memset(b, 0, sizeof(b));
            j=k/2;
            for(f=0; f<v[j].size(); f++){
                h=v[j][f];
                b[h]++;
            }
            ma=0;
            for(f=0; f<26; f++){
                if(b[f]>ma){
                    ma=b[f];
                    an[j]=ma;
                }
            }
            xx+=((n/k)-ma);
        }
        char dd='a'+y;
        if(yy<=xx){
            if(n%2==1){
                char cc='a'+y;
                if(s[n/2]!=cc)cout<<yy-1<<endl;
                else if(vv.size()>1)cout<<yy-1<<endl;
                else cout<<yy<<endl;
            }
            else cout<<yy<<endl;
        }
        else{
            cout<<xx<<endl;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    int t;
    cin>>t;
    while(t--){
        int n, m, i, j, k, x=0, y=0, h, p, f, ma, xx=0, yy;
        cin>>n>>k;
        int b[26], d[26];
        memset(d, 0, sizeof(d));
        string s;
        cin>>s;
        int an[k];
        vector<int>v[n/k];
        for(i=0; i<k; i++){
            for(j=i; j<s.size(); j+=k){
                m=s[j]-'a';
                d[m]++;
                v[i].push_back(m);
            }
        }
        ma=0;
        for(f=0; f<26; f++){
            if(d[f]>ma){
                ma=d[f];
                y=f;
            }
        }
        yy=n-ma;
        for(i=0, j=k -1; i<j; i++, j--){
            memset(b, 0, sizeof(b));
            for(f=0; f<v[i].size(); f++){
                h=v[i][f];
                b[h]++;
            }
            for(f=0; f<v[j].size(); f++){
                h=v[j][f];
                b[h]++;
            }
            ma=0;
            for(f=0; f<26; f++){
                if(b[f]>ma){
                    ma=b[f];
                    an[i]=ma;
                    an[j]=ma;
                }
            }
            xx+=((n/k * 2)-ma);
        }
        if(k%2==1){
        memset(b, 0, sizeof(b));
            j=k/2;
            for(f=0; f<v[j].size(); f++){
                h=v[j][f];
                b[h]++;
            }
            ma=0;
            for(f=0; f<26; f++){
                if(b[f]>ma){
                    ma=b[f];
                    an[j]=ma;
                }
            }
            xx+=((n/k)-ma);
        }
        if(yy<=xx){
            cout<<yy<<endl;
            for(i=0; i<s.size(); i++){
                s[i]='a'+y;
            }
            cout<<s<<endl;
        }
        else{
            cout<<xx<<endl;
            for(i=0; i<k; i++){
                for(j=i; j<s.size(); j+=k){
                    s[j]='a'+an[i];
                }
            }
            cout<<s<<endl;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    int t;
    cin>>t;
    while(t--){
        int n, m, i, j, k, x=0, y=0, h, p, f, ma, xx=0, yy;
        cin>>n>>k;
        int b[26], d[26];
        memset(d, 0, sizeof(d));
        string s;
        cin>>s;
        int an[k];
        vector<int>vv;
        vector<int>v[k];
        for(i=0; i<k; i++){
            for(j=i; j<s.size(); j+=k){
                m=s[j]-'a';
                d[m]++;
                v[i].push_back(m);
            }
        }

     /*   ma=0;
        for(f=0; f<26; f++){
            if(d[f]>ma){
                ma=d[f];
                y=f;
            }
        }
        for(f=0; f<26; f++){
            if(d[f]==ma){
               vv.push_back(f);
            }
        }
        yy=n-ma; */
        for(i=0, j=k -1; i<j; i++, j--){
            memset(b, 0, sizeof(b));
            for(f=0; f<v[i].size(); f++){
                h=v[i][f];
                b[h]++;
            }
            for(f=0; f<v[j].size(); f++){
                h=v[j][f];
                b[h]++;
            }
            ma=0;
            for(f=0; f<26; f++){
                if(b[f]>ma){
                    ma=b[f];
                    an[i]=ma;
                    an[j]=ma;
                }
            }
            xx+=(((n/k) * 2)-ma);
        }
        if(k%2==1){
            memset(b, 0, sizeof(b));
            j=k/2;
            for(f=0; f<v[j].size(); f++){
                h=v[j][f];
                b[h]++;
            }
            ma=0;
            for(f=0; f<26; f++){
                if(b[f]>ma){
                    ma=b[f];
                    an[j]=ma;
                }
            }
            xx+=((n/k)-ma);
        }
      /*  char dd='a'+y;
        if(yy<xx){
            if(n%2==1){
                char cc='a'+y;
                if(s[n/2]!=cc)cout<<yy-1<<endl;
                else if(vv.size()>1)cout<<yy-1<<endl;
                else cout<<yy<<endl;
            }
            else cout<<yy<<endl;
        }
        else{ */
            cout<<xx<<endl;
      //  }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    int t;
    cin>>t;
    while(t--){
        int n, m, i, j, k, x=0, y=0, h, p, f, ma, xx=0, yy;
        cin>>n>>k;
        int b[26], d[26];
        memset(d, 0, sizeof(d));
        string s;
        cin>>s;
        int an[k];
        vector<int>v[k];
        for(i=0; i<k; i++){
            for(j=i; j<s.size(); j+=k){
                m=s[j]-'a';
                d[m]++;
                v[i].push_back(m);

            }
        }

        ma=0;
        for(f=0; f<26; f++){
            if(d[f]>ma){
                ma=d[f];
                y=f;
            }
        }
        yy=n-ma;
        for(i=0, j=k -1; i<j; i++, j--){
            memset(b, 0, sizeof(b));
            for(f=0; f<v[i].size(); f++){
                h=v[i][f];
                b[h]++;
            }
            for(f=0; f<v[j].size(); f++){
                h=v[j][f];
                b[h]++;
            }
            ma=0;
            for(f=0; f<26; f++){
                if(b[f]>ma){
                    ma=b[f];
                    an[i]=ma;
                    an[j]=ma;
                }
            }
            xx+=(((n/k) * 2)-ma);
        }
        if(k%2==1){
            memset(b, 0, sizeof(b));
            j=k/2;
            for(f=0; f<v[j].size(); f++){
                h=v[j][f];
                b[h]++;
            }
            ma=0;
            for(f=0; f<26; f++){
                if(b[f]>ma){
                    ma=b[f];
                    an[j]=ma;
                }
            }
            xx+=((n/k)-ma);
        }
        if(yy<=xx){
            if(n%2==1){
                char cc='a'+y;
                if(s[n/2]!=cc)cout<<yy-1<<endl;
            }
            else cout<<yy<<endl;
        }
        else{
            cout<<xx<<endl;
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
#define int long long
using namespace std;

char a[200010];
int n, k, t;
int b[100010][26];

signed main() {
    cin >> t;
    while(t--) {
        cin >> n >> k >> (a + 1);
        int ans = 0;
        memset(b, 0, sizeof(b));
        for(int i = 1; i <= k; ++i)
            for(int j = i; j <= n; j += k)
                ++b[i][a[j] - 'a'];
        for(int i = 1; i <= (k >> 1); ++i) {
            int tmp = -1;
            for(int j = 0; j < 26; ++j)
                tmp = max(tmp, b[i][j] + b[k - i + 1][j]);
            ans += n / k * 2 - tmp;
        }
        if(k & 1) ans += n / k - *max_element(b[k >> 1 | 1], b[k >> 1 | 1] + 26);
        cout << ans << endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
#define int long long
using namespace std;

char a[200010];
int n, k, t;
int b[100010][26];

signed main() {
    cin >> t;
    while(t--) {
        cin >> n >> k >> (a + 1);
        int ans = 0;
        for(int i = 1; i <= k; ++i)
            for(int j = i; j <= n; j += k)
                ++b[i][a[j] - 'a'];
        for(int i = 1; i <= (k >> 1); ++i) {
            int tmp = -1;
            for(int j = 0; j < 26; ++j)
                tmp = max(tmp, b[i][j] + b[k - i + 1][j]),
                b[i][j] = b[k - i + 1][j] = 0;
            ans += n / k * 2 - tmp;
        }
        if(k & 1) {
            int tmp = -1;
            for(int i = 0; i < 26; ++i)
                tmp = max(tmp, b[(k >> 1) + 1][i]),
                b[(k >> 1) + 1][i] = 0;
            ans += n / k - tmp;
        }
        cout << ans << endl;
        for(int i = 1; i <= k; ++i)
            for(int j = 0; j < 26; ++j)
                b[i][j] = 0;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

char a[100010];
int n, k, t;
int b[100010 >> 1][26];

int main() {
    cin >> t;
    while(t--) {
        cin >> n >> k >> (a + 1);
        int ans = 0;
        memset(b, 0, sizeof(b));
        for(int i = 1; i <= k; ++i)
            for(int j = i; j <= n; j += k)
                ++b[i][a[j] - 'a'];
        for(int i = 1; i <= (k >> 1); ++i) {
            int tmp = -1;
            for(int j = 0; j < 26; ++j)
                tmp = max(tmp, b[i][j] + b[k - i + 1][j]);
            ans += n / k * 2 - tmp;
        }
        if(k & 1) ans += n / k - *max_element(b[k >> 1 | 1], b[k >> 1 | 1] + 26);
        cout << ans << endl;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
#define int long long
using namespace std;

char a[200010];
int n, k, t;
int b[100010][26];

signed main() {
    cin >> t;
    while(t--) {
        cin >> n >> k >> (a + 1);
        int ans = 0;
        for(int i = 1; i <= k; ++i)
            for(int j = i; j <= n; j += k)
                ++b[i][a[j] - 'a'];
        for(int i = 1; i <= (k >> 1); ++i) {
            int tmp = -1;
            for(int j = 0; j < 26; ++j)
                tmp = max(tmp, b[i][j] + b[k - i + 1][j]),
                b[i][j] = b[k - i + 1][j] = 0;
            ans += n / k * 2 - tmp;
        }
        if(k & 1) {
            int tmp = -1;
            for(int i = 0; i < 26; ++i)
                tmp = max(tmp, b[k >> 1 + 1][i]),
                b[k >> 1 + 1][i] = 0;
            ans += n / k - tmp;
        }
        cout << ans << endl;
        // for(int i = 1; i <= k; ++i)
        //     for(int j = 0; j < 26; ++j)
        //         b[i][j] = 0;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
#define int long long
using namespace std;

char a[200010];
int n, k, t;
int b[100010][26];

signed main() {
    cin >> t;
    while(t--) {
        cin >> n >> k >> (a + 1);
        int ans = 0;
        for(int i = 1; i <= k; ++i)
            for(int j = i; j <= n; j += k)
                ++b[i][a[j] - 'a'];
        for(int i = 1; i <= (k >> 1); ++i) {
            int tmp = -1;
            for(int j = 0; j < 26; ++j)
                tmp = max(tmp, b[i][j] + b[k - i + 1][j]);
            ans += n / k * 2 - tmp;
        }
        if(k & 1) ans += n / k - *max_element(b[k >> 1 | 1], b[k >> 1 | 1] + 26);
        cout << ans << endl;
        for(int i = 1; i <= k; ++i)
            for(int j = 0; j < 26; ++j)
                b[i][j] = 0;
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cstring>
#define int long long
using namespace std;

char a[200010];
int n, k, t;
int b[100010][26];

signed main() {
    cin >> t;
    while(t--) {
        memset(b, 0, sizeof(int) * k * 27);
        cin >> n >> k >> (a + 1);
        int ans = 0;
        for(int i = 1; i <= k; ++i)
            for(int j = i; j <= n; j += k)
                ++b[i][a[j] - 'a'];
        for(int i = 1; i <= (k >> 1); ++i) {
            int tmp = -1;
            for(int j = 0; j < 26; ++j)
                tmp = max(tmp, b[i][j] + b[k - i + 1][j]);
            ans += n / k * 2 - tmp;
        }
        if(k & 1) ans += n / k - *max_element(b[k >> 1 | 1], b[k >> 1 | 1] + 26);
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

const int N = 1e6 + 10;

char ch[N];
int c[100];

signed main()
{
	int T;
	cin >> T;
	while (T--)
	{
		int n, k;
		cin >> n >> k;
		cin >> ch + 1;
		int len = strlen(ch + 1);
		for (int i = 1; i <= len; i++)
		{
			ch[i] -= 'a';
		}
		int num = n / k;
		int ans = 0;
		for (int i = 1; i <= (k + 1) / 2; i++)
		{
			int mx = 0;
			memset(c, 0, sizeof(c));
			if (i == (k + 1) / 2 && k % 2)
			{
				for (int j = 1; j <= num; j++)
				{
					c[ch[i + (j - 1) * k]]++;
					mx = max(mx, c[ch[i + (j - 1) * k]]);
				}
				ans += num - mx;
				continue;
			}
			for (int j = 1; j <= num; j++)
			{
				c[ch[i + (j - 1) * k]]++;
				c[ch[k - i + 1 + (j - 1) * k]]++;
				mx = max(mx, max(c[ch[i + (j - 1) * k]], c[ch[n - i + 1 + (j - 1) * k]]));
			}
			ans += num * 2 - mx;
		}
		cout << ans << endl;
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

const int N = 2e5 + 10;

int a[N], c[100];
char ch[N];

signed main()
{
	int T;
	cin >> T;
	while (T--)
	{
		int n, k;
		cin >> n >> k;
		cin >> ch + 1;
		int len = strlen(ch + 1);
		for (int i = 1; i <= len; i++)
			a[i] = ch[i] - 'a';
		int num = n / k;
		int ans = 0;
		for (int i = 1; i <= (k + 1) / 2; i++)
		{
			int mx = 0;
			memset(c, 0, sizeof(c));
			if (2 * i - 1 == k)
			{
				for (int j = 0; j < num; j++)
				{
					c[a[j * k + i]]++;
					mx = max(mx, c[a[j * k + i]]);
				}
				ans += num - mx;
				continue;
			}
			for (int j = 0; j < num; j++)
			{
				c[a[j * k + i]]++;
				c[a[k - i + 1 + j * k]]++;
				mx = max(mx, max(c[a[j * k + i]], c[a[k - i + 1 + j * k]]));
			}
			ans += num * 2 - mx;
		}
		cout << ans << endl;
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

const int N = 1e6 + 10;

char ch[N];
int c[100];

signed main()
{
	int T;
	cin >> T;
	while (T--)
	{
		int n, k;
		cin >> n >> k;
		cin >> ch + 1;
		int len = strlen(ch + 1);
		for (int i = 1; i <= len; i++)
		{
			ch[i] -= 'a';
		}
		int num = n / k;
		int ans = 0;
		for (int i = 1; i <= (k + 1) / 2; i++)
		{
			int mx = 0;
			memset(c, 0, sizeof(c));
			if (i * 2 - 1 == k)
			{
				for (int j = 1; j <= num; j++)
				{
					c[ch[i + (j - 1) * k]]++;
					mx = max(mx, c[ch[i + (j - 1) * k]]);
				}
				ans += num - mx;
				continue;
			}
			for (int j = 1; j <= num; j++)
			{
				c[ch[i + (j - 1) * k]]++;
				c[ch[k - i + 1 + (j - 1) * k]]++;
				mx = max(mx, max(c[ch[i + (j - 1) * k]], c[ch[n - i + 1 + (j - 1) * k]]));
			}
			ans += num * 2 - mx;
		}
		cout << ans << endl;
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

const int N = 1e6 + 10;

char ch[N];
int c[100], a[N];

signed main()
{
	int T;
	cin >> T;
	while (T--)
	{
		int n, k;
		cin >> n >> k;
		cin >> ch + 1;
		int len = strlen(ch + 1);
		for (int i = 1; i <= len; i++)
		{
			a[i] = ch[i] - 'a';
		}
		int num = n / k;
		int ans = 0;
		for (int i = 1; i <= (k + 1) / 2; i++)
		{
			int mx = 0;
			memset(c, 0, sizeof(c));
			if (i * 2 - 1 == k)
			{
				for (int j = 1; j <= num; j++)
				{
					c[a[i + (j - 1) * k]]++;
					mx = max(mx, c[a[i + (j - 1) * k]]);
				}
				ans += num - mx;
				continue;
			}
			for (int j = 1; j <= num; j++)
			{
				c[a[i + (j - 1) * k]]++;
				c[a[k - i + 1 + (j - 1) * k]]++;
				mx = max(mx, max(c[a[i + (j - 1) * k]], c[a[n - i + 1 + (j - 1) * k]]));
			}
			ans += num * 2 - mx;
		}
		cout << ans << endl;
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

const int N = 1e6 + 10;

char ch[N];
int c[100], a[N];

signed main()
{
	int T;
	cin >> T;
	while (T--)
	{
		int n, k;
		cin >> n >> k;
		cin >> ch + 1;
		int len = strlen(ch + 1);
		for (int i = 1; i <= len; i++)
		{
			a[i] = ch[i] - 'a';
		}
		int num = n / k;
		int ans = 0;
		for (int i = 1; i <= (k + 1) / 2; i++)
		{
			int mx = 0;
			memset(c, 0, sizeof(c));
			if (i * 2 - 1 == k)
			{
				for (int j = 1; j <= num; j++)
				{
					c[a[i + (j - 1) * k]]++;
					mx = max(mx, c[a[i + (j - 1) * k]]);
				}
				ans += num - mx;
				continue;
			}
			for (int j = 1; j <= num; j++)
			{
				c[a[i + (j - 1) * k]]++;
				c[a[k - i + 1 + (j - 1) * k]]++;
				mx = max(mx, max(c[a[i + (j - 1) * k]], c[a[k - i + 1 + (j - 1) * k]]));
			}
			ans += num * 2 - mx;
		}
		cout << ans << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin >> t;
    while(t--) {
        int n;
        cin >> n;
        int k;
        cin >> k;

        string s;
        cin >> s;

        int cnt = n / k;
        int resp = 0;

        for (int i = 0; i < k / 2; i++) {
            vector<int> alpha(26);
            int dupl = 0;
            for (int j = 0; j < cnt; j++) {
                int p1 = s[i + j * k] - 'a';
                int p2 = s[k - i - 1 + j * k] - 'a';
                alpha[p1]++;
                alpha[p2]++;
                dupl = max({dupl, alpha[p1], alpha[p2]});
            }
            resp += cnt * 2 - dupl;
        }

        if (k % 2 == 1) {
            vector<int> alpha(26);
            int dupl = 0;
            for (int i = 0; i < cnt; i++) {
                int p = s[i * k / 2];
                alpha[p]++;
                dupl = max(dupl, alpha[p]);
            }
            resp += cnt - dupl;
        }

        cout << resp << '\n';
    }
}


#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin >> t;
    while(t--) {
        int n;
        cin >> n;
        int k;
        cin >> k;

        string s;
        cin >> s;

        int cnt = n / k;
        int resp = 0;
        /*
        for (int i = 0; i < k / 2; i++) {
            vector<int> alpha(26);
            int dupl = 0;
            for (int j = 0; j < cnt; j++) {
                int p1 = s[i + j * k] - 'a';
                int p2 = s[k - i - 1 + j * k] - 'a';
                alpha[p1]++;
                alpha[p2]++;
                dupl = max({dupl, alpha[p1], alpha[p2]});
            }
            resp += cnt * 2 - dupl;
        }

        if (k % 2 == 1) {
            vector<int> alpha(26);
            int dupl = 0;
            for (int i = 0; i < cnt; i++) {
                alpha[s[i * k + k / 2]]++;
                dupl = max(dupl, alpha[s[i * k + k / 2]]);
            }
            resp += cnt - dupl;
        }
        */
        cout << resp << '\n';
    }
}


#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin >> t;
    while(t--) {
        int n;
        cin >> n;
        int k;
        cin >> k;

        string s;
        cin >> s;

        int cnt = n / k;
        int resp = 0;

        for (int i = 0; i < k / 2; i++) {
            vector<int> alpha(26);
            int dupl = 0;
            for (int j = 0; j < cnt; j++) {
                int p1 = s[i + j * k] - 'a';
                int p2 = s[k - i - 1 + j * k] - 'a';
                //cout << char('a' + p1) << '\n';
                //cout << char('a' + p2) << '\n';
                alpha[p1]++;
                alpha[p2]++;
                dupl = max({dupl, alpha[p1], alpha[p2]});
            }
            resp += cnt * 2 - dupl;
        }

        if (k % 2 == 1) {
            vector<int> alpha(26);
            int dupl = 0;
            for (int i = 0; i < cnt; i++) {
                alpha[s[i * k + k / 2]]++;
                dupl = max(dupl, alpha[s[i * k + k / 2]]);
            }
            //cout << dupl << endl;
            //cout << cnt - dupl << endl;
            resp += cnt - dupl;
        }
        cout << resp << '\n';
    }
}


#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin >> t;
    while(t--) {
        int n;
        cin >> n;
        int k;
        cin >> k;

        string s;
        cin >> s;

        int cnt = n / k;
        int resp = 0;

        for (int i = 0; i < k / 2; i++) {
            vector<int> alpha(26);
            int dupl = 0;
            for (int j = 0; j < cnt; j++) {
                int p1 = s[i + j * k] - 'a';
                int p2 = s[k - i - 1 + j * k] - 'a';
                alpha[p1]++;
                alpha[p2]++;
                dupl = max({dupl, alpha[p1], alpha[p2]});
            }
            resp += cnt * 2 - dupl;
        }

        if (k % 2 == 1) {
            vector<int> alpha(26);
            int dupl = 0;
            for (int i = 0; i < cnt; i++) {
                alpha[s[i * k + k / 2]]++;
                dupl = max(dupl, alpha[s[i * k + k / 2]]);
            }
            resp += cnt - dupl;
        }
        cout << resp << '\n';
    }
}


#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin >> t;
    while(t--) {
        int n;
        cin >> n;
        int k;
        cin >> k;

        string s;
        cin >> s;

        int cnt = n / k;
        int resp = 0;

        for (int i = 0; i < k / 2; i++) {
            vector<int> alpha(26);
            int dupl = 0;
            for (int j = 0; j < cnt; j++) {
                int p1 = s[i + j * k] - 'a';
                int p2 = s[k - i - 1 + j * k] - 'a';
                alpha[p1]++;
                alpha[p2]++;
                dupl = max({dupl, alpha[p1], alpha[p2]});
            }
            resp += cnt * 2 - dupl;
        }

        if (k % 2 == 1) {
            vector<int> alpha(26);
            int dupl = 0;
            for (int i = 0; i < cnt; i++) {
                int p = s[i * k + cnt / 2] - 'a';
                alpha[p]++;
                dupl = max(dupl, alpha[p]);
            }
            resp += cnt - dupl;
        }

        cout << resp << '\n';
    }
}


#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin >> t;
    while(t--) {
        int n;
        cin >> n;
        int k;
        cin >> k;

        string s;
        cin >> s;

        int cnt = n / k;
        int resp = 0;

        for (int i = 0; i < k / 2; i++) {
            vector<int> alpha(26);
            int dupl = 0;
            for (int j = 0; j < cnt; j++) {
                int p1 = s[i + j * k] - 'a';
                int p2 = s[k - i - 1 + j * k] - 'a';
                alpha[p1]++;
                alpha[p2]++;
                dupl = max({dupl, alpha[p1], alpha[p2]});
            }
            resp += cnt * 2 - dupl;
        }

        if (k % 2 == 1) {
            vector<int> alpha(26);
            int dupl = 0;
            for (int i = 0; i < cnt; i++) {
                alpha[s[i * k + k / 2]]++;
                dupl = max(dupl, alpha[s[i * k + k / 2]]);
            }
            resp += cnt - dupl;
        }

        cout << resp << '\n';
    }
}


#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin >> t;
    while(t--) {
        int n;
        cin >> n;
        int k;
        cin >> k;

        string s;
        cin >> s;

        int cnt = n / k;
        int resp = 0;

        for (int i = 0; i < k / 2; i++) {
            vector<int> alpha(26);
            int dupl = 0;
            for (int j = 0; j < cnt; j++) {
                int p1 = s[i + j * k] - 'a';
                int p2 = s[k - i - 1 + j * k] - 'a';
                alpha[p1]++;
                alpha[p2]++;
                dupl = max({dupl, alpha[p1], alpha[p2]});
            }
            resp += cnt * 2 - dupl;
        }
        /*
        if (k % 2 == 1) {
            vector<int> alpha(26);
            int dupl = 0;
            for (int i = 0; i < cnt; i++) {
                alpha[s[i * k + k / 2]]++;
                dupl = max(dupl, alpha[s[i * k + k / 2]]);
            }
            resp += cnt - dupl;
        }
        */
        cout << resp << '\n';
    }
}


#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin >> t;
    while(t--) {
        int n;
        cin >> n;
        int k;
        cin >> k;

        string s;
        cin >> s;

        int cnt = n / k;
        int resp = 0;

        for (int i = 0; i < k / 2; i++) {
            vector<int> alpha(26);
            int dupl = 0;
            for (int j = 0; j < cnt; j++) {
                int p1 = s[i + j * k] - 'a';
                int p2 = s[k - i - 1 + j * k] - 'a';
                alpha[p1]++;
                alpha[p2]++;
                dupl = max({dupl, alpha[p1], alpha[p2]});
            }
            resp += cnt * 2 - dupl;
        }

        if (k % 2 == 1) {
            vector<int> alpha(26);
            int dupl = 0;
            for (int i = 0; i < cnt; i++) {
                int p = s[i * k + k / 2] - 'a';
                alpha[p]++;
                dupl = max(dupl, alpha[p]);
            }
            resp += cnt - dupl;
        }

        cout << resp << '\n';
    }
}


#include <string>
#include <iostream>
#include <vector>

using namespace std;

bool kcomp(string s, int k, int n) {
    for(int i = 0; 2*i < n; i++) {
        if(s[n-1-i] != s[i]) {
            return false;
        }
    }
    for(int i = 0; i < k; i++) {
        if(s[i] != s[i+k]) {
            return false;
        }
    }
    return true;
}

vector<char> possible_change(string s, int i, int k, int n) {
    if(i < k) {
        if(s[n-1-i] == s[i+k] && s[n-1-i] != s[i]) {
            return {s[k+1]};
        }

        if(s[n-1-i] == s[i+k] && s[i+k] == s[i]) {
            return {};
        }
        if(s[n-1-i] == s[i] && s[n-1-i] != s[k+i]) {
            return {s[k+i]};
        }
        if(s[n-1-i] != s[i] && s[i] == s[k+i]) {
            return {s[n-i-1]};
        }
        return {s[i+k], s[n-1-i]};
    }
    if(i >= k && i < 2*k) {
        if(s[n-i] == s[i-k] && s[n-1-i] != s[i]) {
            return {s[k+1]};
        }

        if(s[n-i] == s[i-k] && s[i-k] == s[i]) {
            return {};
        }
        if(s[n-1-i] == s[i] && s[n-1-i] != s[i-k]) {
            return {s[i-k]};
        }
        if(s[n-1-i] != s[i] && s[i] == s[i-k]) {
            return {s[n-i-1]};
        }
        return {s[i-k], s[n-1-i]};
    }
    if(s[n-i] != s[i]) {
        return {s[n-1-i]};
    }
    return {};
}

bool dfs(int m, string s, int p, int k, int n, int position) {
    if(p > m) {
        return false;
    }
    if(kcomp(s, k, n)) {
        return true;
    }

    for(int i = position; i < n; i++) {
        vector<char> v = possible_change(s, i, k, n);
        string copie = s;
        for(auto c : v) {
            s[i] = c;
            if(dfs(m, s, p+1, k, n, position+1)) {
                return true;
            }
            s = copie;
        }
    }
    return false;
}

int main() {
    int ninput;
    cin >> ninput;

    for(int i = 0; i < ninput; i++) {
        int n, k;
        cin >> n >> k;
        string s;
        cin >> s;
        int m = 0;
        while(!dfs(m, s, 0, k, n, 0)) {
            m++;
        }
        cout << m << endl;
    }
}


#include <string>
#include <iostream>
#include <unordered_map>

int main() {
    int ninput;
    cin >> ninput;

    for(int i = 0; i < ninput; i++) {
        int n, k;
        cin >> n >> k;
        string s;
        cin >> s;
        int compteur = 0;
        for(int j = 0; j < (k+1)/2; j++) {
            unordered_map<char, int> count;
            int nletter = 0;
            int l = 0;
            while(l*k + j < n) {
                if(count.find(s[l*k+j]) == count.end()) {
                    count.insert(make_pair(s[l*k+j], 1));
                }
                else {
                    count[s[l*k+j]] += 1;
                }
                nletter++;

                if (k-1-j != j) {
                    if(count.find(s[l*k+k-1-j]) == count.end()) {
                        count.insert(make_pair(s[l*k+k-1-j], 1));
                    }
                    else {
                        count[s[l*k+k-1-j]] += 1;
                    }
                    nletter++;
                }
                l++;
            }
            char max_char;
            int cnt = -1;
            for(auto p : count) {
                if(p.second > cnt) {
                    max_char = p.first;
                    cnt = p.second;
                }
            }
            compteur += nletter -cnt;
        }
        cout << compteur << endl;
    }
}


#include <string>
#include <iostream>
#include <vector>

using namespace std;

char best_letter(string s, int i, int k) {
    int n = s.size();
    if(i < k && s[k+i] == s[n-1-i]) {
        return s[k+i];
    }
    if(i >= k && i < 2*k) {
        return s[i-k];
    }
    if(i >= n/2) {
        return s[n-1-i];
    }
    return s[i];
}

int main() {
    int ninput;
    cin >> ninput;

    for(int i = 0; i < ninput; i++) {
        int n, k;
        cin >> n >> k;
        string s;
        cin >> s;
        int compteur =0;
        for(int j = 0; j < n; j++) {
            char c = best_letter(s, j, k);
            if(c != s[j]) {
                s[j] = c;
                compteur++;
            }
        }
        cout << compteur << endl;
    }
}


#include <iostream>
#include <string>
#include <unordered_map>

using namespace std;

int main() {
    int ninput;
    cin >> ninput;

    for(int i = 0; i < ninput; i++) {
        int n, k;
        cin >> n >> k;
        string s;
        cin >> s;
        int compteur = 0;
        for(int j = 0; j < (k+1)/2; j++) {
            unordered_map<char, int> count;
            int nletter = 0;
            int l = 0;
            while(l*k + j < n) {
                if(count.find(s[l*k+j]) == count.end()) {
                    count.insert(make_pair(s[l*k+j], 1));
                }
                else {
                    count[s[l*k+j]] += 1;
                }
                nletter++;

                if (k-1-j != j) {
                    if(count.find(s[l*k+k-1-j]) == count.end()) {
                        count.insert(make_pair(s[l*k+k-1-j], 1));
                    }
                    else {
                        count[s[l*k+k-1-j]] += 1;
                    }
                    nletter++;
                }
                l++;
            }
            char max_char;
            int cnt = -1;
            for(auto p : count) {
                if(p.second > cnt) {
                    max_char = p.first;
                    cnt = p.second;
                }
            }
            compteur += nletter -cnt;
        }
        cout << compteur << endl;
    }
}


#include <string>
#include <iostream>

using namespace std;

char best_letter(string s, int i, int k) {
    int n = s.size();
    if(i < k && s[k+i] == s[n-i]) {
        return s[k+i];
    }
    if(i >= k && i < 2*k) {
        return s[i-k];
    }
    if(i >= n/2) {
        return s[n-i];
    }
    return s[i];
}

int main() {
    int ninput;
    cin >> ninput;

    for(int i = 0; i < ninput; i++) {
        int n, k;
        cin >> n >> k;
        string s;
        cin >> s;
        int compteur =0;
        for(int j = 0; j < n; j++) {
            char c = best_letter(s, j, k);
            if(c != s[j]) {
                s[j] = c;
                compteur++;
            }
        }
        cout << compteur << endl;
    }
}


#include<bits/stdc++.h>
#define pb push_back
#define pob pop_back
#define bs binary_search
#define mp make_pair
#define F first
#define S second
#define It iterator
#define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower);
#define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper);
#define rint register int
#define en "\n"
#define br break
#define con continue
#define fri(n) for(register int i=0;i<n;i++)
#define frj(n) for(register int j=0;j<n;j++)
#define T(i) int i=1;cin>>i;while(i--)
#define fast ios_base::sync_with_stdio(0);cin.tie();cout.tie();
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef vector<pi>vii;
typedef map<int,int> mi;
typedef set<int> S;
typedef list<int> L;
typedef stack<int> St;
typedef queue<int> Qu;
typedef deque<int> Dq;
typedef map<string,int> msi;
void solve()
{
    //fast;
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n,k;cin>>n>>k;
    //cout<<n<<" "<<k<<" ";
    string s;cin>>s;
    int res=0;
    int p;
    p=k/2+k%2;
    for(int i=0;i<p;i++){
        int j=i,l=n-1-i;
        map<char,int>m;
        int maxi=-1,sum=0;
        while(j<n){
            if(2*i+1<k){
                m[s[j]]++;
                m[s[l]]++;
                j+=k;l-=k;
                sum+=2;
            }
            else{
                m[s[j]]++;
                j+=k;
                sum+=1;
            }
        }
        for(int j=0;j<26;j++){
            maxi=max(m[j+97],maxi);
        }
        res+=sum-maxi;
    }
    cout<<res<<endl;
    return;
}
int main()
{
    int t;cin>>t;while(t--){
        solve();
    }
}


#include<bits/stdc++.h>
#define pb push_back
#define pob pop_back
#define bs binary_search
#define mp make_pair
#define F first
#define S second
#define It iterator
#define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower);
#define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper);
#define rint register int
#define en "\n"
#define br break
#define con continue
#define fri(n) for(register int i=0;i<n;i++)
#define frj(n) for(register int j=0;j<n;j++)
#define T(i) int i=1;cin>>i;while(i--)
#define fast ios_base::sync_with_stdio(0);cin.tie();cout.tie();
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef vector<pi>vii;
typedef map<int,int> mi;
typedef set<int> S;
typedef list<int> L;
typedef stack<int> St;
typedef queue<int> Qu;
typedef deque<int> Dq;
typedef map<string,int> msi;
void solve()
{
    fast;
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n,k;cin>>n>>k;
    string s;cin>>s;
    int res=0;
    fri(k/2+min(1,k%2)){
        int j=i,l=n-1-i;
        map<char,int>m;
        int maxi=-1,sum=0;
        while(j<n){
            if(2*i+1<k){
                m[s[j]]++;
                m[s[l]]++;
                j+=k;l-=k;
                sum+=2;
            }
            else{
                m[s[j]]++;
                j+=k;
                sum+=1;
            }
        }
        frj(26){
            maxi=max(m[j+97],maxi);
        }
        res+=sum-maxi;
    }
    cout<<res<<endl;
    return;
}
int main()
{
    T(t){
        solve();
    }
}


#include<bits/stdc++.h>
#define pb push_back
#define pob pop_back
#define bs binary_search
#define mp make_pair
#define F first
#define S second
#define It iterator
#define tol(s) transform(s.begin(),s.end(),s.begin(),::tolower);
#define tou(s) transform(s.begin(),s.end(),s.begin(),::toupper);
#define rint register int
#define en "\n"
#define br break
#define con continue
#define fri(n) for(register int i=0;i<n;i++)
#define frj(n) for(register int j=0;j<n;j++)
#define T(i) int i=1;cin>>i;while(i--)
#define fast ios_base::sync_with_stdio(0);cin.tie();cout.tie();
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef vector<pi>vii;
typedef map<int,int> mi;
typedef set<int> S;
typedef list<int> L;
typedef stack<int> St;
typedef queue<int> Qu;
typedef deque<int> Dq;
typedef map<string,int> msi;
void solve()
{
    fast;
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int n,k;cin>>n>>k;
    string s;cin>>s;
    int res=0;
    int p;
    p=k/2+k%2;
    fri(p){
        int j=i,l=n-1-i;
        map<char,int>m;
        int maxi=-1,sum=0;
        while(j<n){
            if(2*i+1<k){
                m[s[j]]++;
                m[s[l]]++;
                j+=k;l-=k;
                sum+=2;
            }
            else{
                m[s[j]]++;
                j+=k;
                sum+=1;
            }
        }
        frj(26){
            maxi=max(m[j+97],maxi);
        }
        res+=sum-maxi;
    }
    cout<<res<<endl;
    return;
}
int main()
{
    int t;cin>>t;while(t--){
        solve();
    }
}


#include <iostream>
#include <cstring>

using namespace std;
const int MAXN = 200005;
int n, k, bucket[300], len, t, ans, zhong, vis[MAXN], cnt;
char s[MAXN];

int main() {
    cin >> t;
    for (int i = 1; i <= t; ++i) {
        cin >> n >> k >> s;
        len = strlen(s);
        ans = 0;
        for (int j = 0; j < k; ++j) {
            memset(bucket, 0, sizeof(bucket));
            zhong = 0;
            cnt = 0;
            for (int l = j; l < len; l += k) {
                if (vis[l] != i) {
                    vis[l] = i;
                    bucket[s[l]]++;
                    cnt++;
                }
                if (vis[len - 1 - l] != i) {
                    vis[len - 1 - l] = i;
                    bucket[s[len - 1 - l]]++;
                    cnt++;
                }
            }
            for (int i = 'a'; i <= 'z'; ++i) {
                zhong = max(zhong, bucket[i]);
            }
            ans += cnt - zhong;
        }
        cout << ans << endl;
    }
    return 0;
}


#include <iostream>
#include <cstring>

using namespace std;
const int MAXN = 200005;
int n, k, bucket[300], len, t, ans, zhong, vis[MAXN];
char s[MAXN];

int main() {
    cin >> t;
    for (int i = 1; i <= t; ++i) {
        cin >> n >> k >> s;
        len = strlen(s);
        ans = 0;
        for (int j = 0; j < k; ++j) {
            memset(bucket, 0, sizeof(bucket));
            zhong = 0;
            for (int l = j; l < len; l += k) {
                if (vis[l] != i) {
                    vis[l] = i;
                    bucket[s[l]]++;
                }
                if (vis[len - 1 - l] != i) {
                    vis[len - 1 - l] = i;
                    bucket[s[len - 1 - l]]++;
                }
            }
            for (int i = 'a'; i <= 'z'; ++i) {
                zhong = max(zhong, bucket[i]);
            }
//            cout << i << " " << zhong << endl;
            ans += n / k - zhong;
        }
        cout << ans << endl;
    }
    return 0;
}


#include <iostream>
#include <cstring>

using namespace std;
const int MAXN = 200005;
int n, k, bucket[300], len, t, ans, zhong, vis[MAXN], cnt;
char s[MAXN];

int main() {
    cin >> t;
    for (int i = 1; i <= t; ++i) {
        cin >> n >> k >> s;
        len = strlen(s);
        ans = 0;
        for (int j = 0; j < k; ++j) {
            memset(bucket, 0, sizeof(bucket));
            zhong = 0;
            cnt = 0;
            for (int l = j; l < len; l += k) {
                if (vis[l] != i) {
                    vis[l] = i;
                    bucket[s[l]]++;
                    cnt++;
                }
                if (vis[len - 1 - l] != i) {
                    vis[len - 1 - l] = i;
                    bucket[s[len - 1 - l]]++;
                    cnt++;
                }
            }
            for (int i = 'a'; i <= 'z'; ++i) {
                zhong = max(zhong, bucket[i]);
            }
            ans += cnt - zhong;
        }
        cout << ans << endl;
    }
    return 0;

}


#include<bits/stdc++.h>
#define TC int t;cin>>t;while(t--)
#define rep(i,n) for(int i = 0 ; i < n ; i++)
#define repA(i, a, n) for(int i = a ; i < n ; i++)
#define repD(i, a, n) for(int i = n ; i > a ; i--)
#define MOD 1000000007
#define ff first
#define ss second
#define pii pair<int, int>
#define vi vector<int>
#define pb push_back
#define mkpr make_pair
typedef long long ll;
using namespace std;

int main(){
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	TC{
		int n, k;
		string inp;
		cin>>n>>k>>inp;
		int ** posvalues = new int * [k];
		rep(i, k){
			posvalues[i] = new int[26];
			rep(j, 26) posvalues[i][j] = 0;
		}
		int minedit = INT_MAX;
		int numwords = n/k;
		rep(i, numwords){
			string word = inp.substr(i * k, k);
			rep(j, k){
				posvalues[j][word[j] - 'a']++;
			}
		}
		rep(i, numwords){
			string word = inp.substr(i * k, k);
			int changes = 0;
			for(int j = 0 ; j < k/2 ; j++){
				int a1 = word[j] - 'a';
				int a2 = word[k - j - 1] - 'a';
				int c1 = 2 * numwords - posvalues[j][a1] - posvalues[k - j - 1][a1];
				int c2 = 2 * numwords - posvalues[k - j - 1][a2] - posvalues[j][a2];
				changes += min(c1, c2);
			}
			if(k % 2){
				changes += numwords - posvalues[k/2 + 1][word[k/2 + 1] - 'a'];
			}
			minedit = min(changes, minedit);
		}
		rep(i, k) delete [] posvalues[i];
		delete [] posvalues;
		cout<<minedit<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
#define TC int t;cin>>t;while(t--)
#define rep(i,n) for(int i = 0 ; i < n ; i++)
#define repA(i, a, n) for(int i = a ; i < n ; i++)
#define repD(i, a, n) for(int i = n ; i > a ; i--)
#define MOD 1000000007
#define ff first
#define ss second
#define pii pair<int, int>
#define vi vector<int>
#define pb push_back
#define mkpr make_pair
typedef long long ll;
using namespace std;

int main(){
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	TC{
		int n, k;
		string inp;
		cin>>n>>k>>inp;
		int ** posvalues = new int * [k];
		rep(i, k){
			posvalues[i] = new int[26];
			rep(j, 26) posvalues[i][j] = 0;
		}
		int minedit = INT_MAX;
		int numwords = n/k;
		rep(i, numwords){
			string word = inp.substr(i * k, k);
			rep(j, k){
				posvalues[j][word[j] - 'a']++;
			}
		}
		if(k == 1){
			int ans = INT_MAX;
			rep(i, 26){
				ans = min(ans, numwords - posvalues[0][i]);
			}
			cout<<ans<<endl;
			continue;
		}
		rep(i, numwords){
			string word = inp.substr(i * k, k);
			int changes = 0;
			for(int j = 0 ; j < k/2 ; j++){
				int a1 = word[j] - 'a';
				int a2 = word[k - j - 1] - 'a';
				int c1 = 2 * numwords - posvalues[j][a1] - posvalues[k - j - 1][a1];
				int c2 = 2 * numwords - posvalues[k - j - 1][a2] - posvalues[j][a2];
				changes += min(c1, c2);
			}
			if(k % 2){
				changes += numwords - posvalues[k/2 + 1][word[k/2 + 1] - 'a'];
			}
			minedit = min(changes, minedit);
		}
		rep(i, k) delete [] posvalues[i];
		delete [] posvalues;
		cout<<minedit<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
#define TC int t;cin>>t;while(t--)
#define rep(i,n) for(int i = 0 ; i < n ; i++)
#define repA(i, a, n) for(int i = a ; i < n ; i++)
#define repD(i, a, n) for(int i = n ; i > a ; i--)
#define MOD 1000000007
#define ff first
#define ss second
#define pii pair<int, int>
#define vi vector<int>
#define pb push_back
#define mkpr make_pair
typedef long long ll;
using namespace std;

int main(){
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	TC{
		int n, k;
		string inp;
		cin>>n>>k>>inp;
		int ** posvalues = new int * [k];
		int minchanges = 0;
		int numwords = n/k;
		rep(i, k){
			posvalues[i] = new int[26];
			rep(j, 26) posvalues[i][j] = 0;
		}
		rep(i, numwords){
			string word = inp.substr(i * k, k);
			rep(j, k){
				posvalues[j][word[j] - 'a']++;
			}
		}
		rep(i, k/2){
			int minv = INT_MAX;
			rep(j, 26){
				int c = 2 * numwords - posvalues[i][j] - posvalues[k - i - 1][j];
				minv = min(minv, c);
			}
			minchanges += minv;
		}
		if(k%2){
			int pos = (k == 1 ? 0 : k/2 + 1);
			int minv = INT_MAX;
			rep(i, 26){
				minv = min(minv, numwords - posvalues[pos][i]);
			}
			minchanges += minv;
		}
		cout<<minchanges<<endl;
		rep(i, k) delete [] posvalues[i];
		delete [] posvalues;
	}

	return 0;
}


#include<bits/stdc++.h>
#define TC int t;cin>>t;while(t--)
#define rep(i,n) for(int i = 0 ; i < n ; i++)
#define repA(i, a, n) for(int i = a ; i < n ; i++)
#define repD(i, a, n) for(int i = n ; i > a ; i--)
#define MOD 1000000007
#define ff first
#define ss second
#define pii pair<int, int>
#define vi vector<int>
#define pb push_back
#define mkpr make_pair
typedef long long ll;
using namespace std;

int main(){
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	TC{
		int n, k;
		string inp;
		cin>>n>>k>>inp;
		vector<vector<int> >posvalues(k, vector<int>(26, 0));
		int minedit = INT_MAX;
		int numwords = n/k;
		rep(i, numwords){
			string word = inp.substr(i * k, k);
			rep(j, k){
				posvalues[j][word[j] - 'a']++;
			}
		}
		rep(i, numwords){
			string word = inp.substr(i * k, k);
			int changes = 0;
			for(int j = 0 ; j < k/2 ; j++){
				int a1 = word[j] - 'a';
				int a2 = word[k - j - 1] - 'a';
				int c1 = 2 * numwords - posvalues[j][a1] - posvalues[k - j - 1][a1];
				int c2 = 2 * numwords - posvalues[k - j - 1][a2] - posvalues[j][a2];
				changes += min(c1, c2);
			}
			if(k % 2){
				changes += numwords - posvalues[k/2 + 1][word[k/2 + 1] - 'a'];
			}
			minedit = min(changes, minedit);
		}
		cout<<minedit<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
#define TC int t;cin>>t;while(t--)
#define rep(i,n) for(int i = 0 ; i < n ; i++)
#define repA(i, a, n) for(int i = a ; i < n ; i++)
#define repD(i, a, n) for(int i = n ; i > a ; i--)
#define MOD 1000000007
#define ff first
#define ss second
#define pii pair<int, int>
#define vi vector<int>
#define pb push_back
#define mkpr make_pair
typedef long long ll;
using namespace std;

int main(){
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	TC{
		int n, k;
		string inp;
		cin>>n>>k>>inp;
		int ** posvalues = new int * [k];
		int minchanges = 0;
		int numwords = n/k;
		rep(i, k){
			posvalues[i] = new int[26];
			rep(j, 26) posvalues[i][j] = 0;
		}
		rep(i, numwords){
			string word = inp.substr(i * k, k);
			rep(j, k){
				posvalues[j][word[j] - 'a']++;
			}
		}
		rep(i, k/2){
			int minv = INT_MAX;
			rep(j, 26){
				int c = 2 * numwords - posvalues[i][j] - posvalues[k - i - 1][j];
				minv = min(minv, c);
			}
			minchanges += minv;
		}
		if(k%2){
			int pos = (k == 1 ? 0 : k/2);
			int minv = INT_MAX;
			rep(i, 26){
				minv = min(minv, numwords - posvalues[pos][i]);
			}
			minchanges += minv;
		}
		cout<<minchanges<<endl;
		rep(i, k) delete [] posvalues[i];
		delete [] posvalues;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << #x << " = " << x << endl;
int main(){
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int t;
	cin >> t;
	while(t--){
		int n, k;
		cin >> n >> k;
		string s;
		cin >> s;
		vector<int> a[n];
		for(int i=0; i<k; i++){
			for(int j=i+k; j<n; j+=k){
				a[i].push_back(j);
				a[j].push_back(i);
			}
			a[i].push_back(n-1-i);
			if(n-1-i!=i) a[n-1-i].push_back(i);
		}
		int res=0;
		vector<bool> mark(n, 0);
		for(int i=0; i<n; i++){
			if(mark[i]==1) continue;
			vector<int> cnt(26,0);
			cnt[s[i]-'a']++;
			deque<int> q;
			q.push_front(i);
			mark[i]=1;
//			debug(11111111)
			while(!q.empty()){
				int r=q.front();
				for(int j=0; j<a[r].size(); j++){
					if(mark[a[r][j]]==1) continue;
					mark[a[r][j]]=1;
					q.push_back(a[r][j]);
					cnt[s[a[r][j]]-'a']++;
				}
				q.pop_front();
//				debug(333)
			}
//			debug(22222222)
			auto it=max_element(cnt.begin(), cnt.end());
			int sum=0;
			for(auto val: cnt) sum+=val;
			res+=sum-*it;
		}
		cout << res << "\n";
	}
}


#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << #x << " = " << x << endl;
int main(){
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int t;
	cin >> t;
	while(t--){
		int n, k;
		cin >> n >> k;
		string s;
		cin >> s;
		vector<int> a[n];
		for(int i=0; i<k; i++){
			for(int j=i+k; j<n; j+=k){
				a[i].push_back(j);
			}
		}
		for(int i=0; i<n/2; i++){
		    a[i].push_back(n-1-i);
		}
		int res=0;
		vector<bool> mark(n, 0);
		for(int i=0; i<n; i++){
			if(mark[i]==1) continue;
			vector<int> cnt(26,0);
			cnt[s[i]-'a']++;
			deque<int> q;
			q.push_front(i);
			mark[i]=1;
			while(!q.empty()){
				int r=q.front();
				for(int j=0; j<a[r].size(); j++){
					if(mark[a[r][j]]==1) continue;
					mark[a[r][j]]=1;
					q.push_back(a[r][j]);
					cnt[s[a[r][j]]-'a']++;
				}
				q.pop_front();
			}
			auto it=max_element(cnt.begin(), cnt.end());
			int sum=0;
			for(auto val: cnt) sum+=val;
			res+=sum-*it;
		}
		cout << res << "\n";
	}
}


#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << #x << " = " << x << endl;
int main(){
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int t;
	cin >> t;
	while(t--){
		int n, k;
		cin >> n >> k;
		string s;
		cin >> s;
		vector<int> a[n];
		for(int i=0; i<k; i++){
			for(int j=i+k; j<n; j+=k){
				a[i].push_back(j);
				a[j].push_back(i);
			}
		}
		for(int i=0; i<n/2; i++){
		    a[i].push_back(n-1-i);
		}
		int res=0;
		vector<bool> mark(n, 0);
		for(int i=0; i<n; i++){
			if(mark[i]==1) continue;
			vector<int> cnt(26,0);
			cnt[s[i]-'a']++;
			deque<int> q;
			q.push_front(i);
			mark[i]=1;
			while(!q.empty()){
				int r=q.front();
				for(int j=0; j<a[r].size(); j++){
					if(mark[a[r][j]]==1) continue;
					mark[a[r][j]]=1;
					q.push_back(a[r][j]);
					cnt[s[a[r][j]]-'a']++;
				}
				q.pop_front();
			}
			auto it=max_element(cnt.begin(), cnt.end());
			int sum=0;
			for(auto val: cnt) sum+=val;
			res+=sum-*it;
		}
		cout << res << "\n";
	}
}


#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << #x << " = " << x << endl;
int main(){
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	int t;
	cin >> t;
	while(t--){
		int n, k;
		cin >> n >> k;
		string s;
		cin >> s;
		vector<int> a[n];
		for(int i=0; i<k; i++){
			for(int j=i+k; j<n; j+=k){
				a[i].push_back(j);
				a[j].push_back(i);
			}
		}
		for(int i=0; i<n/2; i++){
		    a[i].push_back(n-1-i);
		    a[n-1-i].push_back(i);
		}
		int res=0;
		vector<bool> mark(n, 0);
		for(int i=0; i<n; i++){
			if(mark[i]==1) continue;
			vector<int> cnt(26,0);
			cnt[s[i]-'a']++;
			deque<int> q;
			q.push_front(i);
			mark[i]=1;
			while(!q.empty()){
				int r=q.front();
				for(int j=0; j<a[r].size(); j++){
					if(mark[a[r][j]]==1) continue;
					mark[a[r][j]]=1;
					q.push_back(a[r][j]);
					cnt[s[a[r][j]]-'a']++;
				}
				q.pop_front();
			}
			auto it=max_element(cnt.begin(), cnt.end());
			int sum=0;
			for(auto val: cnt) sum+=val;
			res+=sum-*it;
		}
		cout << res << "\n";
	}
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <vector>
#define N 500010
#define ls x << 1
#define rs x << 1 | 1
#define inf 10000000000
#define inc(i) (++ (i))
#define dec(i) (-- (i))
// #define mid ((l + r) >> 1)
// #define int long long
#define XRZ 998244353
#define debug() puts("XRZ TXDY");
#define mem(i, x) memset(i, x, sizeof(i));
#define Next(i, u) for(register int i = head[u]; i ; i = e[i].nxt)
#define file(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout);
#define Rep(i, a, b) for(register int i = (a) , i##Limit = (b) ; i <= i##Limit ; inc(i))
#define Dep(i, a, b) for(register int i = (a) , i##Limit = (b) ; i >= i##Limit ; dec(i))
int dx[10] = {1, -1, 0, 0};
int dy[10] = {0, 0, 1, -1};
using namespace std;
inline int read() {
    register int x = 0, f = 1; register char c = getchar();
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - 48, c = getchar();
    return x * f;
}
int p[N][40], n, k, T;
char s[N]; void work() {}
signed main() { T = read(); int yy = 1 * 87 - 87 % 45 + 78 / 89 * 21;
    while(T --) { n = read(), k = read(); scanf("%s", s + 1);
		Rep(i, 1, n) Rep(j, 0, 30) work(), p[i][j] = 0; int ans = 0;
		Rep(i, 1, (k + 1) / 2) { work(); int now = 0;
			for(int j = 0; i + j * k <= n; inc(j)) { int qwq = i + j * k;
				inc(p[i][s[i + j * k] - 'a' + 1]); inc(now);
				if(qwq != (k - i + 1) + j * k) inc(p[i][s[(k - i + 1) + j * k] - 'a' + 1]), inc(now);
			} int QwQ = 0;
			Rep(j, 1, 26) QwQ = max(p[i][j], QwQ);
			ans += now - QwQ;
		} printf("%d\n", ans);
	} return 0; int zmy = 23333, zzz = 66666, cyz = 23333;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <vector>
#define N 200010
#define ls x << 1
#define rs x << 1 | 1
#define inf 10000000000
#define inc(i) (++ (i))
#define dec(i) (-- (i))
// #define mid ((l + r) >> 1)
#define int long long
#define XRZ 998244353
#define debug() puts("XRZ TXDY");
#define mem(i, x) memset(i, x, sizeof(i));
#define Next(i, u) for(register int i = head[u]; i ; i = e[i].nxt)
#define file(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout);
#define Rep(i, a, b) for(register int i = (a) , i##Limit = (b) ; i <= i##Limit ; inc(i))
#define Dep(i, a, b) for(register int i = (a) , i##Limit = (b) ; i >= i##Limit ; dec(i))
int dx[10] = {1, -1, 0, 0};
int dy[10] = {0, 0, 1, -1};
using namespace std;
inline int read() {
    register int x = 0, f = 1; register char c = getchar();
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - 48, c = getchar();
    return x * f;
}
int T, n, k, f[N];
char s[N]; vector<int> v[N]; void work() {}
int find(int x) { return x == f[x] ? x : find(f[x]);}
signed main() { T = read(); int yy = 1 * 87 - 87 % 45 + 78 / 89 * 21;
    while(T --) { n = read(), k = read(); scanf("%s", s + 1);
        Rep(i, 1, n) work(), f[i] = i; Rep(i, 1, n - k) f[find(i)] = find(f[i + k]);
        Rep(i, 1, n / 2) work(), f[find(i)] = find(f[n + 1 - i]); Rep(i, 1, n) v[i].clear();
        Rep(i, 1, n) work(), v[find(f[i])].push_back(i); int ans = 0;
        Rep(i, 1, n) { if(!v[i].size()) continue;
            map<int, int> mp; Rep(j, 0, v[i].size() - 1) inc(mp[s[v[i][j]]]); int _max = 0;
            for(map<int,int>::iterator it = mp.begin(); it != mp.end(); it ++) _max = max(_max, it -> second);
            ans += v[i].size() - _max;
        } printf("%d\n", ans);
    }
    return 0; int zmy = 23333, zzz = 66666, cyz = 23333;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <vector>
#define N 200010
#define ls x << 1
#define rs x << 1 | 1
#define inf 10000000000
#define inc(i) (++ (i))
#define dec(i) (-- (i))
// #define mid ((l + r) >> 1)
// #define int long long
#define XRZ 998244353
#define debug() puts("XRZ TXDY");
#define mem(i, x) memset(i, x, sizeof(i));
#define Next(i, u) for(register int i = head[u]; i ; i = e[i].nxt)
#define file(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout);
#define Rep(i, a, b) for(register int i = (a) , i##Limit = (b) ; i <= i##Limit ; inc(i))
#define Dep(i, a, b) for(register int i = (a) , i##Limit = (b) ; i >= i##Limit ; dec(i))
int dx[10] = {1, -1, 0, 0};
int dy[10] = {0, 0, 1, -1};
using namespace std;
inline int read() {
    register int x = 0, f = 1; register char c = getchar();
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - 48, c = getchar();
    return x * f;
}
int T, n, k, f[N];
char s[N]; vector<int> v[N]; void work() {}
int find(int x) { return x == f[x] ? x : find(f[x]);}
signed main() { T = read(); int yy = 1 * 87 - 87 % 45 + 78 / 89 * 21;
    while(T --) { n = read(), k = read(); scanf("%s", s + 1);
        Rep(i, 1, n) f[i] = i; Rep(i, 1, n - k) f[find(i)] = find(f[i + k]);
        Rep(i, 1, n / 2) work(), f[find(i)] = find(f[n + 1 - i]); Rep(i, 1, n) v[i].clear();
        Rep(i, 1, n) v[find(f[i])].push_back(i); int ans = 0;
        Rep(i, 1, n) { if(!v[i].size()) continue;
            map<int, int> mp; Rep(j, 0, v[i].size() - 1) inc(mp[s[v[i][j]]]); int _max = 0;
            for(map<int,int>::iterator it = mp.begin(); it != mp.end(); it ++) _max = max(_max, it -> second);
            ans += v[i].size() - _max;
        } printf("%d\n", ans);
    }
    return 0; int zmy = 23333, zzz = 66666, cyz = 23333;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <unordered_map>
#include <queue>
#include <stack>
#include <cmath>
#include <functional>
#include <deque>
#include <bitset>
#include <climits>
#include <cstdio>
#include <list>
#include <iomanip>
using namespace std;

#define ll long long int
#define mp make_pair
#define pb push_back
#define fi first
#define si second
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define mod 1000000007

class dsu{
	ll* parent,*rank;
	ll totalComponents;
public:

	dsu(ll n){
		parent = new ll[n+1];
		rank = new ll[n+1];
		for(ll i = 0 ; i < n ; i++){
			parent[i] = i;
			rank[i] = 0;
		}
		totalComponents = n;
	}

	ll get(ll v){
		if(v == parent[v])
			return v;

		return parent[v] = get(parent[v]);
	}

	void union_set(ll a,ll b){
		a = get(a);
		b= get(b);
		if(a != b){
			if(rank[a]<rank[b])
				swap(a,b);

			parent[b] = a;
			if(rank[a] == rank[b])
				rank[a]++;

			totalComponents --;
		}
	}
};

ll n,k;
string s;
ll c[200005][26]={0};

int main(){
	fastIO
	#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif

	ll t;
	cin>>t;

	while(t--){
		cin>>n>>k>>s;
		dsu g(n);
		for(ll i = 0 ; i < n ; i++){
			g.union_set(i,n-1-i);
		}

		for(ll i = 0 ; i < n-k ; i++){
			g.union_set(i,i+k);
		}

		for(ll i = 0 ; i < n ; i++)
			for(ll j = 0 ; j < 26 ; j++)
				c[i][j] = 0;

		for(ll i = 0 ; i < n ; i++){
			c[g.get(i)][s[i]-'a']++;
		}

		ll ans = 0 ;
		for(ll i = 0 ; i < n ; i++){
			if(g.get(i) == i){
				ll sum = 0 , mx = 0;
				for(ll j = 0 ; j < 26 ; j++){
					sum += c[i][j];
					mx = max(c[i][j],mx);
				}
				ans += sum-mx;
			}
		}
		cout<<ans<<endl;
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <unordered_map>
#include <queue>
#include <stack>
#include <cmath>
#include <functional>
#include <deque>
#include <bitset>
#include <climits>
#include <cstdio>
#include <list>
#include <iomanip>
using namespace std;

#define ll long long int
#define mp make_pair
#define pb push_back
#define fi first
#define si second
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define mod 1000000007

class dsu{
	vector<ll> parent,rank;
	ll totalComponents;
public:

	dsu(ll n){
		parent.resize(n);
		rank.resize(n);
		for(ll i = 0 ; i < n ; i++){
			parent[i] = i;
			rank[i] = 0;
		}
		totalComponents = n;
	}

	ll get(ll v){
		if(v == parent[v])
			return v;

		return parent[v] = get(parent[v]);
	}

	void union_set(ll a,ll b){
		a = get(a);
		b= get(b);
		if(a != b){
			if(rank[a]<rank[b])
				swap(a,b);

			parent[b] = a;
			if(rank[a] == rank[b])
				rank[a]++;

			totalComponents --;
		}
	}
};

ll n,k;
string s;
ll c[200005][26]={0};

int main(){
	fastIO
	#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif

	ll t;
	cin>>t;

	while(t--){
		cin>>n>>k>>s;
		dsu g(n);
		for(ll i = 0 ; i < n ; i++){
			g.union_set(i,n-1-i);
		}

		for(ll i = 0 ; i < n-k ; i++){
			g.union_set(i,i+k);
		}

		for(ll i = 0 ; i < n ; i++)
			for(ll j = 0 ; j < 26 ; j++)
				c[i][j] = 0;

		for(ll i = 0 ; i < n ; i++){
			c[g.get(i)][s[i]-'a']++;
		}

		ll ans = 0 ;
		for(ll i = 0 ; i < n ; i++){
			if(g.get(i) == i){
				ll sum = 0 , mx = 0;
				for(ll j = 0 ; j < 26 ; j++){
					sum += c[i][j];
					mx = max(c[i][j],mx);
				}
				ans += sum-mx;
			}
		}
		cout<<ans<<endl;
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <unordered_map>
#include <queue>
#include <stack>
#include <cmath>
#include <functional>
#include <deque>
#include <bitset>
#include <climits>
#include <cstdio>
#include <list>
#include <iomanip>
using namespace std;

#define ll long long int
#define mp make_pair
#define pb push_back
#define fi first
#define si second
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL);
#define AS 200005
#define mod 1000000007

class dsu{
	vector<ll> parent,rank;
	ll totalComponents;
public:

	dsu(ll n){
		parent.resize(n);
		rank.resize(n);
		for(ll i = 0 ; i < n ; i++){
			parent[i] = i;
			rank[i] = 0;
		}
		totalComponents = n;
	}

	ll get(ll v){
		if(v == parent[v])
			return v;

		return parent[v] = get(parent[v]);
	}

	void union_set(ll a,ll b){
		a = get(a);
		b= get(b);
		if(a != b){
			if(rank[a]<rank[b])
				swap(a,b);

			parent[b] = a;
			if(rank[a] == rank[b])
				rank[a]++;

			totalComponents --;
		}
	}
};

ll n,k;
string s;

ll solve(){

	cin>>n>>k>>s;
	dsu g(n+2);
	for(ll i = 0 ; i < n ; i++){
		g.union_set(i,n-1-i);
	}

	for(ll i = 0 ; i < n-k ; i++){
		g.union_set(i,i+k);
	}

	ll c[AS][26]={0};
	for(ll i = 0 ; i < n ; i++){
		c[g.get(i)][s[i]-'a']++;
	}

	ll ans = 0 ;
	for(ll i = 0 ; i < n ; i++){
		if(g.get(i) == i){
			ll sum = 0 , mx = 0;
			for(ll j = 0 ; j < 26 ; j++){
				sum += c[i][j];
				mx = max(c[i][j],mx);
			}
			ans += sum-mx;
		}
	}
	return ans;
}

int main(){
	fastIO
	#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif

	ll t;
	cin>>t;

	while(t--){
		cout<<solve()<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, x, n) for(int i = x; i < n; i++)

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int INF = 1e9, MAXN = 2e5 + 999;
int n, k, cnt[MAXN][26];
string s;

int diff(int u, int v)
{
    int ret = 0, mx = 0;
    for (int j = 0; j < 26; j++) {
        ret += cnt[u][j] + cnt[v][j];
        mx = max(mx, cnt[u][j] + cnt[v][j]);
    }
    return ret - mx;
}

void solve()
{
    memset(cnt, 0, sizeof(cnt));
    for (int i = 0; i < n; i++) {
        cnt[i % k][s[i] - 'a']++;
    }

    int res = 0;
    for (int i = 0; i < k; i++)
        res += diff(i, k - 1 - i);
    cout << res / 2 << endl;
}

void read()
{
    cin >> n >> k >> s;
}

int main()
{
    ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);
    int TC;
    cin >> TC;
    while (TC--) {
        read();
        solve();
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, x, n) for(int i = x; i < n; i++)

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int INF = 1e9, MAXN = 2e5 + 999;
int n, k, cnt[MAXN][26];
string s;

int diff(int u, int v)
{
    int ret = 0, mx = 0;
    for (int j = 0; j < 26; j++) {
        ret += cnt[u][j] + cnt[v][j];
        mx = max(mx, cnt[u][j] + cnt[v][j]);
    }

    return ret - mx;
}

int main()
{
    ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);
    int TC;
    cin >> TC;
    while (TC--) {
        cin >> n >> k >> s;

        for (int i=0;i<k;++i){
            for (int j=0;j<26;++j){
                cnt[i][j]=0;
            }
        }

        for (int i = 0; i < n; i++) {
            cnt[i % k][s[i] - 'a']++;
        }

        int res = 0;
        for (int i = 0; i < k; i++)
            res += diff(i, k - 1 - i);
        cout << res / 2 << endl;
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, x, n) for(int i = x; i < n; i++)

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int INF = 1e9, MAXN = 2e5 + 999;
int n, k, cnt[MAXN][26];
string s;

int diff(int u, int v)
{
    int ret = 0, mx = 0;
    for (int j = 0; j < 26; j++) {
        ret += cnt[u][j] + cnt[v][j];
        mx = max(mx, cnt[u][j] + cnt[v][j]);
    }

    return ret - mx;
}

inline void solve()
{
    for (int i=0;i<k;++i){
        for (int j=0;j<26;++j){
            cnt[i][j]=0;
        }
    }

    for (int i = 0; i < n; i++) {
        cnt[i % k][s[i] - 'a']++;
    }

    int res = 0;
    for (int i = 0; i < k; i++)
        res += diff(i, k - 1 - i);
    cout << res / 2 << endl;
}

inline void read()
{
    cin >> n >> k >> s;
}

int main()
{
    ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);
    int TC;
    cin >> TC;
    while (TC--) {
        read();
        solve();

    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, x, n) for(int i = x; i < n; i++)

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

const int INF = 1e9, MAXN = 2e5 + 999;
int n, k, cnt[MAXN][26];
string s;

int diff(int u, int v)
{
    int ret = 0, mx = 0;
    for (int j = 0; j < 26; j++) {
        ret += cnt[u][j] + cnt[v][j];
        mx = max(mx, cnt[u][j] + cnt[v][j]);
    }
}

int main()
{
    ios::sync_with_stdio(0); cin.tie(0), cout.tie(0);
    int TC;
    cin >> TC;
    while (TC--) {
        cin >> n >> k >> s;
        memset(cnt, 0, sizeof(cnt));
        for (int i = 0; i < n; i++) {
            cnt[i % k][s[i] - 'a']++;
        }

        int res = 0;
        for (int i = 0; i < k; i++)
            res += diff(i, k - 1 - i);
        cout << res / 2 << endl;
    }

    return 0;
}


#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
int head[200005],cnt;
struct edge{
	int v,nxt;
}a[1000005];
void add_edge(int u,int v){
	cnt++;
	a[cnt].v=v;
	a[cnt].nxt=head[u];
	head[u]=cnt;
}
bool book[200005];
vector<int> vec1;
void dfs(int u){
	book[u]=1;
	vec1.push_back(u);
	for(int i=head[u];i!=-1;i=a[i].nxt){
		if(book[a[i].v]==0)dfs(a[i].v);
	}
}
int x[100];
int main(){
	ios::sync_with_stdio(false);
	int T;
	int n,k;
	cin>>T;
	memset(head,-1,sizeof(head));
	memset(book,0,sizeof(book));
	while(T--){
		for(int i=0;i<=n;i++)book[i]=0,head[i]=-1;
		cnt=0;
		cin>>n>>k;
		string s;
		cin>>s;
		for(int i=0;i<n;i++){
			if(i+k<n){
				add_edge(i,i+k);
				add_edge(i+k,i);
			}
			add_edge(i,n-i-1);
		}
		int ans=n;
		for(int i=0;i<n;i++){
			if(book[i]!=0)continue;
			dfs(i);
			memset(x,0,sizeof(x));
			for(int j=0;j<vec1.size();j++){
				//cout<<vec1[j]<<' ';
				x[s[vec1[j]]-'a']++;
			}
			int sum=0;
			for(int i=0;i<26;i++)sum=max(sum,x[i]);
			ans-=sum;
			vec1.clear();
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
int head[200005],cnt;
struct edge{
	int v,nxt;
}a[1000005];
void add_edge(int u,int v){
	cnt++;
	a[cnt].v=v;
	a[cnt].nxt=head[u];
	head[u]=cnt;
}
bool book[200005];
vector<int> vec1;
void dfs(int u){
	book[u]=1;
	vec1.push_back(u);
	for(int i=head[u];i!=-1;i=a[i].nxt){
		if(book[a[i].v]==0)dfs(a[i].v);
	}
}
int x[100];
int main(){
	ios::sync_with_stdio(false);
	int T;
	cin>>T;
	while(T--){
		memset(head,-1,sizeof(head));
		memset(book,0,sizeof(book));
		cnt=0;
		int n,k;
		cin>>n>>k;
		string s;
		cin>>s;
		for(int i=0;i<n;i++){
			if(i+k<n){
				add_edge(i,i+k);
				add_edge(i+k,i);
			}
			add_edge(i,n-i-1);
		}
		int ans=n;
		for(int i=0;i<n;i++){
			if(book[i]!=0)continue;
			dfs(i);
			memset(x,0,sizeof(x));
			for(int j=0;j<vec1.size();j++){
				//cout<<vec1[j]<<' ';
				x[s[vec1[j]]-'a']++;
			}
			int sum=0;
			for(int i=0;i<26;i++)sum=max(sum,x[i]);
			ans-=sum;
			vec1.clear();
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
int head[200005],cnt;
struct edge{
	int v,nxt;
}a[1000005];
void add_edge(int u,int v){
	cnt++;
	a[cnt].v=v;
	a[cnt].nxt=head[u];
	head[u]=cnt;
}
bool book[200005];
vector<int> vec1;
void dfs(int u){
	book[u]=1;
	vec1.push_back(u);
	for(int i=head[u];i!=-1;i=a[i].nxt){
		if(book[a[i].v]==0)dfs(a[i].v);
	}
}
int x[105];
int main(){
	ios::sync_with_stdio(false);
	int T;
	int n=0,k=0;
	cin>>T;
	memset(head,-1,sizeof(head));
	memset(book,0,sizeof(book));
	while(T--){
		for(int i=0;i<=n;i++)book[i]=0,head[i]=-1;
		cnt=0;
		cin>>n>>k;
		string s;
		cin>>s;
		for(int i=0;i<n;i++){
			if(i+k<n){
				add_edge(i,i+k);
				add_edge(i+k,i);
			}
			add_edge(i,n-i-1);
		}
		int ans=n;
		for(int i=0;i<n;i++){
			if(book[i]!=0)continue;
			dfs(i);
			memset(x,0,sizeof(x));
			for(int j=0;j<vec1.size();j++){
				//cout<<vec1[j]<<' ';
				x[s[vec1[j]]-'a']++;
			}
			int sum=0;
			for(int i=0;i<26;i++)sum=max(sum,x[i]);
			ans-=sum;
			vec1.clear();
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
int head[200005],cnt;
struct edge{
	int v,nxt;
}a[1000005];
void add_edge(int u,int v){
	cnt++;
	a[cnt].v=v;
	a[cnt].nxt=head[u];
	head[u]=cnt;
}
bool book[200005];
vector<int> vec1;
void dfs(int u){
	book[u]=1;
	vec1.push_back(u);
	for(int i=head[u];i!=-1;i=a[i].nxt){
		if(book[a[i].v]==0)dfs(a[i].v);
	}
}
int x[100];
int main(){
	int T;
	cin>>T;
	while(T--){
		memset(head,-1,sizeof(head));
		memset(book,0,sizeof(book));
		cnt=0;
		int n,k;
		cin>>n>>k;
		string s;
		cin>>s;
		for(int i=0;i<n;i++){
			if(i+k<n){
				add_edge(i,i+k);
				add_edge(i+k,i);
			}
			add_edge(i,n-i-1);
		}
		int ans=n;
		for(int i=0;i<n;i++){
			if(book[i]!=0)continue;
			dfs(i);
			memset(x,0,sizeof(x));
			for(int j=0;j<vec1.size();j++){
				//cout<<vec1[j]<<' ';
				x[s[vec1[j]]-'a']++;
			}
			int sum=0;
			for(int i=0;i<26;i++)sum=max(sum,x[i]);
			ans-=sum;
			vec1.clear();
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
int head[200005],cnt;
struct edge{
	int v,nxt;
}a[1000005];
void add_edge(int u,int v){
	cnt++;
	a[cnt].v=v;
	a[cnt].nxt=head[u];
	head[u]=cnt;
}
bool book[200005];
vector<int> vec1;
void dfs(int u){
	book[u]=1;
	vec1.push_back(u);
	for(int i=head[u];i!=-1;i=a[i].nxt){
		if(book[a[i].v]==0)dfs(a[i].v);
	}
}
int x[100];
int main(){
	ios::sync_with_stdio(false);
	int T;
	int n=0,k=0;
	cin>>T;
	memset(head,-1,sizeof(head));
	memset(book,0,sizeof(book));
	while(T--){
		for(int i=0;i<=n;i++)book[i]=0,head[i]=-1;
		cnt=0;
		cin>>n>>k;
		string s;
		cin>>s;
		for(int i=0;i<n;i++){
			if(i+k<n){
				add_edge(i,i+k);
				add_edge(i+k,i);
			}
			add_edge(i,n-i-1);
		}
		int ans=n;
		for(int i=0;i<n;i++){
			if(book[i]!=0)continue;
			dfs(i);
			memset(x,0,sizeof(x));
			for(int j=0;j<vec1.size();j++){
				//cout<<vec1[j]<<' ';
				x[s[vec1[j]]-'a']++;
			}
			int sum=0;
			for(int i=0;i<26;i++)sum=max(sum,x[i]);
			ans-=sum;
			vec1.clear();
		}
		cout<<ans<<endl;
	}
	return 0;
}


// Problem : C. K-Complete Word
// Contest : Codeforces Round #630 (Div. 2)
// URL : https://codeforces.com/contest/1332/problem/C
// Memory Limit : 512 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
using namespace std;

const int maxn = 2e5 + 5;
char str[maxn];
int f[maxn];
int find(int x) {
    return x == f[x] ? x : f[x] = find(f[x]);
}
int cnt[maxn][30];
int main() {
    int t; scanf("%d", &t);
    while (t--) {
        int n, k; scanf("%d%d", &n, &k);
        scanf("%s", str);
        for (int i = 0; i < n; ++i) {
            f[i] = i;
            for (int j = 0; j < 26; ++j) {
                cnt[i][j] = 0;
            }
            cnt[i][str[i] - 'a']++;
        }
        for (int i = k; i < n; i += k) {
            for (int j = 0; j < k; ++j) {
                int a = find(j), b = find(i + j);
                if (a == b) continue;
                f[b] = a;
                for (int j = 0; j < 26; ++j) {
                    cnt[a][j] += cnt[b][j];
                }
            }
        }
        for (int i = 0; i < n; ++i) {
            int a = find(i), b = find(n - 1 - i);
            if (a == b) continue;
            f[b] = a;
            for (int j = 0; j < 26; ++j) {
                cnt[a][j] += cnt[b][j];
            }
        }
        int ans = 0;
        for (int i = 0; i < n; ++i) {
            if (i == f[i]) {
                int tar = -1, sum = 0;
                for (int j = 0; j < 26; ++j) {
                    sum += cnt[i][j];
                    if (tar == -1 || cnt[i][j] > cnt[i][tar]) {
                        tar = j;
                    }
                }
                ans += sum - cnt[i][tar];
            }
        }
        printf("%d\n", ans);
    }
}


// Problem : C. K-Complete Word
// Contest : Codeforces Round #630 (Div. 2)
// URL : https://codeforces.com/contest/1332/problem/C
// Memory Limit : 512 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const int maxn = 2e5;
char str[maxn];
int main() {
	int t; scanf("%d", &t);
	while (t--) {
		int n, k; scanf("%d%d", &n, &k);
		scanf("%s", str);
		int ans = 0x3f3f3f3f;
		for (int i = 0; i < n; i += k) {
			int tmp = 0;
			for (int ii = 0; ii < n; ii += k) {
				for (int iii = 0; iii < k; ++iii) {
					if (str[i + iii] != str[ii + iii]) {
						tmp++;
					}
				}
			}
			ans = min(ans, tmp);
		}
	}
}


#include <bits/stdc++.h>
using namespace std;

const int maxn = 2e5 + 5;
char str[maxn];
int f[maxn];
int find(int x) {
    return x == f[x] ? x : f[x] = find(f[x]);
}
int cnt[maxn][30];
int main() {
    int t; scanf("%d", &t);
    while (t--) {
        int n, k; scanf("%d%d", &n, &k);
        scanf("%s", str);
        for (int i = 0; i < n; ++i) {
            f[i] = i;
            for (int j = 0; j < 26; ++j) {
                cnt[i][j] = 0;
            }
            cnt[i][str[i] - 'a']++;
        }
        for (int i = k; i < n; i += k) {
            for (int j = 0; j < k; ++j) {
                int a = find(j), b = find(i + j);
                if (a == b) continue;
                f[b] = a;
                for (int j = 0; j < 26; ++j) {
                    cnt[a][j] += cnt[b][j];
                }
            }
        }
        for (int i = 0; i < n; ++i) {
            int a = find(i), b = find(n - 1 - i);
            if (a == b) continue;
            f[b] = a;
            for (int j = 0; j < 26; ++j) {
                cnt[a][j] += cnt[b][j];
            }
        }
        int ans = 0;
        for (int i = 0; i < n; ++i) {
            if (i == f[i]) {
                int tar = -1, sum = 0;
                for (int j = 0; j < 26; ++j) {
                    sum += cnt[i][j];
                    if (tar == -1 || cnt[i][j] > cnt[i][tar]) {
                        tar = j;
                    }
                }
                ans += sum - cnt[i][tar];
            }
        }
        printf("%d\n", ans);
    }
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <climits>
#include <vector>
#include <cstring>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <unordered_map>
#include <string.h>
#include <iomanip>
using namespace std;
#define int long long
#define ull unsigned long long
#define F first
#define S second
#define nl "\n"
#define ld long double
#define div 1000000007

void solve()
{
    int n,k;cin>>n>>k;
    string s;cin>>s;
    string str[n/k];
    int t=0;
    for(int i=0;i<=n-k;i+=k)
    {
        string sub=s.substr(i,k);
        str[t]=sub;
        t++;
    }
    int ans=0;
    for(int j=0;j<k/2;j++)
    {
        int ch1[26];int c1=0;
        memset(ch1,0,sizeof(ch1));
        for(int i=0;i<n/k;i++)
            ch1[str[i][j]-'a']++;
        for(int i=0;i<n/k;i++)
            ch1[str[i][k-1-j]-'a']++;
        for(int p=0;p<26;p++)
            c1+=ch1[p];
        ans+=c1-*max_element(ch1,ch1+26);
    }
    if(k&1)
    {
        int cha[26];
        memset(cha,0,sizeof(cha));
        for(int i=0;i<n/k;i++)
        {
            cha[str[i][k/2]-'a']++;
        }
        int c=0;
        for(int i=0;i<26;i++)
            c+=cha[i];
        c-=*max_element(cha,cha+26);
        ans+=c;
    }
    cout<<ans<<nl;
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <climits>
#include <vector>
#include <cstring>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <unordered_map>
#include <string.h>
#include <iomanip>
using namespace std;
#define int long long
#define ull unsigned long long
#define F first
#define S second
#define nl "\n"
#define ld long double
#define div 1000000007

void solve()
{
    int n,k;cin>>n>>k;
    string s;cin>>s;
    string str[n/k];
    int t=0;
    for(int i=0;i<=n-k;i+=k)
    {
        string sub=s.substr(i,k);
        str[t]=sub;
        t++;
    }
    int ans=0;
    for(int j=0;j<k/2;j++)
    {
        int ch1[26],ch2[26];
        memset(ch1,0,sizeof(ch1));
        memset(ch2,0,sizeof(ch2));
        for(int i=0;i<n/k;i++)
            ch1[str[i][j]-'a']++;
        int c1=0,c2=0;
        for(int p=0;p<26;p++)
            c1+=ch1[p];
        for(int i=0;i<n/k;i++)
            ch2[str[i][k-1-j]-'a']++;
        for(int p=0;p<26;p++)
            c2+=ch2[p];
        int o1=c1-*max_element(ch1,ch1+26);
        int o2=c2-*max_element(ch2,ch2+26);
        int p1=max_element(ch1,ch1+26)-ch1;
        int p2=max_element(ch2,ch2+26)-ch2;
        if(ch1[p1]>1 || ch2[p2]>1)
            ans+=min(o1+n/k-ch2[p1],o2+n/k-ch1[p2]);
        else
        {
            int pos;
            for(int i=0;i<26;i++)
            {
                if(ch1[i]==ch2[i] && ch1[i]>0 && ch2[i]>0)
                {
                    p1=p2=i;
                }
            }
            ans+=min(o1+n/k-ch2[p1],o2+n/k-ch1[p2]);
        }
    }
    if(k&1)
    {
        int cha[26];
        memset(cha,0,sizeof(cha));
        for(int i=0;i<n/k;i++)
        {
            cha[str[i][k/2]-'a']++;
        }
        int c=0;
        for(int i=0;i<26;i++)
            c+=cha[i];
        c-=*max_element(cha,cha+26);
        ans+=c;
    }
    cout<<ans<<nl;
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}