#include <bits/stdc++.h>
using namespace std;
void printVec(vector<int> A);
int Prime[11] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31};
void solve() {
  int N, K;
  cin >> N >> K;
  vector<vector<int>> A(K, vector<int>(26, 0));
  cin.ignore(256, '\n');
  string S;
  getline(cin, S);
  for (int i = 0; i < N; ++i) {
    A[i % K][S[i] - 'a']++;
  }
  int Ans = 0, tmpMax;
  for (int i = 0; i < K / 2; ++i) {
    tmpMax = 0;
    for (int j = 0; j < 26; ++j) {
      if (A[i][j] + A[K - 1 - i][j] > tmpMax) {
        tmpMax = A[i][j] + A[K - 1 - i][j];
      }
      Ans += A[i][j] + A[K - 1 - i][j];
    }
    Ans -= tmpMax;
  }
  if (K % 2) {
    tmpMax = 0;
    for (int i = 0; i < 26; ++i) {
      Ans += A[K / 2][i];
      if (A[K / 2][i] > tmpMax) {
        tmpMax = A[K / 2][i];
      }
    }
    Ans -= tmpMax;
  }
  cout << Ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
void printVec(vector<int> A) {
  for (int i = 0; i < A.size(); ++i) cout << A[i] << " ";
  cout << endl;
}
