#include <bits/stdc++.h>
using namespace std;
int t;
int n, k;
string str;
int v[26];
int main() {
  cin >> t;
  while (t--) {
    cin >> n >> k;
    int p = n / k;
    cin >> str;
    int ans = 0;
    for (int i = 0; i < (k + 1) / 2; i++) {
      memset(v, 0, sizeof(v));
      int ma = 0;
      for (int j = 0; j < p; j++) {
        int k1 = j * k + i;
        int k2 = j * k + k - i - 1;
        v[str[k1] - 'a']++;
        v[str[k2] - 'a']++;
        if (v[str[k1] - 'a'] > ma) ma = v[str[k1] - 'a'];
        if (v[str[k2] - 'a'] > ma) ma = v[str[k2] - 'a'];
      }
      if (k % 2 && i == (k + 1) / 2 - 1)
        ans += p - ma / 2;
      else
        ans += 2 * p - ma;
    }
    cout << ans << endl;
  }
  return 0;
}
