#include <bits/stdc++.h>
int main(int argc, char **argv) {
  std::ios::sync_with_stdio(false), std::cin.tie(nullptr);
  auto solve = [](std::string &s, int n, int k) {
    std::vector<std::vector<int>> graph(n);
    std::vector<int> cnt(26, 0), used(n, 0);
    auto connect = [&](int i, int j) {
      graph[i].push_back(j);
      graph[j].push_back(i);
    };
    for (int i = 0; i < n / 2; i++) connect(i, n - 1 - i);
    for (int i = 0; i < k; i++)
      for (int j = k + i; j < n; j += k) connect(i, j);
    auto bfs = [&](int v) {
      std::queue<int> q;
      q.push(v);
      while (!q.empty()) {
        auto u = q.front();
        q.pop();
        if (used[u]) continue;
        used[u] = true;
        ++cnt[s[u] - 'a'];
        for (auto t : graph[u]) q.push(t);
      }
    };
    int ans = 0;
    for (int i = 0; i < n; i++) {
      if (used[i]) continue;
      std::fill(cnt.begin(), cnt.end(), 0);
      bfs(i);
      int cmax = 0, ccnt = 0;
      for (auto r : cnt) cmax = std::max(cmax, r), ccnt += r;
      ans += ccnt - cmax;
    }
    return ans;
  };
  int t;
  std::cin >> t;
  while (t--) {
    std::string s;
    int n, k;
    std::cin >> n >> k >> s;
    std::cout << solve(s, n, k) << '\n';
  }
  return 0;
}
