#include <bits/stdc++.h>
using namespace std;
char s[200010];
int flag[30];
bool w[200010];
int ans;
int main() {
  int n, k, t;
  cin >> t;
  while (t--) {
    scanf("%d%d%s", &n, &k, s + 1);
    ans = 0;
    for (int i = 1; i <= n; i++) w[i] = false;
    for (int i = 1; i <= n; i++) {
      if (w[i]) continue;
      int num = 0;
      int maxx = 0;
      memset(flag, 0, sizeof(flag));
      for (int j = i; j <= n; j += k) {
        if (w[j]) break;
        w[j] = true;
        num++;
        flag[s[j] - 'a']++;
        maxx = max(maxx, flag[s[j] - 'a']);
      }
      for (int j = n - i + 1; j >= 1; j -= k) {
        if (w[j]) break;
        w[j] = true;
        num++;
        flag[s[j] - 'a']++;
        maxx = max(maxx, flag[s[j] - 'a']);
      }
      ans = ans + num - maxx;
    }
    cout << ans << endl;
  }
}
