K-periodic Garland
You are given a garland consisting of $$$n$$$ lamps. States of the lamps are represented by the string $$$s$$$ of length $$$n$$$. The $$$i$$$-th character of the string $$$s_i$$$ equals '0' if the $$$i$$$-th lamp is turned off or '1' if the $$$i$$$-th lamp is turned on. You are also given a positive integer $$$k$$$.In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa).The garland is called $$$k$$$-periodic if the distance between each pair of adjacent turned on lamps is exactly $$$k$$$. Consider the case $$$k=3$$$. Then garlands "00010010", "1001001", "00010" and "0" are good but garlands "00101001", "1000001" and "01001100" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa.Your task is to find the minimum number of moves you need to make to obtain $$$k$$$-periodic garland from the given one.You have to answer $$$t$$$ independent test cases.
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 25~ 000$$$) — the number of test cases. Then $$$t$$$ test cases follow.The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10^6; 1 \le k \le n$$$) — the length of $$$s$$$ and the required period. The second line of the test case contains the string $$$s$$$ consisting of $$$n$$$ characters '0' and '1'.It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$ ($$$\sum n \le 10^6$$$).
For each test case, print the answer — the minimum number of moves you need to make to obtain $$$k$$$-periodic garland from the given one.
from __future__ import division, print_function
from sys import stdin, stdout
import bisect
import math
import heapq
i_m = 9223372036854775807

def modinv(n, p):
    return pow(n, p - 2, p)

def cin():
    return map(int, sin().split())

def ain():
    return list(map(int, sin().split()))

def sin():
    return input()

def inin():
    return int(input())
import math

def GCD(x, y):
    x = abs(x)
    y = abs(y)
    if min(x, y) == 0:
        return max(x, y)
    while y:
        (x, y) = (y, x % y)
    return x

def Divisors(n):
    l = []
    for i in range(1, int(math.sqrt(n) + 1)):
        if n % i == 0:
            if n // i == i:
                l.append(i)
            else:
                l.append(i)
                l.append(n // i)
    return l
prime = []

def SieveOfEratosthenes(n):
    global prime
    prime = [True for i in range(n + 1)]
    p = 2
    while p * p <= n:
        if prime[p] == True:
            for i in range(p * p, n + 1, p):
                prime[i] = False
        p += 1
    f = []
    for p in range(2, n):
        if prime[p]:
            f.append(p)
    return f
q = []

def dfs(n, d, v, c):
    global q
    v[n] = 1
    x = d[n]
    q.append(n)
    j = c
    for i in x:
        if i not in v:
            f = dfs(i, d, v, c + 1)
            j = max(j, f)
    return j
''

def main():
    for i in range(int(input())):
        (n, k) = map(int, input().split())
        s = input().strip()
        c1 = s.count('1')
        ans = n
        for i in range(k):
            su = 0
            for j in range(i, n, k):
                if s[j] == '1':
                    su += 1
                else:
                    su -= 1
                    su = max(0, su)
                ans = min(ans, c1 - su)
        print(ans)
py2 = round(0.5)
if py2:
    from future_builtins import ascii, filter, hex, map, oct, zip
    range = xrange
import os, sys
from io import IOBase, BytesIO
BUFSIZE = 8192

class FastIO(BytesIO):
    newlines = 0

    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.writable = 'x' in file.mode or 'w' in file.mode
        self.write = super(FastIO, self).write if self.writable else None

    def _fill(self):
        s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
        self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
        return s

    def read(self):
        while self._fill():
            pass
        return super(FastIO, self).read()

    def readline(self):
        while self.newlines == 0:
            s = self._fill()
            self.newlines = s.count(b'\n') + (not s)
        self.newlines -= 1
        return super(FastIO, self).readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.getvalue())
            (self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        if py2:
            self.write = self.buffer.write
            self.read = self.buffer.read
            self.readline = self.buffer.readline
        else:
            self.write = lambda s: self.buffer.write(s.encode('ascii'))
            self.read = lambda : self.buffer.read().decode('ascii')
            self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import sys

class ostream:

    def __lshift__(self, a):
        sys.stdout.write(str(a))
        return self
cout = ostream()
endl = '\n'

def readnumbers(zero=0):
    conv = ord if py2 else lambda x: x
    A = []
    numb = zero
    sign = 1
    i = 0
    s = sys.stdin.buffer.read()
    try:
        while True:
            if s[i] >= b'R'[0]:
                numb = 10 * numb + conv(s[i]) - 48
            elif s[i] == b'-'[0]:
                sign = -1
            elif s[i] != b'\r'[0]:
                A.append(sign * numb)
                numb = zero
                sign = 1
            i += 1
    except:
        pass
    if s and s[-1] >= b'R'[0]:
        A.append(sign * numb)
    return A
if __name__ == '__main__':
    main()
def car():
    for i in range(int(input())):
        (n, k) = map(int, input().split())
        s = input().strip()
        c1 = s.count('1')
        ans = n
        for i in range(k):
            su = 0
            for j in range(i, n, k):
                if s[j] == '1':
                    su += 1
                else:
                    su -= 1
                    su = max(0, su)
                ans = min(ans, c1 - su)
        print(ans)
car()
import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
    (n, k) = map(int, input().split())
    a = input()
    (ans, count, total) = (0, 0, 0)
    for i in range(k):
        count = 0
        for j in range(i, n, k):
            if a[j] == '1':
                count += 1
                total += 1
            else:
                count -= 1
                if count < 0:
                    count = 0
            ans = max(ans, count)
    print(total - ans)