#include <bits/stdc++.h>
using namespace std;
const long long M = 1e9 + 7;
const int N = 1e6 + 7;
bool visited[N];
int cnt[N][26];
string s;
int len, k;
int total;
void dfs(int idx, int curr) {
  visited[curr] = true;
  total++;
  cnt[idx][s[curr] - 'a']++;
  if (!visited[len - curr - 1]) dfs(idx, len - curr - 1);
  if (!visited[curr + k] && curr + k < len) dfs(idx, curr + k);
}
int main() {
  int tt;
  scanf("%d", &tt);
  while (tt--) {
    scanf("%d", &len), scanf("%d", &k);
    for (long long i = 0; i < (int)len + 1; i++) {
      visited[i] = false;
      for (long long j = 0; j < (int)26; j++) cnt[i][j] = 0;
    }
    cin >> s;
    int ans = 0;
    for (long long i = 0; i < (int)len; i++) {
      if (!visited[i]) {
        total = 0;
        dfs(i, i);
        int maxi = 0;
        for (long long j = 0; j < (int)26; j++) maxi = max(maxi, cnt[i][j]);
        ans += (total - maxi);
      }
    }
    printf("%d\n", (int)ans);
  }
  return 0;
}
