#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 17;
const int INF = 0x7fffffff;
int t, n;
int k;
char s[maxn];
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%d", &n);
    scanf("%d", &k);
    scanf("%s", s);
    int ans = 0;
    int all = n / k;
    int num[30] = {0};
    int maxn = 0;
    for (int i = 0; i < k / 2; i++) {
      memset(num, 0, sizeof(num));
      maxn = 0;
      for (int j = 0; j < all; j++) {
        num[s[j * k + i] - 'a']++;
        num[s[j * k + k - 1 - i] - 'a']++;
      }
      for (int j = 0; j < 30; j++) maxn = max(maxn, num[j]);
      ans += 2 * all - maxn;
    }
    if (k & 1) {
      memset(num, 0, sizeof(num));
      maxn = 0;
      for (int j = 0; j < all; j++) {
        num[s[j * k + k / 2] - 'a']++;
      }
      for (int j = 0; j < 30; j++) maxn = max(maxn, num[j]);
      ans += all - maxn;
    }
    printf("%d\n", ans);
  }
}
