#include <bits/stdc++.h>
using namespace std;
long long M = 1e8;
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    int c[26] = {0};
    int ans = 0;
    int m = n / k;
    for (int i = 0; i < k / 2; i++) {
      for (int j = 0; j < 26; j++) c[j] = 0;
      for (int j = 0; j < m; j++) {
        c[s[j * k + i] - 'a']++;
        c[s[(j + 1) * k - 1 - i] - 'a']++;
      }
      int p = 0;
      for (int j = 0; j < 26; j++) {
        p = max(p, c[j]);
      }
      ans += 2 * m - p;
    }
    if (k % 2) {
      for (int j = 0; j < 26; j++) c[j] = 0;
      for (int j = 0; j < m; j++) {
        c[s[j * k + k / 2] - 'a']++;
      }
      int p = 0;
      for (int j = 0; j < 26; j++) {
        p = max(p, c[j]);
      }
      ans += m - p;
    }
    cout << ans << endl;
  }
  return 0;
}
