#include <bits/stdc++.h>
using namespace std;
char s[200005];
int n, k;
int cnt[200005][30];
int r[200005];
int fa(int x) { return r[x] == x ? x : r[x] = fa(r[x]); }
void merge(int x, int y) {
  x = fa(x), y = fa(y);
  if (x == y) return;
  for (int i = 0; i < 26; ++i) cnt[y][i] += cnt[x][i];
  r[x] = y;
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d", &n, &k);
    scanf("%s", s + 1);
    for (int i = 1; i <= n; ++i) {
      r[i] = i;
      memset(cnt[i], 0, sizeof cnt[i]);
      cnt[i][s[i] - 'a'] = 1;
    }
    for (int i = 1; i <= n; ++i) {
      if (i > k) merge(i, i - k);
      merge(i, n - i + 1);
    }
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
      if (r[i] == i) {
        int sum = 0, maxx = 0;
        for (int j = 0; j < 26; ++j) {
          sum += cnt[i][j];
          if (cnt[i][j] > maxx) maxx = cnt[i][j];
        }
        ans += sum - maxx;
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
