#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const int N = 1e6 + 7;
void my_dbg() { cout << endl; }
template <typename Arg, typename... Args>
void my_dbg(Arg A, Args... B) {
  cout << ' ' << A;
  my_dbg(B...);
}
int n, k, ans;
int ctr[N][26];
string s;
int perform(int x, int y) {
  int mx = 0, tot = 0;
  for (int i = 0; i < (int)26; i++) {
    int cur = ctr[x][i] + ctr[y][i];
    tot += cur;
    mx = max(mx, cur);
  }
  return (tot - mx);
}
int main() {
  int tt;
  scanf("%d", &tt);
  while (tt--) {
    scanf("%d", &n), scanf("%d", &k);
    string s;
    cin >> s;
    for (int i = 0; i < (int)k; i++)
      for (int j = 0; j < (int)26; j++) ctr[i][j] = 0;
    for (int i = 0; i < (int)n; i++) ctr[i % k][s[i] - 'a']++;
    ans = 0;
    for (int i = 0; i < (int)k; i++) ans += perform(i, k - 1 - i);
    printf("%d\n", (int)ans / 2);
  }
}
