#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9, MAXN = 2e5 + 999;
int n, k, cnt[MAXN][26];
string s;
int diff(int u, int v) {
  int ret = 0, mx = 0;
  for (int j = 0; j < 26; j++) {
    ret += cnt[u][j] + cnt[v][j];
    mx = max(mx, cnt[u][j] + cnt[v][j]);
  }
  return ret - mx;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int TC;
  cin >> TC;
  while (TC--) {
    cin >> n >> k >> s;
    for (int i = 0; i < k; ++i) {
      for (int j = 0; j < 26; ++j) {
        cnt[i][j] = 0;
      }
    }
    for (int i = 0; i < n; i++) {
      cnt[i % k][s[i] - 'a']++;
    }
    int res = 0;
    for (int i = 0; i < k; i++) res += diff(i, k - 1 - i);
    cout << res / 2 << endl;
  }
  return 0;
}
