#include <bits/stdc++.h>
using namespace std;
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;
    int ans = n;
    for (int i = 0; i <= k - 1 - i; i++) {
      vector<int> a(26, 0);
      for (int j = i; j < n; j += k) {
        ++a[s[j] - 'a'];
      }
      if (i < k - 1 - i) {
        for (int j = k - 1 - i; j < n; j += k) {
          ++a[s[j] - 'a'];
        }
      }
      ans -= *max_element(a.begin(), a.end());
    }
    cout << ans << endl;
  }
}
