#include <bits/stdc++.h>
using namespace std;
int t, n, k;
string s;
map<char, int> m;
int main() {
  cin >> t;
  for (int q = 0; q < t; ++q) {
    cin >> n >> k;
    cin >> s;
    int res = 0;
    int bnd = k / 2;
    if (k % 2) bnd++;
    for (int i = 0; i < bnd; ++i) {
      m.clear();
      for (int j = 0; j < n / k; ++j) {
        if (m.find(s[j * k + i]) != m.end())
          m[s[j * k + i]]++;
        else
          m[s[j * k + i]] = 1;
        if (i != bnd - 1 || k % 2 == 0)
          if (m.find(s[(j + 1) * k - i - 1]) != m.end())
            m[s[(j + 1) * k - i - 1]]++;
          else
            m[s[(j + 1) * k - i - 1]] = 1;
      }
      int mmax = 0;
      for (map<char, int>::iterator it = m.begin(); it != m.end(); ++it) {
        if (it->second > mmax) mmax = it->second;
      }
      if (i != bnd - 1 || k % 2 == 0)
        res += 2 * (n / k) - mmax;
      else
        res += (n / k) - mmax;
    }
    cout << res << '\n';
  }
}
