#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 50;
int T, n, k;
char s[N];
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d", &n, &k);
    scanf("%s", s);
    int ans = 0;
    for (int i = 0; i < (k + 1) / 2; i++) {
      int let[26] = {0};
      for (int j = i; j < n; j += k) {
        let[s[j] - 'a']++;
      }
      if (i != k - 1 - i) {
        for (int j = k - 1 - i; j < n; j += k) {
          let[s[j] - 'a']++;
        }
      }
      int mx = 0;
      for (int j = 0; j < 26; j++) {
        mx = max(mx, let[j]);
      }
      ans += n / k - mx;
      if (i != k - 1 - i) {
        ans += n / k;
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
