#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
template <typename T>
void chmin(T &x, const T &y) {
  if (x > y) x = y;
}
template <typename T>
void chmax(T &x, const T &y) {
  if (x < y) x = y;
}
template <typename T>
T gcd(T x, T y) {
  return y ? gcd(y, x % y) : x;
}
const int N = 2e5 + 5;
int n, k;
char s[N];
int cnt[30][N];
void solve(int times) {
  scanf("%d%d%s", &n, &k, s);
  for (int i = 0; i < 26; ++i)
    for (int j = 0; j <= k; ++j) cnt[i][j] = 0;
  for (int i = 0; i < n; ++i) {
    int t = i % k;
    if (t >= k / 2) t = k - 1 - t;
    ++cnt[s[i] - 'a'][t];
  }
  int ans = 0;
  for (int i = 0; i < k / 2; ++i) {
    int mx = 0;
    for (int j = 0; j < 26; ++j) chmax(mx, cnt[j][i]);
    ans += n / k * 2 - mx;
  }
  if (k % 2) {
    int i = k / 2, mx = 0;
    for (int j = 0; j < 26; ++j) chmax(mx, cnt[j][i]);
    ans += n / k - mx;
  }
  cout << ans << endl;
}
int main() {
  int times = 1;
  int T;
  cin >> T;
  for (; T--;) {
    solve(times++);
  }
  return 0;
}
