#include <bits/stdc++.h>
using namespace std;
inline int read() {
  char c = getchar();
  int x = 0, f = 1;
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * f;
}
const int MOD = 19260817;
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const long long INFLL = 1e18;
template <typename T>
T gcd(T a, T b) {
  return (a % b == 0) ? b : gcd(b, a % b);
}
template <typename T>
inline T lowbit(T x) {
  return x & -x;
}
template <typename T>
inline T _max(T a, T b) {
  return a > b ? a : b;
}
template <typename T>
inline T _min(T a, T b) {
  return a < b ? a : b;
}
const int M = 1e4 + 5;
const int N = 2e5 + 5;
long long kuai(long long b, long long p) {
  long long now = b, ans = 0;
  while (p) {
    if (p % 2 == 0)
      now = now * now % MOD;
    else {
      if (ans == 0) ans = 1;
      ans = ans * now % MOD;
      now = now * now % MOD;
    }
    p /= 2;
  }
  return ans % MOD;
}
long long inv(long long x) { return kuai(x, MOD - 2); }
char s[N];
int cnt[30];
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int n, k;
    scanf("%d%d", &n, &k);
    scanf("%s", s + 1);
    int ans = 0;
    for (int i = 1; i <= (k / 2); i++) {
      memset(cnt, 0, sizeof cnt);
      int ii = 1;
      int tot = 0;
      while (ii <= n) {
        cnt[s[ii + i - 1] - 'a']++;
        cnt[s[ii + k - i] - 'a']++;
        tot += 2;
        ii += k;
      }
      int maxx = 0;
      for (int j = 0; j < 26; j++) maxx = max(maxx, cnt[j]);
      ans += (tot - maxx);
    }
    if (k % 2) {
      memset(cnt, 0, sizeof cnt);
      int ii = 1;
      int tot = 0;
      while (ii <= n) {
        cnt[s[ii + k / 2] - 'a']++;
        tot++;
        ii += k;
      }
      int maxx = 0;
      for (int j = 0; j < 26; j++) maxx = max(maxx, cnt[j]);
      ans += (tot - maxx);
    }
    printf("%d\n", ans);
  }
  return 0;
}
