#include <bits/stdc++.h>
using namespace std;
int read() {
  int res = 0, f = 1;
  char c = getchar();
  while (!isdigit(c) && c != '-') c = getchar();
  if (c == '-') f = -1, c = getchar();
  while (isdigit(c)) res = (res << 3) + (res << 1) + (c ^ 48), c = getchar();
  return res * f;
}
const int maxn = 2e5 + 10;
int n, k, val[26];
char s[maxn];
int main() {
  int T = read();
  while (T--) {
    n = read();
    k = read();
    int ans = 0;
    scanf("%s", s + 1);
    for (int i = 1; i <= k / 2; i++) {
      memset(val, 0, sizeof(val));
      for (int j = 0; j < n / k; j++) {
        val[s[i + j * k] - 'a']++;
        val[s[k - i + 1 + j * k] - 'a']++;
      }
      int tmp = 0;
      for (int j = 0; j < 26; j++) tmp = max(tmp, val[j]);
      ans += n / k * 2 - tmp;
    }
    if (k & 1) {
      memset(val, 0, sizeof(val));
      for (int j = 0; j < n / k; j++) {
        val[s[k / 2 + 1 + j * k] - 'a']++;
      }
      int tmp = 0;
      for (int j = 0; j < 26; j++) tmp = max(tmp, val[j]);
      ans += n / k - tmp;
    }
    printf("%d\n", ans);
  }
  return 0;
}
