#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int pr[N];
int n, k;
int find(int r) {
  if (pr[r] == r)
    return r;
  else
    return pr[r] = find(pr[r]);
}
map<char, int> mp[N];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    for (int i = 0; i < n; i++) mp[i].clear();
    for (int i = 0; i <= n; i++) pr[i] = i;
    string str;
    cin >> str;
    for (int i = 0; i < k; i++) {
      for (int j = i + k; j < n; j += k) {
        int u = find(i);
        int v = find(j);
        pr[v] = u;
      }
    }
    for (int i = 0; i < n; i++) {
      if (i < n - 1 - i) {
        int u = find(i);
        int v = find(n - 1 - i);
        pr[v] = u;
      }
    }
    for (int i = 0; i < n; i++) {
      mp[find(i)][str[i]]++;
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
      if (mp[i].empty()) continue;
      int ret = 0, cnt = 0;
      for (auto u : mp[i]) {
        cnt += u.second;
        ret = max(ret, u.second);
      }
      ans += cnt - ret;
    }
    cout << ans << "\n";
  }
}
