#include <bits/stdc++.h>
using namespace std;
const long long N = 1234567;
const long long MOD = 998244353;
void solve() {
  long long n, m, i, j = 0, k, l = 0, ok = 0, o = 0, p, ans = 0, x, y, a[30];
  string s;
  cin >> n >> k >> s;
  for (long long i = 0; i < k / 2; i++) {
    map<char, long long> mg;
    for (j = 0; j < n; j += k) {
      mg[s[i + j]]++;
      mg[s[j + k - i - 1]]++;
    }
    l = 0;
    for (auto it : mg) l = max(l, it.second);
    ans += l;
  }
  if (k % 2) {
    map<char, long long> mg;
    for (j = 0; j < n; j += k) {
      mg[s[j + k / 2]]++;
    }
    l = 0;
    for (auto it : mg) l = max(l, it.second);
    ans += l;
  }
  cout << (n - ans) << endl;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) solve();
  return 0;
}
