#include<bits/stdc++.h>
using namespace std;

namespace HABIT{
    #define f(a,b,c,d) for(register int a=b,c=d;a<=c;a++)
    #define g(a,b,c,d) for(register int a=b,c=d;a>=c;a--)
    #define LOCAL
    typedef int i32;
    typedef unsigned int u32;
    typedef long long int i64;
    typedef unsigned long long int u64;
    typedef pair<i32,i32> pii;
    const i32 MAXN=1e6+10;
    typedef i32 ar[MAXN];

    template<typename T> inline T Max(T a) { return a; }
    template<typename T,typename... Args> inline T Max(T a,Args... args){
        T b=Max(args...);
        return (a>b)?a:b;
    }
    template<typename T> inline T Min(T a) { return a; }
    template<typename T,typename... Args> inline T Min(T a,Args... args){
        T b=Min(args...);
        return (a<b)?a:b;
    }

    #ifdef LOCAL
        inline char gc() { return getchar(); }
    #else
        inline char gc() {
            static char s[1<<20|1]={0},*p1=s,*p2=s;
            return (p1==p2)&&(p2=(p1=s)+fread(s,1,1<<20,stdin),p1==p2)?EOF:*(p1++);
        }
    #endif
    inline i64 read(){
        register i64 ans=0;register char c=gc();register bool neg=0;
        while(c<48||c>57) neg^=!(c^'-'),c=gc();
        while(c>=48&&c<=57) ans=(ans<<3)+(ans<<1)+(c^48),c=gc();
        return neg?-ans:ans;
    }

    char Output_Ans[1<<20|1],*Output_Cur=Output_Ans;
    inline void output() { Output_Cur-=fwrite(Output_Ans,1,Output_Cur-Output_Ans,stdout); }
    inline void print(char c){
        if(Output_Cur-Output_Ans+1>>20) output();
        *(Output_Cur++)=c;
    }
    inline void print(char *s) { while(*s) print( *(s++) ); }
    inline void print(u64 x){
        if(!x) { print('0'); return ; }
        char buf[30]={0},*p=buf+28;
        while(x) *(p--)=x%10+48,x/=10;
        print(p+1);
    }
    inline void print(u32 x) { print( (u64)x ); }
    inline void print(i64 x){
        if(x<0) print('-'),x=-x;
        print( (u64)x );
    }
    inline void print(i32 x){
        if(x<0) print('-'),x=-x;
        print( (u64)x );
    }
}
using namespace HABIT;

i32 N,K,Ans;
char TurnOn[MAXN];

inline i64 ans(){
    N=read();
    K=read();
    Ans=0;
    scanf("%s",TurnOn);
    char *Head=TurnOn,*Tail=TurnOn+N-1;
    while(*Head=='0') Head++;
    while(*Tail=='0') Tail--;
    if(Head>=Tail) return Ans;
    i32 Cur=0;
    while(Head<=Tail){
        if(Cur%K==0) Ans+=(*Head!='1');
        else Ans+=(*Head!='0');
        Cur++;
        Head++;
    }
    return Ans;
}

int main(){
    f(i,1,I,read()) print( ans() ),print('\n');
    output();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

namespace HABIT{
    #define f(a,b,c,d) for(register int a=b,c=d;a<=c;a++)
    #define g(a,b,c,d) for(register int a=b,c=d;a>=c;a--)
    #define LOCAL
    typedef int i32;
    typedef unsigned int u32;
    typedef long long int i64;
    typedef unsigned long long int u64;
    typedef pair<i32,i32> pii;
    const i32 MAXN=1e6+10;
    typedef i32 ar[MAXN];

    template<typename T> inline T Max(T a) { return a; }
    template<typename T,typename... Args> inline T Max(T a,Args... args){
        T b=Max(args...);
        return (a>b)?a:b;
    }
    template<typename T> inline T Min(T a) { return a; }
    template<typename T,typename... Args> inline T Min(T a,Args... args){
        T b=Min(args...);
        return (a<b)?a:b;
    }

    #ifdef LOCAL
        inline char gc() { return getchar(); }
    #else
        inline char gc() {
            static char s[1<<20|1]={0},*p1=s,*p2=s;
            return (p1==p2)&&(p2=(p1=s)+fread(s,1,1<<20,stdin),p1==p2)?EOF:*(p1++);
        }
    #endif
    inline i64 read(){
        register i64 ans=0;register char c=gc();register bool neg=0;
        while(c<48||c>57) neg^=!(c^'-'),c=gc();
        while(c>=48&&c<=57) ans=(ans<<3)+(ans<<1)+(c^48),c=gc();
        return neg?-ans:ans;
    }

    char Output_Ans[1<<20|1],*Output_Cur=Output_Ans;
    inline void output() { Output_Cur-=fwrite(Output_Ans,1,Output_Cur-Output_Ans,stdout); }
    inline void print(char c){
        if(Output_Cur-Output_Ans+1>>20) output();
        *(Output_Cur++)=c;
    }
    inline void print(char *s) { while(*s) print( *(s++) ); }
    inline void print(u64 x){
        if(!x) { print('0'); return ; }
        char buf[30]={0},*p=buf+28;
        while(x) *(p--)=x%10+48,x/=10;
        print(p+1);
    }
    inline void print(u32 x) { print( (u64)x ); }
    inline void print(i64 x){
        if(x<0) print('-'),x=-x;
        print( (u64)x );
    }
    inline void print(i32 x){
        if(x<0) print('-'),x=-x;
        print( (u64)x );
    }
}
using namespace HABIT;

i32 N,K;
char TurnOn[MAXN];

inline i64 ans(){
    N=read();
    K=read();
    scanf("%s",TurnOn);
    char *Head=TurnOn,*Tail=TurnOn+N-1;
    while(*Head=='0') Head++;
    while(*Tail=='0') Tail--;
    if(Head>=Tail) return 0;
    i32 Cnt=0,Ans=0x3f3f3f3f;
    for(char *p=Head;p<=Tail;p++) Cnt+=(*p=='1');
    f(i,0,I,K-1){
        i32 Tmp=Cnt,Tmpp=0;
        for(char *p=Head+i;p<=Tail;p+=K){
            if(*p=='1') Tmp--;
            else Tmp++;
            Ans=Min(Ans,Tmp);
        }
    }
    return Ans;
}

int main(){
    f(i,1,I,read()) print( ans() ),print('\n');
    output();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

namespace HABIT{
    #define f(a,b,c,d) for(register int a=b,c=d;a<=c;a++)
    #define g(a,b,c,d) for(register int a=b,c=d;a>=c;a--)
    #define LOCAL
    typedef int i32;
    typedef unsigned int u32;
    typedef long long int i64;
    typedef unsigned long long int u64;
    typedef pair<i32,i32> pii;
    const i32 MAXN=1e6+10;
    typedef i32 ar[MAXN];

    template<typename T> inline T Max(T a) { return a; }
    template<typename T,typename... Args> inline T Max(T a,Args... args){
        T b=Max(args...);
        return (a>b)?a:b;
    }
    template<typename T> inline T Min(T a) { return a; }
    template<typename T,typename... Args> inline T Min(T a,Args... args){
        T b=Min(args...);
        return (a<b)?a:b;
    }

    #ifdef LOCAL
        inline char gc() { return getchar(); }
    #else
        inline char gc() {
            static char s[1<<20|1]={0},*p1=s,*p2=s;
            return (p1==p2)&&(p2=(p1=s)+fread(s,1,1<<20,stdin),p1==p2)?EOF:*(p1++);
        }
    #endif
    inline i64 read(){
        register i64 ans=0;register char c=gc();register bool neg=0;
        while(c<48||c>57) neg^=!(c^'-'),c=gc();
        while(c>=48&&c<=57) ans=(ans<<3)+(ans<<1)+(c^48),c=gc();
        return neg?-ans:ans;
    }

    char Output_Ans[1<<20|1],*Output_Cur=Output_Ans;
    inline void output() { Output_Cur-=fwrite(Output_Ans,1,Output_Cur-Output_Ans,stdout); }
    inline void print(char c){
        if(Output_Cur-Output_Ans+1>>20) output();
        *(Output_Cur++)=c;
    }
    inline void print(char *s) { while(*s) print( *(s++) ); }
    inline void print(u64 x){
        if(!x) { print('0'); return ; }
        char buf[30]={0},*p=buf+28;
        while(x) *(p--)=x%10+48,x/=10;
        print(p+1);
    }
    inline void print(u32 x) { print( (u64)x ); }
    inline void print(i64 x){
        if(x<0) print('-'),x=-x;
        print( (u64)x );
    }
    inline void print(i32 x){
        if(x<0) print('-'),x=-x;
        print( (u64)x );
    }
}
using namespace HABIT;

i32 N,K;
char TurnOn[MAXN];

inline i64 ans(){
    N=read();
    K=read();
    scanf("%s",TurnOn);
    char *Head=TurnOn,*Tail=TurnOn+N-1;
    while(*Head=='0') Head++;
    while(*Tail=='0') Tail--;
    if(Head>=Tail) return 0;
    i32 Cnt=0,Ans=0x3f3f3f3f;
    for(char *p=Head;p<=Tail;p++) Cnt+=(*p=='1');
    f(i,0,I,K-1){
        for(char *st=Head+i;st<=Tail;st+=K) if(st==Head+i||*(st-K)=='0'){
            i32 Tmp=Cnt;
            for(char *p=st;p<=Tail;p+=K){
                if(*p=='1') Tmp--;
                else Tmp++;
                Ans=Min(Ans,Tmp);
            }
        }
    }
    return Ans;
}

int main(){
    f(i,1,I,read()) print( ans() ),print('\n');
    output();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

namespace HABIT{
    #define f(a,b,c,d) for(register int a=b,c=d;a<=c;a++)
    #define g(a,b,c,d) for(register int a=b,c=d;a>=c;a--)
    #define LOCAL
    typedef int i32;
    typedef unsigned int u32;
    typedef long long int i64;
    typedef unsigned long long int u64;
    typedef pair<i32,i32> pii;
    const i32 MAXN=1e6+10;
    typedef i32 ar[MAXN];

    template<typename T> inline T Max(T a) { return a; }
    template<typename T,typename... Args> inline T Max(T a,Args... args){
        T b=Max(args...);
        return (a>b)?a:b;
    }
    template<typename T> inline T Min(T a) { return a; }
    template<typename T,typename... Args> inline T Min(T a,Args... args){
        T b=Min(args...);
        return (a<b)?a:b;
    }

    #ifdef LOCAL
        inline char gc() { return getchar(); }
    #else
        inline char gc() {
            static char s[1<<20|1]={0},*p1=s,*p2=s;
            return (p1==p2)&&(p2=(p1=s)+fread(s,1,1<<20,stdin),p1==p2)?EOF:*(p1++);
        }
    #endif
    inline i64 read(){
        register i64 ans=0;register char c=gc();register bool neg=0;
        while(c<48||c>57) neg^=!(c^'-'),c=gc();
        while(c>=48&&c<=57) ans=(ans<<3)+(ans<<1)+(c^48),c=gc();
        return neg?-ans:ans;
    }

    char Output_Ans[1<<20|1],*Output_Cur=Output_Ans;
    inline void output() { Output_Cur-=fwrite(Output_Ans,1,Output_Cur-Output_Ans,stdout); }
    inline void print(char c){
        if(Output_Cur-Output_Ans+1>>20) output();
        *(Output_Cur++)=c;
    }
    inline void print(char *s) { while(*s) print( *(s++) ); }
    inline void print(u64 x){
        if(!x) { print('0'); return ; }
        char buf[30]={0},*p=buf+28;
        while(x) *(p--)=x%10+48,x/=10;
        print(p+1);
    }
    inline void print(u32 x) { print( (u64)x ); }
    inline void print(i64 x){
        if(x<0) print('-'),x=-x;
        print( (u64)x );
    }
    inline void print(i32 x){
        if(x<0) print('-'),x=-x;
        print( (u64)x );
    }
}
using namespace HABIT;

i32 N,K;
char TurnOn[MAXN];

inline i64 ans(){
    N=read();
    K=read();
    scanf("%s",TurnOn);
    char *Head=TurnOn,*Tail=TurnOn+N-1;
    while(*Head=='0') Head++;
    while(*Tail=='0') Tail--;
    if(Head>=Tail) return 0;
    i32 Cnt=0,Ans=0x3f3f3f3f;
    for(char *p=Head;p<=Tail;p++) Cnt+=(*p=='1');
    f(i,0,I,K-1){
        i32 Tmp=Cnt;
        for(char *p=Head;p<=Tail;p+=K)
            if(*p=='1') Tmp--;
            else Tmp++;
        Ans=Min(Ans,Tmp);
    }
    return Ans;
}

int main(){
    f(i,1,I,read()) print( ans() ),print('\n');
    output();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

namespace HABIT{
    #define f(a,b,c,d) for(register int a=b,c=d;a<=c;a++)
    #define g(a,b,c,d) for(register int a=b,c=d;a>=c;a--)
    #define LOCAL
    typedef int i32;
    typedef unsigned int u32;
    typedef long long int i64;
    typedef unsigned long long int u64;
    typedef pair<i32,i32> pii;
    const i32 MAXN=1e6+10;
    typedef i32 ar[MAXN];

    template<typename T> inline T Max(T a) { return a; }
    template<typename T,typename... Args> inline T Max(T a,Args... args){
        T b=Max(args...);
        return (a>b)?a:b;
    }
    template<typename T> inline T Min(T a) { return a; }
    template<typename T,typename... Args> inline T Min(T a,Args... args){
        T b=Min(args...);
        return (a<b)?a:b;
    }

    #ifdef LOCAL
        inline char gc() { return getchar(); }
    #else
        inline char gc() {
            static char s[1<<20|1]={0},*p1=s,*p2=s;
            return (p1==p2)&&(p2=(p1=s)+fread(s,1,1<<20,stdin),p1==p2)?EOF:*(p1++);
        }
    #endif
    inline i64 read(){
        register i64 ans=0;register char c=gc();register bool neg=0;
        while(c<48||c>57) neg^=!(c^'-'),c=gc();
        while(c>=48&&c<=57) ans=(ans<<3)+(ans<<1)+(c^48),c=gc();
        return neg?-ans:ans;
    }

    char Output_Ans[1<<20|1],*Output_Cur=Output_Ans;
    inline void output() { Output_Cur-=fwrite(Output_Ans,1,Output_Cur-Output_Ans,stdout); }
    inline void print(char c){
        if(Output_Cur-Output_Ans+1>>20) output();
        *(Output_Cur++)=c;
    }
    inline void print(char *s) { while(*s) print( *(s++) ); }
    inline void print(u64 x){
        if(!x) { print('0'); return ; }
        char buf[30]={0},*p=buf+28;
        while(x) *(p--)=x%10+48,x/=10;
        print(p+1);
    }
    inline void print(u32 x) { print( (u64)x ); }
    inline void print(i64 x){
        if(x<0) print('-'),x=-x;
        print( (u64)x );
    }
    inline void print(i32 x){
        if(x<0) print('-'),x=-x;
        print( (u64)x );
    }
}
using namespace HABIT;

i32 N,K;
char TurnOn[MAXN];

inline i64 ans(){
    N=read();
    K=read();
    scanf("%s",TurnOn);
    char *Head=TurnOn,*Tail=TurnOn+N-1;
    while(*Head=='0') Head++;
    while(*Tail=='0') Tail--;
    if(Head>=Tail) return 0;
    i32 Cnt=0,Ans=0x3f3f3f3f;
    for(char *p=Head;p<=Tail;p++) Cnt+=(*p=='1');
    f(i,0,I,K-1){
        i32 Cnt1=0,Cnt0=0,Cntt1=0;
        for(char *p=Head+i;p<=Tail;p+=K)
            if(*p=='1') Cnt1++;
            else{
                if(p==Head+i||*(p-K)=='0'){
                    Cnt0++;
                    continue;
                }
                Cntt1=Max(Cnt1,Cntt1+Cnt1-Cnt0);
                Ans=Min(Ans,Cnt-Cntt1);
                Cnt1=0;
                Cnt0=1;
            }
        Cntt1=Max(Cnt1,Cntt1+Cnt1-Cnt0);
        Ans=Min(Ans,Cnt-Cntt1);
    }
    return Ans;
}

int main(){
    f(i,1,I,read()) print( ans() ),print('\n');
    output();
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <list>
#include <map>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define LL long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f
#define PI 3.1415926535898
#define F first
#define S second
#define endl '\n'
#define lson  rt << 1
#define rson  rt << 1 | 1
#define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x)
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std;

const int maxn =1e6 + 7;
const int maxm = 207;
int n, k, a[maxn],  dp[maxn], nub[maxn];
vector<int> v[maxn];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n >> k;
        _rep(i, 0, k) v[i]  .clear();
        char c;
        int tot = 0;
        _rep(i, 1, n)
        {
            cin >> c;
            a[i] = c - '0';
            tot += a[i];
            if (a[i]) v[i % k].push_back(i);
        }
        int ans = inf;
        _rep(i, 0, k - 1)
        {
            if (!v[i].size() || tot - v[i].size() >= ans ) continue;
            memset(dp, 0, sizeof(dp));
            int cnt = 0;
            int tmp = 1;
            f(j, 1, v[i].size())
            {
                if (v[i][j] != v[i][j - 1] + k)
                {
                    nub[++cnt] = tmp;
                    tmp = 1;
                }
                else tmp++;
            }
            nub[++cnt] = tmp;
            _rep(j, 1, cnt) dp[j] = tot - nub[j];
            int p = nub[1];
            int mn = dp[1];
            _rep(j, 2, cnt)
            {
                dp[j] = min(dp[j], dp[j - 1] + (v[i][p] - v[i][p - 1]) / k - 1 - nub[j]);
                mn = min(mn, dp[j]);
                p += nub[j];
            }
            ans = min(mn, ans);
        }
        if (ans == inf) ans = 0;
        cout << ans << endl;
    }
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <list>
#include <map>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define LL long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f
#define PI 3.1415926535898
#define F first
#define S second
#define endl '\n'
#define lson  rt << 1
#define rson  rt << 1 | 1
#define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x)
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std;

const int maxn =1e6 + 7;
const int maxm = 207;
int n, k, a[maxn], sum[maxn], dp[maxn], nub[maxn];
vector<int> v[maxn];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n >> k;
        _rep(i, 0, k) v[i].clear();
        char c;
        int tot = 0;
        _rep(i, 1, n)
        {
            cin >> c;
            a[i] = c - '0';
            tot += a[i];
            if (a[i]) v[i % k].push_back(i);
        }
        int ans = inf;
        _rep(i, 0, k - 1)
        {
            if (!v[i].size()) continue;
            memset(dp, 0, sizeof(dp));
            int cnt = 0;
            int tmp = 1;
            f(j, 1, v[i].size())
            {
                if (v[i][j] != v[i][j - 1] + k)
                {
                    nub[++cnt] = tmp;
                    tmp = 1;
                }
                else tmp++;
            }
            nub[++cnt] = tmp;
            _rep(j, 1, cnt)
            {
                dp[j] = tot - nub[j];
            }
            int p = nub[1];
            int mn = dp[1];
            _rep(j, 2, cnt)
            {
                dp[j] = min(dp[j], dp[j - 1] + (v[i][p] - v[i][p - 1]) / k - 1 - nub[j]);
                mn = min(mn, dp[j]);
                p += nub[j];
            }
            ans = min(mn, ans);
        }
        if (ans == inf) ans = 0;
        cout << ans << endl;
    }
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <list>
#include <map>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define LL long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f
#define PI 3.1415926535898
#define F first
#define S second
#define endl '\n'
#define lson  rt << 1
#define rson  rt << 1 | 1
#define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x)
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std;

const int maxn =1e6 + 7;
const int maxm = 207;
int n, k, a[maxn], sum[maxn], dp[maxn], nub[maxn];
vector<int> v[maxn];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n >> k;
        _rep(i, 1, k) v[i].clear();
        char c;
        int tot = 0;
        _rep(i, 1, n)
        {
            cin >> c;
            a[i] = c - '0';
            tot += a[i];
            if (a[i]) v[i % k].push_back(i);
        }
        int ans = inf;
        _rep(i, 0, k - 1)
        {
            if (!v[i].size()) continue;
            memset(dp, 0, sizeof(dp));
            int cnt = 0;
            int tmp = 1;
            f(j, 1, v[i].size())
            {
                if (v[i][j] != v[i][j - 1] + k)
                {
                    nub[++cnt] = tmp;
                    tmp = 1;
                }
                else tmp++;
            }
            nub[++cnt] = tmp;
            _rep(j, 1, cnt)
            {
                dp[j] = tot - nub[j];
            }
            int p = nub[1];
            int mn = dp[1];
            _rep(j, 2, cnt)
            {
                dp[j] = min(dp[j], dp[j - 1] + (v[i][p] - v[i][p - 1]) / k - 1 - nub[j]);
                mn = min(mn, dp[j]);
                p += nub[j];
            }
            ans = min(mn, ans);
        }
        if (ans == inf) ans = 0;
        cout << ans << endl;
    }
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <list>
#include <map>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define LL long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f
#define PI 3.1415926535898
#define F first
#define S second
#define endl '\n'
#define lson  rt << 1
#define rson  rt << 1 | 1
#define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x)
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std;

const int maxn = 2e5 + 7;
const int maxm = 207;
int n, k, a[maxn], sum[maxn], dp[maxn], nub[maxn];
vector<int> v[maxn];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n >> k;
        f(i, 0, maxn) v[i].clear();
        char c;
        int tot = 0;
        _rep(i, 1, n)
        {
            cin >> c;
            a[i] = c - '0';
            tot += a[i];
            if (a[i]) v[i % k].push_back(i);
        }
        int ans = inf;
        _rep(i, 0, k - 1)
        {
            if (!v[i].size()) continue;
            memset(dp, 0, sizeof(dp));
            int cnt = 0;
            int tmp = 1;
            f(j, 1, v[i].size())
            {
                if (v[i][j] != v[i][j - 1] + k)
                {
                    nub[++cnt] = tmp;
                    tmp = 1;
                }
                else tmp++;
            }
            nub[++cnt] = tmp;
            _rep(j, 1, cnt)
            {
                dp[j] = tot - nub[j];
            }
            int p = nub[1];
            int mn = dp[1];
            _rep(j, 2, cnt)
            {
                dp[j] = min(dp[j], dp[j - 1] + (v[i][p] - v[i][p - 1]) / k - 1 - nub[j]);
                mn = min(mn, dp[j]);
                p += nub[j];
            }
            ans = min(mn, ans);
        }
        if (ans == inf) ans = 0;
        cout << ans << endl;
    }
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <list>
#include <map>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define LL long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f
#define PI 3.1415926535898
#define F first
#define S second
#define endl '\n'
#define lson  rt << 1
#define rson  rt << 1 | 1
#define f(x, y, z) for (int x = (y), __ = (z); x < __; ++x)
#define _rep(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std;

const int maxn =1e6 + 7;
const int maxm = 207;
int n, k, a[maxn],  dp[maxn], nub[maxn];
vector<int> v[maxn];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while (t--)
    {
        cin >> n >> k;
        _rep(i, 0, k) v[i].clear();
        char c;
        int tot = 0;
        _rep(i, 1, n)
        {
            cin >> c;
            a[i] = c - '0';
            tot += a[i];
            if (a[i]) v[i % k].push_back(i);
        }
        int ans = inf;
        _rep(i, 0, k - 1)
        {
            if (!v[i].size() || tot - v[i].size() >= ans ) continue;
            _rep(i, 0, n / k) dp[i] = 0;
            int cnt = 0;
            int tmp = 1;
            f(j, 1, v[i].size())
            {
                if (v[i][j] != v[i][j - 1] + k)
                {
                    nub[++cnt] = tmp;
                    tmp = 1;
                }
                else tmp++;
            }
            nub[++cnt] = tmp;
            _rep(j, 1, cnt) dp[j] = tot - nub[j];
            int p = nub[1];
            int mn = dp[1];
            _rep(j, 2, cnt)
            {
                dp[j] = min(dp[j], dp[j - 1] + (v[i][p] - v[i][p - 1]) / k - 1 - nub[j]);
                mn = min(mn, dp[j]);
                p += nub[j];
            }
            ans = min(mn, ans);
        }
        if (ans == inf) ans = 0;
        cout << ans << endl;
    }
}


