#include <bits/stdc++.h>
using namespace std;
void Fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cerr.tie(NULL);
}
inline int D() {
  int t;
  scanf("%d", &t);
  return t;
}
inline long long llD() {
  long long t;
  scanf("%lld", &t);
  return t;
}
long long MOD = 1e9 + 7;
const int N = 1e2 + 5;
int fr[27];
long long OO = 1e10;
long long n, m, k, cnt, SU, idx, len, MN, MX, T, ans = 1e6;
long long l, r, mid;
long long a, b, c, d;
vector<int> vec;
string str, s;
int main() {
  cin >> T;
  while (T--) {
    cin >> n >> k;
    cin >> str;
    cnt = 0;
    for (int i = 0, j = k - 1; i <= j; ++i, --j) {
      memset(fr, 0, sizeof fr);
      for (int a = i, b = j; a < n && b < n; a += k, b += k)
        ++fr[str[a] - 'a'], ++fr[str[b] - 'a'];
      if (i == j)
        for (int a = i; a < n; a += k) --fr[str[a] - 'a'];
      int MX = -1e9, len = 0;
      for (int a = 0; a < 27; ++a) len += fr[a], MX = max(MX, fr[a]);
      cnt += (len - MX);
    }
    cout << cnt << "\n";
  }
  return 0;
}
