#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 = s.length() / k, word = 0;
    for (int i = 0; i < k / 2; i++) {
      int cmax = 0;
      map<char, int> m;
      for (int j = 0; j < a; j++) {
        m[s[j * k + i]]++;
        cmax = max(cmax, m[s[j * k + i]]);
        m[s[j * k + k - 1 - i]]++;
        cmax = max(cmax, m[s[j * k + k - 1 - i]]);
      }
      word += cmax;
    }
    if (k % 2 == 1) {
      int cmax = 0;
      map<char, int> m;
      for (int j = 0; j < a; j++) {
        m[s[j * k + k / 2]]++;
        cmax = max(cmax, m[s[j * k + k / 2]]);
      }
      word += cmax;
    }
    cout << s.length() - word << endl;
  }
  return 0;
}
