#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    string s;
    int n, k;
    cin >> n >> k;
    cin >> s;
    int sum = 0;
    vector<vector<int>> v;
    for (int i = 0; i < k; i++) {
      vector<int> f(26, 0);
      int cnt = 0;
      int j = i;
      int maxx = 0;
      while (j < n) {
        f[s[j] - 'a']++;
        j += k;
        cnt++;
      }
      v.push_back(f);
    }
    for (int i = 0; i < v.size() / 2; i++) {
      int cnt = 0;
      vector<int> f(26, 0);
      for (int j = 0; j < 26; j++) {
        f[j] = v[i][j] + v[v.size() - 1 - i][j];
        cnt += f[j];
      }
      int maxx = 0;
      for (int j = 0; j < 26; j++) {
        if (f[j] > maxx) {
          maxx = f[j];
        }
      }
      sum += (cnt - maxx);
    }
    if (v.size() % 2) {
      vector<int> f = v[v.size() / 2];
      int maxx = 0;
      int cnt = 0;
      for (int j = 0; j < 26; j++) {
        if (f[j] > maxx) {
          maxx = f[j];
        }
        cnt += f[j];
      }
      sum += (cnt - maxx);
    }
    cout << sum << "\n";
  }
}
