#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "[";
  typename vector<T>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin()) os << ", ";
    os << *it;
  }
  return os << "]";
}
template <typename T>
ostream &operator<<(ostream &os, const set<T> &v) {
  os << "{";
  typename set<T>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin()) os << ", ";
    os << *it;
  }
  return os << "}";
}
template <typename A, typename B>
ostream &operator<<(ostream &os, const map<A, B> &v) {
  os << "[";
  typename map<A, B>::const_iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (it != v.begin()) os << ", ";
    os << it->first << " -> " << it->second;
  }
  return os << "]";
}
template <typename T>
T gcd(T a, T b) {
  if (a == 0 or b == 0) return a + b;
  return gcd(b, a % b);
}
template <typename T>
T lcm(T a, T b) {
  if (a == 0 or b == 0) return 0;
  return a / gcd(a, b) * b;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const long double eps = LDBL_EPSILON;
const long long inf = 1e15;
const long long modn = 1e9 + 7;
const long long maxn = 2e5 + 3;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    long long out = 0;
    for (long long i = 0; i < k / 2; i++) {
      vector<long long> ind;
      for (long long j = i; j < n; j += k) {
        ind.push_back(j);
      }
      for (long long j = k - 1 - i; j < n; j += k) {
        ind.push_back(j);
      }
      long long ans = inf;
      for (long long ch = 0; ch < 26; ch++) {
        long long cur = 0;
        for (long long id : ind) {
          if (s[id] - 'a' != ch) {
            cur++;
          }
        }
        ans = min(ans, cur);
      }
      out += ans;
    }
    if (k & 1) {
      long long ans = inf;
      for (long long ch = 0; ch < 26; ch++) {
        long long cur = 0;
        for (long long id = k / 2; id < n; id += k) {
          if (s[id] - 'a' != ch) {
            cur++;
          }
        }
        ans = min(ans, cur);
      }
      out += ans;
    }
    cout << out << "\n";
  }
  return 0;
}
