#include <bits/stdc++.h>
using namespace std;
vector<int> vec;
void dfs(int start, bool vis[], int n, int k) {
  int i, j;
  vis[start] = true;
  vec.push_back(start);
  for (j = start + k; j < n; j += k) {
    vec.push_back(j);
    vis[j] = true;
  }
  if (k != 1 && find(vec.begin(), vec.end(), n - 1 - start) == vec.end() &&
      n - 1 - start != start) {
    for (j = n - 1 - start; j >= 0; j -= k) {
      vec.push_back(j);
      vis[j] = true;
    }
  }
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, k, i, j, ans = 0;
    cin >> n >> k;
    string s;
    cin >> s;
    bool vis[n];
    for (i = 0; i < n; i++) vis[i] = false;
    int arr[26];
    vector<vector<int>> v;
    for (i = 0; i < n; i++) {
      if (vis[i] == false) {
        dfs(i, vis, n, k);
        v.push_back(vec);
        vec.clear();
      }
    }
    k = 0;
    for (i = 0; i < v.size(); i++) {
      for (j = 0; j < 26; j++) arr[j] = 0;
      for (j = 0; j < v[i].size(); j++) arr[s[v[i][j]] - 'a']++;
      for (j = 0; j < 26; j++) {
        if (arr[j] > arr[k]) k = j;
      }
      for (j = 0; j < 26; j++) {
        if (j != k) ans += arr[j];
      }
    }
    cout << ans << endl;
  }
}
