#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    string str;
    cin >> str;
    int md;
    int ans = 0;
    if (k % 2 == 0) {
      md = (k / 2) - 1;
      for (int i = 0; i <= md; ++i) {
        int mx = 0;
        map<char, int> mp;
        for (int j = 0; j < 26; ++j) {
          char c = 'a' + j;
          mp[c] = 0;
        }
        for (int j = i; j < n; j = j + k) {
          char ch = str[j];
          mp[ch] = mp[ch] + 1;
        }
        for (int j = k - 1 - i; j < n; j = j + k) {
          char ch = str[j];
          mp[ch] = mp[ch] + 1;
        }
        for (int j = 0; j < 26; ++j) {
          char c = 'a' + j;
          mx = max(mx, mp[c]);
        }
        ans += ((n / k) * 2) - mx;
      }
      cout << ans << "\n";
    } else {
      md = (k / 2) - 1;
      for (int i = 0; i <= md; ++i) {
        int mx = 0;
        map<char, int> mp;
        for (int j = 0; j < 26; ++j) {
          char c = 'a' + j;
          mp[c] = 0;
        }
        for (int j = i; j < n; j = j + k) {
          char ch = str[j];
          mp[ch] = mp[ch] + 1;
        }
        for (int j = k - 1 - i; j < n; j = j + k) {
          char ch = str[j];
          mp[ch] = mp[ch] + 1;
        }
        for (int j = 0; j < 26; ++j) {
          char c = 'a' + j;
          mx = max(mx, mp[c]);
        }
        ans += ((n / k) * 2) - mx;
      }
      int mx = 0;
      map<char, int> mp;
      for (int j = 0; j < 26; ++j) {
        char c = 'a' + j;
        mp[c] = 0;
      }
      for (int i = 0; i < (n / k); ++i) {
        char c = str[md + 1 + i * k];
        mp[c] = mp[c] + 1;
      }
      for (int i = 0; i < 26; ++i) {
        char c = 'a' + i;
        mx = max(mx, mp[c]);
      }
      ans += (n / k) - mx;
      cout << ans << "\n";
    }
  }
  return 0;
}
