#include <bits/stdc++.h>
using namespace std;
long long int max(long long int a, long long int b) {
  if (a > b) {
    return a;
  }
  return b;
}
long long int min(long long int a, long long int b) {
  if (a < b) {
    return a;
  }
  return b;
}
long long int power(long long int x, long long int n, long long int p) {
  long long int res = 1;
  while (n > 0) {
    if (n % 2 == 1) {
      res = (res * x) % p;
    }
    x = (x * x) % p;
    n = n / 2;
  }
  return res % p;
}
long long int factorialNumInverse[1000001];
long long int naturalNumInverse[1000001];
long long int fact[1000001];
void InverseofNumber(long long int n, long long int p) {
  naturalNumInverse[0] = naturalNumInverse[1] = 1;
  for (long long int i = 2; i <= n; i++)
    naturalNumInverse[i] = naturalNumInverse[p % i] * (p - p / i) % p;
}
void InverseofFactorial(long long int n, long long int p) {
  factorialNumInverse[0] = factorialNumInverse[1] = 1;
  for (long long int i = 2; i <= n; i++)
    factorialNumInverse[i] =
        (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p;
}
void factorial(long long int n, long long int p) {
  fact[0] = 1;
  for (long long int i = 1; i <= n; i++) {
    fact[i] = (fact[i - 1] * i) % p;
  }
}
long long int nCrModp(long long int n, long long int r, long long int p) {
  long long int ans =
      ((fact[n] * factorialNumInverse[r]) % p * factorialNumInverse[n - r]) % p;
  return ans;
}
long long int n, k;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int tc;
  cin >> tc;
  factorial(100000, 1000000007);
  InverseofNumber(100000, 1000000007);
  InverseofFactorial(100000, 1000000007);
  while (tc--) {
    cin >> n >> k;
    vector<char> v[k];
    string str;
    cin >> str;
    for (int i = 0; i < str.length(); i++) {
      v[i % k].push_back(str[i]);
    }
    long long int ans = 0;
    for (int i = 0; i < k / 2; i++) {
      long long int arr[26];
      memset(arr, 0, sizeof(arr));
      for (int j = 0; j < v[i].size(); j++) {
        arr[v[i][j] - 'a']++;
      }
      for (int j = 0; j < v[k - 1 - i].size(); j++) {
        arr[v[k - 1 - i][j] - 'a']++;
      }
      ans += 2 * (n / k) - *max_element(arr, arr + 26);
    }
    if (k % 2 == 1) {
      long long int arr[26];
      memset(arr, 0, sizeof(arr));
      for (int i = 0; i < v[k / 2].size(); i++) {
        arr[v[k / 2][i] - 'a']++;
      }
      ans += (n / k) - *max_element(arr, arr + 26);
    }
    cout << ans << "\n";
  }
  return 0;
}