#include<bits/stdc++.h>

using namespace std;

#define MAX 10000
#define INF 1e100
#define isset(n,i) ((n>>i)&1LL)

#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define sor(A) sort(A.begin(),A.end())
#define rsor(A) sort(A.rbegin(),A.rend())
#define all(A) A.begin(),A.end()

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<long long ,long long> pll;
typedef pair<long long ,int> pli;
#define VI  vector<int>
#define VLL  vector<long long>
#define VVI vector<vector<int>>
#define VS  vector<string>
#define VVS vector<vector<string>>
#define X first
#define Y second

#define ASSERT(X) {if (!(X)) {printf("\n assertion failed at line %d\n",__LINE__);exit(0);}}

//BEGINTEMPLATE_BY_ACRUSH_TOPCODER

#define SZ(X) ((int)(X.size()))//NOTES:SIZE(

#define LENGTH(X) ((int)(X.length()))//NOTES:LENGTH(

#define two(X) (1<<(X))//NOTES:two(

#define twoL(X) (((unsigned long long)(1))<<(X))//NOTES:twoL(

#define contain(S,X) (((S)&two(X))!=0)//NOTES:contain(

#define containL(S,X) (((S)&twoL(X))!=0)//NOTES:containL(

const double pi=acos(-1.0);//NOTES:pi

const double eps=1e-11;//NOTES:eps

template<class T> inline void checkmin(T &a,T b){if(b<a) a=b;}//NOTES:checkmin(

template<class T> inline void checkmax(T &a,T b){if(b>a) a=b;}//NOTES:checkmax(

template<class T> inline T sqr(T x){return x*x;}//NOTES:sqr

template<class T> inline string bin(T n,int pos=62) { string s; T i,one=1,two=2; for (i = (one << (pos-1)); i > 0; i = (i>>1) ) s += ((n & i)?"1":"0"); return s; }

int lsb(unsigned int v){
	int r;           // result goes here
	static const int MultiplyDeBruijnBitPosition[32] =
	{
	  0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
	  31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
	};
	r = MultiplyDeBruijnBitPosition[((uint32_t)((v & -v) * 0x077CB531U)) >> 27];
	return r+1;
}

template<class T> inline int msb(T n){return (n==0)?0:(1+msb(n>>1));}//NOTES:msb(
template<class T> inline T getbit(T n,int pos){return ((n&(1<<pos))?1:0);}//NOTES:getbit(
template<class T> inline int countbit(T n){return (n==0)?0:(1+countbit(n&(n-1)));}//NOTES:countbit(
template<class T> inline void setbit(T &n,int p){ n = n | (1LL<<p); }

// Can also use popcount for CPU access
// int  __builtin_popcount(unsigned int)
// int  __builtin_popcountll(long long)

//Numberic Functions

template<class T> inline T gcd(T a,T b)//NOTES:gcd(
  {if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}

bool isequal(vector<int> a,vector<int> b){
	REP(i,a.size()) if(a[i]!=b[i]) return false;
	return true;
}

template<class T>
bool allequal(vector<T> a){
	sor(a);
	return a[0]==a[a.size()-1];
}

