#include <bits/stdc++.h>
using namespace std;
int n, k;
string second;
int solvePos(int l, int r) {
  int fr[26];
  for (int i = 0; i < 26; i++) fr[i] = 0;
  if (l < r) {
    for (int i = l; i < n; i += k) {
      fr[second[i] - 'a']++;
    }
    for (int i = r; i < n; i += k) {
      fr[second[i] - 'a']++;
    }
  } else {
    for (int i = r; i < n; i += k) {
      fr[second[i] - 'a']++;
    }
  }
  int maximo = 0, soma = 0;
  for (int i = 0; i < 26; i++) {
    maximo = max(maximo, fr[i]);
    soma += fr[i];
  }
  return soma - maximo;
}
int solve() {
  int ans = 0;
  for (int i = 0; i < (k + 1) / 2; i++) {
    ans += solvePos(i, k - 1 - i);
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    cin >> second;
    cout << solve() << endl;
  }
}
