#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    string s;
    cin >> s;
    int A[26];
    int ans = 0;
    for (int i = 0; i < k / 2; i++) {
      memset(A, 0, sizeof(A));
      for (int j = 0; j < n / k; j++) {
        A[s[i + j * k] - 'a']++;
        A[s[(j + 1) * k - (i)-1] - 'a']++;
      }
      int ans_val = 0;
      int ans_key = 0;
      for (int j = 0; j < 26; j++) {
        if (A[j] > ans_val) {
          ans_val = A[j];
          ans_key = j;
        }
      }
      for (int j = 0; j < n / k; j++) {
        if ((s[i + j * k] - 'a') != ans_key) {
          ans++;
        }
        if ((s[(j + 1) * k - (i)-1] - 'a') != ans_key) {
          ans++;
        }
      }
    }
    if (k % 2 != 0) {
      int i = k / 2;
      memset(A, 0, sizeof(A));
      for (int j = 0; j < n / k; j++) {
        A[s[i + j * k] - 'a']++;
      }
      int ans_val = 0;
      int ans_key = 0;
      for (int j = 0; j < 26; j++) {
        if (A[j] > ans_val) {
          ans_val = A[j];
          ans_key = j;
        }
      }
      for (int j = 0; j < n / k; j++) {
        if ((s[i + j * k] - 'a') != ans_key) {
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
