#include <bits/stdc++.h>
using namespace std;
int analizar(string x) {
  map<char, int> c;
  for (int a = 0; a < x.size(); a++) {
    c[x[a]]++;
  }
  int b = 0;
  map<char, int>::iterator it = c.begin();
  for (; it != c.end(); it++) {
    b = max(b, it->second);
  }
  return x.size() - b;
}
void caso() {
  int n, k, res = 0;
  string s;
  cin >> n >> k;
  cin >> s;
  string l[k];
  for (int a = 0; a < k; a++) {
    for (int b = a; b < n; b += k) {
      l[a] += s[b];
    }
  }
  for (int a = 0; a < k / 2; a++) {
    l[a] += l[k - a - 1];
    res += analizar(l[a]);
  }
  if (k % 2 == 1) {
    res += analizar(l[k / 2]);
  }
  cout << res << endl;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    caso();
  }
  return 0;
}
