#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  vector<vector<int>> count((k + 1) / 2, vector<int>(26));
  for (int i = 0; i < (k + 1) / 2; i++) {
    if (i < (k - 1) / 2 || k % 2 == 0) {
      for (int j = 0; j < n / k; j++) {
        count[i][s[k * j + i] - 'a']++;
        count[i][s[k * j + k - 1 - i] - 'a']++;
      }
    } else {
      for (int j = 0; j < n / k; j++) count[i][s[k * j + i] - 'a']++;
    }
  }
  long long ans = 0;
  for (int i = 0; i < (k + 1) / 2; i++) {
    int max_count = 0;
    for (int j = 0; j < 26; j++) max_count = max(max_count, count[i][j]);
    if (i < (k - 1) / 2 || k % 2 == 0)
      ans += (2 * n / k) - max_count;
    else
      ans += n / k - max_count;
  }
  cout << ans << "\n";
}
int main() {
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
