#include <bits/stdc++.h>
using namespace std;
const double PI = 3.14159265358979323846;
const double eps = (1e-10);
int isGreater(long double x, long double y) {
  if (abs(x - y) < eps) return 0;
  if (x > y) return 1;
  return -1;
}
void solve() {
  int n, k;
  string s;
  cin >> n >> k >> s;
  vector<vector<int> > freq((k + 1) / 2, vector<int>(26, 0));
  for (int i = 0; i < n; i += k) {
    for (int j = i, id = 0; id < k / 2; ++j, ++id) {
      char c = s[j] - 'a';
      freq[id][c]++;
      c = s[k - id - 1 + i] - 'a';
      freq[id][c]++;
    }
    if (k & 1) {
      char c = s[i + k / 2] - 'a';
      freq.back()[c]++;
    }
  }
  int ans = 0;
  for (vector<int> &f : freq) {
    int best = 0, sum = 0;
    for (int x : f) {
      best = max(best, x);
      sum += x;
    }
    ans += sum - best;
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int tc;
  cin >> tc;
  while (tc--) solve();
  return 0;
}
