#include <bits/stdc++.h>
using namespace std;
int find(int x, vector<int> &parent) {
  if (x == parent[x]) return x;
  return parent[x] = find(parent[x], parent);
}
void solve() {
  int n, k;
  cin >> n >> k;
  string s;
  vector<vector<int>> arr(n, vector<int>(26, 0));
  vector<int> parent(n, 0);
  vector<int> rank(n, 0);
  for (int i = 0; i < n; i++) parent[i] = i;
  cin >> s;
  for (int i = 0; i < n; i++) {
    int num = s[i] - 'a';
    arr[i][num] = 1;
  }
  for (int i = 0; i < n / 2; i++) {
    int p1 = find(i, parent);
    int p2 = find(n - 1 - i, parent);
    if (p1 != p2) {
      if (rank[p1] > rank[p2]) {
        parent[p2] = p1;
        for (int i = 0; i < 26; i++) {
          arr[p1][i] += arr[p2][i];
        }
      } else if (rank[p2] > rank[p1]) {
        parent[p1] = p2;
        for (int i = 0; i < 26; i++) arr[p2][i] += arr[p1][i];
      } else {
        parent[p1] = p2;
        for (int i = 0; i < 26; i++) arr[p2][i] += arr[p1][i];
        rank[p2]++;
      }
    }
  }
  for (int i = k; i < n; i++) {
    int p1 = find(i, parent);
    int p2 = find(i - k, parent);
    if (p1 != p2) {
      if (rank[p1] > rank[p2]) {
        parent[p2] = p1;
        for (int i = 0; i < 26; i++) {
          arr[p1][i] += arr[p2][i];
        }
      } else if (rank[p2] > rank[p1]) {
        parent[p1] = p2;
        for (int i = 0; i < 26; i++) arr[p2][i] += arr[p1][i];
      } else {
        parent[p1] = p2;
        for (int i = 0; i < 26; i++) arr[p2][i] += arr[p1][i];
        rank[p2]++;
      }
    }
  }
  vector<int> visited(n, 0);
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (i == parent[i]) {
      int max = -1, cnt = 0;
      for (int j = 0; j < 26; j++) {
        if (arr[i][j] > max) max = arr[i][j];
        cnt += arr[i][j];
      }
      ans += cnt - max;
    }
  }
  cout << ans << endl;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
