#include <bits/stdc++.h>
using namespace std;
long long exp(long long a, long long n) {
  if (n == 0) return 1;
  if (n == 1) return a;
  if (n % 2) {
    long long s = exp(a, n - 1);
    return s * a;
  } else {
    long long s = exp(a, n / 2);
    return s * s;
  }
}
int max(int a, int b, int c) { return max(max(a, b), max(b, c)); }
int min(int a, int b, int c) { return min(min(a, b), min(b, c)); }
vector<int> primes;
void form_primes() {
  vector<int> v(1001, 1);
  for (int i = 2; i <= 1000; i++) {
    if (v[i]) {
      primes.push_back(i);
      for (int j = 2 * i; j <= 1000; j += i) {
        v[j] = 0;
      }
    }
  }
}
void solve() {
  int n, k;
  cin >> n >> k;
  string str;
  cin >> str;
  int cost = 0;
  map<int, int> idx;
  for (int i = 0; i < k; i++) {
    if (idx[i]) continue;
    map<char, int> m;
    for (int j = i; j < n; j += k) {
      if (!idx[j]) {
        m[str[j]] += 1;
        idx[j] += 1;
      }
    }
    for (int j = n - i - 1; j >= 0; j -= k) {
      if (!idx[j]) {
        m[str[j]] += 1;
        idx[j] += 1;
      }
    }
    int ma = 1;
    int ch = 0;
    for (auto a : m) {
      ch += a.second;
      ma = max(ma, a.second);
    }
    cost += (ch - ma);
  }
  cout << cost << "\n";
}
int main() {
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
