#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  while (a != 0 && b != 0) {
    if (a > b) {
      a = a % b;
    } else
      b = b % a;
  }
  return a + b;
}
void bon() {
  int n, k, ans = 0, x;
  cin >> n >> k;
  string s;
  cin >> s;
  for (int i = 0; i < k / 2; i++) {
    int b[100100] = {0};
    int d = 0, m;
    int x = i;
    while (x < n) {
      b[s[x]]++;
      d++;
      x += k;
    }
    x = k - i - 1;
    while (x < n) {
      b[s[x]]++;
      d++;
      x += k;
    }
    m = b[97];
    for (int j = 98; j <= 122; j++) {
      m = max(m, b[j]);
    }
    ans += d - m;
  }
  if (k % 2 == 0)
    cout << ans << endl;
  else {
    int b[100100] = {0};
    int d = 0, m;
    x = k / 2;
    while (x < n) {
      b[s[x]]++;
      d++;
      x += k;
    }
    m = b[97];
    for (int j = 98; j <= 122; j++) {
      m = max(m, b[j]);
    }
    ans += d - m;
    cout << ans << endl;
  }
}
int main() {
  int t = 1;
  cin >> t;
  while (t--) {
    bon();
  }
  return 0;
}
