#include <bits/stdc++.h>
using namespace std;
long long mxx = 1e15;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tc = 1;
  cin >> tc;
  while (tc--) {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    int i = 0, j = k - 1;
    long long ans = 0;
    while (i <= j) {
      int a1[26] = {0}, a2[26] = {0};
      for (int it = i; it < n; it += k) {
        a1[s[it] - 'a']++;
      }
      for (int it = j; it < n; it += k) {
        a2[s[it] - 'a']++;
      }
      long long mn = mxx, tt = n / k;
      for (int first = 0; first < 26; first++) {
        mn = min(mn, 2 * tt - a1[first] - a2[first]);
      }
      if (i == j) mn /= 2;
      ans += mn;
      i++, j--;
    }
    cout << ans << '\n';
  }
  return 0;
}
