#include <bits/stdc++.h>
using namespace std;
template <class T>
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T>
std::string printVector(const std::vector<T> &data) {
  std::stringstream ss;
  std::ostream_iterator<T> out_it(ss, ", ");
  ss << "[";
  std::copy(data.begin(), data.end() - 1, out_it);
  ss << data.back() << "]";
  return ss.str();
}
template <typename T>
void print_array(const T &ary, int size) {
  for (int i = (0); i < (size); ++i) {
    cout << ary[i] << " ";
  }
  cout << endl;
}
const int MOD = 1e9 + 7;
const long long LINF = 1001002003004005006ll;
const int INF = 1001001001;
const double EPS = (1e-10);
long long cnt[112345][26] = {};
signed main(int argc, char const *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(12);
  int t;
  cin >> t;
  for (int _ = (0); _ < (t); ++_) {
    int n, k;
    cin >> n >> k;
    for (int i = (0); i < (k); ++i)
      for (int j = (0); j < (26); ++j) {
        cnt[i][j] = 0;
      }
    string s;
    cin >> s;
    for (int i = (0); i < (n); ++i) {
      cnt[i % k][s[i] - 'a']++;
    }
    long long ans = 0;
    for (int i = (0); i < ((k + 1) / 2); ++i) {
      ;
      if (i == k - 1 - i) {
        long long tmp = 0;
        for (int j = (0); j < (26); ++j) {
          chmax(tmp, cnt[i][j]);
        }
        ans += n / k - tmp;
      } else {
        long long tmp = 0;
        for (int j = (0); j < (26); ++j) {
          chmax(tmp, cnt[i][j] + cnt[k - 1 - i][j]);
        }
        ans += n / k * 2 - tmp;
      };
    }
    cout << ans << endl;
  }
}
