#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
char s[200005];
int a[30][200005];
int main() {
  int t;
  scanf("%d", &t);
  ;
  while (t--) {
    int n, k, cou = 0;
    scanf("%d", &n);
    ;
    scanf("%d", &k);
    ;
    getchar();
    scanf("%s", s);
    ;
    for (int i = 0; i <= n - 1; ++i) {
      a[s[i] - 'a'][i % k]++;
    }
    for (int i = 0; i <= k / 2 - 1; ++i) {
      int maxx = 0, all = 0;
      for (int j = 0; j <= 25; ++j) {
        maxx = max(maxx, a[j][i] + a[j][k - 1 - i]);
        all += (a[j][i] + a[j][k - 1 - i]);
      }
      cou += (all - maxx);
    }
    if (k % 2 != 0) {
      int maxx = 0, all = 0;
      for (int j = 0; j <= 25; ++j) {
        maxx = max(maxx, a[j][k / 2]);
        all += a[j][k / 2];
      }
      cou += (all - maxx);
    }
    printf("%d\n", cou);
    for (int i = 0; i <= n - 1; ++i) {
      a[s[i] - 'a'][i % k] = 0;
    }
  }
}
