#include <bits/stdc++.h>
using namespace std;
int ar[100005][30];
int main() {
  int t, n, x, k;
  cin >> t;
  string s;
  while (t--) {
    cin >> n >> k >> s;
    for (int i = 0; i < n; i++) ar[i % k][s[i] - 'a']++;
    int sum = 0;
    for (int i = 0; i < k; i++) {
      int mx;
      x = mx = 0;
      for (int j = 0; j <= 26; j++) {
        x += ar[i][j] + ar[k - i - 1][j];
        mx = max(mx, ar[i][j] + ar[k - i - 1][j]);
      }
      sum += (x - mx);
    }
    cout << sum / 2 << endl;
    for (int i = 0; i < k; i++) memset(ar[i], 0, sizeof ar[i]);
  }
  return 0;
}
