#include <bits/stdc++.h>
using namespace std;
int tc, n, k;
char c;
int main() {
  scanf("%d", &tc);
  while (tc--) {
    scanf("%d %d\n", &n, &k);
    int banyak[k + 1][27];
    memset(banyak, 0, sizeof(banyak));
    for (int i = 0; i < n; i++) {
      scanf("%c", &c);
      banyak[i % k][c - 97]++;
    }
    int ans = 0;
    for (int i = 0; i < (k + 1) >> 1; i++) {
      int maxi = 0;
      if (i == k - 1 - i) {
        for (int j = 0; j < 26; j++) {
          maxi = max(maxi, banyak[i][j]);
        }
        ans = ans + n / k - maxi;
      } else {
        for (int j = 0; j < 26; j++) {
          maxi = max(maxi, banyak[i][j] + banyak[k - 1 - i][j]);
        }
        ans = ans + 2 * n / k - maxi;
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
