#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, fh = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') fh = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return x * fh;
}
const int maxn = 2e5 + 5;
int a[maxn];
char s[maxn];
inline void work() {
  int n = read(), t = read(), k = n / t, c[27], ans = 0, w;
  scanf("%s", s + 1);
  for (register int i = 1; i <= n; ++i) a[i] = s[i] - 'a' + 1;
  for (register int i = 1; i <= t / 2; ++i) {
    w = 0;
    memset(c, 0, sizeof c);
    for (register int j = 0; j <= k - 1; ++j) {
      int t1 = j * t + i, t2 = (j + 1) * t - i + 1;
      ++c[a[t1]];
      ++c[a[t2]];
    }
    for (register int j = 1; j <= 26; ++j) w = max(w, c[j]);
    ans += 2 * k - w;
  }
  if (t & 1) {
    w = 0;
    memset(c, 0, sizeof c);
    for (register int j = 0; j <= k - 1; ++j) {
      int t1 = j * t + t / 2 + 1;
      ++c[a[t1]];
    }
    for (register int j = 1; j <= 26; ++j) w = max(w, c[j]);
    ans += k - w;
  }
  printf("%d\n", ans);
}
int main() {
  int t = read();
  while (t--) work();
  return 0;
}
