#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long int n, k = 0, sum = 0, m = 0, l = 0, flag, r = 0, p = 0, p1 = 0, x,
                   y, q, lcm, a1, b1, i, j, n1, n2, n0;
  cin >> n >> k;
  char s[n + 1];
  cin >> s;
  long long int a[26];
  for (i = 0; i < (k + 1) / 2; i++) {
    for (r = 0; r < 26; r++) {
      a[r] = 0;
    }
    for (j = i; j < n; j += k) {
      a[s[j] - 'a']++;
      if (k % 2 == 1 && i == k / 2)
        ;
      else
        a[s[j + k - 1 - 2 * i] - 'a']++;
    }
    sum = 0;
    m = 0;
    for (j = 0; j < 26; j++) {
      sum += a[j];
      if (m < a[j]) m = a[j];
    }
    l += sum - m;
  }
  cout << l << "\n";
}
int main() {
  long long int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
}
