#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
bool mod(long double a, long double b) { return a / b - floor(a / b); }
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long i, j, first, t;
  long long N = 1001;
  long long sieve[1001];
  long long col = 0;
  memset(sieve, -1, sizeof(sieve));
  for (i = 2; i < 1001; i++) {
    if (sieve[i] == -1) {
      col++;
      for (j = i; j < N; j += i) {
        if (sieve[j] == -1) {
          sieve[j] = col;
        }
      }
    }
  }
  cin >> t;
  while (t--) {
    long long n, k, m;
    cin >> n >> k;
    string s;
    cin >> s;
    m = (k + 1) / 2;
    set<char> st[m];
    map<char, long long> mp[m];
    for (i = 0; i < n / k; i++) {
      for (j = 0; j < k / 2; j++) {
        st[j].insert(s[i * k + j]);
        mp[j][s[i * k + j]]++;
        st[j].insert(s[(i + 1) * k - (j + 1)]);
        mp[j][s[(i + 1) * k - (j + 1)]]++;
      }
      if (k % 2 == 1) {
        st[j].insert(s[i * k + j]);
        mp[j][s[i * k + j]]++;
      }
    }
    long long sum = 0;
    for (i = 0; i < k / 2; i++) {
      first = 0;
      for (auto it = (st[i]).begin(); it != (st[i]).end(); it++) {
        first = max(first, mp[i][*it]);
      }
      sum += (2 * (n / k) - first);
    }
    if (k % 2 == 1) {
      first = 0;
      for (auto it = (st[i]).begin(); it != (st[i]).end(); it++) {
        first = max(first, mp[i][*it]);
      }
      sum += ((n / k) - first);
    }
    cout << sum << '\n';
  }
  return 0;
}
