#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
   int t,n,i,g,minn;
   cin>>t;
   while(t--)
  {
    int ans=1;
    cin>>n;

    int a[n];
    int b[n];
    vector<int> v;

    for(i=0;i<n;i++)
    {
      cin>>a[i];
      b[i]=a[i];
    }

    if(n==1)
    {
    	cout<<"YES"<<endl;
    	continue;
	}

    sort(b,b+n);

    for(i=0;i<n;i++)
    {
    	cout<<b[i]<<" ";
	}

	cout<<endl;

    minn=*min_element(a , a + n);

    for(i=0;i<n;i++)
    {
       if(a[i]!=b[i])
	   cout<<a[i]%minn<<" ";
	   {
	     if(a[i]%minn==0)
	     {
	     	continue;
		 }

		 else
		 {
			cout<<"tatti ques";
			ans=0;
		 	break;
		 }
	   }

	}

	if(ans==1)
	{
		cout<<"YES"<<endl;;
	}

	else
	{
		cout<<"NO"<<endl;
	}

  }

}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
   int t,n,i,g,minn;
   cin>>t;
   while(t--)
  {
    int ans=1;
    cin>>n;

    int a[n];
    int b[n];
    vector<int> v;

    for(i=0;i<n;i++)
    {
      cin>>a[i];
      b[i]=a[i];
    }

    if(n==1)
    {
    	cout<<"YES"<<endl;
    	continue;
	}

    sort(b,b+n);

    minn=*min_element(a , a + n);

    for(i=0;i<n;i++)
    {
       if(a[i]!=b[i])
	   cout<<a[i]%minn<<" ";
	   {
	     if(a[i]%minn==0)
	     {
	     	continue;
		 }

		 else
		 {
			ans=0;
		 	break;
		 }
	   }

	}

	if(ans==1)
	{
		cout<<"YES"<<endl;;
	}

	else
	{
		cout<<"NO"<<endl;
	}

  }

}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
   int t,n,i,g,temp,minn,ans=1;
   cin>>t;
   while(t--)
  {

    cin>>n;

    int a[n];
    int b[n];
    vector<int> v;

    for(i=0;i<n;i++)
    {
      cin>>a[i];
      b[i]=a[i];
    }

    if(n==1)
    {
    	cout<<"YES";
    	continue;
	}

    sort(b,b+n);

    for(i=0;i<n;i++)
    {
       if(a[i]!=b[i])
	   {
	     v.push_back(a[i]);
	   }

	}

    minn=*min_element(a , a + n);

	for(i=0;i<v.size();i++)
	{
		g=  __gcd(v[i], v[i+1]);
		if(g==minn)
		{
			continue;
		}

		else
		{
			ans=0;
			break;
		}
	}

	if(ans==1)
	{
		cout<<"YES"<<endl;
	}

	else
	{
		cout<<"NO"<<endl;
	}

  }

}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
   int t,n,i,g,temp,minn,ans=1;
   cin>>t;
   while(t--)
  {

    cin>>n;

    int a[n];
    int b[n];
    vector<int> v;

    for(i=0;i<n;i++)
    {
      cin>>a[i];
      b[i]=a[i];
    }

    if(n==1)
    {
    	cout<<"YES"<<endl;
    	continue;
	}

    sort(b,b+n);

    for(i=0;i<n;i++)
    {
       if(a[i]!=b[i])
	   {
	     v.push_back(a[i]);
	   }

	}

    minn=*min_element(a , a + n);

	for(i=0;i<v.size();i++)
	{
		g=  __gcd(v[i], v[i+1]);
		if(g==minn)
		{
			continue;
		}

		else
		{
			ans=0;
			break;
		}
	}

	if(ans==1)
	{
		cout<<"YES"<<endl;
	}

	else
	{
		cout<<"NO"<<endl;
	}

  }

}


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{
   int t,n,i,g,minn,ans=1;
   cin>>t;
   while(t--)
  {

    cin>>n;

    int a[n];
    int b[n];
    vector<int> v;

    for(i=0;i<n;i++)
    {
      cin>>a[i];
      b[i]=a[i];
    }

    if(n==1)
    {
    	cout<<"YES"<<endl;
    	continue;
	}

    sort(b,b+n);

    minn=*min_element(a , a + n);

    for(i=0;i<n;i++)
    {
       if(a[i]!=b[i])
	   {
	     if(b[i]%minn==0)
	     {
	     	continue;
		 }

		 else
		 {
			ans=0;
		 	break;
		 }
	   }

	}

	if(ans==1)
	{
		cout<<"YES"<<endl;;
	}

	else
	{
		cout<<"NO"<<endl;
	}

  }

}


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

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;
        int a[n];
        int i;
        int b[n];
        for(i=0; i<n; i++)
        {
            cin >> a[i];
            b[i]=a[i];
        }
        sort(b, b+n);
        int min=b[0];
        int flag=0;
        for(i=0; i<n; i++)
        {
            if(a[i]!=b[i] && a[i]%min!=0)
            {
                flag++;
                break;
            }
        }

        if(flag==0)
        {
            cout << "YES" << endl;
        }
        else
            cout << "NO" << endl;
    }
    return 0;
}


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

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;
        int a[n];
        int i;
        for(i=0; i<n; i++)
        {
            cin >> a[i];
        }
        int smallest=*min_element(a, a+n);
        for(i=1; i<n; i++)
        {
            if(a[i]<a[i-1]  && __gcd(a[i], a[i-1])<=smallest)
            {
                swap(a[i], a[i-1]);
                i=1;
            }
        }
        int flag=0;
        for(i=1; i<n; i++)
        {
            if(a[i]<a[i-1])
            flag++;
        }
        if(flag==0)
        {
            cout << "YES" << endl;
        }
        else
            cout << "NO" << endl;
    }
    return 0;
}


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

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;
        int a[n];
        int i;
        for(i=0; i<n; i++)
        {
            cin >> a[i];
        }
        int smallest=*min_element(a, a+n);
        int flag=0;
        for(i=0; i<n; i++)
        {
            if(flag!=0)
                break;
            for(j=1; j<n; j++)
            {
                if(a[j]<a[i])
                {
                    if(__gcd(a[j], a[i])==smallest)
                    {
                        swap(a[i], a[j]);
                    }
                    else
                    {
                        flag++;
                        break;
                    }
                }

            }
        }

        if(flag==0)
        {
            cout << "YES" << endl;
        }
        else
            cout << "NO" << endl;
    }
    return 0;
}


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

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;
        int a[n];
        int i;
        for(i=0; i<n; i++)
        {
            cin >> a[i];
        }
        int smallest=min_element(a, a+n);
        for(i=1; i<n; i++)
        {
            if(a[i]>a[i-1]  && __gcd(a[i], a[i-1])<smallest)
            {
                swap(a[i], a[i-1]);
                i=1;
            }
        }
        int flag=0;
        for(i=1; i<n; i++)
        {
            if(a[i]<a[i-1])
            flag++;
        }
        if(flag==0)
        {
            cout << "YES" << endl;
        }
        else
            cout << "NO" << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAX_LETTER = 'z' - 'a' + 1;
const int MAXN = 2e5;

int counts[MAXN][MAX_LETTER];

int midpoint(int k) {
    if (k % 2 == 0) {
        return k / 2 - 1;
    }
    return k / 2;
}

int to_subword_pos(int k, int i) {
    int result = i % k;
    if (result > midpoint(k)) {
        result = k - result - 1;
    }
    return result;
}

void solve() {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    memset(counts, 0, sizeof(counts));

    for (int i = 0; i < n; i++) {
        int j = to_subword_pos(k, i);
        int letter = s[i] - 'a';
        counts[j][letter]++;
    }

    vector<char> ans(midpoint(k) + 1);
    for (int i = 0; i < ans.size(); i++) {
        int found_max = 0;
        int max_letter = 0;
        for (int l = 0; l < MAX_LETTER; l++) {
            if (counts[i][l] > found_max) {
                found_max = counts[i][l];
                max_letter = l;
            }
        }
        ans[i] = max_letter + 'a';
    }

    int times_changed = 0;
    for (int i = 0; i < n; i++) {
        int j = to_subword_pos(k, i);
        if (s[i] != ans[j]) {
            times_changed++;
        }
    }
    printf("%d\n", times_changed);
}

