#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, 1, -1, -1, -1, 1, 1};
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
template <class T>
inline T biton(T n, T pos) {
  return n | ((T)1 << pos);
}
template <class T>
inline T bitoff(T n, T pos) {
  return n & ~((T)1 << pos);
}
template <class T>
inline T ison(T n, T pos) {
  return (bool)(n & ((T)1 << pos));
}
template <class T>
inline T gcd(T a, T b) {
  while (b) {
    a = a % b;
    swap(a, b);
  }
  return a;
}
template <class T>
inline T bigmod(T p, T e, T m) {
  T ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1) ret = (ret * p) % m;
    p = (p * p) % m;
  }
  return (T)ret;
};
void solve() {
  long long n, k;
  cin >> n >> k;
  string str;
  cin >> str;
  vector<vector<int>> items(k);
  for (long long i = 0; i < k; i++) {
    items[i].resize(26);
    for (long long c = 'a'; c <= 'z'; c++) {
      long long cnt = 0;
      for (long long j = i; j < n; j = j + k) {
        if (str[j] != c) {
          cnt++;
        }
      }
      items[i][c - 'a'] = cnt;
    }
  }
  long long ans = 0;
  for (long long i = 0; i < (k + 1) / 2; i++) {
    long long subanswer = 1e18;
    for (long long c = 'a'; c <= 'z'; c++) {
      if (i == k - i - 1) {
        long long a = items[i][c - 'a'];
        subanswer = min(a, subanswer);
      } else {
        long long a = items[i][c - 'a'] + items[k - i - 1][c - 'a'];
        subanswer = min(a, subanswer);
      }
    }
    ans += subanswer;
  }
  cout << ans << "\n";
  ;
}
int main() {
  ios_base::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(10);
  cout.tie(nullptr);
  cin.tie(nullptr);
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
