#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e5 + 9;
const long long inf = 20122007;
long long t, n, k, j, i, p;
string s;
long long res[maxn][26], maxx[maxn];
long long max3(long long x, long long y, long long z) {
  return max(x, max(y, z));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  while (t--) {
    cin >> n >> k >> s;
    s = " " + s;
    p = ceil((long double)k / 2);
    for (i = 0; i < k; i++) {
      maxx[i] = 0;
      for (j = 0; j < 26; j++) res[i][j] = 0;
    }
    for (i = 1; i <= n; i += k) {
      for (j = i; j <= i + p - 1; j++) {
        res[j - i][s[j] - 'a']++;
        long long g = i + k - 1 - j + i;
        if (j != g) res[j - i][s[g] - 'a']++;
        maxx[j - i] =
            max3(maxx[j - i], res[j - i][s[j] - 'a'], res[j - i][s[g] - 'a']);
      }
    }
    long long ans = 0;
    for (i = 0; i < p; i++) {
      if (k % 2 != 0 && i == p - 1)
        ans += n / k - maxx[i];
      else
        ans += (n / k) * 2 - maxx[i];
    }
    cout << ans << "\n";
  }
}
