#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, k, ans = 0;
    cin >> n >> k;
    char s[n];
    for (int i = 0; i < n; i++) cin >> s[i];
    for (int i = 0; i < (k + 1) / 2; i++) {
      int ara[26];
      for (int j = 0; j < 26; j++) ara[j] = 0;
      int a1 = 0, a2 = -1;
      for (int j = i; j < n; j += k) {
        ara[s[j] - 'a']++, a1++;
      }
      if (k - i - 1 != i)
        for (int j = k - i - 1; j < n; j += k) ara[s[j] - 'a']++, a1++;
      for (int i = 0; i < 26; i++) a2 = max(ara[i], a2);
      ans += a1 - a2;
    }
    cout << ans << "\n";
  }
}
