#include <bits/stdc++.h>
using namespace std;
long long int n, k;
string s;
int main() {
  long long int tc;
  scanf("%lld", &tc);
  while (tc--) {
    scanf("%lld%lld", &n, &k);
    long long int mp[30];
    vector<long long int> v[n + 5];
    cin >> s;
    long long int p = k / 2;
    for (long long int i = 0; i < n; i++) {
      v[i % k].push_back(s[i] - 'a');
    }
    long long int ans = 0;
    for (long long int i = 0; i < p; i++) {
      memset(mp, 0, sizeof(mp));
      for (long long int j = 0; j < (long long int)v[i].size(); j++)
        mp[v[i][j]]++;
      for (long long int j = 0; j < (long long int)v[k - i - 1].size(); j++)
        mp[v[k - i - 1][j]]++;
      long long int mot = v[i].size() + v[k - i - 1].size(), mx = 0;
      for (long long int j = 0; j < 26; j++) {
        mx = max(mp[j], mx);
      }
      ans += mot - mx;
    }
    if (k % 2) {
      memset(mp, 0, sizeof(mp));
      for (long long int i = 0; i < (long long int)v[k / 2].size(); i++)
        mp[v[k / 2][i]]++;
      long long int mot = v[k / 2].size(), mx = 0;
      for (long long int i = 0; i <= 26; i++) {
        mx = max(mp[i], mx);
      }
      ans += mot - mx;
    }
    printf("%lld\n", ans);
  }
}
