#include <bits/stdc++.h>
using namespace std;
int mod = 1e9 + 7;
int mat[2 * 100001][26];
int n, k;
int count3 = 0;
void find_most(int x, int y) {
  int total = 0, mx = 0;
  for (int i = 0; i < 26; i++) {
    total += mat[x][i] + mat[y][i];
    mx = max(mx, mat[x][i] + mat[y][i]);
  }
  count3 += total - mx;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> k;
    string s;
    cin >> s;
    for (int i = 0; i < k; i++) {
      for (int j = 0; j < 26; j++) mat[i][j] = 0;
    }
    for (int i = 0; i < n; i++) {
      mat[i % k][s[i] - 'a']++;
    }
    for (int i = 0; i < k; i++) {
      find_most(i, k - i - 1);
    }
    cout << (count3) / 2;
    count3 = 0;
    cout << "\n";
  }
  return 0;
}
