#include <bits/stdc++.h>
using namespace std;
bool isprime(long long n) {
  for (long long i = 2; i * i <= n; i++) {
    if (n % i == 0) return false;
  }
  return true;
}
bool sortbysec(const pair<long long, long long> &a,
               const pair<long long, long long> &b) {
  return a.second < b.second;
}
void frooty() {
  long long n, k;
  cin >> n >> k;
  string s;
  cin >> s;
  long long ans = 0;
  for (long long i = 0; i <= k / 2; i++) {
    map<long long, long long> m;
    int fl = 0;
    if ((k % 2 == 1) && i == k / 2) {
      fl = 1;
      for (long long j = i; j < n; j += k) {
        m[s[j] - 'a']++;
      }
    }
    if (i < k / 2) {
      fl = 1;
      for (long long j = i; j < n; j += k) {
        m[s[j] - 'a']++;
      }
      for (long long j = k - 1 - i; j < n; j += k) m[s[j] - 'a']++;
    }
    long long maxx = -1;
    for (long long j = 0; j < 26; j++) {
      if (m[j] > maxx) {
        maxx = m[j];
      }
    }
    if (i == k / 2 && fl == 1)
      ans += (n / k) - maxx;
    else if (fl == 1)
      ans += (2 * n / k) - maxx;
  }
  cout << ans << endl;
}
int main() {
  long long t;
  cin >> t;
  while (t--) {
    frooty();
  }
}
