#include <bits/stdc++.h>
using namespace std;
long long gcd(long long x, long long y) { return y > 0 ? gcd(y, x % y) : x; }
long long lcm(long long x, long long y) { return x / gcd(x, y) * y; }
const int N = 2000010, M = 3000010;
const int MAXN = 0X3f3f3f3f;
const int maxn = 2e5 + 10;
const int mod = 1e9 + 7;
const int HX = 131;
char s[N];
int ch[N];
int main() {
  int t, n, k;
  cin >> t;
  while (t--) {
    int ans = 0;
    scanf("%d%d", &n, &k);
    scanf("%s", s);
    for (int i = 0; i <= (k - 1) >> 1; i++) {
      for (int j = i; j < n; j += k) {
        int q = j - 2 * i + k - 1;
        ch[s[j]]++;
        if (j != q) ch[s[q]]++;
      }
      int p = 0;
      for (int i = 'a'; i <= 'z'; i++) {
        if (ch[i] > p) p = ch[i];
        ch[i] = 0;
      }
      if (k & 1 && i == k / 2)
        ans += n / k - p;
      else
        ans += n / k * 2 - p;
    }
    printf("%d\n", ans);
  }
  return 0;
}