template<class T1,class T2>
void printpairs(vector< pair<T1,T2> > p){
    for(auto t:p){
        cout << "(" << t.first << " " << t.second << ")  ";
    }
    cout << endl;
}

template<class T>
void printvec(vector<T> a){
	REP(i,a.size())cout << a[i] << " ";
	cout<< endl;
}

void printarray(int *a,int n){
	REP(i,n)cout << *(a+i) << " ";
	cout<< endl;
}

template<typename T>
void printmatrix(vector< vector<T> > a,int n,int m,int width=5){
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cout << setw(width) << a[i][j] ;
		}
		cout << endl;
	}
}

template<typename T>
void readmatrix(vector< vector<T> > &a,int n,int m){
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cin >> a[i][j];
		}
	}
}

template<class T>
void printsubarray(vector<T> a,int start ,int end){
	if(start<0 or end>a.size()-1) cout << "Out of bounds" << endl;
	for(int i=start;i<=end;i++)cout << a[i] << " ";
	cout<< endl;
}

bool isprime(int n){
	if(n<2) return false;
	if(n==2) return true;
	for(int i=2;i*i<=n;i++){
		if(n%i==0) return false;
	}
	return true;
}

// Error checking

#define error(args...) { vector<string> _v; split(#args, ',', _v); err(_v.begin(), args); cerr<< endl; }

void split(const string& s, char c, vector<string>& result) {
    stringstream ss(s);
    string x;
    while (getline(ss, x, c))
        result.push_back(x);
}

void err(vector<string>::iterator it) {}
template<typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
    cerr << it -> substr((*it)[0] == ' ', it -> length()) << " = " << a << ' ';
    err(++it, args...);
}

/*
vector<vector<ll>> a(n, vector<ll>(m,0) );
readmatrix(a,n,m);
printmatrix(a,n,m);
*/

void yup(){ cout << "Yes" << endl; }
void nope(){ cout << "No" << endl; }
void Yup(){ cout << "YES" << endl; }
void Nope(){ cout << "NO" << endl; }

template<typename T>
void readvec(vector<T>& a,int size){
	T tmp;
	a.clear();
	REP(i,size){
		cin>>tmp;
		a.push_back(tmp);
	}
}

//ENDTEMPLATE_BY_ACRUSH_TOPCODER

int n,k;
int cost(VI &pos){
    int c=0;
    int n=pos.size();
    int start=0,end=n-1;
    int delcount1=0,delcount2=0;

    for(int i=1;i<n;i++){
        int gap=pos[i]-pos[i-1];
        c+=(gap-k)/k;
        if(c>i){
            start = i;
            delcount1 = i;
        }
    }

    c=0;
    for(int i=n-2;i>=0;i--){
        int gap=pos[i+1]-pos[i];
        c+=(gap-k)/k;
        if(c>n-1-i){
            end = i;
            delcount2 = n-1-i;
        }
    }

    /*
    error(start,end);
    error(delcount1,delcount2);
    cerr << endl;
    */

    if(start<end)
        c=delcount1+delcount2;
    else
        c=max(0,n-1);

    for(int i=start;i<end;i++){
        int gap=pos[i+1]-pos[i];
        c+=(gap-k)/k;
    }

    return c;
}

void solvecase(){
    cin >> n >> k;
    string s;
    cin >> s;
    VI p;
    int m=0;
    for(int i = 0;i<n;i++){
        if(s[i] == '1') { p.push_back(i); m++; }
    }
    //cout << m << k << endl;
    VI gap;
    int ans=0;
    VVI pos(k);
    map<int,int> freq;

    for(int x:p){
        freq[x%k]=0;
    }

    for(int x:p){
        freq[x%k]++;
        pos[x%k].push_back(x);
    }

    int best=max(0,m-1);
    for(auto it=freq.begin();it!=freq.end();it++){
        int mod = it->first;
        int cur = m-(it->second)+cost(pos[mod]);
        //cout << "m: " << m-(it->second) << " mod: " << mod << " cur: " << cur << "\n";
        best = min(cur,best);
    }
    if(best!=1e9)
        cout << best << endl;
    else
        cout << 0 << endl;

}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int cases=1;
  cin >> cases;
  while(cases--){
	  solvecase();
  }
  return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define MAX 10000
#define INF 1e100
#define isset(n,i) ((n>>i)&1LL)

#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define sor(A) sort(A.begin(),A.end())
#define rsor(A) sort(A.rbegin(),A.rend())
#define all(A) A.begin(),A.end()

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<long long ,long long> pll;
typedef pair<long long ,int> pli;
#define VI  vector<int>
#define VLL  vector<long long>
#define VVI vector<vector<int>>
#define VS  vector<string>
#define VVS vector<vector<string>>
#define X first
#define Y second

#define ASSERT(X) {if (!(X)) {printf("\n assertion failed at line %d\n",__LINE__);exit(0);}}

//BEGINTEMPLATE_BY_ACRUSH_TOPCODER

#define SZ(X) ((int)(X.size()))//NOTES:SIZE(

#define LENGTH(X) ((int)(X.length()))//NOTES:LENGTH(

#define two(X) (1<<(X))//NOTES:two(

#define twoL(X) (((unsigned long long)(1))<<(X))//NOTES:twoL(

#define contain(S,X) (((S)&two(X))!=0)//NOTES:contain(

#define containL(S,X) (((S)&twoL(X))!=0)//NOTES:containL(

const double pi=acos(-1.0);//NOTES:pi

const double eps=1e-11;//NOTES:eps

template<class T> inline void checkmin(T &a,T b){if(b<a) a=b;}//NOTES:checkmin(

template<class T> inline void checkmax(T &a,T b){if(b>a) a=b;}//NOTES:checkmax(

template<class T> inline T sqr(T x){return x*x;}//NOTES:sqr

template<class T> inline string bin(T n,int pos=62) { string s; T i,one=1,two=2; for (i = (one << (pos-1)); i > 0; i = (i>>1) ) s += ((n & i)?"1":"0"); return s; }

int lsb(unsigned int v){
	int r;           // result goes here
	static const int MultiplyDeBruijnBitPosition[32] =
	{
	  0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
	  31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
	};
	r = MultiplyDeBruijnBitPosition[((uint32_t)((v & -v) * 0x077CB531U)) >> 27];
	return r+1;
}

template<class T> inline int msb(T n){return (n==0)?0:(1+msb(n>>1));}//NOTES:msb(
template<class T> inline T getbit(T n,int pos){return ((n&(1<<pos))?1:0);}//NOTES:getbit(
template<class T> inline int countbit(T n){return (n==0)?0:(1+countbit(n&(n-1)));}//NOTES:countbit(
template<class T> inline void setbit(T &n,int p){ n = n | (1LL<<p); }

// Can also use popcount for CPU access
// int  __builtin_popcount(unsigned int)
// int  __builtin_popcountll(long long)

//Numberic Functions

template<class T> inline T gcd(T a,T b)//NOTES:gcd(
  {if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}

bool isequal(vector<int> a,vector<int> b){
	REP(i,a.size()) if(a[i]!=b[i]) return false;
	return true;
}

template<class T>
bool allequal(vector<T> a){
	sor(a);
	return a[0]==a[a.size()-1];
}

template<class T1,class T2>
void printpairs(vector< pair<T1,T2> > p){
    for(auto t:p){
        cout << "(" << t.first << " " << t.second << ")  ";
    }
    cout << endl;
}

template<class T>
void printvec(vector<T> a){
	REP(i,a.size())cout << a[i] << " ";
	cout<< endl;
}

void printarray(int *a,int n){
	REP(i,n)cout << *(a+i) << " ";
	cout<< endl;
}

template<typename T>
void printmatrix(vector< vector<T> > a,int n,int m,int width=5){
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cout << setw(width) << a[i][j] ;
		}
		cout << endl;
	}
}

template<typename T>
void readmatrix(vector< vector<T> > &a,int n,int m){
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cin >> a[i][j];
		}
	}
}

template<class T>
void printsubarray(vector<T> a,int start ,int end){
	if(start<0 or end>a.size()-1) cout << "Out of bounds" << endl;
	for(int i=start;i<=end;i++)cout << a[i] << " ";
	cout<< endl;
}

bool isprime(int n){
	if(n<2) return false;
	if(n==2) return true;
	for(int i=2;i*i<=n;i++){
		if(n%i==0) return false;
	}
	return true;
}

// Error checking

#define error(args...) { vector<string> _v; split(#args, ',', _v); err(_v.begin(), args); cerr<< endl; }

void split(const string& s, char c, vector<string>& result) {
    stringstream ss(s);
    string x;
    while (getline(ss, x, c))
        result.push_back(x);
}

void err(vector<string>::iterator it) {}
template<typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
    cerr << it -> substr((*it)[0] == ' ', it -> length()) << " = " << a << ' ';
    err(++it, args...);
}

/*
vector<vector<ll>> a(n, vector<ll>(m,0) );
readmatrix(a,n,m);
printmatrix(a,n,m);
*/

void yup(){ cout << "Yes" << endl; }
void nope(){ cout << "No" << endl; }
void Yup(){ cout << "YES" << endl; }
void Nope(){ cout << "NO" << endl; }

template<typename T>
void readvec(vector<T>& a,int size){
	T tmp;
	a.clear();
	REP(i,size){
		cin>>tmp;
		a.push_back(tmp);
	}
}

//ENDTEMPLATE_BY_ACRUSH_TOPCODER

int n,k;
int cost(VI &pos){
    int c=0;
    int n=pos.size();
    for(int i=0;i<n-1;i++){
        int gap=pos[i+1]-pos[i];
        c+=(gap-k)/k;
    }
    return c;
}

void solvecase(){
    cin >> n >> k;
    string s;
    cin >> s;
    VI p;
    int m=0;
    for(int i = 0;i<n;i++){
        if(s[i] == '1') { p.push_back(i); m++; }
    }
    //cout << m << k << endl;
    VI gap;
    int ans=0;
    VVI pos(k);
    map<int,int> freq;

    for(int x:p){
        freq[x%k]=0;
    }

    for(int x:p){
        freq[x%k]++;
        pos[x%k].push_back(x);
    }

    int best=1e9;
    for(auto it=freq.begin();it!=freq.end();it++){
        int mod = it->first;
        int cur = m-(it->second)+cost(pos[mod]);
        //cout << "m: " << m-(it->second) << " mod: " << mod << " cur: " << cur << "\n";
        best = min(cur,best);
    }
    if(best!=1e9)
        cout << best << endl;
    else
        cout << 0 << endl;

}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int cases=1;
  cin >> cases;
  while(cases--){
	  solvecase();
  }
  return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define MAX 10000
#define INF 1e100
#define isset(n,i) ((n>>i)&1LL)

#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define sor(A) sort(A.begin(),A.end())
#define rsor(A) sort(A.rbegin(),A.rend())
#define all(A) A.begin(),A.end()

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<long long ,long long> pll;
typedef pair<long long ,int> pli;
#define VI  vector<int>
#define VLL  vector<long long>
#define VVI vector<vector<int>>
#define VS  vector<string>
#define VVS vector<vector<string>>
#define X first
#define Y second

#define ASSERT(X) {if (!(X)) {printf("\n assertion failed at line %d\n",__LINE__);exit(0);}}

//BEGINTEMPLATE_BY_ACRUSH_TOPCODER

#define SZ(X) ((int)(X.size()))//NOTES:SIZE(

#define LENGTH(X) ((int)(X.length()))//NOTES:LENGTH(

#define two(X) (1<<(X))//NOTES:two(

#define twoL(X) (((unsigned long long)(1))<<(X))//NOTES:twoL(

#define contain(S,X) (((S)&two(X))!=0)//NOTES:contain(

#define containL(S,X) (((S)&twoL(X))!=0)//NOTES:containL(

const double pi=acos(-1.0);//NOTES:pi

const double eps=1e-11;//NOTES:eps

template<class T> inline void checkmin(T &a,T b){if(b<a) a=b;}//NOTES:checkmin(

template<class T> inline void checkmax(T &a,T b){if(b>a) a=b;}//NOTES:checkmax(

template<class T> inline T sqr(T x){return x*x;}//NOTES:sqr

template<class T> inline string bin(T n,int pos=62) { string s; T i,one=1,two=2; for (i = (one << (pos-1)); i > 0; i = (i>>1) ) s += ((n & i)?"1":"0"); return s; }

int lsb(unsigned int v){
	int r;           // result goes here
	static const int MultiplyDeBruijnBitPosition[32] =
	{
	  0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
	  31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
	};
	r = MultiplyDeBruijnBitPosition[((uint32_t)((v & -v) * 0x077CB531U)) >> 27];
	return r+1;
}

template<class T> inline int msb(T n){return (n==0)?0:(1+msb(n>>1));}//NOTES:msb(
template<class T> inline T getbit(T n,int pos){return ((n&(1<<pos))?1:0);}//NOTES:getbit(
template<class T> inline int countbit(T n){return (n==0)?0:(1+countbit(n&(n-1)));}//NOTES:countbit(
template<class T> inline void setbit(T &n,int p){ n = n | (1LL<<p); }

// Can also use popcount for CPU access
// int  __builtin_popcount(unsigned int)
// int  __builtin_popcountll(long long)

//Numberic Functions

template<class T> inline T gcd(T a,T b)//NOTES:gcd(
  {if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}

bool isequal(vector<int> a,vector<int> b){
	REP(i,a.size()) if(a[i]!=b[i]) return false;
	return true;
}

template<class T>
bool allequal(vector<T> a){
	sor(a);
	return a[0]==a[a.size()-1];
}

template<class T1,class T2>
void printpairs(vector< pair<T1,T2> > p){
    for(auto t:p){
        cout << "(" << t.first << " " << t.second << ")  ";
    }
    cout << endl;
}

template<class T>
void printvec(vector<T> a){
	REP(i,a.size())cout << a[i] << " ";
	cout<< endl;
}

