#include <bits/stdc++.h>
using namespace std;
long long cl(long long n, long long m) {
  if (n % m)
    return n / m + 1;
  else
    return n / m;
}
long long pw(long long a, long long b) {
  long long r = 1;
  while (b > 0) {
    if (b & 1) r = r * a;
    a = a * a;
    b >>= 1;
  }
  return r;
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long tx = 1;
  cin >> tx;
  while (tx--) {
    long long n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    long long change[n];
    memset(change, 0, sizeof(change));
    long long count = 0;
    long long idx = 0;
    long long ans = 0;
    while (count < n) {
      long long here = 0;
      map<char, long long> m;
      long long curr = idx;
      while (change[curr] == 0) {
        count++;
        change[curr] = 1;
        here++;
        m[s[curr]] += 1;
        curr = curr + k;
        if (curr >= n) break;
      }
      curr = n - 1 - idx;
      while (change[curr] == 0) {
        count++;
        change[curr] = 1;
        here++;
        m[s[curr]] += 1;
        curr = curr - k;
        if (curr < 0) break;
      }
      long long mi = n;
      for (auto p : m) {
        mi = min(mi, here - p.second);
      }
      ans += mi;
      idx++;
    }
    cout << ans << endl;
  }
}
