#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 2;
vector<pair<int, int> > par(N);
vector<int> freq;
long long icount = 0, n, k;
string s;
bool valid(int x) { return x >= 0 && x < n; }
pair<long long, long long> findpar(int x) {
  if (par[x].first != x)
    return findpar(par[x].first);
  else
    return {x, par[x].second};
}
void unite(int a, int b) {
  pair<long long, long long> x = findpar(b), y = findpar(a);
  if (x.first != y.first) {
    par[x.first].first = y.first;
    par[x.first].second += y.second;
  }
  return;
}
void combine(int node) {
  if (node < 0 || node >= n) return;
  freq[s[node] - 'a']++;
  if (valid(node + k) && findpar(node).first != findpar(node + k).first) {
    unite(node, node + k);
    combine(node + k);
  }
  if (valid(n - node - 1) &&
      findpar(node).first != findpar(n - node - 1).first) {
    unite(node, n - node - 1);
    combine(n - node - 1);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    icount = 0;
    cin >> n >> k;
    for (int i = 0; i <= n; i++) {
      par[i].first = i;
      par[i].second = 1;
    }
    cin >> s;
    for (int i = 0; i < n; i++) {
      freq.clear();
      freq.resize(26);
      combine(i);
      icount += par[i].second - *max_element((freq).begin(), (freq).end());
    }
    cout << icount << endl;
  }
  return 0;
}
