#include <bits/stdc++.h>
using namespace std;
int mod = 998244353;
const int MAXN = 4e5 + 2;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  cin >> t;
  while (t--) {
    int n, k;
    string a;
    int s[27]{0};
    cin >> n >> k;
    cin >> a;
    int ans = n;
    for (int i = 0; i < (k + 1) / 2; i++) {
      memset(s, 0, sizeof(s));
      int t = 1;
      for (int j = i; j < a.size(); j += k) {
        s[a[j] - 'a']++;
        if (t * k - 1 - i != j) s[a[t * k - 1 - i] - 'a']++;
        t++;
      }
      sort(s, s + 27);
      ans = ans - s[26];
    }
    cout << ans << endl;
  }
  return 0;
}
