#include <bits/stdc++.h>
using namespace std;
long long primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
int main() {
  int t, n, k;
  cin >> t;
  string s;
  while (t--) {
    cin >> n >> k;
    cin >> s;
    int link[n][27];
    memset(link, 0, sizeof link);
    for (int i = 0; i < k; i++) {
      for (int j = 0; j < n; j += k) {
        int mm = j + i;
        int nn = j + k - 1 - i;
        if (mm == nn)
          link[i][s[mm] - 'a']++;
        else {
          link[i][s[mm] - 'a']++;
          link[i][s[nn] - 'a']++;
        }
      }
      for (int kk = 0; kk < 26; kk++) link[i][26] += link[i][kk];
    }
    long long ans = 0;
    for (int i = 0; i < (k + 1) / 2; i++) {
      ans += link[i][26] - *max_element(link[i], link[i] + 26);
    }
    cout << ans << '\n';
  }
  return 0;
}
