#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;
map<char, long long> freq;
map<long long, long long> vis;
long long maxv = -1;
long long sz = 0;
char s[N];
void dfs(vector<long long> gr[], long long ind) {
  vis[ind] = true;
  sz++;
  freq[s[ind]]++;
  maxv = max(maxv, freq[s[ind]]);
  for (auto it : gr[ind])
    if (!vis[it]) dfs(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;
    vis.clear();
    vector<long long> gr[n + 1];
    long long moves = 0;
    for (long long i = 1; i <= n; i++) cin >> s[i];
    for (long long i = 1; i <= n / 2; i++) {
      gr[i].push_back(i + k);
      gr[i + k].push_back(i);
      gr[i].push_back(n + 1 - i);
      gr[n + 1 - i].push_back(i);
    }
    for (long long i = 1; i <= n; i++) {
      if (vis[i]) continue;
      dfs(gr, i);
      moves += (sz - maxv);
      freq.clear();
      maxv = -1;
      sz = 0;
    }
    cout << moves << "\n";
  }
}
