#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.sync_with_stdio(false), cin.tie(nullptr);
  int tests;
  cin >> tests;
  while (tests--) {
    int n, k;
    string s;
    cin >> n >> k >> s;
    const int need = n / k;
    vector<vector<int>> counts(k, vector<int>(26, 0));
    for (int i = 0; i < n; i++) {
      counts[i % k][s[i] - 'a']++;
    }
    int ans = 0;
    for (int i = 0, j = k - 1; i <= j; i++, j--) {
      int cost = need * 2 - counts[i][0] - counts[j][0];
      for (int c = 1; c < 26; c++) {
        cost = min(cost, need * 2 - counts[i][c] - counts[j][c]);
      }
      ans += (i == j ? cost / 2 : cost);
    }
    cout << ans << "\n";
  }
}
