#include <bits/stdc++.h>
using namespace std;
void print(vector<long long> v) {
  long long n = v.size();
  for (long long i = 0; i < n; i++) {
    cout << v[i] << " ";
  }
}
long long power(long long a, long long b) {
  long long res = 1;
  while (b != 0) {
    if (b & 1) {
      res *= a;
    }
    a = (a * a);
    b = (b / 2);
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long t;
  cin >> t;
  while (t--) {
    long long n, k, ans = 0;
    cin >> n >> k;
    string s;
    cin >> s;
    string s1 = s;
    reverse(s1.begin(), s1.end());
    string s2 = s + s1;
    for (long long i = 0; i < k; i++) {
      long long cnt = 0;
      map<char, long long> mp;
      for (long long j = i; j < 2 * n; j += k) {
        mp[s2[j]]++;
      }
      long long mx = 0;
      for (auto it = mp.begin(); it != mp.end(); it++) {
        mx = max(mx, it->second);
        cnt += it->second;
      }
      ans += (cnt - mx);
    }
    cout << ans / 2 << '\n';
  }
  return 0;
}
