#include <bits/stdc++.h>
using namespace std;
long long num[26];
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long a, b, c;
    scanf("%lld %lld\n", &a, &b);
    string s;
    cin >> s;
    c = a / b;
    int d = s.length();
    string ans1, ans2;
    for (long long i = 0; i < b; i++) ans1 += " ";
    for (long long i = 0; i < (b + 1) / 2; i++) {
      memset(num, 0, sizeof(num));
      for (long long j = 0; j < c; j++) {
        long long tmp = j * b;
        num[s[tmp + i] - 'a']++;
        num[s[tmp + b - i - 1] - 'a']++;
      }
      long long sum = 0, p = 0;
      for (int i = 0; i < 26; i++) {
        if (sum < num[i]) {
          sum = num[i];
          p = i;
        }
      }
      ans1[i] = p + 'a';
      ans1[b - i - 1] = p + 'a';
    }
    int ans = 0;
    for (int i = 0; i < d; i++) {
      if (ans1[i % b] != s[i]) ans++;
    }
    cout << ans << endl;
  }
  return 0;
}
