#include <bits/stdc++.h>
using namespace std;
bool areTests = 1;
void foobar() {
  long long int n, k, i, j;
  string str;
  cin >> n >> k;
  cin >> str;
  long long int p = n / k;
  vector<string> mat;
  for (i = 0; i < n; i += k) {
    string sub = str.substr(i, k);
    mat.push_back(sub);
  }
  long long int rows = p;
  long long int cols = k;
  map<char, long long int> M[cols];
  for (j = 0; j < cols; ++j) {
    for (i = 0; i < rows; ++i) {
      M[j][mat[i][j]]++;
    }
  }
  long long int ans = INT_MAX;
  long long int kk = 0;
  long long int ii = 0, jj = cols - 1;
  while (ii <= jj) {
    long long int ans = INT_MAX;
    for (char ch = 'a'; ch <= 'z'; ++ch) {
      long long int pii = rows - M[ii][ch];
      long long int pjj = rows - M[jj][ch];
      if (ii == jj) {
        ans = min(ans, min(pii, pjj));
      } else {
        ans = min(ans, pii + pjj);
      }
    }
    ii++;
    jj--;
    kk += ans;
  }
  cout << kk << endl;
}
int main() {
  long long int test = 1;
  if (areTests) {
    cin >> test;
  }
  while (test--) foobar();
}
