#include <bits/stdc++.h>
using namespace std;
int n, k, m, t;
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> t;
  while (t--) {
    cin >> n >> k;
    string s;
    cin >> s;
    int ans = 0;
    for (int i = 0; 2 * i < k; i++) {
      int mx = 0, cc = 0;
      vector<int> cnt(26, 0);
      for (int j = i; j < n; j += k) {
        cnt[s[j] - 'a']++;
        cc++;
      }
      if (i != k - 1 - i) {
        for (int j = k - 1 - i; j < n; j += k) {
          cnt[s[j] - 'a']++;
          cc++;
        }
      }
      for (int j = 0; j < 26; j++) mx = max(cnt[j], mx);
      ans += cc - mx;
    }
    cout << ans << endl;
  }
}
