#include <bits/stdc++.h>
using namespace std;
void fun() {}
int md = 1e9 + 7;
int __gcd(int a, int b) {
  if (b == 0) return a;
  return __gcd(b, a % b);
}
int poww(int a, int b, int md) {
  if (b < 0) return 0;
  if (a == 0) return 0;
  int res = 1;
  while (b) {
    if (b & 1) {
      res = (1ll * res * a) % md;
    }
    a = (1ll * a * a) % md;
    b >>= 1;
  }
  return res;
}
int poww(int a, int b) {
  if (b < 0) return 0;
  if (a == 0) return 0;
  int res = 1;
  while (b) {
    if (b & 1) {
      res = (1ll * res * a);
    }
    a = (1ll * a * a);
    b >>= 1;
  }
  return res;
}
void ainp(int arr[], int n) {
  for (int i = 1; i <= n; i++) cin >> arr[i];
}
void disp(int arr[], int n) {
  for (int i = 1; i <= n; i++) cout << arr[i] << " ";
  cout << "\n";
}
void dispv(vector<int> &v) {
  for (int i = 0; i < v.size(); i++) cout << v[i] << " ";
  cout << "\n";
}
int divide(int a, int b, int md) {
  int rr = a * (poww(b, md - 2, md));
  rr %= md;
  return rr;
}
const int size = 3e5 + 10;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  fun();
  int t;
  cin >> t;
  while (t--) {
    int n, k, ans = 0;
    cin >> n >> k;
    string s;
    cin >> s;
    int hashe[k][26];
    memset(hashe, 0, sizeof(hashe));
    for (int i = 0; i < n; i++) {
      hashe[i % k][s[i] - 'a']++;
    }
    for (int i = 0; i < ceil(1.0 * k / 2); i++) {
      int total = 0;
      int maxfreq = 0;
      for (int j = 0; j < 26; j++) {
        if (i == k - 1 - i) {
          total += hashe[i][j];
          maxfreq = max(maxfreq, hashe[i][j]);
        } else {
          total += hashe[i][j] + hashe[k - 1 - i][j];
          maxfreq = max(maxfreq, hashe[i][j] + hashe[k - 1 - i][j]);
        }
      }
      ans += total - maxfreq;
    }
    cout << ans << "\n";
  }
  return 0;
}
