#include <bits/stdc++.h>
#pragma GCC optimize("O2")
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline long long add(long long a, long long b, long long m) {
  return (((a % m + b % m) % m + m) % m);
}
inline long long mul(long long a, long long b, long long m) {
  return (((a % m * b % m) % m + m) % m);
}
const double EPS = 1e-6;
const long long MOD = 1e9 + 7;
template <typename T, typename U>
ostream &operator<<(ostream &out, const pair<T, U> &p) {
  out << '[' << p.first << ", " << p.second << ']';
  return out;
}
template <template <typename, typename...> class ContainerType,
          typename ValueType, typename... Args>
void print_container(const ContainerType<ValueType, Args...> &c) {
  cout << "{ ";
  for (const auto &v : c) cout << v << ' ';
  cout << '}' << '\n';
}
struct dsu {
  vector<vector<int> > info;
  vector<int> par, sz;
  dsu(int n, string &s) {
    par.assign(n + 1, -1);
    sz.assign(n + 1, 1);
    iota(par.begin(), par.end(), 0);
    info.assign(n + 1, vector<int>(26, 0));
    for (int i = 0; i < s.length(); i++) info[i + 1][s[i] - 'a']++;
  }
  int find(int x) {
    if (x != par[x]) return (par[x] = find(par[x]));
    return par[x];
  }
  bool unite(int x, int y) {
    int a = find(x);
    int b = find(y);
    if (a != b) {
      if (sz[a] < sz[b]) {
        par[a] = b;
        sz[b] += sz[a];
        for (int i = 0; i < 26; i++) info[b][i] += info[a][i];
      } else {
        par[b] = a;
        sz[a] += sz[b];
        for (int i = 0; i < 26; i++) info[a][i] += info[b][i];
      }
      return true;
    } else
      return false;
  }
};
void solve() {
  int n, k;
  cin >> n >> k;
  string str;
  cin >> str;
  dsu D(n, str);
  for (int i = 0; i + k < n; i++) D.unite(i + 1, i + k + 1);
  int l = 0, r = n - 1;
  while (l < r) {
    D.unite(l + 1, r + 1);
    l++;
    r--;
  }
  set<int> comp;
  for (int i = 0; i < n; i++) comp.insert(D.find(i + 1));
  int ans = 0;
  for (auto itr : comp) {
    int mx = 0, sz = D.sz[itr];
    for (auto x : D.info[itr]) mx = max(x, mx);
    ans += sz - mx;
  }
  cout << ans << '\n';
  return;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  clock_t start, stop;
  int t = 1;
  cin >> t;
  for (int tc = 1; tc < t + 1; tc++) solve();
  return 0;
}
