#include <bits/stdc++.h>
using namespace std;
void solve(string& s, int n, int k) {
  s = "-" + s;
  int ans = 0;
  for (int i = 1; i <= k / 2; i++) {
    map<char, int> mp;
    mp[s[i]]++;
    int maxCount = 1, count = 1;
    for (int j = k - i + 1; j <= n; j += k) {
      mp[s[j]]++;
      maxCount = max(maxCount, mp[s[j]]);
      count++;
    }
    for (int j = k + i; j <= n; j += k) {
      mp[s[j]]++;
      maxCount = max(maxCount, mp[s[j]]);
      count++;
    }
    ans += count - maxCount;
  }
  if (k % 2 != 0) {
    map<char, int> mp;
    int count = 0, maxCount = 0;
    for (int i = (k + 1) / 2; i <= n; i += k) {
      mp[s[i]]++;
      maxCount = max(maxCount, mp[s[i]]);
      count++;
    }
    ans += count - maxCount;
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t = 1;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    solve(s, n, k);
  }
}
