#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
const long long inf = 1e18;
long long i, j, ans, k, m, n, qq, t, it, ma = -inf, mi = inf, x, y, col,
                                         md = 998244353;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cin >> qq;
  while (qq--) {
    cin >> n >> k;
    m = n / k;
    ans = 0;
    string second;
    cin >> second;
    vector<vector<long long> > a(k, vector<long long>(26, 0));
    for (i = 0; i < n; i++) a[i % k][second[i] - 'a']++;
    for (i = 0; i < k / 2; i++) {
      for (j = 0; j < 26; j++) {
        a[i][j] += a[k - 1 - i][j];
      }
      ans += 2 * m - *max_element(a[i].begin(), a[i].end());
    }
    if (k % 2 == 1) ans += m - *max_element(a[i].begin(), a[i].end());
    cout << ans << '\n';
  }
  return 0;
}