void printarray(int *a,int n){
	REP(i,n)cout << *(a+i) << " ";
	cout<< endl;
}

template<typename T>
void printmatrix(vector< vector<T> > a,int n,int m,int width=5){
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cout << setw(width) << a[i][j] ;
		}
		cout << endl;
	}
}

template<typename T>
void readmatrix(vector< vector<T> > &a,int n,int m){
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cin >> a[i][j];
		}
	}
}

template<class T>
void printsubarray(vector<T> a,int start ,int end){
	if(start<0 or end>a.size()-1) cout << "Out of bounds" << endl;
	for(int i=start;i<=end;i++)cout << a[i] << " ";
	cout<< endl;
}

bool isprime(int n){
	if(n<2) return false;
	if(n==2) return true;
	for(int i=2;i*i<=n;i++){
		if(n%i==0) return false;
	}
	return true;
}

// Error checking

#define error(args...) { vector<string> _v; split(#args, ',', _v); err(_v.begin(), args); cerr<< endl; }

void split(const string& s, char c, vector<string>& result) {
    stringstream ss(s);
    string x;
    while (getline(ss, x, c))
        result.push_back(x);
}

void err(vector<string>::iterator it) {}
template<typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
    cerr << it -> substr((*it)[0] == ' ', it -> length()) << " = " << a << ' ';
    err(++it, args...);
}

/*
vector<vector<ll>> a(n, vector<ll>(m,0) );
readmatrix(a,n,m);
printmatrix(a,n,m);
*/

void yup(){ cout << "Yes" << endl; }
void nope(){ cout << "No" << endl; }
void Yup(){ cout << "YES" << endl; }
void Nope(){ cout << "NO" << endl; }

template<typename T>
void readvec(vector<T>& a,int size){
	T tmp;
	a.clear();
	REP(i,size){
		cin>>tmp;
		a.push_back(tmp);
	}
}

//ENDTEMPLATE_BY_ACRUSH_TOPCODER

int n,k;
int cost(VI &pos){
    int c=0;
    int n=pos.size();
    int start=0,end=n-1;
    int delcount1=0,delcount2=0;

    for(int i=1;i<n;i++){
        int gap=pos[i]-pos[i-1];
        c+=(gap-k)/k;
        if(c>i){
            start = i;
            delcount1 = i;
        }
    }

    c=0;
    for(int i=n-2;i>=0;i--){
        int gap=pos[i+1]-pos[i];
        c+=(gap-k)/k;
        if(c>n-1-i){
            end = i;
            delcount2 = n-1-i;
        }
    }

    /*
    error(start,end);
    error(delcount1,delcount2);
    cerr << endl;
    */

    c=delcount1+delcount2;

    for(int i=start;i<end;i++){
        int gap=pos[i+1]-pos[i];
        c+=(gap-k)/k;
    }

    return c;
}

void solvecase(){
    cin >> n >> k;
    string s;
    cin >> s;
    VI p;
    int m=0;
    for(int i = 0;i<n;i++){
        if(s[i] == '1') { p.push_back(i); m++; }
    }
    //cout << m << k << endl;
    VI gap;
    int ans=0;
    VVI pos(k);
    map<int,int> freq;

    for(int x:p){
        freq[x%k]=0;
    }

    for(int x:p){
        freq[x%k]++;
        pos[x%k].push_back(x);
    }

    int best=1e9;
    for(auto it=freq.begin();it!=freq.end();it++){
        int mod = it->first;
        int cur = m-(it->second)+cost(pos[mod]);
        //cout << "m: " << m-(it->second) << " mod: " << mod << " cur: " << cur << "\n";
        best = min(cur,best);
    }
    if(best!=1e9)
        cout << best << endl;
    else
        cout << 0 << endl;

}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int cases=1;
  cin >> cases;
  while(cases--){
	  solvecase();
  }
  return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define MAX 10000
#define INF 1e100
#define isset(n,i) ((n>>i)&1LL)

#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define sor(A) sort(A.begin(),A.end())
#define rsor(A) sort(A.rbegin(),A.rend())
#define all(A) A.begin(),A.end()

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<long long ,long long> pll;
typedef pair<long long ,int> pli;
#define VI  vector<int>
#define VLL  vector<long long>
#define VVI vector<vector<int>>
#define VS  vector<string>
#define VVS vector<vector<string>>
#define X first
#define Y second

#define ASSERT(X) {if (!(X)) {printf("\n assertion failed at line %d\n",__LINE__);exit(0);}}

//BEGINTEMPLATE_BY_ACRUSH_TOPCODER

#define SZ(X) ((int)(X.size()))//NOTES:SIZE(

#define LENGTH(X) ((int)(X.length()))//NOTES:LENGTH(

#define two(X) (1<<(X))//NOTES:two(

#define twoL(X) (((unsigned long long)(1))<<(X))//NOTES:twoL(

#define contain(S,X) (((S)&two(X))!=0)//NOTES:contain(

#define containL(S,X) (((S)&twoL(X))!=0)//NOTES:containL(

const double pi=acos(-1.0);//NOTES:pi

const double eps=1e-11;//NOTES:eps

template<class T> inline void checkmin(T &a,T b){if(b<a) a=b;}//NOTES:checkmin(

template<class T> inline void checkmax(T &a,T b){if(b>a) a=b;}//NOTES:checkmax(

template<class T> inline T sqr(T x){return x*x;}//NOTES:sqr

template<class T> inline string bin(T n,int pos=62) { string s; T i,one=1,two=2; for (i = (one << (pos-1)); i > 0; i = (i>>1) ) s += ((n & i)?"1":"0"); return s; }

int lsb(unsigned int v){
	int r;           // result goes here
	static const int MultiplyDeBruijnBitPosition[32] =
	{
	  0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
	  31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
	};
	r = MultiplyDeBruijnBitPosition[((uint32_t)((v & -v) * 0x077CB531U)) >> 27];
	return r+1;
}

template<class T> inline int msb(T n){return (n==0)?0:(1+msb(n>>1));}//NOTES:msb(
template<class T> inline T getbit(T n,int pos){return ((n&(1<<pos))?1:0);}//NOTES:getbit(
template<class T> inline int countbit(T n){return (n==0)?0:(1+countbit(n&(n-1)));}//NOTES:countbit(
template<class T> inline void setbit(T &n,int p){ n = n | (1LL<<p); }

// Can also use popcount for CPU access
// int  __builtin_popcount(unsigned int)
// int  __builtin_popcountll(long long)

//Numberic Functions

template<class T> inline T gcd(T a,T b)//NOTES:gcd(
  {if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}

bool isequal(vector<int> a,vector<int> b){
	REP(i,a.size()) if(a[i]!=b[i]) return false;
	return true;
}

template<class T>
bool allequal(vector<T> a){
	sor(a);
	return a[0]==a[a.size()-1];
}

template<class T1,class T2>
void printpairs(vector< pair<T1,T2> > p){
    for(auto t:p){
        cout << "(" << t.first << " " << t.second << ")  ";
    }
    cout << endl;
}

template<class T>
void printvec(vector<T> a){
	REP(i,a.size())cout << a[i] << " ";
	cout<< endl;
}

void printarray(int *a,int n){
	REP(i,n)cout << *(a+i) << " ";
	cout<< endl;
}

template<typename T>
void printmatrix(vector< vector<T> > a,int n,int m,int width=5){
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cout << setw(width) << a[i][j] ;
		}
		cout << endl;
	}
}

template<typename T>
void readmatrix(vector< vector<T> > &a,int n,int m){
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cin >> a[i][j];
		}
	}
}

template<class T>
void printsubarray(vector<T> a,int start ,int end){
	if(start<0 or end>a.size()-1) cout << "Out of bounds" << endl;
	for(int i=start;i<=end;i++)cout << a[i] << " ";
	cout<< endl;
}

bool isprime(int n){
	if(n<2) return false;
	if(n==2) return true;
	for(int i=2;i*i<=n;i++){
		if(n%i==0) return false;
	}
	return true;
}

// Error checking

#define error(args...) { vector<string> _v; split(#args, ',', _v); err(_v.begin(), args); cerr<< endl; }

void split(const string& s, char c, vector<string>& result) {
    stringstream ss(s);
    string x;
    while (getline(ss, x, c))
        result.push_back(x);
}

void err(vector<string>::iterator it) {}
template<typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
    cerr << it -> substr((*it)[0] == ' ', it -> length()) << " = " << a << ' ';
    err(++it, args...);
}

/*
vector<vector<ll>> a(n, vector<ll>(m,0) );
readmatrix(a,n,m);
printmatrix(a,n,m);
*/

void yup(){ cout << "Yes" << endl; }
void nope(){ cout << "No" << endl; }
void Yup(){ cout << "YES" << endl; }
void Nope(){ cout << "NO" << endl; }

template<typename T>
void readvec(vector<T>& a,int size){
	T tmp;
	a.clear();
	REP(i,size){
		cin>>tmp;
		a.push_back(tmp);
	}
}

//ENDTEMPLATE_BY_ACRUSH_TOPCODER

int n,k;
int cost(VI &pos){
    int c=0;
    int n=pos.size();
    int start=0,end=n-1;
    int delcount1=0,delcount2=0;
    int best=max(0,n-1);

    for(int i=1;i<n;i++){
        int gap=pos[i]-pos[i-1];
        c+=(gap-k)/k;
        if(c>i){
            start = i;
            delcount1 = i;
            c=i;
        }
        best = min(best,c+(n-i-1));
    }
    return best;
}

void solvecase(){
    cin >> n >> k;
    string s;
    cin >> s;
    VI p;
    int m=0;
    for(int i = 0;i<n;i++){
        if(s[i] == '1') { p.push_back(i); m++; }
    }
    //cout << m << k << endl;
    VI gap;
    int ans=0;
    VVI pos(k);
    map<int,int> freq;

    for(int x:p){
        freq[x%k]=0;
    }

    for(int x:p){
        freq[x%k]++;
        pos[x%k].push_back(x);
    }

    int best=max(0,m-1);
    for(auto it=freq.begin();it!=freq.end();it++){
        int mod = it->first;
        int cur = m-(it->second)+cost(pos[mod]);
        //cout << "m: " << m-(it->second) << " mod: " << mod << " cur: " << cur << "\n";
        best = min(cur,best);
    }
    cout << best << endl;

}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int cases=1;
  cin >> cases;
  while(cases--){
	  solvecase();
  }
  return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define MAX 10000
#define INF 1e100
#define isset(n,i) ((n>>i)&1LL)

#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define sor(A) sort(A.begin(),A.end())
#define rsor(A) sort(A.rbegin(),A.rend())
#define all(A) A.begin(),A.end()

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<long long ,long long> pll;
typedef pair<long long ,int> pli;
#define VI  vector<int>
#define VLL  vector<long long>
#define VVI vector<vector<int>>
#define VS  vector<string>
#define VVS vector<vector<string>>
#define X first
#define Y second

#define ASSERT(X) {if (!(X)) {printf("\n assertion failed at line %d\n",__LINE__);exit(0);}}

//BEGINTEMPLATE_BY_ACRUSH_TOPCODER

#define SZ(X) ((int)(X.size()))//NOTES:SIZE(

#define LENGTH(X) ((int)(X.length()))//NOTES:LENGTH(

#define two(X) (1<<(X))//NOTES:two(

#define twoL(X) (((unsigned long long)(1))<<(X))//NOTES:twoL(

#define contain(S,X) (((S)&two(X))!=0)//NOTES:contain(

#define containL(S,X) (((S)&twoL(X))!=0)//NOTES:containL(

const double pi=acos(-1.0);//NOTES:pi

const double eps=1e-11;//NOTES:eps

template<class T> inline void checkmin(T &a,T b){if(b<a) a=b;}//NOTES:checkmin(

template<class T> inline void checkmax(T &a,T b){if(b>a) a=b;}//NOTES:checkmax(

template<class T> inline T sqr(T x){return x*x;}//NOTES:sqr

template<class T> inline string bin(T n,int pos=62) { string s; T i,one=1,two=2; for (i = (one << (pos-1)); i > 0; i = (i>>1) ) s += ((n & i)?"1":"0"); return s; }

int lsb(unsigned int v){
	int r;           // result goes here
	static const int MultiplyDeBruijnBitPosition[32] =
	{
	  0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
	  31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
	};
	r = MultiplyDeBruijnBitPosition[((uint32_t)((v & -v) * 0x077CB531U)) >> 27];
	return r+1;
}

template<class T> inline int msb(T n){return (n==0)?0:(1+msb(n>>1));}//NOTES:msb(
template<class T> inline T getbit(T n,int pos){return ((n&(1<<pos))?1:0);}//NOTES:getbit(
template<class T> inline int countbit(T n){return (n==0)?0:(1+countbit(n&(n-1)));}//NOTES:countbit(
template<class T> inline void setbit(T &n,int p){ n = n | (1LL<<p); }

// Can also use popcount for CPU access
// int  __builtin_popcount(unsigned int)
// int  __builtin_popcountll(long long)

//Numberic Functions

template<class T> inline T gcd(T a,T b)//NOTES:gcd(
  {if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}

bool isequal(vector<int> a,vector<int> b){
	REP(i,a.size()) if(a[i]!=b[i]) return false;
	return true;
}

template<class T>
bool allequal(vector<T> a){
	sor(a);
	return a[0]==a[a.size()-1];
}

template<class T1,class T2>
void printpairs(vector< pair<T1,T2> > p){
    for(auto t:p){
        cout << "(" << t.first << " " << t.second << ")  ";
    }
    cout << endl;
}

template<class T>
void printvec(vector<T> a){
	REP(i,a.size())cout << a[i] << " ";
	cout<< endl;
}

void printarray(int *a,int n){
	REP(i,n)cout << *(a+i) << " ";
	cout<< endl;
}

