#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<int> m;
  int t;
  cin >> t;
  int n, k;
  string s;
  while (t--) {
    cin >> n >> k >> s;
    int num_str = n / k, res = 0, num = n * 2 / k;
    for (int i = 0; i < k / 2; ++i) {
      vector<int> lett(26);
      for (int j = 0; j < num_str; ++j) {
        ++lett[s[j * k + i] - 'a'];
        ++lett[s[(j + 1) * k - 1 - i] - 'a'];
      }
      int ans = 0;
      for (auto &cnt : lett) {
        ans = max(ans, cnt);
      }
      res += num - ans;
    }
    if (k & 1) {
      int i = k / 2;
      vector<int> lett(26);
      for (int j = 0; j < num_str; ++j) {
        ++lett[s[j * k + i] - 'a'];
      }
      int ans = 0;
      for (auto &cnt : lett) {
        ans = max(ans, cnt);
      }
      res += num / 2 - ans;
    }
    cout << res << endl;
  }
  return 0;
}
