#include <bits/stdc++.h>
#pragma GCC optimize "trapv"
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    vector<vector<int>> v(k, vector<int>(26));
    string s;
    cin >> s;
    for (int i = 0; i < k; i++) {
      for (int j = i; j < n; j += k) {
        v[i][s[j] - 'a']++;
      }
    }
    int count = 0;
    for (int i = 0; i < k / 2; i++) {
      int maxx = 0;
      for (int j = 0; j < 26; j++) {
        maxx = max(maxx, v[i][j] + v[k - 1 - i][j]);
      }
      count += 2 * (n / k) - maxx;
    }
    if (k % 2) {
      int maxx = 0;
      for (int j = 0; j < 26; j++) {
        maxx = max(maxx, v[k / 2][j]);
      }
      count += n / k - maxx;
    }
    cout << count << endl;
  }
  return 0;
}
