#include <bits/stdc++.h>
using namespace std;
vector<long long> a;
int main() {
  long long t;
  cin >> t;
  for (long long ind = 0; ind < t; ++ind) {
    long long n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    a.clear();
    a.resize(26, 0);
    long long num = n / k, ans = 0;
    for (long long i = 0; i < k; ++i) {
      if (i > k - i - 1) break;
      for (long long j = i; j < n; j += k) {
        a[s[j] - 'a']++;
      }
      bool p = false;
      if (i != k - i - 1) {
        p = true;
        for (long long j = k - i - 1; j < n; j += k) {
          a[s[j] - 'a']++;
        }
      }
      long long m = a[0];
      for (long long j = 1; j < 26; ++j) {
        if (a[j] > m) m = a[j];
      }
      if (p)
        ans += 2 * num - m;
      else
        ans += num - m;
      a.clear();
      a.resize(26, 0);
    }
    cout << ans << '\n';
  }
}
