#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int k;
    cin >> k;
    string s;
    cin >> s;
    int mx;
    unordered_map<char, int> freq;
    int temp = (n / k - 1);
    int res = 0;
    for (int i = 0; i < k / 2; i++) {
      for (int l = 0; l < 26; l++) freq['a' + l] = 0;
      mx = 0;
      for (int m = 0; m <= temp; m++) {
        freq[s[i + (k * m)]]++;
        if (freq[s[i + (k * m)]] > mx) mx = freq[s[i + (k * m)]];
      }
      int j = k - i - 1;
      for (int m = 0; m <= temp; m++) {
        freq[s[j + (k * m)]]++;
        if (freq[s[j + (k * m)]] > mx) mx = freq[s[j + (k * m)]];
      }
      res += 2 * temp + 2 - mx;
    }
    if (k & 1) {
      for (int l = 0; l < 26; l++) freq['a' + l] = 0;
      mx = 0;
      int i = k / 2;
      for (int m = 0; m <= temp; m++) {
        freq[s[i + (k * m)]]++;
        if (freq[s[i + (k * m)]] > mx) mx = freq[s[i + (k * m)]];
      }
      res += (temp + 1) - mx;
    }
    cout << res << endl;
  }
}
