#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int MOD = 1000000007;
int tab[26][200005];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, k, ans = 0;
    cin >> n >> k;
    string p;
    cin >> p;
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < 26; ++j) tab[j][i] = 0;
    for (int i = 0; i < n; ++i) tab[(int)(p[i] - 'a')][i % k]++;
    for (int i = 0; i < k / 2; ++i) {
      int add = 1000000000;
      for (int j = 0; j < 26; ++j)
        add = min(add, 2 * (n / k) - tab[j][i] - tab[j][k - i - 1]);
      ans += add;
    }
    if (k & 1) {
      int add = 1000000000;
      for (int j = 0; j < 26; ++j) add = min(add, (n / k) - tab[j][k / 2]);
      ans += add;
    }
    cout << ans << "\n";
  }
  return 0;
}