template<typename T>
void printmatrix(vector< vector<T> > a,int n,int m,int width=5){
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cout << setw(width) << a[i][j] ;
		}
		cout << endl;
	}
}

template<typename T>
void readmatrix(vector< vector<T> > &a,int n,int m){
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cin >> a[i][j];
		}
	}
}

template<class T>
void printsubarray(vector<T> a,int start ,int end){
	if(start<0 or end>a.size()-1) cout << "Out of bounds" << endl;
	for(int i=start;i<=end;i++)cout << a[i] << " ";
	cout<< endl;
}

bool isprime(int n){
	if(n<2) return false;
	if(n==2) return true;
	for(int i=2;i*i<=n;i++){
		if(n%i==0) return false;
	}
	return true;
}

// Error checking

#define error(args...) { vector<string> _v; split(#args, ',', _v); err(_v.begin(), args); cerr<< endl; }

void split(const string& s, char c, vector<string>& result) {
    stringstream ss(s);
    string x;
    while (getline(ss, x, c))
        result.push_back(x);
}

void err(vector<string>::iterator it) {}
template<typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
    cerr << it -> substr((*it)[0] == ' ', it -> length()) << " = " << a << ' ';
    err(++it, args...);
}

/*
vector<vector<ll>> a(n, vector<ll>(m,0) );
readmatrix(a,n,m);
printmatrix(a,n,m);
*/

void yup(){ cout << "Yes" << endl; }
void nope(){ cout << "No" << endl; }
void Yup(){ cout << "YES" << endl; }
void Nope(){ cout << "NO" << endl; }

template<typename T>
void readvec(vector<T>& a,int size){
	T tmp;
	a.clear();
	REP(i,size){
		cin>>tmp;
		a.push_back(tmp);
	}
}

//ENDTEMPLATE_BY_ACRUSH_TOPCODER

int n,k;
int cost(VI &pos){
    int c=0;
    int n=pos.size();
    int start=0,end=n-1;
    int delcount1=0,delcount2=0;

    for(int i=1;i<n;i++){
        int gap=pos[i]-pos[i-1];
        c+=(gap-k)/k;
        if(c>i){
            start = i;
            delcount1 = i;
        }
    }

    c=0;
    for(int i=n-2;i>=0;i--){
        int gap=pos[i+1]-pos[i];
        c+=(gap-k)/k;
        if(c>n-1-i){
            end = i;
            delcount2 = n-1-i;
        }
    }

    /*
    error(start,end);
    error(delcount1,delcount2);
    cerr << endl;
    */

    if(start<end)
        c=delcount1+delcount2;
    else
        c=max(0,n-1);

    for(int i=start;i<end;i++){
        int gap=pos[i+1]-pos[i];
        c+=(gap-k)/k;
    }

    return c;
}

void solvecase(){
    cin >> n >> k;
    string s;
    cin >> s;
    VI p;
    int m=0;
    for(int i = 0;i<n;i++){
        if(s[i] == '1') { p.push_back(i); m++; }
    }
    //cout << m << k << endl;
    VI gap;
    int ans=0;
    VVI pos(k);
    map<int,int> freq;

    for(int x:p){
        freq[x%k]=0;
    }

    for(int x:p){
        freq[x%k]++;
        pos[x%k].push_back(x);
    }

    int best=max(0,m-1);
    for(auto it=freq.begin();it!=freq.end();it++){
        int mod = it->first;
        int cur = m-(it->second)+cost(pos[mod]);
        //cout << "m: " << m-(it->second) << " mod: " << mod << " cur: " << cur << "\n";
        best = min(cur,best);
    }
    cout << best << endl;

}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int cases=1;
  cin >> cases;
  while(cases--){
	  solvecase();
  }
  return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define MAX 10000
#define INF 1e100
#define isset(n,i) ((n>>i)&1LL)

#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define sor(A) sort(A.begin(),A.end())
#define rsor(A) sort(A.rbegin(),A.rend())
#define all(A) A.begin(),A.end()

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<long long ,long long> pll;
typedef pair<long long ,int> pli;
#define VI  vector<int>
#define VLL  vector<long long>
#define VVI vector<vector<int>>
#define VS  vector<string>
#define VVS vector<vector<string>>
#define X first
#define Y second

#define ASSERT(X) {if (!(X)) {printf("\n assertion failed at line %d\n",__LINE__);exit(0);}}

//BEGINTEMPLATE_BY_ACRUSH_TOPCODER

#define SZ(X) ((int)(X.size()))//NOTES:SIZE(

#define LENGTH(X) ((int)(X.length()))//NOTES:LENGTH(

#define two(X) (1<<(X))//NOTES:two(

#define twoL(X) (((unsigned long long)(1))<<(X))//NOTES:twoL(

#define contain(S,X) (((S)&two(X))!=0)//NOTES:contain(

#define containL(S,X) (((S)&twoL(X))!=0)//NOTES:containL(

const double pi=acos(-1.0);//NOTES:pi

const double eps=1e-11;//NOTES:eps

template<class T> inline void checkmin(T &a,T b){if(b<a) a=b;}//NOTES:checkmin(

template<class T> inline void checkmax(T &a,T b){if(b>a) a=b;}//NOTES:checkmax(

template<class T> inline T sqr(T x){return x*x;}//NOTES:sqr

template<class T> inline string bin(T n,int pos=62) { string s; T i,one=1,two=2; for (i = (one << (pos-1)); i > 0; i = (i>>1) ) s += ((n & i)?"1":"0"); return s; }

int lsb(unsigned int v){
	int r;           // result goes here
	static const int MultiplyDeBruijnBitPosition[32] =
	{
	  0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
	  31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
	};
	r = MultiplyDeBruijnBitPosition[((uint32_t)((v & -v) * 0x077CB531U)) >> 27];
	return r+1;
}

template<class T> inline int msb(T n){return (n==0)?0:(1+msb(n>>1));}//NOTES:msb(
template<class T> inline T getbit(T n,int pos){return ((n&(1<<pos))?1:0);}//NOTES:getbit(
template<class T> inline int countbit(T n){return (n==0)?0:(1+countbit(n&(n-1)));}//NOTES:countbit(
template<class T> inline void setbit(T &n,int p){ n = n | (1LL<<p); }

// Can also use popcount for CPU access
// int  __builtin_popcount(unsigned int)
// int  __builtin_popcountll(long long)

//Numberic Functions

template<class T> inline T gcd(T a,T b)//NOTES:gcd(
  {if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}

bool isequal(vector<int> a,vector<int> b){
	REP(i,a.size()) if(a[i]!=b[i]) return false;
	return true;
}

template<class T>
bool allequal(vector<T> a){
	sor(a);
	return a[0]==a[a.size()-1];
}

template<class T1,class T2>
void printpairs(vector< pair<T1,T2> > p){
    for(auto t:p){
        cout << "(" << t.first << " " << t.second << ")  ";
    }
    cout << endl;
}

template<class T>
void printvec(vector<T> a){
	REP(i,a.size())cout << a[i] << " ";
	cout<< endl;
}

void printarray(int *a,int n){
	REP(i,n)cout << *(a+i) << " ";
	cout<< endl;
}

template<typename T>
void printmatrix(vector< vector<T> > a,int n,int m,int width=5){
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cout << setw(width) << a[i][j] ;
		}
		cout << endl;
	}
}

template<typename T>
void readmatrix(vector< vector<T> > &a,int n,int m){
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cin >> a[i][j];
		}
	}
}

template<class T>
void printsubarray(vector<T> a,int start ,int end){
	if(start<0 or end>a.size()-1) cout << "Out of bounds" << endl;
	for(int i=start;i<=end;i++)cout << a[i] << " ";
	cout<< endl;
}

bool isprime(int n){
	if(n<2) return false;
	if(n==2) return true;
	for(int i=2;i*i<=n;i++){
		if(n%i==0) return false;
	}
	return true;
}

// Error checking

#define error(args...) { vector<string> _v; split(#args, ',', _v); err(_v.begin(), args); cerr<< endl; }

void split(const string& s, char c, vector<string>& result) {
    stringstream ss(s);
    string x;
    while (getline(ss, x, c))
        result.push_back(x);
}

void err(vector<string>::iterator it) {}
template<typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
    cerr << it -> substr((*it)[0] == ' ', it -> length()) << " = " << a << ' ';
    err(++it, args...);
}

/*
vector<vector<ll>> a(n, vector<ll>(m,0) );
readmatrix(a,n,m);
printmatrix(a,n,m);
*/

void yup(){ cout << "Yes" << endl; }
void nope(){ cout << "No" << endl; }
void Yup(){ cout << "YES" << endl; }
void Nope(){ cout << "NO" << endl; }

template<typename T>
void readvec(vector<T>& a,int size){
	T tmp;
	a.clear();
	REP(i,size){
		cin>>tmp;
		a.push_back(tmp);
	}
}

//ENDTEMPLATE_BY_ACRUSH_TOPCODER

int n,k;
int cost(VI &pos){
    int c=0;
    int n=pos.size();
    int start=0,end=n-1;
    int delcount1=0,delcount2=0;

    for(int i=1;i<n;i++){
        int gap=pos[i]-pos[i-1];
        c+=(gap-k)/k;
        if(c>i){
            start = i;
            delcount1 = i;
            c=i;
        }
    }

    c=0;
    for(int i=n-2;i>=start;i--){
        int gap=pos[i+1]-pos[i];
        c+=(gap-k)/k;
        if(c>n-1-i){
            end = i;
            delcount2 = n-1-i;
            c = delcount2;
        }
    }

    /*
    error(start,end);
    error(delcount1,delcount2);
    cerr << endl;
    */

    if(start<end)
        c=delcount1+delcount2;
    else{
        c=max(0,n-1);
        return c;
    }
    for(int i=start;i<end;i++){
        int gap=pos[i+1]-pos[i];
        c+=(gap-k)/k;
    }
    return c;
}

void solvecase(){
    cin >> n >> k;
    string s;
    cin >> s;
    VI p;
    int m=0;
    for(int i = 0;i<n;i++){
        if(s[i] == '1') { p.push_back(i); m++; }
    }
    //cout << m << k << endl;
    VI gap;
    int ans=0;
    VVI pos(k);
    map<int,int> freq;

    for(int x:p){
        freq[x%k]=0;
    }

    for(int x:p){
        freq[x%k]++;
        pos[x%k].push_back(x);
    }

    int best=max(0,m-1);
    for(auto it=freq.begin();it!=freq.end();it++){
        int mod = it->first;
        int cur = m-(it->second)+cost(pos[mod]);
        //cout << "m: " << m-(it->second) << " mod: " << mod << " cur: " << cur << "\n";
        best = min(cur,best);
    }
    cout << best << endl;

}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int cases=1;
  cin >> cases;
  while(cases--){
	  solvecase();
  }
  return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define MAX 10000
#define INF 1e100
#define isset(n,i) ((n>>i)&1LL)

#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define sor(A) sort(A.begin(),A.end())
#define rsor(A) sort(A.rbegin(),A.rend())
#define all(A) A.begin(),A.end()

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<long long ,long long> pll;
typedef pair<long long ,int> pli;
#define VI  vector<int>
#define VLL  vector<long long>
#define VVI vector<vector<int>>
#define VS  vector<string>
#define VVS vector<vector<string>>
#define X first
#define Y second

#define ASSERT(X) {if (!(X)) {printf("\n assertion failed at line %d\n",__LINE__);exit(0);}}

//BEGINTEMPLATE_BY_ACRUSH_TOPCODER

#define SZ(X) ((int)(X.size()))//NOTES:SIZE(

#define LENGTH(X) ((int)(X.length()))//NOTES:LENGTH(

#define two(X) (1<<(X))//NOTES:two(

#define twoL(X) (((unsigned long long)(1))<<(X))//NOTES:twoL(

#define contain(S,X) (((S)&two(X))!=0)//NOTES:contain(

#define containL(S,X) (((S)&twoL(X))!=0)//NOTES:containL(

const double pi=acos(-1.0);//NOTES:pi

const double eps=1e-11;//NOTES:eps

template<class T> inline void checkmin(T &a,T b){if(b<a) a=b;}//NOTES:checkmin(

template<class T> inline void checkmax(T &a,T b){if(b>a) a=b;}//NOTES:checkmax(

template<class T> inline T sqr(T x){return x*x;}//NOTES:sqr

template<class T> inline string bin(T n,int pos=62) { string s; T i,one=1,two=2; for (i = (one << (pos-1)); i > 0; i = (i>>1) ) s += ((n & i)?"1":"0"); return s; }

int lsb(unsigned int v){
	int r;           // result goes here
	static const int MultiplyDeBruijnBitPosition[32] =
	{
	  0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
	  31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
	};
	r = MultiplyDeBruijnBitPosition[((uint32_t)((v & -v) * 0x077CB531U)) >> 27];
	return r+1;
}

template<class T> inline int msb(T n){return (n==0)?0:(1+msb(n>>1));}//NOTES:msb(
template<class T> inline T getbit(T n,int pos){return ((n&(1<<pos))?1:0);}//NOTES:getbit(
template<class T> inline int countbit(T n){return (n==0)?0:(1+countbit(n&(n-1)));}//NOTES:countbit(
template<class T> inline void setbit(T &n,int p){ n = n | (1LL<<p); }

// Can also use popcount for CPU access
// int  __builtin_popcount(unsigned int)
// int  __builtin_popcountll(long long)

//Numberic Functions

template<class T> inline T gcd(T a,T b)//NOTES:gcd(
  {if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}

bool isequal(vector<int> a,vector<int> b){
	REP(i,a.size()) if(a[i]!=b[i]) return false;
	return true;
}

template<class T>
bool allequal(vector<T> a){
	sor(a);
	return a[0]==a[a.size()-1];
}

template<class T1,class T2>
void printpairs(vector< pair<T1,T2> > p){
    for(auto t:p){
        cout << "(" << t.first << " " << t.second << ")  ";
    }
    cout << endl;
}

