#include <bits/stdc++.h>
using namespace std;
const long long init = 1e9 + 7;
const long long N = 1e5 + 7;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    long long ans = 0;
    for (int i = 0; i < (k + 1) / 2; ++i) {
      vector<int> v(26, 0);
      int ct = 0;
      for (int j = i; j < n; j += k) {
        ++v[s[j] - 'a'];
        int y = (ct + 1) * k - 1 - (j % k);
        if (j != y) ++v[s[y] - 'a'];
        ++ct;
      }
      char ch;
      int sum = 0, x = -1;
      for (int j = 0; j < 26; ++j) {
        sum += v[j];
        if (v[j] > x) {
          ch = j + 'a';
          x = v[j];
        }
      }
      ans += sum - v[ch - 'a'];
    }
    cout << ans << endl;
  }
  return 0;
}
