#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tc;
  cin >> tc;
  while (tc--) {
    int n, k, ans = 0;
    cin >> n >> k;
    string s;
    cin >> s;
    bool vis[n];
    memset(vis, false, sizeof(vis));
    vector<int> cnt(26);
    vector<char> chars(26);
    iota(begin(chars), end(chars), 'a');
    int count = 0;
    for (int i = 0; i < k; i++) {
      count = 0;
      for (int j = 0; j < 26; j++) {
        cnt[j] = 0;
      }
      for (int j = i; j < n; j += k) {
        if (!vis[j]) {
          vis[j] = true;
          count++;
          char c = s[j];
          cnt[c - 97]++;
          int m = (n - 1) - j;
          if (!vis[m]) {
            vis[m] = true;
            count++;
            char cm = s[m];
            cnt[cm - 97]++;
          }
        }
      }
      int maxv = *max_element(begin(cnt), end(cnt));
      ans += (count - maxv);
    }
    cout << ans << "\n";
  }
  return 0;
}