int main() {
    int T;
    cin >> T;
    for (int t = 1; t <= T; t++) {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAX_LETTER = 'z' - 'a' + 1;
const int MAXN = 2e5;

int counts[MAXN][MAX_LETTER];
char ans[MAXN];
int max_found[MAXN];

int midpoint(int k) {
    if (k % 2 == 0) {
        return k / 2 - 1;
    }
    return k / 2;
}

int to_subword_pos(int k, int i) {
    int result = i % k;
    if (result > midpoint(k)) {
        result = k - result - 1;
    }
    return result;
}

void solve() {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    memset(counts, 0, sizeof(counts));
    memset(ans, 0, sizeof(ans));
    memset(max_found, 0, sizeof(max_found));

    int not_changed = 0;
    for (int i = 0; i < n; i++) {
        int j = to_subword_pos(k, i);
        int letter = s[i] - 'a';
        counts[j][letter]++;
        if (counts[j][letter] > max_found[j]) {
            not_changed -= max_found[j];
            max_found[j] = counts[j][letter];
            ans[j] = letter + 'a';
            not_changed += max_found[j];
        }
    }

    printf("%d\n", n - not_changed);
}

int main() {
    int T;
    cin >> T;
    for (int t = 1; t <= T; t++) {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAX_LETTER = 'z' - 'a' + 1;
const int MAXN = 2e5;

int counts[MAXN][MAX_LETTER];
char ans[MAXN];
int max_found[MAXN];

int midpoint(int k) {
    return (k / 2) - 1 + (k % 2);
}

int to_subword_pos(int k, int i) {
    int result = i % k;
    if (result > midpoint(k)) {
        result = k - result - 1;
    }
    return result;
}

void solve() {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    memset(counts, 0, sizeof(counts));
    memset(max_found, 0, sizeof(max_found));

    int not_changed = 0;
    for (int i = 0; i < n; i++) {
        int j = to_subword_pos(k, i);
        int letter = s[i] - 'a';
        counts[j][letter]++;
        if (counts[j][letter] > max_found[j]) {
            not_changed -= max_found[j];
            max_found[j] = counts[j][letter];
            not_changed += max_found[j];
        }
    }

    cout << n - not_changed << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int T;
    cin >> T;
    for (int t = 1; t <= T; t++) {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAX_LETTER = 'z' - 'a' + 1;
const int MAXN = 2e5;

int counts[MAXN][MAX_LETTER];

int midpoint(int k) {
    if (k % 2 == 0) {
        return k / 2 - 1;
    }
    return k / 2;
}

int to_subword_pos(int k, int i) {
    int result = i % k;
    if (result > midpoint(k)) {
        result = k - result - 1;
    }
    return result;
}

void solve() {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    memset(counts, 0, sizeof(counts));

    vector<char> ans(midpoint(k) + 1);
    vector<int> max_found(midpoint(k) + 1, 0);
    for (int i = 0; i < n; i++) {
        int j = to_subword_pos(k, i);
        int letter = s[i] - 'a';
        counts[j][letter]++;
        if (counts[j][letter] > max_found[j]) {
            max_found[j] = counts[j][letter];
            ans[j] = letter + 'a';
        }
    }

    int times_changed = 0;
    for (int i = 0; i < n; i++) {
        int j = to_subword_pos(k, i);
        if (s[i] != ans[j]) {
            times_changed++;
        }
    }
    printf("%d\n", times_changed);
}

int main() {
    int T;
    cin >> T;
    for (int t = 1; t <= T; t++) {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAX_LETTER = 'z' - 'a' + 1;
const int MAXN = 2e5;

int counts[MAXN][MAX_LETTER];
char ans[MAXN];
int max_found[MAXN];

int midpoint(int k) {
    if (k % 2 == 0) {
        return k / 2 - 1;
    }
    return k / 2;
}

int to_subword_pos(int k, int i) {
    int result = i % k;
    if (result > midpoint(k)) {
        result = k - result - 1;
    }
    return result;
}

void solve() {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    memset(counts, 0, sizeof(counts));
    memset(max_found, 0, sizeof(max_found));

    int not_changed = 0;
    for (int i = 0; i < n; i++) {
        int j = to_subword_pos(k, i);
        int letter = s[i] - 'a';
        counts[j][letter]++;
        if (counts[j][letter] > max_found[j]) {
            not_changed -= max_found[j];
            max_found[j] = counts[j][letter];
            not_changed += max_found[j];
        }
    }

    cout << n - not_changed << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int T;
    cin >> T;
    for (int t = 1; t <= T; t++) {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAX_LETTER = 'z' - 'a' + 1;
const int MAXN = 2e5;

int counts[MAXN][MAX_LETTER];
char ans[MAXN];
int max_found[MAXN];

int midpoint(int k) {
    if (k % 2 == 0) {
        return k / 2 - 1;
    }
    return k / 2;
}

int to_subword_pos(int k, int i) {
    int result = i % k;
    if (result > midpoint(k)) {
        result = k - result - 1;
    }
    return result;
}

void solve() {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    memset(counts, 0, sizeof(counts));
    memset(max_found, 0, sizeof(max_found));

    int not_changed = 0;
    for (int i = 0; i < n; i++) {
        int j = to_subword_pos(k, i);
        int letter = s[i] - 'a';
        counts[j][letter]++;
        if (counts[j][letter] > max_found[j]) {
            not_changed -= max_found[j];
            max_found[j] = counts[j][letter];
            not_changed += max_found[j];
        }
    }

    printf("%d\n", n - not_changed);
}

int main() {
    int T;
    cin >> T;
    for (int t = 1; t <= T; t++) {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAX_LETTER = 'z' - 'a' + 1;
const int MAXN = 2e5;

int counts[MAXN][MAX_LETTER];

int to_subword_pos(int k, int i) {
    int result = i % k;
    if (result > k / 2) {
        result = k - result - 1;
    }
    return result;
}

void solve() {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    memset(counts, 0, sizeof(counts));

    for (int i = 0; i < n; i++) {
        int j = to_subword_pos(k, i);
        int letter = s[i] - 'a';
        counts[j][letter]++;
    }

    vector<char> ans(n);
    for (int i = 0; i < ans.size(); i++) {
        int found_max = 0;
        int max_letter = 0;
        for (int l = 0; l < MAX_LETTER; l++) {
            if (counts[i][l] > found_max) {
                found_max = counts[i][l];
                max_letter = l;
            }
        }
        ans[i] = max_letter + 'a';
    }

    int times_changed = 0;
    for (int i = 0; i < n; i++) {
        int j = to_subword_pos(k, i);
        if (s[i] != ans[j]) {
            times_changed++;
        }
    }
    printf("%d\n", times_changed);
}

int main() {
    int T;
    cin >> T;
    for (int t = 1; t <= T; t++) {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAX_LETTER = 'z' - 'a' + 1;
const int MAXN = 2e5;

int counts[MAXN][MAX_LETTER];
char ans[MAXN];
int max_found[MAXN];

int midpoint(int k) {
    if (k % 2 == 0) {
        return k / 2 - 1;
    }
    return k / 2;
}

int to_subword_pos(int k, int i) {
    int result = i % k;
    if (result > midpoint(k)) {
        result = k - result - 1;
    }
    return result;
}

void solve() {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    memset(counts, 0, sizeof(counts));
    memset(max_found, 0, sizeof(max_found));

    int not_changed = 0;
    for (int i = 0; i < n; i++) {
        int j = to_subword_pos(k, i);
        int letter = s[i] - 'a';
        counts[j][letter]++;
        if (counts[j][letter] > max_found[j]) {
            not_changed -= max_found[j];
            max_found[j] = counts[j][letter];
            not_changed += max_found[j];
        }
    }

    printf("%d\n", n - not_changed);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin >> T;
    for (int t = 1; t <= T; t++) {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAX_LETTER = 'z' - 'a' + 1;
const int MAXN = 2e5;

int counts[MAXN][MAX_LETTER];

int midpoint(int k) {
    if (k % 2 == 0) {
        return k / 2 - 1;
    }
    return k / 2;
}

int to_subword_pos(int k, int i) {
    int result = i % k;
    if (result > midpoint(k)) {
        result = k - result - 1;
    }
    return result;
}

void solve() {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    memset(counts, 0, sizeof(counts));

    vector<char> ans(midpoint(k) + 1);
    vector<int> max_found(midpoint(k) + 1, 0);
    int not_changed = 0;
    for (int i = 0; i < n; i++) {
        int j = to_subword_pos(k, i);
        int letter = s[i] - 'a';
        counts[j][letter]++;
        if (counts[j][letter] > max_found[j]) {
            not_changed -= max_found[j];
            max_found[j] = counts[j][letter];
            ans[j] = letter + 'a';
            not_changed += max_found[j];
        }
    }

    printf("%d\n", n - not_changed);
}

int main() {
    int T;
    cin >> T;
    for (int t = 1; t <= T; t++) {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAX_LETTER = 'z' - 'a' + 1;
const int MAXN = 2e5;

int counts[MAXN][MAX_LETTER];
char ans[MAXN];
int max_found[MAXN];

int midpoint(int k) {
    return (k / 2) - 1 + (k % 2);
}

int to_subword_pos(int k, int i) {
    int result = i % k;
    if (result > midpoint(k)) {
        result = k - result - 1;
    }
    return result;
}

void solve() {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < MAX_LETTER; j++) {
            counts[i][j] = 0;
        }
    }
    for (int i = 0; i < n; i++) {
        max_found[i] = 0;
    }

    int not_changed = 0;
    for (int i = 0; i < n; i++) {
        int j = to_subword_pos(k, i);
        int letter = s[i] - 'a';
        counts[j][letter]++;
        if (counts[j][letter] > max_found[j]) {
            not_changed -= max_found[j];
            max_found[j] = counts[j][letter];
            not_changed += max_found[j];
        }
    }

    cout << n - not_changed << endl;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int T;
    cin >> T;
    for (int t = 1; t <= T; t++) {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAX_LETTER = 'z' - 'a' + 1;
const int MAXN = 2e5;

int counts[MAXN][MAX_LETTER];

int to_subword_pos(int k, int i) {
    int result = i % k;
    if (result > k / 2) {
        result = k - result - 1;
    }
    return result;
}

void solve() {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    memset(counts, 0, sizeof(counts));

    for (int i = 0; i < n; i++) {
        int j = to_subword_pos(k, i);
        int letter = s[i] - 'a';
        counts[j][letter]++;
    }

    vector<char> ans(k/2 + 1);
    for (int i = 0; i < ans.size(); i++) {
        int found_max = 0;
        int max_letter = 0;
        for (int l = 0; l < MAX_LETTER; l++) {
            if (counts[i][l] > found_max) {
                found_max = counts[i][l];
                max_letter = l;
            }
        }
        ans[i] = max_letter + 'a';
    }

    int times_changed = 0;
    for (int i = 0; i < n; i++) {
        int j = to_subword_pos(k, i);
        if (s[i] != ans[j]) {
            times_changed++;
        }
    }
    printf("%d\n", times_changed);
}

int main() {
    int T;
    cin >> T;
    for (int t = 1; t <= T; t++) {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

typedef long long int ll;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>

ordered_set S;

int main()
{
    FIO
    ll t;
    cin >> t;

    while(t--)
    {
        ll n,k;
        cin >> n >> k;
        string s;
        cin >> s;

        ll ans = 0;
        bool vis[n+1]={0};
        for(ll i=0;i<k;i++)
        {
            if(vis[i]==0)
            {
                ll a[26]={0};
                for(ll j=i;j<n;j+=k)
                {
                    if(vis[j]==0)
                    {
                        a[s[j]-'a']++;
                        vis[j] = 1;
                    }
                    if(vis[n-1-j]==0)
                    {
                        vis[n-1-j] = 1;
                        a[s[n-1-j]-'a']++;
                    }
                }

                ll mx = 0;
                ll cnt=0;
                for(ll j=0;j<26;j++)
                {
                    cnt += a[j];
                    mx = max(mx , a[j]);
                }
                //cout << cnt << " " << mx << endl;
                ans += (cnt-mx);
            }
        }
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

typedef long long int ll;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>

ordered_set S;

bool vis[200005];
vector<ll> adj[200005];
ll a[26];

void dfs(ll n, string s)
{
    vis[n] = 1;
    a[s[n]-'a']++;

    for(ll i=0;i<adj[n].size();i++)
    {
        if(!vis[adj[n][i]])
            dfs(adj[n][i],s);
    }
}

int main()
{
    FIO
    ll t;
    cin >> t;

    while(t--)
    {
        ll n,k;
        cin >> n >> k;

        string s;
        cin >> s;

        for(ll i=0;i<n;i++)
        {
            vis[i] = 0;
            adj[i].clear();
        }

        for(ll i=0;i<n;i++)
            adj[i].push_back(n-1-i);

        for(ll i=0;i<n;i++)
        {
            if(i-k>=0)
            {
                adj[i-k].push_back(i);
                adj[i].push_back(i-k);
            }
        }

        // for(ll i=0;i<n;i++)
        // {
        //     cout << i << endl;
        //     for(ll j=0;j<adj[i].size();j++)
        //         cout << adj[i][j] << " ";
        //     cout << endl;
        // }

        ll ans =0 ;
        for(ll i=0;i<n;i++)
        {
            if(!vis[i])
            {
                //cout << i << endl;
                for(ll j=0;j<26;j++)
                    a[j] = 0;

                dfs(i,s);
                ll sum = 0;
                ll mx = 0;
                for(ll j=0;j<26;j++)
                {
                    sum += a[j];
                    mx = max(mx , a[j]);
                }
                //cout << sum << " " << mx << endl;
                ans += (sum-mx);
            }
        }

        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

typedef long long int ll;

bool vis[200000];
vector<vector<ll> > adj(200000);
ll a[26];

void dfs(ll n, string &s)
{
    vis[n] = 1;
    a[s[n]-'a']++;

    for(ll i=0;i<adj[n].size();i++)
    {
        if(!vis[adj[n][i]])
            dfs(adj[n][i],s);
    }
}

int main()
{
    FIO
    ll t;
    cin >> t;

    while(t--)
    {
        ll n,k;
        cin >> n >> k;

        string s;
        cin >> s;

        for(ll i=0;i<n;i++)
        {
            vis[i] = 0;
            adj[i].clear();
        }

        for(ll i=0;i<n;i++)
            adj[i].push_back(n-1-i);

        for(ll i=0;i<n;i++)
        {
            if(i-k>=0)
            {
                adj[i-k].push_back(i);
                adj[i].push_back(i-k);
            }
        }

        // for(ll i=0;i<n;i++)
        // {
        //     cout << i << endl;
        //     for(ll j=0;j<adj[i].size();j++)
        //         cout << adj[i][j] << " ";
        //     cout << endl;
        // }

        ll ans =0 ;
        for(ll i=0;i<n;i++)
        {
            if(!vis[i])
            {
                //cout << i << endl;
                for(ll j=0;j<26;j++)
                    a[j] = 0;

                dfs(i,s);
                ll sum = 0;
                ll mx = 0;
                for(ll j=0;j<26;j++)
                {
                    sum += a[j];
                    mx = max(mx , a[j]);
                }
                //cout << sum << " " << mx << endl;
                ans += (sum-mx);
            }
        }

        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

typedef long long int ll;

bool vis[200000];
vector<vector<ll> > adj(200000);
ll a[26];

void dfs(ll n, string &s)
{
    vis[n] = 1;
    a[s[n]-'a']++;

    for(ll i=0;i<adj[n].size();i++)
    {
        if(!vis[adj[n][i]])
            dfs(adj[n][i],s);
    }
}

int main()
{
    FIO
    ll t;
    cin >> t;

    while(t--)
    {
        ll n,k;
        cin >> n >> k;

        string s;
        cin >> s;

        for(ll i=0;i<n;i++)
        {
            vis[i] = 0;
            adj[i].clear();
        }

        for(ll i=0;i<n;i++)
            adj[i].push_back(n-1-i);

        for(ll i=0;i<n;i++)
        {
            adj[i].push_back((i+k)%n);
        }

        // for(ll i=0;i<n;i++)
        // {
        //     cout << i << endl;
        //     for(ll j=0;j<adj[i].size();j++)
        //         cout << adj[i][j] << " ";
        //     cout << endl;
        // }

        ll ans =0 ;
        for(ll i=0;i<n;i++)
        {
            if(!vis[i])
            {
                //cout << i << endl;
                for(ll j=0;j<26;j++)
                    a[j] = 0;

                dfs(i,s);
                ll sum = 0;
                ll mx = 0;
                for(ll j=0;j<26;j++)
                {
                    sum += a[j];
                    mx = max(mx , a[j]);
                }
                //cout << sum << " " << mx << endl;
                ans += (sum-mx);
            }
        }

        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

typedef long long int ll;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>

ordered_set S;

bool vis[200010];
vector<ll> adj[200010];
ll a[26];

void dfs(ll n, string s)
{
    vis[n] = 1;
    a[s[n]-'a']++;

    for(ll i=0;i<adj[n].size();i++)
    {
        if(!vis[adj[n][i]])
            dfs(adj[n][i],s);
    }
}

int main()
{
    FIO
    ll t;
    cin >> t;

    while(t--)
    {
        ll n,k;
        cin >> n >> k;

        string s;
        cin >> s;

        for(ll i=0;i<n;i++)
        {
            vis[i] = 0;
            adj[i].clear();
        }

        for(ll i=0;i<n;i++)
            adj[i].push_back(n-1-i);

        for(ll i=0;i<n;i++)
        {
            adj[i].push_back((i+k)%n);
            adj[(i+k)%n].push_back(i);
        }

        // for(ll i=0;i<n;i++)
        // {
        //     cout << i << endl;
        //     for(ll j=0;j<adj[i].size();j++)
        //         cout << adj[i][j] << " ";
        //     cout << endl;
        // }

        ll ans =0 ;
        for(ll i=0;i<n;i++)
        {
            if(!vis[i])
            {
                //cout << i << endl;
                for(ll j=0;j<26;j++)
                    a[j] = 0;

                dfs(i,s);
                ll sum = 0;
                ll mx = 0;
                for(ll j=0;j<26;j++)
                {
                    sum += a[j];
                    mx = max(mx , a[j]);
                }
                //cout << sum << " " << mx << endl;
                ans += (sum-mx);
            }
        }

        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

typedef long long int ll;

bool vis[200005];
vector<vector<ll> > adj(200005);
ll a[26];

void dfs(ll n, string s)
{
    vis[n] = 1;
    a[s[n]-'a']++;

    for(ll i=0;i<adj[n].size();i++)
    {
        if(!vis[adj[n][i]])
            dfs(adj[n][i],s);
    }
}

int main()
{
    FIO
    ll t;
    cin >> t;

    while(t--)
    {
        ll n,k;
        cin >> n >> k;

        string s;
        cin >> s;

        for(ll i=0;i<n;i++)
        {
            vis[i] = 0;
            adj[i].clear();
        }

        for(ll i=0;i<n;i++)
            adj[i].push_back(n-1-i);

        for(ll i=0;i<n;i++)
        {
            if(i-k>=0)
            {
                adj[i-k].push_back(i);
                adj[i].push_back(i-k);
            }
        }

        // for(ll i=0;i<n;i++)
        // {
        //     cout << i << endl;
        //     for(ll j=0;j<adj[i].size();j++)
        //         cout << adj[i][j] << " ";
        //     cout << endl;
        // }

        ll ans =0 ;
        for(ll i=0;i<n;i++)
        {
            if(!vis[i])
            {
                //cout << i << endl;
                for(ll j=0;j<26;j++)
                    a[j] = 0;

                dfs(i,s);
                ll sum = 0;
                ll mx = 0;
                for(ll j=0;j<26;j++)
                {
                    sum += a[j];
                    mx = max(mx , a[j]);
                }
                //cout << sum << " " << mx << endl;
                ans += (sum-mx);
            }
        }

        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

typedef long long int ll;

bool vis[200000];
vector<vector<ll> > adj(200000);
ll a[26];
string s;

void dfs(ll n)
{
    vis[n] = 1;
    a[s[n]-'a']++;

    for(ll i=0;i<adj[n].size();i++)
    {
        if(!vis[adj[n][i]])
            dfs(adj[n][i]);
    }
}

int main()
{
    FIO
    ll t;
    cin >> t;

    while(t--)
    {
        ll n,k;
        cin >> n >> k;
        cin >> s;

        for(ll i=0;i<n;i++)
        {
            vis[i] = 0;
            adj[i].clear();
        }

        for(ll i=0;i<n;i++)
            adj[i].push_back(n-1-i);

        for(ll i=0;i<n;i++)
        {
            if(i-k>=0)
            {
                adj[i-k].push_back(i);
                adj[i].push_back(i-k);
            }
        }

        // for(ll i=0;i<n;i++)
        // {
        //     cout << i << endl;
        //     for(ll j=0;j<adj[i].size();j++)
        //         cout << adj[i][j] << " ";
        //     cout << endl;
        // }

        ll ans =0 ;
        for(ll i=0;i<n;i++)
        {
            if(!vis[i])
            {
                //cout << i << endl;
                for(ll j=0;j<26;j++)
                    a[j] = 0;

                dfs(i);
                ll sum = 0;
                ll mx = 0;
                for(ll j=0;j<26;j++)
                {
                    sum += a[j];
                    mx = max(mx , a[j]);
                }
                //cout << sum << " " << mx << endl;
                ans += (sum-mx);
            }
        }

        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

typedef long long int ll;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>

ordered_set S;

bool vis[200010];
vector<ll> adj[200010];
ll a[26];

void dfs(ll n, string s)
{
    vis[n] = 1;
    a[s[n]-'a']++;

    for(ll i=0;i<adj[n].size();i++)
    {
        if(!vis[adj[n][i]])
            dfs(adj[n][i],s);
    }
}

int main()
{
    FIO
    ll t;
    cin >> t;

    while(t--)
    {
        ll n,k;
        cin >> n >> k;

        string s;
        cin >> s;

        for(ll i=0;i<n;i++)
        {
            vis[i] = 0;
            adj[i].clear();
        }

        for(ll i=0;i<n;i++)
            adj[i].push_back(n-1-i);

        for(ll i=0;i<n;i++)
        {
            adj[i].push_back((i+k)%n);
            //adj[(i+k)%n].push_back(i);
        }

        // for(ll i=0;i<n;i++)
        // {
        //     cout << i << endl;
        //     for(ll j=0;j<adj[i].size();j++)
        //         cout << adj[i][j] << " ";
        //     cout << endl;
        // }

        ll ans =0 ;
        for(ll i=0;i<n;i++)
        {
            if(!vis[i])
            {
                //cout << i << endl;
                for(ll j=0;j<26;j++)
                    a[j] = 0;

                dfs(i,s);
                ll sum = 0;
                ll mx = 0;
                for(ll j=0;j<26;j++)
                {
                    sum += a[j];
                    mx = max(mx , a[j]);
                }
                //cout << sum << " " << mx << endl;
                ans += (sum-mx);
            }
        }

        cout << ans << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef vector<ll> vec;
#define inf LLONG_MAX
#define bp __builtin_popcountll
#define pb push_back
#define ff first
#define ss second
#define P pair<ll,ll>
#define N 200005
#define ALL(a) a.begin(),a.end()
#define LG 20
#define int ll
#define MOD 1000000007
#define FAST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

string s;
bool visit[N];
vector<vec> g(N);

void DFS(ll x,string &p)
{
      visit[x]=true;
      p+=s[x];

      for(auto X:g[x])
      {
            if(!visit[X])
                  DFS(X,p);
      }
}

void solve()
{
      ll n,k;
      cin>>n>>k;

      cin>>s;

      s='a'+s;

      for(int i=1;i<=n;i++)
      {
            g[i].clear();
            visit[i]=false;
      }

      for(int i=1;i<=n;i++)
      {
            g[i].pb(n+1-i);
            g[n+1-i].pb(i);
      }

      for(int i=1;i<=n-k;i++)
      {
            g[i].pb(i+k);
            g[i+k].pb(i);
      }

      ll ans=0;
      for(int i=1;i<=n;i++)
      {
            if(!visit[i])
            {
                  string p="";
                  DFS(i,p);

                  ll mx=0,f[26]={0};

                  for(auto X:p)
                        f[X-'a']++;

                  for(int j=0;j<26;j++)
                  {
                        mx=max(mx,f[j]);
                  }

                  ans+=(p.size()-mx);

            }
      }

      cout<<ans<<"\n";

}

int32_t main()
{
      FAST;

      ll t=1;
      cin>>t;
      while(t--)
      {
            solve();
      }

}


#include <bits/stdc++.h>
using namespace std;
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

typedef long long int ll;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>

ordered_set S;

bool vis[200005];
vector<vector<ll> > adj(200005);
ll a[26];

void dfs(ll n, string s)
{
    vis[n] = 1;
    a[s[n]-'a']++;

    for(ll i=0;i<adj[n].size();i++)
    {
        if(!vis[adj[n][i]])
            dfs(adj[n][i],s);
    }
}

int main()
{
    FIO
    ll t;
    cin >> t;

    while(t--)
    {
        ll n,k;
        cin >> n >> k;

        string s;
        cin >> s;

        for(ll i=0;i<n;i++)
        {
            vis[i] = 0;
            adj[i].clear();
        }

        for(ll i=0;i<n;i++)
            adj[i].push_back(n-1-i);

        for(ll i=0;i<n;i++)
        {
            if(i-k>=0)
            {
                adj[i-k].push_back(i);
                adj[i].push_back(i-k);
            }
        }

        // for(ll i=0;i<n;i++)
        // {
        //     cout << i << endl;
        //     for(ll j=0;j<adj[i].size();j++)
        //         cout << adj[i][j] << " ";
        //     cout << endl;
        // }

        ll ans =0 ;
        for(ll i=0;i<n;i++)
        {
            if(!vis[i])
            {
                //cout << i << endl;
                for(ll j=0;j<26;j++)
                    a[j] = 0;

                dfs(i,s);
                ll sum = 0;
                ll mx = 0;
                for(ll j=0;j<26;j++)
                {
                    sum += a[j];
                    mx = max(mx , a[j]);
                }
                //cout << sum << " " << mx << endl;
                ans += (sum-mx);
            }
        }

        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

typedef long long int ll;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>

ordered_set S;

bool vis[20005];
vector<ll> adj[20005];
ll a[26];

void dfs(ll n, string s)
{
    vis[n] = 1;
    a[s[n]-'a']++;

    for(ll i=0;i<adj[n].size();i++)
    {
        if(!vis[adj[n][i]])
            dfs(adj[n][i],s);
    }
}

int main()
{
    FIO
    ll t;
    cin >> t;

    while(t--)
    {
        ll n,k;
        cin >> n >> k;

        string s;
        cin >> s;

        for(ll i=0;i<n;i++)
        {
            vis[i] = 0;
            adj[i].clear();
        }

        for(ll i=0;i<n;i++)
            adj[i].push_back(n-1-i);

        for(ll i=0;i<n;i++)
        {
            if(i-k>=0)
            {
                adj[i-k].push_back(i);
                adj[i].push_back(i-k);
            }
        }

        // for(ll i=0;i<n;i++)
        // {
        //     cout << i << endl;
        //     for(ll j=0;j<adj[i].size();j++)
        //         cout << adj[i][j] << " ";
        //     cout << endl;
        // }

        ll ans =0 ;
        for(ll i=0;i<n;i++)
        {
            if(!vis[i])
            {
                //cout << i << endl;
                for(ll j=0;j<26;j++)
                    a[j] = 0;

                dfs(i,s);
                ll sum = 0;
                ll mx = 0;
                for(ll j=0;j<26;j++)
                {
                    sum += a[j];
                    mx = max(mx , a[j]);
                }
                //cout << sum << " " << mx << endl;
                ans += (sum-mx);
            }
        }

        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

typedef long long int ll;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>

ordered_set S;

bool vis[200005];
vector<vector<ll> > adj[200005];
ll a[26];

void dfs(ll n, string s)
{
    vis[n] = 1;
    a[s[n]-'a']++;

    for(ll i=0;i<adj[n].size();i++)
    {
        if(!vis[adj[n][i]])
            dfs(adj[n][i],s);
    }
}

int main()
{
    FIO
    ll t;
    cin >> t;

    while(t--)
    {
        ll n,k;
        cin >> n >> k;

        string s;
        cin >> s;

        for(ll i=0;i<n;i++)
        {
            vis[i] = 0;
            adj[i].clear();
        }

        for(ll i=0;i<n;i++)
            adj[i].push_back(n-1-i);

        for(ll i=0;i<n;i++)
        {
            if(i-k>=0)
            {
                adj[i-k].push_back(i);
                adj[i].push_back(i-k);
            }
        }

        // for(ll i=0;i<n;i++)
        // {
        //     cout << i << endl;
        //     for(ll j=0;j<adj[i].size();j++)
        //         cout << adj[i][j] << " ";
        //     cout << endl;
        // }

        ll ans =0 ;
        for(ll i=0;i<n;i++)
        {
            if(!vis[i])
            {
                //cout << i << endl;
                for(ll j=0;j<26;j++)
                    a[j] = 0;

                dfs(i,s);
                ll sum = 0;
                ll mx = 0;
                for(ll j=0;j<26;j++)
                {
                    sum += a[j];
                    mx = max(mx , a[j]);
                }
                //cout << sum << " " << mx << endl;
                ans += (sum-mx);
            }
        }

        cout << ans << endl;
    }
    return 0;
}


// first second push_back unordered return continue break vector visited check flag bool while iterator begin end lower_bound upper_bound temp true false ll_MAX ll_MIN insert erase clear pop push compare ll64_MAX ll64_MIN  reverse replace stringstream string::npos length substr front
#include<bits/stdc++.h>
using namespace std;
#define ll unsigned short int
#define all(x) x.begin(),x.end()
//#define (x)  get<0>(x)
//#define (x)  get<1>(x)
//#define (x)  get<2>(x)
#define MOD 1000000007
void solve() {
ll n , k ; cin>>n>>k ;
string s ; cin>>s;
vector<ll> v[n+1];
unordered_set<ll>u ;
for(ll i=0;i<n;i++){
for(ll j = i; j<n;j=j+k){
	// cout<<i<<" adf"<<j<<endl;
	v[i].push_back(j);
	v[j].push_back(i);
}
}

for(ll i = 0 ; i< n/2 ; i++){
	v[i].push_back(n-1-i);
	v[n-1-i].push_back(i);
}

ll ans = 0 ;

for(ll i = 0 ; i<n ; i++){
if(u.find(i)==u.end()){
	unordered_map<char,ll> temp;
	queue<ll> q;
	q.push(i);
	while(!q.empty()){
		ll node = q.front();
		// cout<<i<<" "<<node<<endl;
		temp[s[node]]++;
		u.insert(node);
		for(auto j:v[node]){
			if(u.find(j)==u.end()){
				u.insert(j);
				q.push(j);
			}
		}
		q.pop();
	}

	ll count = 0 , mxi = 0;
	for(auto it:temp){
		count+=it.second;
		mxi= max(mxi , it.second);
	}

	ans+=count-mxi;

}

}
cout<<ans<<endl;

}

signed main() {
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	ll t;
	cin >> t;
	while(t--)
		solve();
}


// first second push_back unordered return continue break vector visited check flag bool while iterator begin end lower_bound upper_bound temp true false INT_MAX INT_MIN insert erase clear pop push compare INT64_MAX INT64_MIN  reverse replace stringstream string::npos length substr front
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int mod = 1e9 + 7 ;
const int inf =
 1e9 + 5 ;
 //1e18L + 5;
const int nax = 2e5 + 5;
void min_self(int&a , int b){a=min(a,b);}
void max_self(int&a , int b){a=max(a,b);}
void add_self(int&a , int b){a=(a+b)%mod;}
#define all(x) x.begin(),x.end()
//#define (x)  get<0>(x)
//#define (x)  get<1>(x)
//#define (x)  get<2>(x)
void solve() {

int n , k ;
cin >> n >> k ;
string s ;
cin >> s ;
// for(int i = 0 ; i<n ; i++){
// 	cout << i << " "<< s[i] << endl;
// }
int ans  = 0 ;

for(int i = 0 ; i<k/2 ; i ++ ){
	int res = 0 , total  =0  ;
	vector<int>count(26);

// cout <<  i <<  " ";

for(int j = i ; j < n ;j+=k){
	// cout << j  <<  " " ;
 count[s[j] - 'a'] ++ ;
 max_self(res , count[s[j] - 'a']);
 total++;
}
for(int j = n -1 - i ; j>=0 ; j-=k){
	// cout << j <<  " " ;
	count[s[j] - 'a'] ++ ;
 max_self(res , count[s[j] - 'a']);
 total++;
}
// cout << endl;
ans += total - res ;
}

if((k)%2!=0){
		int res = 0 , total  =0  ;
	vector<int>count(26);

	// cout << (k-1)/2  << " ";
for( int j = (k-1)/2   ; j<n ; j+=k){

// cout << j << " ";

 count[s[j] - 'a'] ++ ;
 max_self(res , count[s[j] - 'a']);
 total++;

}
// cout << endl;
ans += total - res ;

}

cout << ans  << endl;

// cout << ans ;

}

signed main() {
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t;
	cin >> t;
	while(t--)
		solve();
}


// first second push_back unordered return continue break vector visited check flag bool while iterator begin end lower_bound upper_bound temp true false INT_MAX INT_MIN insert erase clear pop push compare INT64_MAX INT64_MIN  reverse replace stringstream string::npos length substr front
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end()
//#define (x)  get<0>(x)
//#define (x)  get<1>(x)
//#define (x)  get<2>(x)
#define MOD 1000000007
void solve() {
int n , k ; cin>>n>>k ;
string s ; cin>>s;
vector<int> v[n+1];
unordered_set<int>u ;
for(int i=0;i<n;i++){
for(int j = i; j<n;j=j+k){
	// cout<<i<<" adf"<<j<<endl;
	v[i].push_back(j);
	v[j].push_back(i);
}
}

for(int i = 0 ; i< n/2 ; i++){
	v[i].push_back(n-1-i);
	v[n-1-i].push_back(i);
}

int ans = 0 ;

for(int i = 0 ; i<n ; i++){
if(u.find(i)==u.end()){
	unordered_map<char,int> temp;
	queue<int> q;
	q.push(i);
	while(!q.empty()){
		int node = q.front();
		// cout<<i<<" "<<node<<endl;
		temp[s[node]]++;
		u.insert(node);
		for(auto j:v[node]){
			if(u.find(j)==u.end()){
				u.insert(j);
				q.push(j);
			}
		}
		q.pop();
	}

	int count = 0 , mxi = 0;
	for(auto it:temp){
		count+=it.second;
		mxi= max(mxi , it.second);
	}

	ans+=count-mxi;

}

}
cout<<ans<<endl;

}

signed main() {
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t;
	cin >> t;
	while(t--)
		solve();
}


// first second push_back unordered return continue break vector visited check flag bool while iterator begin end lower_bound upper_bound temp true false ll_MAX ll_MIN insert erase clear pop push compare ll64_MAX ll64_MIN  reverse replace stringstream string::npos length substr front
#include<bits/stdc++.h>
using namespace std;
#define ll unsigned short int
#define all(x) x.begin(),x.end()
//#define (x)  get<0>(x)
//#define (x)  get<1>(x)
//#define (x)  get<2>(x)
#define MOD 1000000007
void solve() {
ll n , k ; cin>>n>>k ;
string s ; cin>>s;
vector<ll> v[n+1];
unordered_set<ll>u ;
for(ll i=0;i<n;i++){
for(ll j = i; j<n;j=j+k){
	// cout<<i<<" adf"<<j<<endl;
	v[i].push_back(j);
	v[j].push_back(i);
}
}

for(ll i = 0 ; i< n/2 ; i++){
	v[i].push_back(n-1-i);
	v[n-1-i].push_back(i);
}

ll ans = 0 ;

for(ll i = 0 ; i<n ; i++){
if(u.find(i)==u.end()){
	unordered_map<char,ll> temp;
	queue<ll> q;
	q.push(i);
	while(!q.empty()){
		ll node = q.front();
		// cout<<i<<" "<<node<<endl;
		temp[s[node]]++;
		u.insert(node);
		for(auto j:v[node]){
			if(u.find(j)==u.end()){
				u.insert(j);
				q.push(j);
			}
		}
		q.pop();
	}

	ll count = 0 , mxi = 0;
	for(auto it:temp){
		count+=it.second;
		mxi= max(mxi , it.second);
	}

	ans+=count-mxi;

temp.clear();

}

}
cout<<ans<<endl;

for(int i = 0 ;i<n;i++){
	v[i].clear();
}
u.clear();

}

signed main() {
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	ll t;
	cin >> t;
	while(t--)
		solve();
}


// first second push_back unordered return continue break vector visited check flag bool while iterator begin end lower_bound upper_bound temp true false INT_MAX INT_MIN insert erase clear pop push compare INT64_MAX INT64_MIN  reverse replace stringstream string::npos length substr front
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end()
//#define (x)  get<0>(x)
//#define (x)  get<1>(x)
//#define (x)  get<2>(x)
#define MOD 1000000007
void solve() {
int n , k ; cin>>n>>k ;
string s ; cin>>s;
vector<unsigned short int> v[n+1];
unordered_set<int>u ;
for(int i=0;i<n;i++){
for(int j = i; j<n;j=j+k){
	// cout<<i<<" adf"<<j<<endl;
	v[i].push_back(j);
	v[j].push_back(i);
}
}

for(int i = 0 ; i< n/2 ; i++){
	v[i].push_back(n-1-i);
	v[n-1-i].push_back(i);
}

int ans = 0 ;

for(int i = 0 ; i<n ; i++){
if(u.find(i)==u.end()){
	unordered_map<char,int> temp;
	queue<int> q;
	q.push(i);
	while(!q.empty()){
		int node = q.front();
		// cout<<i<<" "<<node<<endl;
		temp[s[node]]++;
		u.insert(node);
		for(auto j:v[node]){
			if(u.find(j)==u.end()){
				u.insert(j);
				q.push(j);
			}
		}
		q.pop();
	}

	int count = 0 , mxi = 0;
	for(auto it:temp){
		count+=it.second;
		mxi= max(mxi , it.second);
	}

	ans+=count-mxi;

}

}
cout<<ans<<endl;

}

signed main() {
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t;
	cin >> t;
	while(t--)
		solve();
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<map>
#include<string>
using namespace std;
typedef long long ll;
const int N=2e5+5;
const int mod=19260817;
int st[N][27];
int main(){
    string s;
    int t;
    cin>>t;
    while(t--){
        memset(st,0,sizeof st);
        int n,k;
        cin>>n>>k;
        cin>>s;
        int i;
        int j;
        for(i=0;i<n;i++){
            int sign=i%k;
            int name=s[i]-'a';
            st[sign][name]++;
        }
        int flag=n/k;
        if(k%2){
            int res=0;
            for(i=0,j=k-1;i<j;i++,j--){
                int tmp=0;
                for(int l=0;l<26;l++){
                    tmp=max(tmp,st[i][l]+st[j][l]);
                }
                res+=(flag*2-tmp);
            }
            int tmp=0;
            for(int l=0;l<26;l++){
                tmp=max(tmp,st[i][l]);
            }
            res+=(flag-tmp);
            printf("%d\n",res);
        }
        else{
            int res=0;
            for(i=0,j=k-1;i<j;i++,j--){
                int tmp=0;
                for(int l=0;l<26;l++){
                    tmp=max(tmp,st[i][l]+st[j][l]);
                }
                res+=(flag*2-tmp);
            }
            printf("%d\n",res);
        }
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<map>
#include<string>
using namespace std;
typedef long long ll;
const int N=2e5+5;
const int mod=19260817;
int st[N][27];
int main(){
    string s;
    int t;
    cin>>t;
    while(t--){
        memset(st,0,sizeof st);
        int n,k;
        cin>>n>>k;
        cin>>s;
        s=" "+s;
        int i;
        int j;
        for(i=1;i<=n;i++){
            for(j=i;j<=n;j+=k){
                int sign=s[j]-'a';
                st[i][sign]++;
                //num[i].push_back(sign);
            }
        }
        int flag=n/k;
        if(k%2){
            int res=0;
            for(i=1,j=k;i<j;i++,j--){
                int tmp=0;
                for(int l=0;l<26;l++){
                    tmp=max(tmp,st[i][l]+st[j][l]);
                }
                res+=(flag*2-tmp);
            }
            int tmp=0;
            for(int l=0;l<26;l++){
                tmp=max(tmp,st[i][l]);
            }
            res+=(flag-tmp);
            printf("%d\n",res);
        }
        else{
            int res=0;
            for(i=1,j=k;i<j;i++,j--){
                int tmp=0;
                for(int l=0;l<26;l++){
                    tmp=max(tmp,st[i][l]+st[j][l]);
                }
                res+=(flag*2-tmp);
            }
            printf("%d\n",res);
        }
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<map>
#include<string>
using namespace std;
typedef long long ll;
const int N=2e5+5;
const int mod=19260817;
int st[N][27];
int main(){
    string s;
    int t;
    cin>>t;
    while(t--){
        memset(st,0,sizeof st);
        int n,k;
        cin>>n>>k;
        cin>>s;
        s=" "+s;
        int i;
        int j;
        for(i=1;i<=n;i++){
            for(j=i;j<=n;j+=k){
                int sign=s[j]-'a';
                st[i][sign]++;
                //num[i].push_back(sign);
            }
        }
        int flag=n/k;
        if(k%2){
            int res=0;
            for(i=1,j=k;i<j;i++,j--){
                int tmp=0;
                for(int l=0;l<26;l++){
                    tmp=max(tmp,st[i][l]+st[j][l]);
                }
                res+=(flag*2-tmp);
            }
            int tmp=0;
            for(int l=0;l<26;l++){
                tmp=max(tmp,st[i][l]);
            }
            res+=(flag-tmp);
            cout<<res<<endl;
        }
        else{
            int res=0;
            for(i=1,j=k;i<j;i++,j--){
                int tmp=0;
                for(int l=0;l<26;l++){
                    tmp=max(tmp,st[i][l]+st[j][l]);
                }
                res+=(flag*2-tmp);
            }
            cout<<res<<endl;
        }
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<map>
#include<string>
using namespace std;
typedef long long ll;
const int N=2e5+5;
const int mod=19260817;
int st[N][27];
int main(){
    string s;
    int t;
    cin>>t;
    while(t--){
        int n,k;
        cin>>n>>k;
        cin>>s;
        int i;
        int j;
        for(i=0;i<=n;i++){
            for(j=0;j<26;j++)
                st[i][j]=0;
        }
        for(i=1;i<=n;i++){
            int sign=(i-1)%k;
            int a=s[i]-'a';
            st[sign+1][a]++;
        }
        int flag=n/k;
        if(k%2){
            int res=0;
            for(i=1,j=k;i<j;i++,j--){
                int tmp=0;
                for(int l=0;l<26;l++){
                    tmp=max(tmp,st[i][l]+st[j][l]);
                }
                res+=(flag*2-tmp);
            }
            int tmp=0;
            for(int l=0;l<26;l++){
                tmp=max(tmp,st[i][l]);
            }
            res+=(flag-tmp);
            printf("%d\n",res);
        }
        else{
            int res=0;
            for(i=1,j=k;i<j;i++,j--){
                int tmp=0;
                for(int l=0;l<26;l++){
                    tmp=max(tmp,st[i][l]+st[j][l]);
                }
                res+=(flag*2-tmp);
            }
            printf("%d\n",res);
        }
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<map>
#include<string>
using namespace std;
typedef long long ll;
const int N=2e5+5;
const int mod=19260817;
int st[N][27];
int main(){
    string s;
    int t;
    cin>>t;
    while(t--){
        int n,k;
        cin>>n>>k;
        cin>>s;
        s=" "+s;
        int i;
        int j;
        for(i=0;i<=n;i++){
            for(j=0;j<26;j++)
                st[i][j]=0;
        }
        for(i=1;i<=n;i++){
            int sign=(i-1)%k;
            sign++;
            int hh=s[i]-'a';
            st[sign][hh]++;
        }
        int flag=n/k;
        if(k%2){
            int res=0;
            for(i=1,j=k;i<j;i++,j--){
                int tmp=0;
                for(int l=0;l<26;l++){
                    tmp=max(tmp,st[i][l]+st[j][l]);
                }
                res+=(flag*2-tmp);
            }
            int tmp=0;
            for(int l=0;l<26;l++){
                tmp=max(tmp,st[i][l]);
            }
            res+=(flag-tmp);
            printf("%d\n",res);
        }
        else{
            int res=0;
            for(i=1,j=k;i<j;i++,j--){
                int tmp=0;
                for(int l=0;l<26;l++){
                    tmp=max(tmp,st[i][l]+st[j][l]);
                }
                res+=(flag*2-tmp);
            }
            printf("%d\n",res);
        }
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<map>
using namespace std;
typedef long long ll;
const int N=2e5+5;
const int mod=19260817;
vector<char> num[N];
int st[N][27];
int main(){
    string s;
    int t;
    cin>>t;
    while(t--){
        memset(st,0,sizeof st);
        int n,k;
        cin>>n>>k;
        cin>>s;
        s=" "+s;
        int i;
        int j;
        for(i=1;i<=n;i++){
            for(j=i;j<=n;j+=k){
                int sign=s[j]-'a';
                st[i][sign]++;
                //num[i].push_back(sign);
            }
        }
        int flag=n/k;
        if(k%2){
            int res=0;
            for(i=1,j=k;i<j;i++,j--){
                int tmp=0;
                for(int l=0;l<26;l++){
                    tmp=max(tmp,st[i][l]+st[j][l]);
                }
                res+=(flag*2-tmp);
            }
            int tmp=0;
            for(int l=0;l<26;l++){
                tmp=max(tmp,st[i][l]);
            }
            res+=(flag-tmp);
            cout<<res<<endl;
        }
        else{
            int res=0;
            for(i=1,j=k;i<j;i++,j--){
                int tmp=0;
                for(int l=0;l<26;l++){
                    tmp=max(tmp,st[i][l]+st[j][l]);
                }
                res+=(flag*2-tmp);
            }
            cout<<res<<endl;
        }
    }
    return 0;
}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <iostream>
//end of libraries ;
#define INF 9999999999
#define N 300003
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int a[N][459];
void solve(){
	int n , k , ans = 0 , c = 0 , l = 0;
	string s;
	cin>>n>>k;
	for(int i = 0 ; i < k ; i++){
		for(int j = 97 ; j < 123 ; j++) a[i][j] = 0;
	}
	cin>>s;
	for(int i = 0 ; i < s.length() ; i++){
		a[i%k][s[i]]++;
	}
	if(k%2!=0) l = k/2;
	else l = k/2-1;
	for(int i = 0 ; i <= l ; i++){
		c = 0;
		for(int j = 97 ; j < 123 ; j++){
			if(k%2 == 0) c = max(a[i][j] + a[k-i-1][j],c);
			else if(i!=l) c = max(a[i][j] + a[k-i-1][j],c);
			else c =  max(a[i][j],c);
		}
		 // cout<<c<<endl;
		ans += c;
	}
	// cout<<"\n";
	cout<<n-ans<<"\n";

}
int main(){
fcin;
int t;
// t = 1;
cin>>t;
while(t--){
	solve();
}
}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <iostream>
//end of libraries ;
#define INF 9999999999
#define N 300003
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
void solve(){
	int n , k , ans = 0 , c = 0 , l = 0;
	map <int,int> a[40];
	string s;
	cin>>n>>k;
	cin>>s;
	for(int i = 0 ; i < s.length() ; i++){
		a[i%k][s[i]]++;
	}
	if(k%2!=0) l = k/2;
	else l = k/2-1;
	for(int i = 0 ; i <= l ; i++){
		c = 0;
		for(int j = 97 ; j < 123 ; j++){
			if(i!=l) c = max(a[i][j] + a[k-i-1][j],c);
			else c =  max(a[i][j],c);
		}
		ans += c;
	}
	// cout<<"\n";
	cout<<n-ans<<"\n";

}
int main(){
fcin;
int t;
// t = 1;
cin>>t;
while(t--){
	solve();
}
}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <iostream>
//end of libraries ;
#define INF 9999999999
#define N 300003
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
void solve(){
	int n , k , ans = 0 , c = 0 , l = 0;
	map <int,int> a[40];
	string s;
	cin>>n>>k;
	cin>>s;
	for(int i = 0 ; i < s.length() ; i++){
		a[i%k][s[i]]++;
	}
	if(k%2!=0) l = k/2;
	else l = k/2-1;
	for(int i = 0 ; i <= l ; i++){
		c = 0;
		for(int j = 97 ; j < 123 ; j++){
			if(k%2 == 0) c = max(a[i][j] + a[k-i-1][j],c);
			else if(i!=l) c = max(a[i][j] + a[k-i-1][j],c);
			else c =  max(a[i][j],c);
		}
		 // cout<<c<<endl;
		ans += c;
	}
	// cout<<"\n";
	cout<<n-ans<<"\n";

}
int main(){
fcin;
int t;
// t = 1;
cin>>t;
while(t--){
	solve();
}
}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <iostream>
//end of libraries ;
#define INF 9999999999
#define N 300003
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int a[N][31] , check[N];
void solve(){
	int n , k , ans = 0 , c = 0 , l = 0;
	string s;
	cin>>n>>k;
	cin>>s;
	// clear
	for(int i = 0 ; i < k ; i++){
		check[i] = 0;
		for(int j = 97 ; j < 123 ; j++) a[i][j] = 0;
	}
	// clear

	for(int i = 0 ; i < s.length() ; i++){
		a[i%k][s[i]]++;
	}
	if(k%2!=0) l = k/2;
	else l = k/2-1;
	for(int i = 0 ; i <= l ; i++){
		c = 0;
		for(int j = 97 ; j < 123 ; j++){
			if(i!=l) c = max(a[i][j] + a[k-i-1][j],c);
			else c =  max(a[i][j],c);
		}
		ans += c;
	}
	// cout<<"\n";
	cout<<n-ans<<"\n";

}
int main(){
fcin;
int t;
// t = 1;
cin>>t;
while(t--){
	solve();
}
}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <iostream>
//end of libraries ;
#define INF 9999999999
#define N 300003
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int a[N][125];
void solve(){
	int n , k , ans = 0 , c = 0 , l = 0;
	string s;
	cin>>n>>k;
	for(int i = 0 ; i < k ; i++){
		for(int j = 97 ; j < 123 ; j++) a[i][j] = 0;
	}
	cin>>s;
	for(int i = 0 ; i < s.length() ; i++){
		a[i%k][s[i]]++;
	}
	if(k%2!=0) l = k/2;
	else l = k/2-1;
	for(int i = 0 ; i <= l ; i++){
		c = 0;
		for(int j = 97 ; j < 123 ; j++){
			if(k%2 == 0) c = max(a[i][j] + a[k-i-1][j],c);
			else if(i!=l) c = max(a[i][j] + a[k-i-1][j],c);
			else c =  max(a[i][j],c);
		}
		 // cout<<c<<endl;
		ans += c;
	}
	// cout<<"\n";
	cout<<n-ans<<"\n";

}
int main(){
fcin;
int t;
// t = 1;
cin>>t;
while(t--){
	solve();
}
}


//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <iostream>
//end of libraries ;
#define INF 9999999999
#define N 300003
#define F first
#define S second
#define pb push_back
#define ll long long
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int a[N][131] , check[N];
void solve(){
	int n , k , ans = 0 , c = 0 , l = 0;
	string s;
	cin>>n>>k;
	cin>>s;
	// clear
	for(int i = 0 ; i < k ; i++){
		for(int j = 97 ; j < 123 ; j++) a[i][j] = 0;
	}
	// clear

	for(int i = 0 ; i < s.length() ; i++){
		a[i%k][s[i]]++;
	}
	if(k%2!=0) l = k/2;
	else l = k/2-1;
	for(int i = 0 ; i <= l ; i++){
		c = 0;
		for(int j = 97 ; j < 123 ; j++){
			if(i!=l) c = max(a[i][j] + a[k-i-1][j],c);
			else c =  max(a[i][j],c);
		}
		ans += c;
	}
	// cout<<"\n";
	cout<<n-ans<<"\n";

}
int main(){
fcin;
int t;
// t = 1;
cin>>t;
while(t--){
	solve();
}
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#include<map>
#include<cmath>
#define MAX 200005
typedef long long ll;
using namespace std;
const double eps=1e-6;
const double pi=acos(-1.0);
const ll mo=1e9+7;

int mp[MAX][27];
char s[MAX];
int n,k;

int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		scanf("%d %d",&n,&k);
		cin >> s;
		memset(mp,0,sizeof(mp));
		int L=strlen(s);
		for(int i=0;i<L;i++)
		{
			int loc=s[i]-'a';
			mp[i%k][loc]++;
		}
		int ans=0;
		int m;
		if(k%2==0)
			m=k/2;
		else
			m=k/2+1;
		for(int i=0;i<m;i++)
		{
			int max1=0;
			int sum=0;
			if(k%2!=0&&i==m-1)
			{
				for(int j=0;j<27;j++)
			    {

				    sum+=mp[i][j];
				    max1=max(max1,mp[i][j]);
			    }
				ans+=sum-max1;
				continue;
			}
			for(int j=0;j<27;j++)
			{

				sum+=mp[i][j]+mp[k-i-1][j];
				max1=max(max1,mp[i][j]+mp[k-i-1][j]);
			}
			ans+=sum-max1;
		}

		printf("%d\n",ans);
	}

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#include<map>
#include<cmath>
#define MAX 200005
typedef long long ll;
using namespace std;
const double eps=1e-6;
const double pi=acos(-1.0);
const ll mo=1e9+7;

int mp[MAX][27];
char s[MAX];
int n,k;

int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		scanf("%d %d",&n,&k);
		cin >> s;
		memset(mp,0,sizeof(mp));
		int L=strlen(s);
		for(int i=0;i<L;i++)
		{
			mp[i%k][s[i]-'a']++;
		}
		int ans=0;
		int m;
		if(k%2==0)
			m=k/2;
		else
			m=k/2+1;
		for(int i=0;i<m;i++)
		{
			int max1=0;
			int sum=0;
			if(k%2!=0&&i==m-1)
			{
				for(int j=0;j<27;j++)
				{
					sum+=mp[i][j];
				    max1=max(max1,mp[i][j]);
				}
				ans+=sum-max1;
				continue;
			}
			for(int j=0;j<27;j++)
			{
				sum+=mp[i][j]+mp[k-i-1][j];
				max1=max(max1,mp[i][j]+mp[k-i-1][j]);
			}
			ans+=sum-max1;
		}

		printf("%d\n",ans);
	}

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#include<map>
#include<cmath>
#define MAX 200005
typedef long long ll;
using namespace std;
const double eps=1e-6;
const double pi=acos(-1.0);
const ll mo=1e9+7;

int mp[MAX][27];
string s;
int n,k;

int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		scanf("%d %d",&n,&k);
		cin >> s;

		for(int i=0;i<=k;i++)
			for(int j=0;j<27;j++)
				mp[i][j]=0;

		int L=s.length();
		for(int i=0;i<L;i++)
			mp[i%k][s[i]-'a']++;

		int ans=0;
		int m;
		if(k%2==0)
			m=k/2;
		else
			m=k/2+1;
		for(int i=0;i<m;i++)
		{
			int max1=0;
			int sum=0;
			if(k%2!=0&&i==m-1)
			{
				for(int j=0;j<27;j++)
				{
					sum+=mp[i][j];
				    max1=max(max1,mp[i][j]);
				}
				ans+=sum-max1;
				continue;
			}
			for(int j=0;j<27;j++)
			{
				sum+=mp[i][j]+mp[k-i-1][j];
				max1=max(max1,mp[i][j]+mp[k-i-1][j]);
			}
			ans+=sum-max1;
		}

		printf("%d\n",ans);
	}

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#include<map>
#include<cmath>
#define MAX 200005
typedef long long ll;
using namespace std;
const double eps=1e-6;
const double pi=acos(-1.0);
const ll mo=1e9+7;

int mp[MAX][27];
char s[MAX];
int n,k;

int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		scanf("%d %d",&n,&k);
		cin >> s;
		memset(mp,0,sizeof(mp));
		int L=strlen(s);
		for(int i=0;i<L;i++)
		{
			int loc=s[i]-'a';
			mp[i%k][loc]++;
		}
		int ans=0;
		int m;
		if(k%2==0)
			m=k/2;
		else
			m=k/2+1;
		for(int i=0;i<m;i++)
		{
			int max1=0;
			int sum=0;
			if(k%2!=0&&i==m-1)
			{
				for(int j=0;j<27;j++)
			    {
					if(mp[i][j]==0&&mp[k-i-1][j]==0)
					     continue;
				    sum+=mp[i][j];
				    max1=max(max1,mp[i][j]);
			    }
				ans+=sum-max1;
				continue;
			}
			for(int j=0;j<27;j++)
			{
				if(mp[i][j]==0&&mp[k-i-1][j]==0)
					continue;
				sum+=mp[i][j]+mp[k-i-1][j];
				max1=max(max1,mp[i][j]+mp[k-i-1][j]);
			}
			ans+=sum-max1;
		}

		printf("%d\n",ans);
	}

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#include<map>
#include<cmath>
#define MAX 200005
typedef long long ll;
using namespace std;
const double eps=1e-6;
const double pi=acos(-1.0);
const ll mo=1e9+7;

int mp[MAX][27];
string s;
int n,k;

int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		scanf("%d %d",&n,&k);
		cin >> s;

		for(int i=0;i<=k;i++)
			for(int j=0;j<27;j++)
				mp[i][j]=0;

		int L=s.length();
		for(int i=0;i<L;i++)
			mp[i%k][s[i]-'a']++;

		int ans=0;
		int m;
		if(k%2==0)
			m=k/2;
		else
			m=k/2+1;
		for(int i=0;i<m;i++)
		{
			int max1=0;
			int sum=0;
			if(k%2!=0&&i==m-1)
			{
				for(int j=0;j<27;j++)
				{
					sum+=mp[i][j];
				    max1=max(max1,mp[i][j]);
				}
				ans+=sum-max1;
				continue;
			}
			for(int j=0;j<27;j++)
			{
				sum+=mp[i][j]+mp[k-i-1][j];
				max1=max(max1,mp[i][j]+mp[k-i-1][j]);
			}
			ans+=sum-max1;
		}

		printf("%d\n",ans);
	}

	system("pause");
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#include<map>
#include<cmath>
#define MAX 200005
typedef long long ll;
using namespace std;
const double eps=1e-6;
const double pi=acos(-1.0);
const ll mo=1e9+7;

int mp[MAX][27];
char s[MAX];
int n,k;

int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		cin >> n>> k;
		cin >> s;
		memset(mp,0,sizeof(mp));
		int L=strlen(s);
		for(int i=0;i<L;i++)
		{
			int loc=s[i]-'a';
			mp[i%k][loc]++;
		}
		int ans=0;
		int m;
		if(k%2==0)
			m=k/2;
		else
			m=k/2+1;
		for(int i=0;i<m;i++)
		{
			int max1=0;
			int sum=0;
			if(k%2!=0&&i==m-1)
			{
				for(int j=0;j<27;j++)
			    {
				    sum+=mp[i][j];
				    max1=max(max1,mp[i][j]);
			    }
				ans+=sum-max1;
				continue;
			}
			for(int j=0;j<27;j++)
			{
				sum+=mp[i][j]+mp[k-i-1][j];
				max1=max(max1,mp[i][j]+mp[k-i-1][j]);
			}
			ans+=sum-max1;
		}

		cout << ans <<endl;
	}

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#include<map>
#include<cmath>
#define MAX 200005
typedef long long ll;
using namespace std;
const double eps=1e-6;
const double pi=acos(-1.0);
const ll mo=1e9+7;

int mp[MAX][27];
char s[MAX];
int n,k;

int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		scanf("%d %d",&n,&k);
		cin >> s;
		memset(mp,0,sizeof(mp));
		int L=strlen(s);
		for(int i=0;i<L;i++)
		{
			int loc=s[i]-'a';
			mp[i%k][loc]++;
		}
		int ans=0;
		int m;
		if(k%2==0)
			m=k/2;
		else
			m=k/2+1;
		for(int i=0;i<m;i++)
		{
			int max1=0;
			if(k%2!=0&&i==m-1)
			{
				for(int j=0;j<27;j++)
				    max1=max(max1,mp[i][j]);
				ans+=max1;
				continue;
			}
			for(int j=0;j<27;j++)
				max1=max(max1,mp[i][j]+mp[k-i-1][j]);
			ans+=max1;
		}

		printf("%d\n",n-ans);
	}

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
#include<map>
#include<cmath>
#define MAX 200005
typedef long long ll;
using namespace std;
const double eps=1e-6;
const double pi=acos(-1.0);
const ll mo=1e9+7;

int mp[MAX][27];
string s;
int n,k;

int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		scanf("%d %d",&n,&k);
		cin >> s;
		memset(mp,0,sizeof(mp));
		int L=s.length();
		for(int i=0;i<L;i++)
		{
			mp[i%k][s[i]-'a']++;
		}
		int ans=0;
		int m;
		if(k%2==0)
			m=k/2;
		else
			m=k/2+1;
		for(int i=0;i<m;i++)
		{
			int max1=0;
			int sum=0;
			if(k%2!=0&&i==m-1)
			{
				for(int j=0;j<27;j++)
				{
					sum+=mp[i][j];
				    max1=max(max1,mp[i][j]);
				}
				ans+=sum-max1;
				continue;
			}
			for(int j=0;j<27;j++)
			{
				sum+=mp[i][j]+mp[k-i-1][j];
				max1=max(max1,mp[i][j]+mp[k-i-1][j]);
			}
			ans+=sum-max1;
		}

		printf("%d\n",ans);
	}

	return 0;
}


#include<stdio.h>
#include<string>
#include<algorithm>
#include<vector>
#include<math.h>
#include<queue>
#include<map>
#include<set>
#include<climits>
#include<iostream>
#include<functional>
using namespace std;

int T;

int main() {
	cin >> T;
	while (T--) {
		int ans = 0;
		int arr[200002] = { 0 };
		int a, b;
		string s;
		cin >> a >> b;
		cin >> s;
		int subcnt = 0;
		for (int i = 0; i < b && subcnt<=a; i++) {
			int cnt = 0;
			int alpha[30] = { 0 };
			int t = i;
			if (arr[t] == 1)continue;
			while (t < a) {
				if (arr[t] == 0) {
					arr[t] = 1;
					alpha[s[t] - 97]++;
					cnt++;
				}
				if (arr[a - 1 - t] == 0) {
					arr[a - 1 - t] = 1;
					alpha[s[a - 1 - t] - 97]++;
					cnt++;
				}
				t = t + b;
			}
			int max_n = 0;
			for (int i = 0; i < 27; i++) {
				max_n = max(alpha[i], max_n);
			}
			ans += (cnt - max_n);
			subcnt += cnt;
		}
		printf("%d\n", ans);

	}
}


#include<stdio.h>
#include<string>
#include<algorithm>
#include<vector>
#include<math.h>
#include<queue>
#include<map>
#include<set>
#include<climits>
#include<iostream>
#include<functional>
using namespace std;

int T;

int main() {
	cin >> T;
	while (T--) {
		int ans = 0;
		int arr[200002] = { 0 };
		int a, b;
		string s;
		cin >> a >> b;
		cin >> s;
		int ss = s.size();
		int al[30] = { 0 };
		int max_alp = 0;
		for (int i = 0; i < ss; i++) {
			al[s[i]]++;
			max_alp = max(max_alp, al[s[i]]);
		}
		if (b == 1) {
			printf("%d\n",a- max_alp);
			continue;
		}
		int subcnt = 0;
		for (int i = 0; i < b && subcnt<=a; i++) {
			int cnt = 0;
			int alpha[30] = { 0 };
			int t = i;
			int max_n = 0;
			if (arr[t] == 1)continue;
			while (t < a) {
				if (arr[t] == 0) {
					arr[t] = 1;
					alpha[s[t] - 97]++;
					max_n = max(alpha[s[t] - 97], max_n);
					cnt++;
				}
				if (arr[a - 1 - t] == 0) {
					arr[a - 1 - t] = 1;
					alpha[s[a - 1 - t] - 97]++;
					max_n = max(alpha[s[a - 1 - t] - 97], max_n);
					cnt++;
				}
				t = t + b;
			}

			ans += (cnt - max_n);
			subcnt += cnt;
		}
		printf("%d\n", ans);

	}
}


#include<stdio.h>
#include<string>
#include<algorithm>
#include<vector>
#include<math.h>
#include<queue>
#include<map>
#include<set>
#include<climits>
#include<iostream>
#include<functional>
using namespace std;

int T;

int main() {
	cin >> T;
	while (T--) {
		int ans = 0;
		int arr[200002] = { 0 };
		int a, b;
		string s;
		cin >> a >> b;
		cin >> s;
		for (int i = 0; i < b; i++) {
			int cnt = 0;
			int alpha[30] = { 0 };
			int t = i;
			if (arr[t] == 1)continue;
			while (t<a) {
				if (arr[t] == 0) {
					arr[t] = 1;
					alpha[s[t] - 97]++;
					cnt++;
				}
				if (arr[a-1-t] == 0) {
					arr[a-1  - t] = 1;
					alpha[s[a-1 - t] - 97]++;
					cnt++;
				}
				t = t + b;
			}
			int max_n = 0;
			for (int i = 0; i < 30; i++) {
				max_n = max(alpha[i], max_n);
			}
			ans += (cnt-max_n);
		}
		printf("%d\n", ans);

	}
}


#include<stdio.h>
#include<string>
#include<algorithm>
#include<vector>
#include<math.h>
#include<queue>
#include<map>
#include<set>
#include<climits>
#include<iostream>
#include<functional>
using namespace std;

int T;

int main() {
	cin >> T;
	while (T--) {
		int ans = 0;
		int arr[200002] = { 0 };
		int a, b;
		string s;
		cin >> a >> b;
		cin >> s;
		for (int i = 0; i <= min(a/b+1,b-1); i++) {
			int cnt = 0;
			int alpha[30] = { 0 };
			int t = i;
			if (arr[t] == 1)continue;
			while (t<a) {
				if (arr[t] == 0) {
					arr[t] = 1;
					alpha[s[t] - 97]++;
					cnt++;
				}
				if (arr[a-1-t] == 0) {
					arr[a-1  - t] = 1;
					alpha[s[a-1 - t] - 97]++;
					cnt++;
				}
				t = t + b;
			}
			int max_n = 0;
			for (int i = 0; i < 30; i++) {
				max_n = max(alpha[i], max_n);
			}
			ans += (cnt-max_n);
		}
		printf("%d\n", ans);

	}
}


#include<stdio.h>
#include<string>
#include<algorithm>
#include<vector>
#include<math.h>
#include<queue>
#include<map>
#include<set>
#include<climits>
#include<iostream>
#include<functional>
using namespace std;

int T;

int main() {
	cin >> T;
	while (T--) {
		int ans = 0;
		int arr[200002] = { 0 };
		int a, b;
		string s;
		cin >> a >> b;
		cin >> s;
		int ss = s.size();
		int al[30] = { 0 };
		int max_alp = 0;
		for (int i = 0; i < ss; i++) {
			al[s[i]-97]++;
			max_alp = max(max_alp, al[s[i]-97]);
		}
		if (b == 1) {
			printf("%d\n", a - max_alp);
			continue;
		}
		if (b == 2 && a % 2 == 0) {
			printf("%d\n", a - max_alp);
			continue;
		}
		int subcnt = 0;
		for (int i = 0; i < b && subcnt <= a; i++) {
			int cnt = 0;
			int alpha[30] = { 0 };
			int t = i;
			int max_n = 0;
			if (arr[t] == 1)continue;
			while (t < a) {
				if (arr[t] == 0) {
					arr[t] = 1;
					alpha[s[t] - 97]++;
					max_n = max(alpha[s[t] - 97], max_n);
					cnt++;
				}
				if (arr[a - 1 - t] == 0) {
					arr[a - 1 - t] = 1;
					alpha[s[a - 1 - t] - 97]++;
					max_n = max(alpha[s[a - 1 - t] - 97], max_n);
					cnt++;
				}
				t = t + b;
			}

			ans += (cnt - max_n);
			subcnt += cnt;
		}
		printf("%d\n", ans);

	}
}


#include<stdio.h>
#include<string>
#include<algorithm>
#include<vector>
#include<math.h>
#include<queue>
#include<map>
#include<set>
#include<climits>
#include<iostream>
#include<functional>
using namespace std;

int T;

int main() {
	cin >> T;
	while (T--) {
		int ans = 0;
		int arr[200002] = { 0 };
		int a, b;
		string s;
		cin >> a >> b;
		cin >> s;
		int ss = s.size();
		int al[30] = { 0 };
		int max_alp = 0;
		for (int i = 0; i < ss; i++) {
			al[s[i]]++;
			max_alp = max(max_alp, al[s[i]]);
		}
		if (b == 1) {
			printf("%d\n",a- max_alp);
			continue;
		}
		if (b == 2&&a%2==0) {
			printf("%d\n", a - max_alp);
			continue;
		}

		int subcnt = 0;
		for (int i = 0; i < b && subcnt<=a; i++) {
			int cnt = 0;
			int alpha[30] = { 0 };
			int t = i;
			int max_n = 0;
			if (arr[t] == 1)continue;
			while (t < a) {
				if (arr[t] == 0) {
					arr[t] = 1;
					alpha[s[t] - 97]++;
					max_n = max(alpha[s[t] - 97], max_n);
					cnt++;
				}
				if (arr[a - 1 - t] == 0) {
					arr[a - 1 - t] = 1;
					alpha[s[a - 1 - t] - 97]++;
					max_n = max(alpha[s[a - 1 - t] - 97], max_n);
					cnt++;
				}
				t = t + b;
			}

			ans += (cnt - max_n);
			subcnt += cnt;
		}
		printf("%d\n", ans);

	}
}


#include<stdio.h>
#include<string>
#include<algorithm>
#include<vector>
#include<math.h>
#include<queue>
#include<map>
#include<set>
#include<climits>
#include<iostream>
#include<functional>
using namespace std;

int T;

int main() {
	cin >> T;
	while (T--) {
		int ans = 0;
		int arr[200002] = { 0 };
		int a, b;
		string s;
		cin >> a >> b;
		cin >> s;
		int ss = s.size();
		int al[30] = { 0 };
		int max_alp = 0;
		for (int i = 0; i < ss; i++) {
			al[s[i]-97]++;
			max_alp = max(max_alp, al[s[i]]);
		}
		if (b == 1) {
			printf("%d\n", a - max_alp);
			continue;
		}
		if (b == 2 && a % 2 == 0) {
			printf("%d\n", a - max_alp);
			continue;
		}
		int subcnt = 0;
		for (int i = 0; i < b && subcnt <= a; i++) {
			int cnt = 0;
			int alpha[30] = { 0 };
			int t = i;
			int max_n = 0;
			if (arr[t] == 1)continue;
			while (t < a) {
				if (arr[t] == 0) {
					arr[t] = 1;
					alpha[s[t] - 97]++;
					max_n = max(alpha[s[t] - 97], max_n);
					cnt++;
				}
				if (arr[a - 1 - t] == 0) {
					arr[a - 1 - t] = 1;
					alpha[s[a - 1 - t] - 97]++;
					max_n = max(alpha[s[a - 1 - t] - 97], max_n);
					cnt++;
				}
				t = t + b;
			}

			ans += (cnt - max_n);
			subcnt += cnt;
		}
		printf("%d\n", ans);

	}
}


#include<stdio.h>
#include<string>
#include<algorithm>
#include<vector>
#include<math.h>
#include<queue>
#include<map>
#include<set>
#include<climits>
#include<iostream>
#include<functional>
using namespace std;

int T;

int main() {
	cin >> T;
	while (T--) {
		int ans = 0;
		int arr[200002] = { 0 };
		int a, b;
		string s;
		cin >> a >> b;
		cin >> s;
		int subcnt = 0;
		for (int i = 0; i < b && subcnt<=a; i++) {
			int cnt = 0;
			int alpha[30] = { 0 };
			int t = i;
			int max_n = 0;
			if (arr[t] == 1)continue;
			while (t < a) {
				if (arr[t] == 0) {
					arr[t] = 1;
					alpha[s[t] - 97]++;
					max_n = max(alpha[s[t] - 97], max_n);
					cnt++;
				}
				if (arr[a - 1 - t] == 0) {
					arr[a - 1 - t] = 1;
					alpha[s[a - 1 - t] - 97]++;
					max_n = max(alpha[s[a - 1 - t] - 97], max_n);
					cnt++;
				}
				t = t + b;
			}

			ans += (cnt - max_n);
			subcnt += cnt;
		}
		printf("%d\n", ans);

	}
}


#include<stdio.h>
#include<string>
#include<algorithm>
#include<vector>
#include<math.h>
#include<queue>
#include<map>
#include<set>
#include<climits>
#include<iostream>
#include<functional>
using namespace std;

int T;
int arr[200002];
int main() {
	cin >> T;
	while (T--) {
		int ans = 0;
		int a, b;
		string s;
		cin >> a >> b;
		cin >> s;
		for(int i=0;i<a;i++)arr[i]=0;
		int ss = s.size();
		int al[30] = { 0 };
		int max_alp = 0;
		for (int i = 0; i < ss; i++) {
			al[s[i]-97]++;
			max_alp = max(max_alp, al[s[i]-97]);
		}
		if (b == 1) {
			printf("%d\n", a - max_alp);
			continue;
		}
		if (b == 2 && a % 2 == 0) {
			printf("%d\n", a - max_alp);
			continue;
		}
		int subcnt = 0;
		for (int i = 0; i < b && subcnt <= a; i++) {
			int cnt = 0;
			int alpha[30] = { 0 };
			int t = i;
			int max_n = 0;
			if (arr[t] == 1)continue;
			while (t < a) {
				if (arr[t] == 0) {
					arr[t] = 1;
					alpha[s[t] - 97]++;
					max_n = max(alpha[s[t] - 97], max_n);
					cnt++;
				}
				if (arr[a - 1 - t] == 0) {
					arr[a - 1 - t] = 1;
					alpha[s[a - 1 - t] - 97]++;
					max_n = max(alpha[s[a - 1 - t] - 97], max_n);
					cnt++;
				}
				t = t + b;
			}

			ans += (cnt - max_n);
			subcnt += cnt;
		}
		printf("%d\n", ans);

	}
}


#include<stdio.h>
#include<string>
#include<algorithm>
#include<vector>
#include<math.h>
#include<queue>
#include<map>
#include<set>
#include<climits>
#include<iostream>
#include<functional>
using namespace std;

int T;

int main() {
	cin >> T;
	while (T--) {
		int ans = 0;
		int arr[200002] = { 0 };
		int a, b;
		string s;
		cin >> a >> b;
		cin >> s;
		for (int i = 0; i <= min(a/b+1,b); i++) {
			int cnt = 0;
			int alpha[30] = { 0 };
			int t = i;
			if (arr[t] == 1)continue;
			while (t<a) {
				if (arr[t] == 0) {
					arr[t] = 1;
					alpha[s[t] - 97]++;
					cnt++;
				}
				if (arr[a-1-t] == 0) {
					arr[a-1  - t] = 1;
					alpha[s[a-1 - t] - 97]++;
					cnt++;
				}
				t = t + b;
			}
			int max_n = 0;
			for (int i = 0; i < 30; i++) {
				max_n = max(alpha[i], max_n);
			}
			ans += (cnt-max_n);
		}
		printf("%d\n", ans);

	}
}


#include <iostream>
#include <bits/stdc++.h>
#include <cassert>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define forn(i,n)for(int i=0;i<n;i++)
#define forna(i,a,n)for(int i=a;i<n;i++)
#define pb push_back
#define mp make_pair
#define maap map<int,int>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mod 998244353
#define ff first
#define ss second
#define INF 1e7
#define MAX 500002
typedef long long ll;
using namespace std;
using namespace __gnu_pbds;

int main()
{
    IOS
    int t;cin>>t;
    int x=0;
    while(t--){
        int n,k;cin>>n>>k;
        string s; cin>>s;
        int m = k/2;
        int c[m][26];
        forn(i,m) forn(j,26) c[i][j]=0;
        forn(i,m){
            for(int j=i;j<n;j += k){
                c[i][s[j]-'a']++;
                c[i][s[n-1-j]-'a']++;
            }
        }
        int ans=0;
        forn(i,m){
            int ma=-1;
            forn(j,26) ma= max(ma,c[i][j]);
            ans += (2*n/k - ma);
        }
        if(k%2){
            int x[26] ={0};
            for(int j = n/k;j<n;j += k){
                x[s[j]-'a']++;
            }
            int ma=-1;
            forn(j,26) ma= max(ma,x[j]);
            ans += (n/k - ma);
        }
        cout<<ans<<endl;
    }
}


#include <iostream>
#include <bits/stdc++.h>
#include <cassert>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define forn(i,n)for(int i=0;i<n;i++)
#define forna(i,a,n)for(int i=a;i<n;i++)
#define pb push_back
#define mp make_pair
#define maap map<int,int>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mod 998244353
#define ff first
#define ss second
#define INF 1e7
#define MAX 500002
typedef long long ll;
using namespace std;
using namespace __gnu_pbds;

int main()
{
    IOS
    int t;cin>>t;
    while(t--){
        int n,k;cin>>n>>k;
        string s; cin>>s;
        int m = k/2;
        int c[m][26];
        if(k==1){
            int y[26]={0};
            int ma=-1;
            forn(i,n) y[s[i]-'a']++;
            forn(i,26) ma = max(ma,y[i]);
            cout<<n-ma<<endl;
            continue;
        }
        forn(i,m) forn(j,26) c[i][j]=0;
        forn(i,m){
            for(int j=i;j<n;j += k){
                c[i][s[j]-'a']++;
                c[i][s[n-1-j]-'a']++;
            }
        }
        int ans=0;
        forn(i,m){
            int ma=-1;
            forn(j,26) ma= max(ma,c[i][j]);
            ans += (2*n/k - ma);
        }
        if(k%2){
            int x[26] ={0};
            for(int j = k/2;j<n;j += k){
                x[s[j]-'a']++;
            }
            int ma=-1;
            forn(j,26) ma= max(ma,x[j]);
            ans += (n/k - ma);
        }
        cout<<ans<<endl;
    }
}


#include <iostream>
#include <bits/stdc++.h>
#include <cassert>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define forn(i,n)for(int i=0;i<n;i++)
#define forna(i,a,n)for(int i=a;i<n;i++)
#define pb push_back
#define mp make_pair
#define maap map<int,int>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mod 998244353
#define ff first
#define ss second
#define INF 1e7
#define MAX 500002
typedef long long ll;
using namespace std;
using namespace __gnu_pbds;

int main()
{
    IOS
    int t;cin>>t;
    while(t--){
        int n,k;cin>>n>>k;
        string s; cin>>s;
        int m = k/2;
        int c[m][26];
        forn(i,m) forn(j,26) c[i][j]=0;
        forn(i,m){
            for(int j=i;j<n;j += k){
                c[i][s[j]-'a']++;
                c[i][s[n-1-j]-'a']++;
            }
        }
        int ans=0;
        forn(i,m){
            int ma=-1;
            forn(j,26) ma= max(ma,c[i][j]);
            ans += (2*n/k - ma);
        }
        if(k%2){
            int x[26] ={0};
            for(int j = k/2;j<n;j += k){
                x[s[j]-'a']++;
            }
            int ma=-1;
            forn(j,26) ma= max(ma,x[j]);
            ans += (n/k - ma);
        }
        cout<<ans<<endl;
    }
}


#include <iostream>
#include <bits/stdc++.h>
#include <cassert>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define forn(i,n)for(int i=0;i<n;i++)
#define forna(i,a,n)for(int i=a;i<n;i++)
#define pb push_back
#define mp make_pair
#define maap map<int,int>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mod 998244353
#define ff first
#define ss second
#define INF 1e7
#define MAX 500002
typedef long long ll;
using namespace std;
using namespace __gnu_pbds;

int main()
{
    IOS
    int t;cin>>t;
    while(t--){
        int n,k;cin>>n>>k;
        string s; cin>>s;
        int m = k/2;
        int c[m][26];
        forn(i,m) forn(j,26) c[i][j]=0;
        forn(i,m){
            for(int j=i;j<n;j += k){
                c[i][s[j]-'a']++;
                c[i][s[n-1-j]-'a']++;
            }
        }
        int ans=0;
        forn(i,m){
            int ma=-1;
            forn(j,26) ma= max(ma,c[i][j]);
            ans += (2*n/k - ma);
        }
        if(k%2){
            int x[26] ={0};
            for(int j = n/k;j<n;j += k){
                x[s[j]-'a']++;
            }
            int ma=-1;
            forn(j,26) ma= max(ma,x[j]);
            ans += (n/k - ma);
        }
        cout<<ans<<endl;
    }
}


#include <iostream>
#include <bits/stdc++.h>
#include <cassert>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define forn(i,n)for(int i=0;i<n;i++)
#define forna(i,a,n)for(int i=a;i<n;i++)
#define pb push_back
#define mp make_pair
#define maap map<int,int>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mod 998244353
#define ff first
#define ss second
#define INF 1e7
#define MAX 500002
typedef long long ll;
using namespace std;
using namespace __gnu_pbds;

int main()
{
    IOS
    int t;cin>>t;
    while(t--){
        int n,k;cin>>n>>k;
        string s; cin>>s;
        if(n==1 && k==1){
            cout<<0<<endl;
            continue;
        }
        int m = k/2;
        int c[m][26];
        forn(i,m) forn(j,26) c[i][j]=0;
        forn(i,m){
            for(int j=i;j<n;j += k){
                c[i][s[j]-'a']++;
                c[i][s[n-1-j]-'a']++;
            }
        }
        int ans=0;
        forn(i,m){
            int ma=-1;
            forn(j,26) ma= max(ma,c[i][j]);
            ans += (2*n/k - ma);
        }
        if(k%2){
            int x[26] ={0};
            for(int j = n/k;j<n;j += k){
                x[s[j]-'a']++;
            }
            int ma=-1;
            forn(j,26) ma= max(ma,x[j]);
            ans += (n/k - ma);
        }
        cout<<ans<<endl;
    }
}


#include <iostream>
#include <bits/stdc++.h>
#include <cassert>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define forn(i,n)for(int i=0;i<n;i++)
#define forna(i,a,n)for(int i=a;i<n;i++)
#define pb push_back
#define mp make_pair
#define maap map<int,int>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mod 998244353
#define ff first
#define ss second
#define INF 1e7
#define MAX 500002
typedef long long ll;
using namespace std;
using namespace __gnu_pbds;

int main()
{
    IOS
    int t;cin>>t;
    int x=0;
    while(t--){
        int n,k;cin>>n>>k;
        string s; cin>>s;
        int m = k/2;
        int c[m][26];
        if(k==1){
            int y[26]={0};
            int ma=-1;
            forn(i,n) y[s[i]-'a']++;
            forn(i,26) ma = max(ma,y[i]);
            cout<<n-ma<<endl;
            continue;
        }
        forn(i,m) forn(j,26) c[i][j]=0;
        forn(i,m){
            for(int j=i;j<n;j += k){
                c[s[j]-'a']++;
                c[s[n-1-j]-'a']++;
            }
        }
        int ans=0;
        forn(i,m){
            int ma=-1;
            forn(j,26) ma= max(ma,c[i][j]);
            ans += (2*n/k - ma);
        }
        if(k%2){
            int x[26] ={0};
            for(int j = n/k;j += k;j++){
                x[s[j]-'a']++;
            }
            int ma=-1;
            forn(j,26) ma= max(ma,x[j]);
            ans += (n/k - ma);
        }
        cout<<ans<<endl;
    }
}


#include <iostream>
#include <bits/stdc++.h>
#include <cassert>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define forn(i,n)for(int i=0;i<n;i++)
#define forna(i,a,n)for(int i=a;i<n;i++)
#define pb push_back
#define mp make_pair
#define maap map<int,int>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define mod 998244353
#define ff first
#define ss second
#define INF 1e7
#define MAX 500002
typedef long long ll;
using namespace std;
using namespace __gnu_pbds;

int main()
{
    IOS
    int t;cin>>t;
    int x=0;
    while(t--){
        int n,k;cin>>n>>k;
        string s; cin>>s;
        int m = k/2;
        int c[m][26];
        if(k==1){
            int y[26]={0};
            int ma=-1;
            forn(i,n) y[s[i]-'a']++;
            forn(i,26) ma = max(ma,y[i]);
            cout<<n-ma<<endl;
            continue;
        }
        forn(i,m) forn(j,26) c[i][j]=0;
        forn(i,m){
            for(int j=i;j<n;j += k){
                c[i][s[j]-'a']++;
                c[i][s[n-1-j]-'a']++;
            }
        }
        int ans=0;
        forn(i,m){
            int ma=-1;
            forn(j,26) ma= max(ma,c[i][j]);
            ans += (2*n/k - ma);
        }
        if(k%2){
            int x[26] ={0};
            for(int j = n/k;j<n;j += k){
                x[s[j]-'a']++;
            }
            int ma=-1;
            forn(j,26) ma= max(ma,x[j]);
            ans += (n/k - ma);
        }
        cout<<ans<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e+5;
vector<ll> adj[maxn+10];
vector<ll> visited;
vector<ll> freq(26);
string k;
void dfs(ll &node, ll &cnt, ll &mx){
        visited[node]++;
        cnt++;
        freq[k[node]-'a']++;
        mx = max(freq[k[node]-'a'], mx);
        for(auto x: adj[node]){
            if(!visited[x]){
                dfs(x, cnt, mx);
            }
        }
}
int main(){
    ll t; cin >> t;
    while(t--){
        ll n, a, ans = 0; cin >> n >> a >> k;
        visited.clear();
        visited.resize(n+5);
        for(int i = 0 ; i < n; i++) adj[i].clear();
        for(ll i = 0; i < n-a; i++){
            adj[i].push_back(a+i);
            adj[a+i].push_back(i);
            adj[n-i-1].push_back(i);
            adj[i].push_back(n-i-1);
        }
        for(ll i = 0; i < n; i++){
            if(!visited[i]){
                for(int i = 0; i < 26; i++) freq[i] = 0;
                ll cnt=0, mx = 0;
                dfs(i, cnt, mx);
                ans += cnt - mx;

            }
        }

        cout << ans << "\n";

    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e+5;
vector<ll> adj[maxn+10];
vector<ll> visited;
vector<ll> freq(26);
string k;
void dfs(ll &node, ll &cnt, ll &mx){
        visited[node]++;
        cnt++;
        freq[k[node]-'a']++;
        mx = max(freq[k[node]-'a'], mx);
        for(auto x: adj[node]){
            if(!visited[x]){
                dfs(x, cnt, mx);
            }
        }
}
int main(){
    ll t; cin >> t;
    while(t--){
        ll n, a, ans = 0; cin >> n >> a >> k;
        visited.clear();
        visited.resize(n+5);
        for(int i = 0 ; i < n; i++) adj[i].clear();
        for(ll i = 0; i < n; i++){
            if(i+a < n){
                adj[i].push_back(a+i);
                adj[a+i].push_back(i);
            }
            adj[n-i-1].push_back(i);
            adj[i].push_back(n-i-1);
        }
        for(ll i = 0; i < n; i++){
            if(!visited[i]){
                for(int i = 0; i < 26; i++) freq[i] = 0;
                ll cnt=0, mx = 0;
                dfs(i, cnt, mx);
                ans += cnt - mx;

            }
        }

        cout << ans << "\n";

    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e+5;
vector<ll> adj[maxn+10];
vector<ll> visited;
vector<ll> freq(26);
string k;
void dfs(ll &node, ll &cnt, ll &mx){
        visited[node]++;
        cnt++;
        freq[k[node]-'a']++;
        mx = max(freq[k[node]-'a'], mx);
        for(auto x: adj[node]){
            if(!visited[x]){
                dfs(x, cnt2, mx);
            }
        }
}
int main(){
    ll t; cin >> t;
    while(t--){
        ll n, a, ans = 0; cin >> n >> a >> k;
        visited.clear();
        visited.resize(n+5);
        for(int i = 0 ; i < maxn; i++) adj[i].clear();
        for(ll i = 0; i < n-a; i++){
            adj[i].push_back(a+i);
            adj[a+i].push_back(i);
            adj[n-i-1].push_back(i);
            adj[i].push_back(n-i-1);
        }
        for(ll i = 0; i < n; i++){
            if(!visited[i]){
                for(int i = 0; i < 26; i++) freq[i] = 0;
                ll cnt=0, mx = 0;
                dfs(i, cnt, mx);
                ans += cnt - mx;

            }
        }

        cout << ans << "\n";

    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e+5;
vector<ll> adj[maxn+10];
vector<ll> visited;
vector<ll> freq(26);
string k;
void dfs(ll &node, ll &cnt, ll &mx){
        visited[node]++;
        cnt++;
        freq[k[node]-'a']++;
        mx = max(freq[k[node]-'a'], mx);
        for(auto x: adj[node]){
            if(!visited[x]){
                dfs(x, cnt, mx);
            }
        }
}
int main(){
    ll t; cin >> t;
    while(t--){
        ll n, a, ans = 0; cin >> n >> a >> k;
        visited.clear();
        visited.resize(n+5);
        for(int i = 0 ; i < maxn; i++) adj[i].clear();
        for(ll i = 0; i < n-a; i++){
            adj[i].push_back(a+i);
            adj[a+i].push_back(i);
            adj[n-i-1].push_back(i);
            adj[i].push_back(n-i-1);
        }
        for(ll i = 0; i < n; i++){
            if(!visited[i]){
                for(int i = 0; i < 26; i++) freq[i] = 0;
                ll cnt=0, mx = 0;
                dfs(i, cnt, mx);
                ans += cnt - mx;

            }
        }

        cout << ans << "\n";

    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e+5;
vector<ll> adj[maxn+10];
vector<ll> visited;
vector<ll> freq(26);
string k;
void dfs(ll &node, ll &cnt, ll &mx){
        visited[node]++;
        cnt++;
        freq[k[node]-'a']++;
        mx = max(freq[k[node]-'a'], mx);
        for(auto x: adj[node]){
            if(!visited[x]){
                dfs(x, cnt, mx);
            }
        }
}
int main(){
    ll t; cin >> t;
    while(t--){
        ll n, a, ans = 0; cin >> n >> a >> k;
        visited.clear();
        visited.resize(n+5);
        for(int i = 0 ; i < n; i++) adj[i].clear();
        for(ll i = 0; i < n; i++){
            if(i+k < n){
                adj[i].push_back(a+i);
                adj[a+i].push_back(i);
            }
            adj[n-i-1].push_back(i);
            adj[i].push_back(n-i-1);
        }
        for(ll i = 0; i < n; i++){
            if(!visited[i]){
                for(int i = 0; i < 26; i++) freq[i] = 0;
                ll cnt=0, mx = 0;
                dfs(i, cnt, mx);
                ans += cnt - mx;

            }
        }

        cout << ans << "\n";

    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e+5;
vector<ll> adj[maxn+10];
vector<ll> visited;
map<ll,ll> freq;
string k;
void dfs(ll &node, ll &cnt, ll &mx){
        visited[node]++;
        cnt++;
        freq[k[node]-'a']++;
        mx = max(freq[k[node]-'a'], mx);
        for(auto x: adj[node]){
            if(!visited[x]){
                dfs(x, cnt, mx);
            }
        }
}
int main(){
    ll t; cin >> t;
    while(t--){
        ll n, a, ans = 0; cin >> n >> a >> k;
        visited.clear();
        visited.resize(n+5);
        for(int i = 0 ; i < maxn; i++) adj[i].clear();
        for(ll i = 0; i < n-a; i++){
            adj[i].push_back(a+i);
            adj[a+i].push_back(i);
            adj[n-i-1].push_back(i);
            adj[i].push_back(n-i-1);
        }
        for(ll i = 0; i < n; i++){
            if(!visited[i]){
                freq.clear();
                ll cnt=0, mx = 0;
                dfs(i, cnt, mx);
                ans += cnt - mx;

            }
        }

        cout << ans << "\n";

    }
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e+5;
vector<ll> adj[maxn+10];
vector<ll> visited;
map<ll,ll> freq(26);
string k;
void dfs(ll &node, ll &cnt, ll &mx){
        visited[node]++;
        cnt++;
        freq[k[node]-'a']++;
        mx = max(freq[k[node]-'a'], mx);
        for(auto x: adj[node]){
            if(!visited[x]){
                dfs(x, cnt, mx);
            }
        }
}
int main(){
    ll t; cin >> t;
    while(t--){
        ll n, a, ans = 0; cin >> n >> a >> k;
        visited.clear();
        visited.resize(n+5);
        for(int i = 0 ; i < maxn; i++) adj[i].clear();
        for(ll i = 0; i < n-a; i++){
            adj[i].push_back(a+i);
            adj[a+i].push_back(i);
            adj[n-i-1].push_back(i);
            adj[i].push_back(n-i-1);
        }
        for(ll i = 0; i < n; i++){
            if(!visited[i]){
                freq.clear();
                ll cnt=0, mx = 0;
                dfs(i, cnt, mx);
                ans += cnt - mx;

            }
        }

        cout << ans << "\n";

    }
}


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

#define ll long long
#define ld long double
#define pb push_back

#define sec second
#define fir first
#define mo 1000000007
#define inf 1e18
#define rep(i, s, n) for (ll i = s; i < n; i = i + 1)

ll mod(ll n) { return (n % (ll)mo + (ll)mo)%(ll)mo;}
ll gcd(ll a,ll b) {if (b == 0) return a;return gcd(b, a % b);}

vector<vector<ll>> adj(200005);
bool vis[200005];
ll cnt[27];
void dfs(ll u,string s){
 	//cout<<u<<endl;
 	char c = s[u-1];
 	//cout<<c<<endl;
 	int in = (ll)(s[u-1]) - 96;
 	cnt[in] ++ ;
 	//cout<<in<<" "<<cnt[in]<<endl;

 	vis[u]=true;
 	for(auto v :adj[u]){
 		if(vis[v]==false){
 			//vis[v]=true;
 			dfs(v,s);
 		}
 	}
 }

int main(){
	int t;cin>>t;
	while(t--){

		ll n,k;cin>>n>>k;
		string s;
		cin>>s;
		rep(i,1,n+1){
			adj[i].clear();
		}
		rep(i,1,n+1){
			 adj[i].pb(n+1-i);
			//adj[n+1-i].pb(i);
			if((i+k)<=n ){
				adj[i].pb(i+k);
			}
		}
			ll ans=0;
			memset(vis,false,sizeof(vis));
			rep(i,1,n+1){
				memset(cnt,0,sizeof(cnt));
				if(vis[i]==false){
					dfs(i,s);
					ll sum=0;
					ll maxi = 0;
					rep(i,1,27){
						sum+=cnt[i];
						//cout<<sum<<cnt[i]<<endl;
						maxi = max(cnt[i],maxi);
					}

					ans+=(sum-maxi);

				}
				//cout<<"ans"<<ans<<endl;
			}
			cout<<ans<<endl;

	}

}