#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    long long ans = 0;
    vector<long long> freq(26, 0);
    for (long long j = 0; j < k / 2; j++) {
      fill(freq.begin(), freq.end(), 0);
      for (long long i = j; i < n; i += k) freq[s[i] - 'a']++;
      for (long long i = k - j - 1; i < n; i += k) freq[s[i] - 'a']++;
      char element_in_this_pos =
          'a' + (max_element(freq.begin(), freq.end()) - freq.begin());
      for (long long i = j; i < n; i += k) {
        ans += (s[i] != element_in_this_pos);
        s[i] = element_in_this_pos;
      }
      for (long long i = k - j - 1; i < n; i += k) {
        ans += (s[i] != element_in_this_pos);
        s[i] = element_in_this_pos;
      }
    }
    if (k & 1) {
      fill(freq.begin(), freq.end(), 0);
      for (long long i = k / 2; i < n; i += k) freq[s[i] - 'a']++;
      char element_in_this_pos =
          'a' + (max_element(freq.begin(), freq.end()) - freq.begin());
      for (long long i = k / 2; i < n; i += k) {
        ans += (s[i] != element_in_this_pos);
        s[i] = element_in_this_pos;
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
