#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  int ans[t];
  for (int i = 0; i < t; i++) {
    ans[i] = 0;
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    int l = 0;
    int r = k - 1;
    while (l <= r) {
      int b[26] = {0};
      int left = l;
      int right = r;
      if (l != r) {
        while (left < s.size() && right < s.size()) {
          b[s[left] - 'a']++;
          left += k;
          b[s[right] - 'a']++;
          right += k;
        }
      } else {
        while (left < s.size()) {
          b[s[left] - 'a']++;
          left += k;
        }
      }
      int mx = 0, sm = 0;
      for (int j = 0; j < 26; j++) {
        mx = max(b[j], mx);
        sm += b[j];
      }
      ans[i] += sm - mx;
      l++;
      r--;
    }
  }
  for (int i = 0; i < t; i++) cout << ans[i] << endl;
  return 0;
}
