#include <bits/stdc++.h>
using namespace std;
long long powermod(long long x, long long n, long long M) {
  long long result = 1;
  while (n > 0) {
    if (n % 2 == 1) result = (result * x) % M;
    x = (x * x) % M;
    n = n / 2;
  }
  return result;
}
vector<long long> readll() {
  cin >> ws;
  vector<long long> v;
  string input;
  getline(cin, input);
  cout << input;
  istringstream is(input);
  long long num;
  while (is >> num) v.push_back(num);
  return v;
}
long long modinv(long long n, long long p) { return powermod(n, p - 2, p); }
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long fact[1000005], invf[1000005];
void facmod(long long n, long long M) {
  long long i;
  fact[0] = fact[1] = 1;
  for (i = 2; i <= n; ++i) {
    fact[i] = ((fact[i - 1]) % M * (i % M)) % M;
  }
  invf[n] = modinv(fact[n], M);
  for (i = n - 1; i >= 0; --i) {
    invf[i] = invf[i + 1] * (i + 1);
    invf[i] %= M;
  }
}
void solve() {
  long long i, j, n, k;
  string s;
  cin >> n >> k;
  cin >> s;
  long long a[k][26];
  for (i = 0; i < k; ++i) {
    for (j = 0; j < 26; ++j) {
      a[i][j] = 0;
    }
  }
  for (i = 0; i < n; ++i) {
    long long gr = i / k;
    long long pos = i - gr * k;
    a[min(pos, k - 1 - pos)][s[i] - 'a']++;
  }
  long long ngr = n / k;
  long long ans = 0;
  for (i = 0; i < (k + 1) / 2; ++i) {
    long long mx = 0, x = 0;
    for (j = 0; j < 26; ++j) {
      x += a[i][j];
      mx = max(mx, a[i][j]);
    }
    ans += x - mx;
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  long long t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
