#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
char buff[N];
int freq[26 + 7];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n, k;
    scanf("%d %d", &n, &k);
    scanf(" %s", buff);
    string str = (string)buff;
    int ans = 0;
    for (int l = 0; l < ceil(k / 2.0); l++) {
      int i = 0;
      memset(freq, 0, sizeof freq);
      while (i < n) {
        freq[str[i + l] - 'a']++;
        if (i + l != i + k - 1 - l) {
          freq[str[i + k - 1 - l] - 'a']++;
          ans += 2;
        } else
          ans++;
        i += k;
      }
      int mx = 0;
      for (int j = 0; j < 26; j++) mx = max(mx, freq[j]);
      ans -= mx;
    }
    printf("%d\n", ans);
  }
}
