#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
const int inf = 0x7f7f7f7f;
int n, k, cnt[30];
char s[maxn];
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d", &n, &k);
    getchar();
    scanf("%s", s);
    int ans = 0;
    for (int i = 0, j = k - 1; i <= j; i++, j--) {
      memset(cnt, 0, sizeof(cnt));
      for (int ss = 0; ss < n; ss += k)
        cnt[s[ss + i] - 'a']++, cnt[s[ss + j] - 'a']++;
      int sum = 0, mx = 0;
      for (int l = 0; l < 26; l++) sum += cnt[l], mx = max(mx, cnt[l]);
      if (i != j)
        ans += sum - mx;
      else
        ans += (sum - mx) / 2;
    }
    printf("%d\n", ans);
  }
  return 0;
}
