#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const long long N = 2e5 + 5;
long long visit[N] = {0}, inti[N], out[N], lev[N], tim = 0, prime[1005];
int main() {
  long long t, n, k;
  cin >> t;
  while (t--) {
    string s;
    cin >> n >> k;
    cin >> s;
    long long tempk = k / 2, p = 1, j, ans = 0, temp;
    while (tempk--) {
      j = p;
      temp = k - p;
      map<long long, long long> freq;
      while (j <= n) {
        freq[s[j - 1] - 'a']++;
        freq[s[temp] - 'a']++;
        j += k;
        temp += k;
      }
      long long maxm = 0, sum = 0;
      for (int i = 0; i < 26; i++) {
        sum += freq[i];
        maxm = max(freq[i], maxm);
      }
      ans += sum - maxm;
      p++;
    }
    if (k % 2) {
      map<long long, long long> freq1;
      for (int i = k / 2; i < n; i += k) freq1[s[i] - 'a']++;
      long long sum = 0, maxm = 0;
      for (int i = 0; i < 26; i++) {
        sum += freq1[i];
        maxm = max(freq1[i], maxm);
      }
      ans += sum - maxm;
    }
    cout << ans << endl;
  }
  return 0;
}
