#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
const long long LL_INF = 0x3f3f3f3f3f3f3f3f;
template <typename T>
T gcd(T a, T b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long power(long long a, long long b, long long m = 1000000007) {
  long long answer = 1;
  while (b) {
    if (b & 1) answer = (answer * a) % m;
    b /= 2;
    a = (a * a) % m;
  }
  return answer;
}
using namespace std;
void ctrl() { cout << "Control" << endl; }
long long make_num(string p) {
  stringstream geek(p);
  long long x = 0;
  geek >> x;
  return x;
}
string make_str(long long x) {
  ostringstream str1;
  str1 << x;
  string geek = str1.str();
  return geek;
}
vector<long long> v[200000 + 5];
string p;
bool vis[200000 + 5];
vector<long long> comp;
void dfs(long long x) {
  vis[x] = 1;
  comp.push_back(x);
  for (auto it : v[x]) {
    if (!vis[it]) dfs(it);
  }
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  long long tc;
  cin >> tc;
  for (long long t = 1; t <= tc; t++) {
    long long n, k;
    cin >> n >> k;
    for (long long i = 1; i <= n; i++) v[i].clear(), vis[i] = 0;
    cin >> p;
    for (long long i = 1; i <= n; i++) {
      if (i + k <= n) {
        v[i].push_back(i + k);
        v[i + k].push_back(i);
      }
      v[i].push_back(n + 1 - i);
      v[n + 1 - i].push_back(i);
    }
    long long ans = 0;
    for (long long i = 1; i <= n; i++) {
      if (!vis[i]) {
        comp.clear();
        dfs(i);
        unordered_map<char, long long> mp;
        for (auto it : comp) {
          mp[p[it - 1]]++;
        }
        long long mx = 0;
        for (auto it : mp) {
          mx = max(mx, it.second);
        }
        ans += (comp.size() - mx);
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