template<class T>
void printvec(vector<T> a){
	REP(i,a.size())cout << a[i] << " ";
	cout<< endl;
}

void printarray(int *a,int n){
	REP(i,n)cout << *(a+i) << " ";
	cout<< endl;
}

template<typename T>
void printmatrix(vector< vector<T> > a,int n,int m,int width=5){
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cout << setw(width) << a[i][j] ;
		}
		cout << endl;
	}
}

template<typename T>
void readmatrix(vector< vector<T> > &a,int n,int m){
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cin >> a[i][j];
		}
	}
}

template<class T>
void printsubarray(vector<T> a,int start ,int end){
	if(start<0 or end>a.size()-1) cout << "Out of bounds" << endl;
	for(int i=start;i<=end;i++)cout << a[i] << " ";
	cout<< endl;
}

bool isprime(int n){
	if(n<2) return false;
	if(n==2) return true;
	for(int i=2;i*i<=n;i++){
		if(n%i==0) return false;
	}
	return true;
}

// Error checking

#define error(args...) { vector<string> _v; split(#args, ',', _v); err(_v.begin(), args); cerr<< endl; }

void split(const string& s, char c, vector<string>& result) {
    stringstream ss(s);
    string x;
    while (getline(ss, x, c))
        result.push_back(x);
}

void err(vector<string>::iterator it) {}
template<typename T, typename... Args>
void err(vector<string>::iterator it, T a, Args... args) {
    cerr << it -> substr((*it)[0] == ' ', it -> length()) << " = " << a << ' ';
    err(++it, args...);
}

/*
vector<vector<ll>> a(n, vector<ll>(m,0) );
readmatrix(a,n,m);
printmatrix(a,n,m);
*/

void yup(){ cout << "Yes" << endl; }
void nope(){ cout << "No" << endl; }
void Yup(){ cout << "YES" << endl; }
void Nope(){ cout << "NO" << endl; }

template<typename T>
void readvec(vector<T>& a,int size){
	T tmp;
	a.clear();
	REP(i,size){
		cin>>tmp;
		a.push_back(tmp);
	}
}

//ENDTEMPLATE_BY_ACRUSH_TOPCODER

int n,k;
int cost(VI &pos){
    int c=0;
    int n=pos.size();
    int start=0,end=n-1;
    int delcount1=0,delcount2=0;

    for(int i=1;i<n;i++){
        int gap=pos[i]-pos[i-1];
        c+=(gap-k)/k;
        if(c>i){
            start = i;
            delcount1 = i;
        }
    }

    c=0;
    for(int i=n-2;i>=0;i--){
        int gap=pos[i+1]-pos[i];
        c+=(gap-k)/k;
        if(c>n-1-i){
            end = i;
            delcount2 = n-1-i;
        }
    }

    /*
    error(start,end);
    error(delcount1,delcount2);
    cerr << endl;
    */

    if(start<end)
        c=delcount1+delcount2;
    else
        c=max(0,n-1);

    for(int i=start;i<end;i++){
        int gap=pos[i+1]-pos[i];
        c+=(gap-k)/k;
    }

    return c;
}

