#include <bits/stdc++.h>
using namespace std;
const long long N = 1e+6 + 5;
long long big = 1e+18 + 3;
long long mod = 1e+9 + 7;
long long freq[26] = {0}, vis[N];
long long maxv = -1;
long long sz = 0;
void dfs(string &s, vector<vector<long long>> &gr, long long ind) {
  vis[ind] = 1;
  sz++;
  freq[s[ind] - (long long)'a']++;
  for (auto it : gr[ind])
    if (!vis[it]) dfs(s, gr, it);
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n;
    long long k;
    cin >> n >> k;
    string s;
    for (long long i = 0; i <= n; i++) vis[i] = 0;
    vector<vector<long long>> gr(n + 1);
    long long moves = 0;
    cin >> s;
    for (long long i = 0; i <= n - 1; i++) {
      if (i + k < n) gr[i].push_back(i + k), gr[i + k].push_back(i);
      if (i - k >= 0) gr[i].push_back(i - k), gr[i - k].push_back(i);
      gr[i].push_back(n - i - 1);
      gr[n - i - 1].push_back(i);
    }
    for (long long i = 0; i <= n - 1; i++) {
      if (vis[i]) continue;
      dfs(s, gr, i);
      for (long long x = 0; x <= 25; x++) maxv = max(maxv, freq[x]);
      moves += (sz - maxv);
      for (long long x = 0; x <= 25; x++) freq[x] = 0;
      maxv = -1;
      sz = 0;
    }
    cout << moves << "\n";
  }
}
