import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;

auto cnt = new int[](26);

void solve() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto K = s[1];
    auto S = readln.chomp;

    int ans = 0;

    foreach (i; 0..K/2+K%2) {
        cnt[] = 0;
        if (K % 2 && i == K / 2) {
            foreach (j; 0..N/K) {
                int a = j * K + i;
                cnt[S[a]-'a'] += 1;
                ans += 1;
            }
        } else {
            foreach (j; 0..N/K) {
                int a = j * K + i;
                int b = (j + 1) * K - i - 1;
                cnt[S[a]-'a'] += 1;
                cnt[S[b]-'a'] += 1;
                ans += 2;
            }
        }
        ans -= cnt.reduce!max;
    }

    ans.writeln;
}

void main() {
    auto T = readln.chomp.to!int;
    while (T--) {
        solve();
    }
}
