#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void ckmax(T &a, T b) {
  if (a < b) a = b;
}
template <class T>
inline void ckmin(T &a, T b) {
  if (a > b) a = b;
}
const int p[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
int t, n, k, cnt[200005][30];
char s[200005];
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%d%d%s", &n, &k, s + 1);
    for (int i = 1, I = (k) + 1; i < I; ++i)
      for (int j = 0, I = (25) + 1; j < I; ++j) cnt[i][j] = 0;
    for (int j = 0; j < n; j += k) {
      for (int i = 1, I = (k) + 1; i < I; ++i) ++cnt[i][s[i + j] - 'a'];
    }
    int ans = 0;
    for (int i = 1, I = (k >> 1) + 1; i < I; ++i) {
      int mx = 0;
      for (int j = 0, I = (25) + 1; j < I; ++j)
        ckmax(mx, cnt[i][j] + cnt[k - i + 1][j]);
      ans += (n / k) * 2 - mx;
    }
    if (k & 1)
      ans += n / k - *max_element(cnt[k + 1 >> 1], cnt[k + 1 >> 1] + 26);
    printf("%d\n", ans);
  }
  return 0;
}