void solvecase(){
    cin >> n >> k;
    string s;
    cin >> s;
    VI p;
    int m=0;
    for(int i = 0;i<n;i++){
        if(s[i] == '1') { p.push_back(i); m++; }
    }
    //cout << m << k << endl;
    VI gap;
    int ans=0;
    VVI pos(k);
    map<int,int> freq;

    for(int x:p){
        freq[x%k]=0;
    }

    for(int x:p){
        freq[x%k]++;
        pos[x%k].push_back(x);
    }

    int best=1e9;
    for(auto it=freq.begin();it!=freq.end();it++){
        int mod = it->first;
        int cur = m-(it->second)+cost(pos[mod]);
        //cout << "m: " << m-(it->second) << " mod: " << mod << " cur: " << cur << "\n";
        best = min(cur,best);
    }
    if(best!=1e9)
        cout << best << endl;
    else
        cout << 0 << endl;

}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int cases=1;
  cin >> cases;
  while(cases--){
	  solvecase();
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1123456;

int cnt[MAX];
int maxr[MAX], minn[MAX];

int main()
{
    int T;
    cin >> T;
    memset(maxr, -1, sizeof(maxr));
    memset(minn, 0x3f, sizeof(minn));

    while (T--) {
        int n, k;
        cin >> n >> k;
        int sum = 0, ans = n;
        for (int i = 0; i < n; ++i) {
            int t;
            scanf("%1d", &t);
            cnt[i % k] += t;
            if (t) {
                maxr[i % k] = max(maxr[i % k], i);
                minn[i % k] = min(minn[i % k], i);
            }

            sum += t;
        }

        for (int i = 0; i < k; ++i) {

            int need = 0;
            if (maxr[i] != -1 && minn[i] < n) need = (maxr[i] - minn[i] + 1 + (k - 1)) / k;

            ans = min(ans, sum - cnt[i] + (need - cnt[i]));

            cnt[i] = 0;
            maxr[i] = -1;
            minn[i] = 0x3f3f3f3f;
        }
        cout << ans << endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 212345;

int cnt[MAX];
int maxr[MAX], minn[MAX];

int main()
{
    int T;
    cin >> T;
    memset(maxr, -1, sizeof(maxr));
    memset(minn, 0x3f, sizeof(minn));

    while (T--) {
        int n, k;
        cin >> n >> k;
        int sum = 0, ans = n;
        for (int i = 0; i < n; ++i) {
            int t;
            scanf("%1d", &t);
            cnt[i % k] += t;
            if (t) {
                maxr[i % k] = max(maxr[i % k], i);
                minn[i % k] = min(minn[i % k], i);
            }

            sum += t;
        }

        for (int i = 0; i < k; ++i) {

            int need = 0;
            if (maxr[i] != -1 && minn[i] < n) need = (maxr[i] - minn[i] + 1 + (k - 1)) / k;

//            printf("%d %d %d %d\n", i, need, cnt[i], maxr[i]);
            ans = min(ans, sum - cnt[i] + (need - cnt[i]));

            cnt[i] = 0;
            maxr[i] = -1;
            minn[i] = 0x3f3f3f3f;
        }
        cout << ans << endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1123456;

int cnt[MAX];
int maxr[MAX], minn[MAX];

int main()
{
    int T;
    cin >> T;
    memset(maxr, -1, sizeof(maxr));
    memset(minn, 0x3f, sizeof(minn));

    while (T--) {
        int n, k;
        cin >> n >> k;
        int sum = 0, ans = n;
        for (int i = 0; i < n; ++i) {
            int t;
            scanf("%1d", &t);
            cnt[i % k] += t;
            if (t) {
                maxr[i % k] = max(maxr[i % k], i);
                minn[i % k] = min(minn[i % k], i);
            }

            sum += t;
        }

        ans = sum;

        for (int i = 0; i < k; ++i) {

            int need = 0;
            if (maxr[i] != -1 && minn[i] < n) need = (maxr[i] - minn[i] + 1 + (k - 1)) / k;

            ans = min(ans, max(0, cnt[i] - 1) + (sum - cnt[i]));

            ans = min(ans, sum - cnt[i] + (need - cnt[i]));

            cnt[i] = 0;
            maxr[i] = -1;
            minn[i] = 0x3f3f3f3f;
        }
        cout << ans << endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1123456;

int cnt[MAX];
int data[MAX];

int main()
{
    int T;
    cin >> T;

    while (T--) {
        int n, k;
        cin >> n >> k;
        int sum = 0, ans = n;

        for (int i = 0; i < n; ++i) {
            int t;
            scanf("%1d", &t);
            cnt[i % k] += t;
            data[i] = t;

            sum += t;
        }

        ans = sum;

        for (int i = 0; i < k; ++i) {
            int oo = 0;
            int tans = 0;

            for (int j = i; j < n; j += k) {
                int v;
                if (data[j]) v = 1;
                else v = -1;
                tans = max(v, tans + v);
                oo = max(tans, oo);
            }

//            printf("%d\n", tans);

            oo -= cnt[i];
            oo *= -1;

            ans = min(oo + (sum - cnt[i]), ans);

            cnt[i] = 0;
        }

        cout << ans << endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1123456;

int cnt[MAX];
int data[MAX];

int main()
{
    int T;
    cin >> T;

    while (T--) {
        int n, k;
        cin >> n >> k;
        int sum = 0, ans = n;

        for (int i = 0; i < n; ++i) {
            int t;
            scanf("%1d", &t);
            cnt[i % k] += t;
            data[i] = t;

            sum += t;
        }

        ans = sum;

        for (int i = 0; i < k; ++i) {
            int oo = 0;
            int tans = 0;

            for (int j = i; j < n; j += k) {
                int v;
                if (data[j]) v = -1;
                else v = 1;
                tans = min(v, tans + v);
                oo = min(tans, oo);
            }

            oo += cnt[i];

            ans = min(oo + (sum - cnt[i]), ans);

            cnt[i] = 0;
        }

        cout << ans << endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 212345;

int cnt[MAX];
int maxr[MAX];

int main()
{
    int T;
    cin >> T;
    memset(maxr, -1, sizeof(maxr));
    while (T--) {
        int n, k;
        cin >> n >> k;
//        printf("%d %d\n", n, k);
        int sum = 0, ans = n;
        for (int i = 0; i < n; ++i) {
            int t;
            scanf("%1d", &t);
            cnt[i % k] += t;
            if (t)
            maxr[i % k] = max(maxr[i % k], i);
            sum += t;
        }

        for (int i = 0; i < k; ++i) {

            int need = 0;
            if (maxr[i] != -1) need = (maxr[i] - i + 1 + (k - 1)) / k;

//            printf("%d %d %d %d\n", i, need, cnt[i], maxr[i]);
            ans = min(ans, sum - cnt[i] + (need - cnt[i]));

            cnt[i] = 0;
            maxr[i] = -1;
        }
        cout << ans << endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1123456;

int cnt[MAX];
int data[MAX];

int main()
{
    int T;
    cin >> T;

    while (T--) {
        int n, k;
        cin >> n >> k;
        int sum = 0, ans = n;

        for (int i = 0; i < n; ++i) {
            int t;
            scanf("%1d", &t);
            cnt[i % k] += t;
            data[i] = t;

            sum += t;
        }

        ans = sum;

        for (int i = 0; i < k; ++i) {
            int tans = 0;

            for (int j = i; j < n; j += k) {
                int v;
                if (data[j]) v = 1;
                else v = -1;
                tans = max(v, tans + v);
            }

            tans -= cnt[i];
            tans *= -1;

            ans = min(tans + (sum - cnt[i]), ans);

            cnt[i] = 0;
        }

        cout << ans << endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1123456;

int cnt[MAX];
int data[MAX];
int maxr[MAX], minn[MAX];

int main()
{
    int T;
    cin >> T;
    memset(maxr, -1, sizeof(maxr));
    memset(minn, 0x3f, sizeof(minn));

    while (T--) {
        int n, k;
        cin >> n >> k;
        int sum = 0, ans = n;
        for (int i = 0; i < n; ++i) {
            int t;
            scanf("%1d", &t);
            cnt[i % k] += t;
            data[i] = t;
            if (t) {
                maxr[i % k] = max(maxr[i % k], i);
                minn[i % k] = min(minn[i % k], i);
            }

            sum += t;
        }

        ans = sum;

        for (int i = 0; i < k; ++i) {
            int tans = 0;
            if (maxr[i] != -1) {
                int pre = 0;
                for (int j = minn[i]; j <= maxr[i]; j += k) {
                    int v = 0;
                    if (data[j]) v = 1;
                    else v = -1;
                    tans = max(v, tans + v);
                }
                tans -= cnt[i];
                tans *= -1;
            }

//            printf("%d %d\n", tans, cnt[i]);

            ans = min(tans + (sum - cnt[i]), ans);

            cnt[i] = 0;
            maxr[i] = -1;
            minn[i] = 0x3f3f3f3f;
        }

        cout << ans << endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

const int MAX = 1123456;

int cnt[MAX];
int data[MAX];
int maxr[MAX], minn[MAX];

int main()
{
    int T;
    cin >> T;
    memset(maxr, -1, sizeof(maxr));
    memset(minn, 0x3f, sizeof(minn));

    while (T--) {
        int n, k;
        cin >> n >> k;
        int sum = 0, ans = n;
        for (int i = 0; i < n; ++i) {
            int t;
            scanf("%1d", &t);
            cnt[i % k] += t;
            data[i] = t;
            if (t) {
                maxr[i % k] = max(maxr[i % k], i);
                minn[i % k] = min(minn[i % k], i);
            }

            sum += t;
        }

        ans = sum;

        for (int i = 0; i < k; ++i) {
            int tans = 0;
            if (maxr[i] != -1) {
                for (int j = minn[i]; j <= maxr[i]; j += k) {
                    int v;
                    if (data[j]) v = 1;
                    else v = -1;
                    tans = max(v, tans + v);
                }
                tans -= cnt[i];
                tans *= -1;
            }

            ans = min(tans + (sum - cnt[i]), ans);

            int need = 0;
            if (maxr[i] != -1 && minn[i] < n) need = (maxr[i] - minn[i] + 1 + (k - 1)) / k;

            ans = min(ans, max(0, cnt[i] - 1) + (sum - cnt[i]));

            ans = min(ans, sum - cnt[i] + (need - cnt[i]));

            cnt[i] = 0;
            maxr[i] = -1;
            minn[i] = 0x3f3f3f3f;
        }

        cout << ans << endl;
    }
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<bitset>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<map>

using namespace std;

int read()
{
	int ans=0,f=1;
	char c=getchar();
	while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();}
	return ans*f;
}

const int N=1e6+5,inf=0x7f7f7f7f;
int t,n,k,dp[N][2],sum[N],ans;
char s[N];

int main()
{
	memset(dp,0x7f,sizeof(dp));
	t=read();
	while(t--)
	{
		n=read();k=read();
		scanf("%s",s+1);
		for(int i=1;i<=n;++i)
			sum[i]=sum[i-1]+(s[i]-'0');
		ans=sum[n];
		for(int i=1;i<=n;++i)
		{
			dp[i][0]=sum[i-1];
			if(i-k>=1)
				dp[i][1]=min(dp[i-k][1]+sum[i-1]-sum[i-k],dp[i-k][0]+sum[i-1]-sum[i-k]);
			if(s[i]=='0')
			{
				if(dp[i][0]!=inf)
					dp[i][0]++;
				if(dp[i][1]!=inf)
					dp[i][1]++;
			}

		}
		int now=0;
		for(int i=n;i>=1;--i)
		{
			ans=min(ans,min(dp[i][1]+now,dp[i][0]+now));
			if(s[i]=='1')
				now++;
		}

		for(int i=1;i<=n;++i)
		{
			sum[i]=0;
			dp[i][0]=inf;
			dp[i][1]=inf;
		}
		printf("%d\n",ans);
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<bitset>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<map>

using namespace std;

int read()
{
	int ans=0,f=1;
	char c=getchar();
	while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();}
	return ans*f;
}

int T,n,k,ans;
char s[40],t[40];

void dfs(int u);

int main()
{
	freopen("yangli.in","r",stdin);
	freopen("baoli.out","w",stdout);
	T=read();
	while(T--)
	{
		ans=0x7fffffff;
		n=read();k=read();
		scanf("%s",s+1);
		dfs(1);
		printf("%d\n",ans);
	}

	return 0;
}

void dfs(int u)
{
	if(u==n+1)
	{
		int res=0,now=-1;
		for(int i=1;i<=n;++i)
		{
			if(t[i]=='1')
			{
				if(now!=-1&&now!=k-1)
					return;
				else
					now=0;
			}
			else
			{
				if(now!=-1)
					now++;
			}
			if(s[i]!=t[i])
				res++;
		}

		ans=min(ans,res);
		return;
	}
	t[u]='0';
	dfs(u+1);
	t[u]='1';
	dfs(u+1);
}


#include<bits/stdc++.h>

using namespace std;

int read()
{
    int ans=0,f=1;
    char c=getchar();
    while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();}
    return ans*f;
}

const int N=1e6+5,inf=0x7f7f7f7f;
int t,n,k,dp[N][2],sum[N],ans;
char s[N];

int main()
{
    memset(dp,0x7f,sizeof(dp));
    // ½« dp Êý×é³õÊ¼»¯£¬½«ËùÓÐ¿ÉÄÜÏÈÉèÎª²»ºÏ·¨
    t=read();
    while(t--)
    {
        n=read();k=read();
        scanf("%s",s+1);
        for(int i=1;i<=n;++i)
        // Í³¼ÆÇ°×ººÍ
            sum[i]=sum[i-1]+(s[i]-'0');
        ans=sum[n];  // ÕâÀïµÄÒâË¼ÊÇ°Ñ´ð°¸³õÊ¼»¯Îª£º°ÑËùÓÐµÄ 1 ¶¼±ä³É 0 ËùÐèÒªµÄ²Ù×÷´ÎÊý
        for(int i=1;i<=n;++i)
        {
            dp[i][0]=sum[i-1];
            // dp[i][0] µÄ×ªÒÆ
            if(i-k>=1)
                dp[i][1]=min(dp[i-k][1]+sum[i-1]-sum[i-k],dp[i-k][0]+sum[i-1]-sum[i-k]);
            // dp[i][1] µÄ×ªÒÆ
            if(s[i]=='0')
            {
                if(dp[i][0]!=inf)
                    dp[i][0]++;
                if(dp[i][1]!=inf)
                    dp[i][1]++;
            }

        }
        int num=0;
        // Í³¼Æ´ð°¸µÄÊ±ºòÎ¬»¤Ò»¸ö num£¬·ÀÖ¹³öÏÖ²»ºÏ·¨Çé¿ö
        for(int i=n;i>=1;--i)
        {
            ans=min(ans,min(dp[i][1]+num,dp[i][0]+num));
            if(s[i]=='1')
                num++;
        }

        for(int i=1;i<=n;++i)
        // ÕâÊÇÕâÌâµÄÒ»¸ö¿Óµã£¬Ö±½Ó memset »á T£¬±ØÐëÊÖ¶¯Çå¿Õ
        {
            sum[i]=0;
            dp[i][0]=inf;
            dp[i][1]=inf;
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<bitset>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<map>

using namespace std;

int read()
{
	int ans=0,f=1;
	char c=getchar();
	while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();}
	return ans*f;
}

const int N=1e6+5,inf=0x7f7f7f7f;
int t,n,k,dp[N][2],sum[N],ans;
char s[N];

int main()
{
	memset(dp,0x7f,sizeof(dp));
	t=read();
	while(t--)
	{
		n=read();k=read();
		scanf("%s",s+1);
		ans=inf;
		for(int i=1;i<=n;++i)
			sum[i]=sum[i-1]+(s[i]-'0');
		for(int i=1;i<=n;++i)
		{
			dp[i][0]=sum[i]-1;
			if(i-k>=1)
				dp[i][1]=min(dp[i-k][1]+sum[i-1]-sum[i-k],dp[i-k][0]+sum[i-1]-sum[i-k]);
			if(s[i]=='0')
			{
				if(dp[i][0]!=inf)
					dp[i][0]++;
				if(dp[i][1]!=inf)
					dp[i][1]++;
			}

		}
		for(int i=n;i>=1;--i)
			if(s[i]=='1')
			{
				ans=dp[i][1];
				break;
			}
		for(int i=1;i<=n;++i)
		{
			sum[i]=0;
			dp[i][0]=inf;
			dp[i][1]=inf;
		}
		if(ans==inf)
			printf("0\n");
		else
			printf("%d\n",ans);
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<bitset>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<map>

using namespace std;

int read()
{
	int ans=0,f=1;
	char c=getchar();
	while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();}
	return ans*f;
}

const int N=1e6+5;
int t,n,k,dp[N][2],sum[N],ans;
char s[N];

int main()
{
	t=read();
	while(t--)
	{
		n=read();k=read();
		scanf("%s",s+1);
		memset(sum,0,sizeof(sum));
		memset(dp,0x7f,sizeof(dp));
		ans=0x7f7f7f7f;
		for(int i=1;i<=n;++i)
			sum[i]=sum[i-1]+(s[i]-'0');
		for(int i=1;i<=n;++i)
		{
			dp[i][0]=sum[i];
			if(i-k>=1)
				dp[i][1]=min(dp[i-k][1]+sum[i-1]-sum[i-k],dp[i-k][0]+sum[i-1]-sum[i-k]);
			if(s[i]=='0')
			{
				dp[i][0]++;
				dp[i][1]++;
			}

		}
		for(int i=n;i>=1;--i)
			if(s[i]=='1')
			{
				ans=dp[i][1];
				break;
			}
		if(ans==0x7f7f7f7f)
			printf("0\n");
		else
			printf("%d\n",ans-1);
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

int read()
{
    int ans=0,f=1;
    char c=getchar();
    while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();}
    return ans*f;
}

const int N=1e6+5,inf=0x7f7f7f7f;
int t,n,k,dp[N][2],sum[N],ans;
char s[N];

int main()
{
    memset(dp,0x7f,sizeof(dp));
    // 将 dp 数组初始化，将所有可能先设为不合法
    t=read();
    while(t--)
    {
        n=read();k=read();
        scanf("%s",s+1);
        for(int i=1;i<=n;++i)
        // 统计前缀和
            sum[i]=sum[i-1]+(s[i]-'0');
        ans=sum[n]  // 这里的意思是把答案初始化为：把所有的 1 都变成 0 所需要的操作次数
        for(int i=1;i<=n;++i)
        {
            dp[i][0]=sum[i-1];
            // dp[i][0] 的转移
            if(i-k>=1)
                dp[i][1]=min(dp[i-k][1]+sum[i-1]-sum[i-k],dp[i-k][0]+sum[i-1]-sum[i-k]);
            // dp[i][1] 的转移
            if(s[i]=='0')
            {
                if(dp[i][0]!=inf)
                    dp[i][0]++;
                if(dp[i][1]!=inf)
                    dp[i][1]++;
            }

        }
        int num=0;
        // 统计答案的时候维护一个 num，防止出现不合法情况
        for(int i=n;i>=1;--i)
        {
            ans=min(ans,min(dp[i][1]+num,dp[i][0]+num));
            if(s[i]=='1')
                now++;
        }

        for(int i=1;i<=n;++i)
        // 这是这题的一个坑点，直接 memset 会 T，必须手动清空
        {
            sum[i]=0;
            dp[i][0]=inf;
            dp[i][1]=inf;
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<bitset>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<map>

using namespace std;

int read()
{
	int ans=0,f=1;
	char c=getchar();
	while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();}
	return ans*f;
}

const int N=1e6+5;
int t,n,k,dp[N][2],sum[N],len,ans;
char s[N];

int main()
{
	t=read();
	while(t--)
	{
		n=read();k=read();
		scanf("%s",s+1);
		len=strlen(s+1);
		memset(sum,0,sizeof(sum));
		memset(dp,0x7f,sizeof(dp));
		ans=0x7f7f7f7f;
		for(int i=1;i<=len;++i)
			sum[i]=sum[i-1]+(s[i]-'0');
		for(int i=1;i<=len;++i)
		{
			dp[i][0]=sum[i];
			if(i-k>=1)
				dp[i][1]=min(dp[i-k][1]+sum[i-1]-sum[i-k],dp[i-k][0]+sum[i-1]-sum[i-k]);
			if(s[i]=='0')
			{
				dp[i][0]++;
				dp[i][1]++;
			}

		}
		for(int i=len;i>=1;--i)
			if(s[i]=='1')
			{
				ans=dp[i][1];
				break;
			}
		if(ans==0x7f7f7f7f)
			printf("0\n");
		else
			printf("%d\n",ans-1);
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<bitset>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<map>

using namespace std;

int read()
{
	int ans=0,f=1;
	char c=getchar();
	while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();}
	return ans*f;
}

const int N=1e6+5,inf=0x7f7f7f7f;
int t,n,k,dp[N][2],sum[N],ans;
char s[N];

int main()
{
	memset(dp,0x7f,sizeof(dp));
	t=read();
	while(t--)
	{
		n=read();k=read();
		scanf("%s",s+1);
		ans=inf;
		for(int i=1;i<=n;++i)
			sum[i]=sum[i-1]+(s[i]-'0');
		for(int i=1;i<=n;++i)
		{
			dp[i][0]=sum[i-1];
			if(i-k>=1)
				dp[i][1]=min(dp[i-k][1]+sum[i-1]-sum[i-k],dp[i-k][0]+sum[i-1]-sum[i-k]);
			if(s[i]=='0')
			{
				if(dp[i][0]!=inf)
					dp[i][0]++;
				if(dp[i][1]!=inf)
					dp[i][1]++;
			}

		}
		int now=0;
		for(int i=n;i>=1;--i)
		{
			ans=min(ans,min(dp[i][1]+now,dp[i][0]+now));
			if(s[i]=='1')
				now++;
		}

		for(int i=1;i<=n;++i)
		{
			sum[i]=0;
			dp[i][0]=inf;
			dp[i][1]=inf;
		}
		if(ans==inf)
			printf("0\n");
		else
			printf("%d\n",ans);
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<bitset>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<map>

using namespace std;

int read()
{
	int ans=0,f=1;
	char c=getchar();
	while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();}
	return ans*f;
}

const int N=1e6+5,inf=0x7f7f7f7f;
int t,n,k,dp[N][2],sum[N],ans;
char s[N];

int main()
{
	memset(dp,0x7f,sizeof(dp));
	t=read();
	while(t--)
	{
		n=read();k=read();
		scanf("%s",s+1);
		ans=inf;
		for(int i=1;i<=n;++i)
			sum[i]=sum[i-1]+(s[i]-'0');
		for(int i=1;i<=n;++i)
		{
			dp[i][0]=sum[i]-1;
			if(i-k>=1)
				dp[i][1]=min(dp[i-k][1]+sum[i-1]-sum[i-k],dp[i-k][0]+sum[i-1]-sum[i-k]);
			if(s[i]=='0')
			{
				if(dp[i][0]!=inf)
					dp[i][0]++;
				if(dp[i][1]!=inf)
					dp[i][1]++;
			}

		}
		int now=0;
		for(int i=n;i>=1;--i)
		{
			ans=min(ans,min(dp[i][1]+now,dp[i][0]+now));
			if(s[i]=='1')
				now++;
		}

		for(int i=1;i<=n;++i)
		{
			sum[i]=0;
			dp[i][0]=inf;
			dp[i][1]=inf;
		}
		if(ans==inf)
			printf("0\n");
		else
			printf("%d\n",ans);
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<bitset>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<map>

using namespace std;

int read()
{
	int ans=0,f=1;
	char c=getchar();
	while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();}
	return ans*f;
}

const int N=1e6+5,inf=0x7f7f7f7f;
int t,n,k,dp[N][2],sum[N],ans;
char s[N];

int main()
{
	memset(dp,0x7f,sizeof(dp));
	t=read();
	while(t--)
	{
		n=read();k=read();
		scanf("%s",s+1);
		ans=inf;
		for(int i=1;i<=n;++i)
			sum[i]=sum[i-1]+(s[i]-'0');
		for(int i=1;i<=n;++i)
		{
			dp[i][0]=sum[i-1];
			if(i-k>=1)
				dp[i][1]=min(dp[i-k][1]+sum[i-1]-sum[i-k],dp[i-k][0]+sum[i-1]-sum[i-k]);
			if(s[i]=='0')
			{
				if(dp[i][0]!=inf)
					dp[i][0]++;
				if(dp[i][1]!=inf)
					dp[i][1]++;
			}

		}
		int now=0;
		for(int i=n;i>=1;--i)
		{
			ans=min(ans,min(dp[i][1]+now,dp[i][0]+now));
			if(s[i]=='1')
				now++;
		}

		for(int i=1;i<=n;++i)
		{
			sum[i]=0;
			dp[i][0]=inf;
			dp[i][1]=inf;
		}
		printf("%d\n",ans);
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<bitset>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<map>

using namespace std;

int read()
{
	int ans=0,f=1;
	char c=getchar();
	while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();}
	return ans*f;
}

const int N=1e6+5;
int t,n,k,dp[N][2],sum[N],ans;
char s[N];

int main()
{
	memset(dp,0x7f,sizeof(dp));
	t=read();
	while(t--)
	{
		n=read();k=read();
		scanf("%s",s+1);
		ans=0x7f7f7f7f;
		for(int i=1;i<=n;++i)
			sum[i]=sum[i-1]+(s[i]-'0');
		for(int i=1;i<=n;++i)
		{
			dp[i][0]=sum[i];
			if(i-k>=1)
				dp[i][1]=min(dp[i-k][1]+sum[i-1]-sum[i-k],dp[i-k][0]+sum[i-1]-sum[i-k]);
			if(s[i]=='0')
			{
				dp[i][0]++;
				dp[i][1]++;
			}

		}
		for(int i=n;i>=1;--i)
			if(s[i]=='1')
			{
				ans=dp[i][1];
				break;
			}
		for(int i=1;i<=n;++i)
		{
			sum[i]=0;
			dp[i][0]=0x7f7f7f7f;
			dp[i][1]=0x7f7f7f7f;
		}
		if(ans==0x7f7f7f7f)
			printf("0\n");
		else
			printf("%d\n",ans-1);
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <numeric>
#include <list>
#include <string>
#define ll long long
using namespace std;
#define MAXN 100001
#define INF 0x3f3f3f3f

vector<int> nums;
int cache[1000011][3];

int ff(int n, int status)
{
    int &ret = cache[n][status];
    if (ret != -1)
        return ret;
    if (n == nums.size() - 1)
    {
        if (status == 0)
            return ret = 0;
        else if (status == 1)
            return ret = 0;
        else
            return ret = (nums[n] == 1);
    }
    if (status == 0)
    {
        return ret = min(
                   min(
                       ff(n + 1, 0) + (nums[n] == 1),
                       ff(n + 1, 1) + (nums[n] == 0)),
                   ff(n + 1, 2) + (nums[n] == 0));
    }
    if (status == 1)
    {
        return ret = min(ff(n + 1, 1) + (nums[n] == 0),
                         ff(n + 1, 2) + (nums[n] == 1));
    }
    else
        return ret = ff(n + 1, 2) + (nums[n] == 1);
}

int single_line(vector<int> v, int one_num)
{
    nums = v;
    for (int i = 0; i < v.size() + 1; i++)
        for (int j = 0; j < 3; j++)
            cache[i][j] = -1;
    return ff(0, 0);
}

int f(int n, int k, string str)
{
    vector<vector<int>> vec(k);
    vector<int> ones(k);
    for (int i = 0; i < n; i++)
    {
        vec[i % k].push_back(str[i] == '1');
        if (str[i] == '1')
            ones[i % k]++;
    }

    int one_num = 0;
    for (int i = 0; i < k; i++)
        one_num += ones[i];

    int result = 1e9;
    for (int i = 0; i < k; i++)
    {
        int single_line_result = single_line(vec[i], ones[i]);
        int full_line_result = single_line_result + one_num - ones[i];
        if (result > full_line_result)
            result = full_line_result;
    }
    return result;
}

int main()
{
    int tc;
    cin >> tc;
    while (tc--)
    {
        int n, k;
        cin >> n >> k;
        string str;
        cin >> str;
        cout << f(n, k, str) << '\n';
    }
}


#include <iostream>
#include <vector>
#include <numeric>
#include <list>
#include <string>
#define ll long long
using namespace std;
#define MAXN 100001
#define INF 0x3f3f3f3f

vector<int> nums;

int ff(int n, int status)
{
    if (n == nums.size() - 1)
    {
        if (status == 0)
            return 0;
        else if (status == 1)
            return 0;
        else
            return nums[n] == 1;
    }
    if (status == 0)
    {
        return min(
            min(
                ff(n + 1, 0) + (nums[n] == 1),
                ff(n + 1, 1) + (nums[n] == 0)),
            ff(n + 1, 2) + (nums[n] == 0));
    }
    if (status == 1)
    {
        return min(ff(n + 1, 1) + (nums[n] == 0),
                   ff(n + 1, 2) + (nums[n] == 1));
    }
    else
        return ff(n + 1, 2) + (nums[n] == 1);
}

int single_line(vector<int> v, int one_num)
{
    nums = v;
    return ff(0, 0);
}

int f(int n, int k, string str)
{
    vector<vector<int>> vec(k);
    vector<int> ones(k);
    for (int i = 0; i < n; i++)
    {
        vec[i % k].push_back(str[i] == '1');
        if (str[i] == '1')
            ones[i % k]++;
    }

    int one_num = 0;
    for (int i = 0; i < k; i++)
        one_num += ones[i];

    int result = 1e9;
    for (int i = 0; i < k; i++)
    {
        int single_line_result = single_line(vec[i], ones[i]);
        int full_line_result = single_line_result + one_num - ones[i];
        if (result > full_line_result)
            result = full_line_result;
    }
    return result;
}

int main()
{
    int tc;
    cin >> tc;
    while (tc--)
    {
        int n, k;
        cin >> n >> k;
        string str;
        cin >> str;
        cout << f(n, k, str) << '\n';
    }
}


#include <iostream>
#include <vector>
#include <numeric>
#include <list>
#include <string>
#define ll long long
using namespace std;
#define MAXN 100001
#define INF 0x3f3f3f3f

int single_line(vector<int> v, int one_num)
{
    int cur = 0, big = 0;
    for (int i = 0; i < v.size(); i++)
    {
        if (v[i])
            cur++;
        else
            cur = 0;
        big = max(big, cur);
    }
    return one_num - big;
}

int f(int n, int k, string str)
{
    vector<vector<int>> vec(k);
    vector<int> ones(k);
    for (int i = 0; i < n; i++)
    {
        vec[i % k].push_back(str[i] == '1');
        if (str[i] == '1')
            ones[i % k]++;
    }

    int one_num = 0;
    for (int i = 0; i < k; i++)
        one_num += ones[i];

    int result = 1e9;
    for (int i = 0; i < k; i++)
    {
        int single_line_result = single_line(vec[i], ones[i]);
        int full_line_result = single_line_result + one_num - ones[i];
        if (result > full_line_result)
            result = full_line_result;
    }
    return result;
}

int main()
{
    int tc;
    cin >> tc;
    while (tc--)
    {
        int n, k;
        cin >> n >> k;
        string str;
        cin >> str;
        cout << f(n, k, str) << '\n';
    }
}


#include <iostream>
#include <vector>
#include <numeric>
#include <list>
#include <string>
#define ll long long
using namespace std;
#define MAXN 100001
#define INF 0x3f3f3f3f

vector<int> nums;

int ff(int n, int status)
{
    if (n == nums.size() - 1)
    {
        if (status == 0)
            return 0;
        else if (status == 1)
            return 0;
        else
            return nums[n] == 1;
    }
    if (status == 0)
    {
        return min(
            min(
                ff(n + 1, 0) + nums[n] == 1,
                ff(n + 1, 1) + nums[n] == 0),
            ff(n + 1, 2) + nums[n] == 0);
    }
    if (status == 1)
    {
        return min(ff(n + 1, 1) + nums[n] == 0,
                   ff(n + 1, 2) + nums[n] == 1);
    }
    if (status == 2)
        return ff(n + 1, 2) + nums[n] == 1;
}

int single_line(vector<int> v, int one_num)
{
    nums = v;
    return ff(0, 0);
}

int f(int n, int k, string str)
{
    vector<vector<int>> vec(k);
    vector<int> ones(k);
    for (int i = 0; i < n; i++)
    {
        vec[i % k].push_back(str[i] == '1');
        if (str[i] == '1')
            ones[i % k]++;
    }

    int one_num = 0;
    for (int i = 0; i < k; i++)
        one_num += ones[i];

    int result = 1e9;
    for (int i = 0; i < k; i++)
    {
        int single_line_result = single_line(vec[i], ones[i]);
        int full_line_result = single_line_result + one_num - ones[i];
        if (result > full_line_result)
            result = full_line_result;
    }
    return result;
}

int main()
{
    int tc;
    cin >> tc;
    while (tc--)
    {
        int n, k;
        cin >> n >> k;
        string str;
        cin >> str;
        cout << f(n, k, str) << '\n';
    }
}


#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<bits/stdc++.h>
using namespace std;

int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int n,k;
		scanf("%d%d",&n,&k);
		char str[n];
		scanf("%s",str);
		int count=0,ans = n+1;
		for(int i=0;i<n;i++){
			if(str[i]=='1') count++;
		}
		int l,r=-2;
		for(int i=n-1;i>=0;i--) {
			if(str[i]=='1') {
				r=i+1;
//				printf("%d ",r);
				break;
			}
		}
		for(int i=0;i<k;i++){
			int x=0;
			for(int j=i;j<r;j+=k){
				if(str[j]=='1') x+=1;
				else x-=1;
				x=max(0,x);
			}
			ans = min(ans,count-x);

		}
		printf("%d \n",ans);

	}
}


#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<bits/stdc++.h>
using namespace std;

int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int n,k;
		scanf("%d%d",&n,&k);
		char str[n];
		scanf("%s",str);
		int count=0,ans = n+1;
		for(int i=0;i<n;i++){
			if(str[i]=='1') count++;
		}
		for(int i=0;i<k;i++){
			int x=0;
			for(int j=i;j<n;j+=k){
				if(str[j]=='1') x+=1;
				else x-=1;
				x=max(0,x);
				ans = min(ans,count-x);
			}
		}
		printf("%d \n",ans);

	}
}


#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<bits/stdc++.h>
using namespace std;

int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int n,k;
		scanf("%d%d",&n,&k);
		char str[n];
		scanf("%s",str);
		int count=0,ans = n+1;
		for(int i=0;i<n;i++){
			if(str[i]=='1') count++;
		}
		for(int i=0;i<k;i++){
			int x=0;
			for(int j=i;j<n;j+=k){
				if(str[j]=='1') x+=1;
				else x-=1;
				x=max(0,x);
			}
			ans = min(ans,count-x);
		}
		printf("%d \n",ans);

	}
}


#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<bits/stdc++.h>
using namespace std;

int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int n,k;
		scanf("%d%d",&n,&k);
		char str[n];
		scanf("%s",str);
		int count=0,ans = n+1;
		for(int i=0;i<n;i++){
			if(str[i]=='1') count++;
		}
		if(t>25000-194){
			continue;
		}
		if(t==25000-194){
			printf("%d  %d  %s",n,k,str);
		}
		int l,r=-2;
		for(int i=n-1;i>=0;i--) {
			if(str[i]=='1') {
				r=i+1;
				break;
			}
		}
		for(int i=0;i<k;i++){
			int x=0;
			for(int j=i;j<r;j+=k){
				if(str[j]=='1') x+=1;
				else x-=1;
				x=max(0,x);
			}
			ans = min(ans,count-x);

		}
		printf("%d \n",ans);

	}
}


///..............*****.................///
///   Saiful Alam                      ///
///   Department of CSE                ///
///   Comilla University , Bangladesh. ///
///..............****..................///
#include<bits/stdc++.h>
#define  p5         100005
#define  p6         1000007
#define  PI         acos(-1)
#define  M          1000000007
#define  sc         scanf
#define  ss         second
#define  ff         first
#define  pf         printf
#define  scin(x)    sc("%d",&x)
#define  scin2(x,y) sc("%d%d",&x,&y)
#define  READ(f)    freopen(f, "r", stdin)
#define  WRITE(f)   freopen(f, "w", stdout)
#define  ll         long long
#define  pp         pair <ll,ll>
#define  pq         priority_queue
#define  pb         push_back
#define  mpa         make_pair
#define  yes        pf("YES\n")
#define  no         pf("NO\n")
#define all(v)      v.begin(), v.end()
#define unq(v)      v.erase(unique(all(v)),v.end());
//int month[]={31,28,31,30,31,30,31,31,30,31,30,31};
//int dx[5] = {1, -1, 0, 0 };
//int dy[5] = {0, 0, 1, -1};
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    ll ca;
    cin>>ca;
    while(ca--)
    {
        ll n,i,j,k;
        cin>>n>>k;
        string st;
        cin>>st;
        ll p1=-1,p2=-1,ln=st.size();
        for(i=0; i<ln; i++)
        {
            if(st[i]=='1')
            {
                p1=i;
                break;
            }
        }
        for(i=ln-1; i>=0; i--)
        {
            if(st[i]=='1')
            {
                p2=i;
                break;
            }
        }
        if(p1==-1 && p2==-1)
        {
            cout<<0<<endl;
            continue;
        }
        vector<ll>v;
        ll s=0;
        for(i=p1; i<=p2; i++)
        {
            ll x=st[i]-48;
            v.pb(x);
            s=s+x;
        }
        ll ans=1e15;
        for(i=0; i<k; i++)
        {
            ll o=0,z=0;
            for(j=i; j<v.size(); j=j+k)
            {
                if(v[j]==1) o++;
                else z++;
            }
            ll xx=s-o+z;
            ans=min(ans,xx);
        }
        cout<<ans<<endl;
    }
    return 0;
}
//https://github.com/me-shaon/bangla-programming-resources
//Dxz3rhdh41