#include <bits/stdc++.h>
using namespace std;
long long M = 998244353;
long long fact[1000005];
long long gcd(long long a, long long b) {
  while (a != 0 && b != 0) {
    if (a > b)
      a %= b;
    else
      b %= a;
  }
  return max(a, b);
}
long long power(long long a, long long b, long long m) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = (ans * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return ans;
}
long long modInverse(long long a, long long m) {
  long long g = gcd(a, m);
  if (g == 1) return power(a, m - 2, m);
}
long long ncr(long long n, long long r, long long m) {
  if (r == 0) return 1;
  return (fact[n] * modInverse(fact[r], m) % m * modInverse(fact[n - r], m) %
          m) %
         m;
}
void boost() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
int main() {
  boost();
  long long t;
  cin >> t;
  while (t--) {
    long long n, k, i, j;
    cin >> n >> k;
    string s;
    cin >> s;
    long long t = n / k, a, b, ans = 0;
    i = 0;
    j = k - 1;
    while (i < j) {
      int cnt[26] = {0}, maxm = 0, q = t * 2;
      for (a = 0; a < t; a++) {
        b = s[i + a * k] - 'a';
        cnt[b]++;
        maxm = max(cnt[b], maxm);
        b = s[j + a * k] - 'a';
        cnt[b]++;
        maxm = max(cnt[b], maxm);
      }
      ans += q - maxm;
      i++;
      j--;
    }
    if (i == j) {
      int cnt[26] = {0}, maxm = 0, q = t;
      for (a = 0; a < t; a++) {
        b = s[i + a * k] - 'a';
        cnt[b]++;
        maxm = max(maxm, cnt[b]);
      }
      ans += q - maxm;
    }
    cout << ans << endl;
  }
}
