#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tc;
  cin >> tc;
  while (tc--) {
    long long n, k, ans = 0;
    string s;
    cin >> n >> k >> s;
    for (int i = 0; i < (k + 1) / 2; ++i) {
      long long sum = 0, mx = 0;
      map<char, long long> mp;
      for (int j = i; j < n; j += k) ++mp[s[j]];
      if (i != k - i - 1)
        for (int j = k - i - 1; j < n; j += k) ++mp[s[j]];
      for (auto it : mp) sum += it.second, mx = max(mx, it.second);
      ans += sum - mx;
    }
    cout << ans << '\n';
  }
  return 0;
}
