#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    int l = n / k;
    vector<vector<int>> c(k, vector<int>(26, 0));
    for (int i = 0; i < n; ++i) {
      ++c[i % k][s[i] - 'a'];
    }
    int ans = 0;
    for (int i = 0; i < (k + 1) / 2; ++i) {
      int max_cnt = 0, sum = 0;
      for (int j = 0; j < 26; ++j) {
        int cnt = (i != k - i - 1) ? c[i][j] + c[k - i - 1][j] : c[i][j];
        sum += cnt;
        if (cnt > max_cnt) {
          max_cnt = cnt;
        }
      }
      ans += sum - max_cnt;
    }
    cout << ans << '\n';
  }
  return 0;
}
