#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n, m, k, i, j, mi, ma, x, y, flag = 0, count = 0, ans = 0;
    cin >> n >> k;
    string s;
    vector<long long> cou(26, 0);
    cin >> s;
    if (k % 2 == 1 && k > 1) {
      for (i = k / 2; i < n; i = i + k) cou[s[i] - 'a']++;
      sort(cou.begin(), cou.end());
      for (i = 0; i < 25; i++) {
        if (cou[i] > 0) count += cou[i];
      }
      ans += count;
    }
    if (k % 2 == 1 && k == 1) {
      for (i = k / 2; i < n; i = i + k) cou[s[i] - 'a']++;
      sort(cou.begin(), cou.end());
      for (i = 0; i < 25; i++) ans += cou[i];
    }
    for (i = 0; i < k / 2; i++) {
      for (mi = 0; mi < 26; mi++) cou[mi] = 0;
      count = 0;
      for (j = i; j < n; j = j + k) cou[s[j] - 'a']++;
      for (j = n - (1 + i); j >= 0; j = j - k) cou[s[j] - 'a']++;
      sort(cou.begin(), cou.end());
      for (mi = 0; mi < 25; mi++) count += cou[mi];
      ans += count;
    }
    cout << ans << "\n";
  }
}
