#include <bits/stdc++.h>
using namespace std;
int book[200010][40];
int main() {
  ios::sync_with_stdio(false);
  int T;
  cin >> T;
  while (T--) {
    int n, k;
    string str;
    cin >> n >> k >> str;
    int ans = n;
    int len = str.length();
    for (int i = 0; i < k; i++) {
      for (int l = 0; l < 27; l++) book[i][l] = 0;
      for (int j = i; j < len; j = j + k) book[i][str[j] - 'a']++;
    }
    for (int i = 0; i < (k + 1) / 2; i++) {
      int maxn = 0;
      for (int j = 0; j < 26; j++) {
        if (k % 2 == 0 || (k % 2 == 1 && i != (k + 1) / 2 - 1))
          book[i][j] = book[i][j] + book[k - i - 1][j];
        maxn = max(maxn, book[i][j]);
      }
      ans = ans - maxn;
    }
    cout << ans << endl;
  }
  return 0;
}
