#include <bits/stdc++.h>
using namespace std;
int main() {
  int tc, n, k;
  string s;
  bool is[2 * 100000 + 10];
  int ted[30], maximum, ans, x, p;
  cin >> tc;
  for (; tc; tc--) {
    cin >> n >> k >> s;
    ans = 0;
    for (int i = 0; i < k; i++) is[i] = true;
    for (int i = 0; i < k; i++) {
      if (is[i]) {
        for (int j = 0; j < 30; j++) ted[j] = 0;
        maximum = 0;
        p = 0;
        for (int j = i; j < n; j += k) {
          ted[s[j] - 'a']++;
          maximum = max(maximum, ted[s[j] - 'a']);
          p++;
        }
        x = (n - 1 - i) % k;
        if (x != i) {
          for (int j = x; j < n; j += k) {
            ted[s[j] - 'a']++;
            maximum = max(maximum, ted[s[j] - 'a']);
            p++;
          }
        }
        is[i] = false;
        is[x] = false;
        ans += p - maximum;
      }
    }
    cout << ans << endl;
  }
}
