#include <bits/stdc++.h>
int letters[200000][26];
char str[200000];
int diff(int a, int b) {
  int sum = 0, max = 0;
  for (int i = 0; i < 26; i++) {
    sum += letters[a][i] + letters[b][i];
    max = std::max(max, letters[a][i] + letters[b][i]);
  }
  return sum - max;
}
int main() {
  int Tests, n, k, changes;
  scanf("%d", &Tests);
  for (int curtest = 0; curtest < Tests; curtest++) {
    scanf("%d %d", &n, &k);
    for (int i = 0; i < k; i++)
      for (int e = 0; e < 26; e++) letters[i][e] = 0;
    scanf("%s", str);
    for (int i = 0; i < n; i++) letters[i % k][str[i] - 'a']++;
    changes = 0;
    for (int i = 0; i < k; i++) {
      changes += diff(i, k - 1 - i);
    }
    std::cout << changes / 2 << "\n";
  }
  return 0;
}
