#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t, a, b, c, k, i, n;
  cin >> t;
  for (long j = 1; j <= t; j++) {
    cin >> n >> k;
    char a[n];
    for (i = 1; i <= n; i++) {
      cin >> a[i];
    }
    int matr[k + 5]['z' - 'a' + 4];
    for (i = 1; i <= k; i++)
      for (char y = 'a'; y <= 'z'; y++) {
        matr[i][y] = 0;
      }
    for (long r = 1; r <= k; r++) {
      for (long z = 0; z < n / k; z++) {
        matr[r][a[z * k + r]]++;
      }
    }
    int maxi;
    long long result = 0;
    int cpu = 0;
    for (int i = 1; i <= ceil(double(k) / 2.0); i++) {
      maxi = 0;
      for (char y = 'a'; y <= 'z'; y++) {
        cpu = matr[i][y] + matr[k - i + 1][y];
        maxi = max(maxi, cpu);
      }
      if ((k % 2 != 0) && (i * 2 == k + 1)) {
        result += n / k - maxi / 2;
      } else
        result += 2 * n / k - maxi;
    }
    cout << result << endl;
  }
  return 0;
}
