#include <bits/stdc++.h>
using namespace std;
long long parent[200001], Size[200001];
long long find_set(long long x) {
  if (x == parent[x]) return parent[x];
  return parent[x] = find_set(parent[x]);
}
void Union(long long x, long long y) {
  x = find_set(x);
  y = find_set(y);
  if (Size[x] > Size[y]) {
    parent[y] = parent[x];
    Size[x] += Size[y];
  } else {
    parent[x] = parent[y];
    Size[y] += Size[x];
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long t;
  cin >> t;
  while (t--) {
    long long n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    for (long long i = 0; i <= n; i++) {
      parent[i] = i;
      Size[i] = 1;
    }
    for (long long i = 0; i < n - k; i++) {
      Union(i, i + k);
      if (i < n / 2) Union(i, n - i - 1);
    }
    vector<long long> v[n + 1];
    for (long long i = 0; i < n; i++) {
      long long p = find_set(i);
      v[p].push_back(s[i] - 'a');
    }
    long long ans = 0;
    for (long long i = 0; i < n; i++) {
      if (v[i].size() == 0) continue;
      map<long long, long long> mm;
      for (auto j : v[i]) {
        mm[j]++;
      }
      long long Max = INT_MIN;
      for (auto i : mm) Max = max(Max, i.second);
      ans += v[i].size() - Max;
    }
    cout << ans << "\n";
  }
}
