The only difference between easy and hard versions is the size of the input.

You are given a string $s$ consisting of $n$ characters, each character is 'R', 'G' or 'B'.

You are also given an integer $k$. Your task is to change the minimum number of characters in the initial string $s$ so that after the changes there will be a string of length $k$ that is a substring of $s$, and is also a substring of the infinite string "RGBRGBRGB ...".

A string $a$ is a substring of string $b$ if there exists a positive integer $i$ such that $a_1 = b_i$, $a_2 = b_{i + 1}$, $a_3 = b_{i + 2}$, ..., $a_{|a|} = b_{i + |a| - 1}$. For example, strings "GBRG", "B", "BR" are substrings of the infinite string "RGBRGBRGB ..." while "GR", "RGR" and "GGG" are not.

You have to answer $q$ independent queries.


-----Input-----

The first line of the input contains one integer $q$ ($1 \le q \le 2000$) — the number of queries. Then $q$ queries follow.

The first line of the query contains two integers $n$ and $k$ ($1 \le k \le n \le 2000$) — the length of the string $s$ and the length of the substring.

The second line of the query contains a string $s$ consisting of $n$ characters 'R', 'G' and 'B'.

It is guaranteed that the sum of $n$ over all queries does not exceed $2000$ ($\sum n \le 2000$).


-----Output-----

For each query print one integer — the minimum number of characters you need to change in the initial string $s$ so that after changing there will be a substring of length $k$ in $s$ that is also a substring of the infinite string "RGBRGBRGB ...".


-----Example-----
Input
3
5 2
BGGGG
5 3
RBRGR
5 5
BBBRR

Output
1
0
3



-----Note-----

In the first example, you can change the first character to 'R' and obtain the substring "RG", or change the second character to 'R' and obtain "BR", or change the third, fourth or fifth character to 'B' and obtain "GB".

In the second example, the substring is "BRG".

Source: codeforces | Difficulty: MEDIUM
Tags: Implementation


Solutions:
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = input()
	s = ['RGB' * (k // 3) + 'RGB'[:k % 3], 'GBR' * (k // 3) + 'GBR'[:k % 3], 'BRG' * (k // 3) + 'BRG'[:k % 3]]
	minn = n
	for i in range(n - k + 1):
		for j in s:
			total = 0
			for l in range(k):
				total += 1 if a[i + l] != j[l] else 0
			minn = min(total, minn)
			if minn == 0:
				break
		if minn == 0:
			break
	print(minn)


import sys
input = sys.stdin.buffer.readline

def main():
	q = int(input())
	for _ in range(q):
		(n, k) = map(int, input().split())
		s = str(input())
		s = s[2:len(s) - 3]
		X = [[0] * 3 for _ in range(n + 1)]
		for i in range(n):
			if i % 3 == 0:
				if s[i] == 'R':
					X[i + 1][0] = 1
				elif s[i] == 'G':
					X[i + 1][1] = 1
				else:
					X[i + 1][2] = 1
			elif i % 3 == 1:
				if s[i] == 'G':
					X[i + 1][0] = 1
				elif s[i] == 'B':
					X[i + 1][1] = 1
				else:
					X[i + 1][2] = 1
			elif s[i] == 'B':
				X[i + 1][0] = 1
			elif s[i] == 'R':
				X[i + 1][1] = 1
			else:
				X[i + 1][2] = 1
		for i in range(n):
			for j in range(3):
				X[i + 1][j] += X[i][j]
		ans = 10 ** 18
		for i in range(n - k + 1):
			temp = 0
			for j in range(3):
				temp = max(temp, X[i + k][j] - X[i][j])
			ans = min(ans, k - temp)
		print(ans)
main()


def rgb(n, k, s):
	(t, result) = ('RGB', list())
	for i in range(n - k + 1):
		for j in range(3):
			count = 0
			for z in range(k):
				if s[i + z] != t[(z + j) % 3]:
					count += 1
			result.append(count)
	return min(result)
q = int(input())
lst = list()
for y in range(q):
	(N, K) = [int(w) for w in input().split()]
	v = input()
	lst.append(rgb(N, K, v))
for elem in lst:
	print(elem)


def naiveSolve():
	return

def getMinChanges(s, t, n, k):
	arr = [0] * n
	for i in range(n):
		if s[i] != t[i]:
			arr[i] = 1
	p = arr
	for i in range(1, n):
		p[i] += p[i - 1]

	def query(l, r):
		res = p[r]
		if l - 1 >= 0:
			res -= p[l - 1]
		return res
	ans = inf
	for l in range(n):
		r = l + k - 1
		if r == n:
			break
		ans = min(ans, query(l, r))
	return ans

def main():
	q = int(input())
	allans = []
	for _ in range(q):
		(n, k) = readIntArr()
		s = input()
		ans = inf
		m = (n + 2) // 3
		testStrings = [('RGB' * m)[:n], ('GBR' * m)[:n], ('BRG' * m)[:n]]
		for t in testStrings:
			ans = min(ans, getMinChanges(s, t, n, k))
		allans.append(ans)
	multiLineArrayPrint(allans)
	return
import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')

def oneLineArrayPrint(arr):
	print(' '.join([str(x) for x in arr]))

def multiLineArrayPrint(arr):
	print('\n'.join([str(x) for x in arr]))

def multiLineArrayOfArraysPrint(arr):
	print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))

def readIntArr():
	return [int(x) for x in input().split()]

def makeArr(defaultValFactory, dimensionArr):
	dv = defaultValFactory
	da = dimensionArr
	if len(da) == 1:
		return [dv() for _ in range(da[0])]
	else:
		return [makeArr(dv, da[1:]) for _ in range(da[0])]

def queryInteractive(l, r):
	print('? {} {}'.format(l, r))
	sys.stdout.flush()
	return int(input())

def answerInteractive(x):
	print('! {}'.format(x))
	sys.stdout.flush()
inf = float('inf')
MOD = 10 ** 9 + 7
from math import gcd, floor, ceil
for _abc in range(1):
	main()


import sys
for _ in range(int(input())):
	(n, k) = map(int, sys.stdin.readline().split())
	a = sys.stdin.readline()
	if n == 1:
		print(0)
	else:
		next = a[:2]
		parsum1 = [0 for _ in range(n + 1)]
		parsum2 = [0 for _ in range(n + 1)]
		parsum3 = [0 for _ in range(n + 1)]
		check = 'RGB'
		i1 = 0
		s1 = 0
		i2 = 1
		s2 = 0
		i3 = 2
		s3 = 0
		for i in range(n):
			if a[i] != check[i1]:
				s1 += 1
			if a[i] != check[i2]:
				s2 += 1
			if a[i] != check[i3]:
				s3 += 1
			i1 = i1 + 1 if i1 + 1 < 3 else 0
			i2 = i2 + 1 if i2 + 1 < 3 else 0
			i3 = i3 + 1 if i3 + 1 < 3 else 0
			parsum1[i + 1] = s1
			parsum2[i + 1] = s2
			parsum3[i + 1] = s3
		mn1 = n
		mn2 = n
		mn3 = n
		i = 1
		while 1:
			mxRange = i + k - 1
			if mxRange <= n:
				mn1 = min(parsum1[mxRange] - parsum1[i - 1], mn1)
				mn2 = min(parsum2[mxRange] - parsum2[i - 1], mn2)
				mn3 = min(parsum3[mxRange] - parsum3[i - 1], mn3)
			else:
				break
			i += 1
		sys.stdout.write('{}\n'.format(min(mn1, mn2, mn3)))


def process(S, k):
	n = len(S)
	answer = float('inf')
	current = [0, 0, 0]
	indexes = {'R': 0, 'G': 1, 'B': 2}
	for i in range(k):
		c = S[i]
		index = (indexes[c] - i) % 3
		for j in range(3):
			current[j] += 1
		current[index] -= 1
	answer = min(answer, min(current))
	for i in range(k, n):
		c1 = S[i - k]
		index1 = (indexes[c1] - i + k) % 3
		for j in range(3):
			current[j] -= 1
		current[index1] += 1
		c = S[i]
		index = (indexes[c] - i) % 3
		for j in range(3):
			current[j] += 1
		current[index] -= 1
		answer = min(answer, min(current))
	return answer
t = int(input())
for i in range(t):
	(n, k) = [int(x) for x in input().split()]
	S = input()
	print(process(S, k))


T = int(input())
for _ in range(T):
	(n, k) = list(map(int, input().split()))
	s = input()
	rq1 = ''
	rq2 = ''
	rq3 = ''
	for i in range(k):
		if i % 3 == 0:
			rq1 = rq1 + 'R'
			rq2 = rq2 + 'G'
			rq3 = rq3 + 'B'
		elif i % 3 == 1:
			rq1 = rq1 + 'G'
			rq2 = rq2 + 'B'
			rq3 = rq3 + 'R'
		elif i % 3 == 2:
			rq1 = rq1 + 'B'
			rq2 = rq2 + 'R'
			rq3 = rq3 + 'G'
	ans = 1000000000000000000
	for i in range(0, len(s) - k + 1):
		a1 = 0
		a2 = 0
		a3 = 0
		for j in range(i, i + k):
			if s[j] != rq1[j - i]:
				a1 += 1
			if s[j] != rq2[j - i]:
				a2 += 1
			if s[j] != rq3[j - i]:
				a3 += 1
		ans = min(ans, min(a1, a2, a3))
	print(ans)


import sys
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
for tc in range(int(input())):
	(n, k) = I()
	s = input().strip()
	ch = 'RGB'
	ans = 10 ** 9 + 7
	for ty in range(3):
		cur = 0
		for i in range(k):
			if s[i] != ch[(i + ty) % 3]:
				cur += 1
		ans = min(ans, cur)
		for j in range(k, n):
			if s[j - k] != ch[(j + ty - k) % 3]:
				cur -= 1
			if s[j] != ch[(j + ty) % 3]:
				cur += 1
			ans = min(ans, cur)
	print(ans)


import sys

def main():
	N = int(sys.stdin.readline().strip())
	for _ in range(N):
		(n, k) = [int(x) for x in sys.stdin.readline().strip().split()]
		s = sys.stdin.readline().strip()
		S = 'RGB'
		err = [[0] for _ in S]
		res = [n for i in range(len(S))]
		for (i, c) in enumerate(s):
			for j in range(3):
				prev = err[j][-1]
				err[j].append(prev + (0 if S[(i + j) % 3] == c else 1))
				if i + 1 >= k:
					res[j] = min(res[j], err[j][-1] - err[j][-k - 1])
		print(min(*res))
main()


import sys
q = int(sys.stdin.readline())
for Q in range(q):
	(n, k) = list(map(int, sys.stdin.readline().split()))
	s = sys.stdin.readline()
	max_count = 0
	ans = ''
	if n == 1 or k == 1:
		print(0)
	else:
		for i in range(n - k + 1):
			count1 = 0
			count2 = 0
			count3 = 0
			sq = s[i:i + k]
			for i in range(k):
				if i % 3 == 0 and sq[i] == 'R':
					count1 += 1
				elif i % 3 == 1 and sq[i] == 'G':
					count1 += 1
				elif i % 3 == 2 and sq[i] == 'B':
					count1 += 1
			for i in range(k):
				if i % 3 == 0 and sq[i] == 'G':
					count2 += 1
				elif i % 3 == 1 and sq[i] == 'B':
					count2 += 1
				elif i % 3 == 2 and sq[i] == 'R':
					count2 += 1
			for i in range(k):
				if i % 3 == 0 and sq[i] == 'B':
					count3 += 1
				elif i % 3 == 1 and sq[i] == 'R':
					count3 += 1
				elif i % 3 == 2 and sq[i] == 'G':
					count3 += 1
			c = max(count1, count2, count3)
			max_count = max(c, max_count)
			if max_count == k:
				break
		print(k - max_count)


from sys import stdin, stdout
from collections import Counter
import math

def rsingle_int():
	return int(stdin.readline().rstrip())

def rmult_int():
	return [int(x) for x in stdin.readline().rstrip().split()]

def rmult_str():
	return stdin.readline().rstrip().split()

def r_str():
	return stdin.readline().rstrip()

def rsingle_char():
	return stdin.read(1)

def main():
	q = rsingle_int()
	rgb = 'RGB'
	for i in range(q):
		(n, k) = rmult_int()
		s = r_str()
		min_all = k
		for i in range(n - k + 1):
			for shift in range(3):
				min = 0
				for k_i in range(k):
					if s[i + k_i] != rgb[(shift + k_i) % 3]:
						min += 1
				if min_all > min:
					min_all = min
		print(min_all)
main()


T = int(input())
for t in range(T):
	(N, K) = map(int, input().split())
	s = input()
	D = K
	t = 'RGB'
	if K == 1:
		D = 0
	else:
		for m in range(3):
			d = 0
			a = [0] * N
			for i in range(N):
				if s[i] != t[(m + i) % 3]:
					a[i] = 1
				d = d + a[i]
				if i > K - 1:
					d = d - a[i - K]
				if d < D and i >= K - 1:
					D = d
				if D == 0:
					break
			if D == 0:
				break
	print(D)


t = 'RGB'
q = int(input())
for l in range(q):
	(n, k) = map(int, input().split())
	s = input()
	ans = 1000000000.0
	for j in range(n - k + 1):
		for offset in range(3):
			cur = 0
			for pos in range(k):
				if s[j + pos] != t[(pos + offset) % 3]:
					cur += 1
			ans = min(ans, cur)
	print(ans)


from sys import stdin
input = stdin.readline
q = int(input())
for _ in range(q):
	(n, k) = [int(i) for i in input().split()]
	s = input()
	start = [0] * n
	for i in range(n):
		if s[i] == 'R':
			start[i] = i % 3
		elif s[i] == 'G':
			start[i] = (i - 1) % 3
		else:
			start[i] = (i - 2) % 3
	act = [0] * 3
	for i in range(k):
		act[start[i]] += 1
	res = max(act)
	for i in range(k, n):
		act[start[i]] += 1
		act[start[i - k]] -= 1
		res = max([act[start[i]], res])
	print(k - res)


import atexit
import io
import sys
_INPUT_LINES = sys.stdin.read().splitlines()
input = iter(_INPUT_LINES).__next__
_OUTPUT_BUFFER = io.StringIO()
sys.stdout = _OUTPUT_BUFFER

@atexit.register
def write():
	sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
R = lambda : map(int, input().split())
MAXC = int(100000.0)
for _ in range(int(input())):
	(n, k) = R()
	S = input()
	col = 'RGB'
	ans = sys.maxsize
	for j in range(n - k + 1):
		for offset in range(3):
			cnt = 0
			for pos in range(k):
				if S[j + pos] != col[(pos + offset) % 3]:
					cnt += 1
			ans = min(ans, cnt)
	print(ans)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	x = ('RGB' * (k + 1))[:k]
	y = ('GBR' * (k + 1))[:k]
	z = ('BRG' * (k + 1))[:k]
	i = 0
	j = k - 1
	m = pow(10, 10)
	while j < n:
		a = 0
		b = 0
		c = 0
		temp = s[i:j + 1]
		for l in range(len(temp)):
			if x[l] != temp[l]:
				a += 1
			if y[l] != temp[l]:
				b += 1
			if z[l] != temp[l]:
				c += 1
		m = min(a, b, c, m)
		if m == 0:
			break
		j += 1
		i += 1
	print(m)


import os
import sys
from io import BytesIO, IOBase
import heapq as h
from bisect import bisect_left
from types import GeneratorType
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		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 time
start_time = time.time()
import collections as col
import math, string
from functools import reduce

def getInts():
	return [int(s) for s in input().split()]

def getInt():
	return int(input())

def getStrs():
	return [s for s in input().split()]

def getStr():
	return input()

def listStr():
	return list(input())
MOD = 10 ** 9 + 7

def solve():
	(N, K) = getInts()
	best = 10 ** 9
	S = listStr()
	A = []
	for s in S:
		if s == 'R':
			A.append(0)
		elif s == 'G':
			A.append(1)
		else:
			A.append(2)
	for option in range(3):
		P = [0]
		curr = 0
		for (i, a) in enumerate(A):
			if (a + option) % 3 == i % 3:
				curr += 1
			P.append(curr)
		for i in range(K, N + 1):
			best = min(best, K - (P[i] - P[i - K]))
	return best
for _ in range(getInt()):
	print(solve())


import sys
from sys import stdin, stdout
import bisect
import math
import heapq

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 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

def ch(x, y, z, n):
	a = []
	for i in range(n):
		if i % 3 == 0:
			a.append(x)
		elif i % 3 == 1:
			a.append(y)
		else:
			a.append(z)
	return a

def main():
	t = inin()
	for _ in range(t):
		(n, k) = cin()
		a = sin()
		l = ch('R', 'G', 'B', n)
		ans = 0
		an = 10000000000
		for i in range(k):
			if a[i] != l[i]:
				ans += 1
		an = min(ans, an)
		for i in range(k, n):
			if a[i] != l[i]:
				ans += 1
			if a[i - k] != l[i - k]:
				ans -= 1
			an = min(ans, an)
		l = ch('G', 'B', 'R', n)
		ans = 0
		for i in range(k):
			if a[i] != l[i]:
				ans += 1
		an = min(ans, an)
		for i in range(k, n):
			if a[i] != l[i]:
				ans += 1
			if a[i - k] != l[i - k]:
				ans -= 1
			an = min(ans, an)
		l = ch('B', 'R', 'G', n)
		ans = 0
		for i in range(k):
			if a[i] != l[i]:
				ans += 1
		an = min(ans, an)
		for i in range(k, n):
			if a[i] != l[i]:
				ans += 1
			if a[i - k] != l[i - k]:
				ans -= 1
			an = min(ans, an)
		print(an)
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'0'[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'0'[0]:
		A.append(sign * numb)
	return A
main()


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	r1 = 'RGB' * (k // 3) + 'RGB'[:k % 3]
	r2 = 'GBR' * (k // 3) + 'GBR'[:k % 3]
	r3 = 'BRG' * (k // 3) + 'BRG'[:k % 3]
	ans = 1000000000.0
	for i in range(n - k + 1):
		t1 = t2 = t3 = 0
		for j in range(k):
			if s[i + j] != r1[j]:
				t1 += 1
			if s[i + j] != r2[j]:
				t2 += 1
			if s[i + j] != r3[j]:
				t3 += 1
		ans = min([ans, t1, t2, t3])
	print(ans)


import sys

def changes(s1, s2, k):
	ctr = 0
	for i in range(k):
		if s1[i] != s2[i]:
			ctr += 1
	return ctr
q = int(sys.stdin.readline())
ans_arr = []
for x in range(q):
	[n, k] = [int(i) for i in sys.stdin.readline().split()]
	s = sys.stdin.readline()
	a = ['R', 'G', 'B']
	tmp = ''
	for e in range(k + 2):
		tmp += a[e % 3]
	mini = k + 1
	for i in range(n - k + 1):
		mini = min(mini, changes(s[i:i + k], tmp[:k], k))
		mini = min(mini, changes(s[i:i + k], tmp[1:k + 1], k))
		mini = min(mini, changes(s[i:i + k], tmp[2:k + 2], k))
	ans_arr.append(str(mini))
print('\n'.join(ans_arr))


q = int(input())
sr = 'RGB' * (10000 // 3 + 3)
sb = 'BRG' * (10000 // 3 + 3)
sg = 'GBR' * (10000 // 3 + 3)
for _ in range(q):
	(n, k) = map(int, input().split())
	v = input()
	ans = n
	rpref = [1]
	gpref = [1]
	bpref = [1]
	if v[0] == 'B':
		bpref = [0]
	elif v[0] == 'R':
		rpref = [0]
	else:
		gpref = [0]
	for i in range(1, n):
		if v[i] != sr[i]:
			rpref.append(rpref[i - 1] + 1)
		else:
			rpref.append(rpref[i - 1])
		if v[i] != sb[i]:
			bpref.append(bpref[i - 1] + 1)
		else:
			bpref.append(bpref[i - 1])
		if v[i] != sg[i]:
			gpref.append(gpref[i - 1] + 1)
		else:
			gpref.append(gpref[i - 1])
	for i in range(n):
		if i + k <= n:
			val = rpref[i + k - 1] - rpref[i]
			if v[i] != sr[i]:
				val += 1
			ans = min(ans, val)
			val = gpref[i + k - 1] - gpref[i]
			if v[i] != sg[i]:
				val += 1
			ans = min(ans, val)
			val = bpref[i + k - 1] - bpref[i]
			if v[i] != sb[i]:
				val += 1
			ans = min(ans, val)
		else:
			break
	print(ans)


from sys import stdin
t = int(input())
for _ in range(t):
	(n, k) = map(int, stdin.readline().split())
	s = stdin.readline()
	one = 'RGB' * (n // 3)
	s1 = 'RGB'
	one = one + s1[:n % 3]
	two = 'GBR' * (n // 3)
	s2 = 'GBR'
	two = two + s2[:n % 3]
	s3 = 'BRG'
	three = s3 * (n // 3)
	three = three + s3[:n % 3]
	(i, j) = (0, 0)
	(count1, count2, count3, m) = (0, 0, 0, n)
	while j < n:
		while j - i < k:
			if s[j] != one[j]:
				count1 += 1
			if s[j] != two[j]:
				count2 += 1
			if s[j] != three[j]:
				count3 += 1
			j += 1
		m = min(count1, count2, count3, m)
		if s[i] != one[i]:
			count1 -= 1
		if s[i] != two[i]:
			count2 -= 1
		if s[i] != three[i]:
			count3 -= 1
		i += 1
	print(m)


import sys
input = sys.stdin.readline

def main():
	Q = int(input())
	data = []
	for _ in range(Q):
		(N, K) = map(int, input().split())
		S = input().rstrip()
		data.append([N, K, S])
	RGB = []
	for i in range(300000):
		if i % 3 == 0:
			RGB.append('R')
		elif i % 3 == 1:
			RGB.append('G')
		else:
			RGB.append('B')
	for (N, K, S) in data:
		ans = N
		dp = [[0 for _ in range(N)] for _ in range(3)]
		for j in range(3):
			for i in range(N):
				if S[i] != RGB[i + j]:
					dp[j][i] = 1
		for j in range(3):
			p = 0
			for k in range(K):
				p += dp[j][k]
			ans = min(ans, p)
			for i in range(1, N - K + 1):
				p += dp[j][K - 1 + i] - dp[j][i - 1]
				ans = min(ans, p)
		print(ans)
main()


from sys import stdin
input = stdin.readline
from sys import stdin
input = stdin.readline
rgb = 'RGB'
for i in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	ans = k
	for i in range(3):
		a = [0] * n
		c = 0
		for j in range(n):
			a[j] = s[j] != rgb[(i + j) % 3]
			c += a[j]
			if j >= k:
				c -= a[j - k]
			if j >= k - 1:
				ans = min(ans, c)
	print(ans)


q = int(input())
t = 'RGB'
for loop in range(q):
	z = input().split()
	(n, k) = (int(z[0]), int(z[1]))
	s = input()
	ans = 10 ** 9
	for j in range(0, n - k + 1):
		for offset in range(0, 3):
			c = 0
			for pos in range(0, k):
				if s[j + pos] != t[(offset + pos) % 3]:
					c = c + 1
			ans = min(ans, c)
	print(ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	r = 'RGB' * (n // 3) + 'RGB'[:n % 3]
	g = 'GBR' * (n // 3) + 'GBR'[:n % 3]
	b = 'BRG' * (n // 3) + 'BRG'[:n % 3]
	ra = []
	ga = []
	ba = []
	for i in range(n):
		if r[i] == s[i]:
			ra.append(0)
		elif r[i] != s[i]:
			ra.append(1)
		if g[i] == s[i]:
			ga.append(0)
		elif g[i] != s[i]:
			ga.append(1)
		if b[i] == s[i]:
			ba.append(0)
		elif b[i] != s[i]:
			ba.append(1)
	pre_r = [0, ra[0]]
	pre_g = [0, ga[0]]
	pre_b = [0, ba[0]]
	ans = float('INF')
	for i in range(1, n):
		pre_r.append(pre_r[i] + ra[i])
		pre_g.append(pre_g[i] + ga[i])
		pre_b.append(pre_b[i] + ba[i])
	for i in range(k, n + 1):
		ans = min(ans, pre_r[i] - pre_r[i - k], pre_g[i] - pre_g[i - k], pre_b[i] - pre_b[i - k])
	print(ans)


tmpt = input()
t = int(tmpt)
ss = []
while t > 0:
	t -= 1
	(n, k) = (int(x) for x in input().split(' '))
	str = input()
	leng = len(str)
	tmp = ''
	for i in range(0, k):
		if i % 3 == 0:
			tmp += 'R'
		elif i % 3 == 1:
			tmp += 'G'
		elif i % 3 == 2:
			tmp += 'B'
	ss.append(tmp)
	tmp = ''
	for i in range(0, k):
		if i % 3 == 0:
			tmp += 'G'
		elif i % 3 == 1:
			tmp += 'B'
		elif i % 3 == 2:
			tmp += 'R'
	ss.append(tmp)
	tmp = ''
	for i in range(0, k):
		if i % 3 == 0:
			tmp += 'B'
		elif i % 3 == 1:
			tmp += 'R'
		elif i % 3 == 2:
			tmp += 'G'
	ss.append(tmp)
	tmp = ''
	ans = 1061109567
	for i in range(0, 3):
		for j in range(0, leng):
			if j + k - 1 >= leng:
				break
			cnt = 0
			for idx in range(0, k):
				if str[j + idx] != ss[i][idx]:
					cnt += 1
			ans = min(cnt, ans)
	print(ans)
	ss.clear()


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		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')
from math import factorial
from collections import Counter, defaultdict
from heapq import heapify, heappop, heappush

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)
mod = 998244353
INF = float('inf')

def main():
	for _ in range(N()):
		(n, k) = RL()
		s = input()
		rec = ['R', 'G', 'B']
		res = INF
		for i in range(3):
			sm = [0]
			x = i
			for j in range(n):
				if s[j] == rec[x % 3]:
					sm.append(sm[-1])
				else:
					sm.append(sm[-1] + 1)
				x = (x + 1) % 3
			for j in range(k, len(sm)):
				res = min(res, sm[j] - sm[j - k])
		print(res)
main()


def answer():
	(n, k) = map(int, input().split(' '))
	in_str = input()
	r = k // 3
	rgb_strs = []
	for key in ['RGB', 'GBR', 'BRG']:
		s = key * (r + 1)
		s = s[:k]
		rgb_strs.append(s)
	max_count = k
	for i in range(n - k + 1):
		in_tmp_str = in_str[i:i + k]
		for rgb_str in rgb_strs:
			count = 0
			for (i, in_c) in enumerate(in_tmp_str):
				if in_c == rgb_str[i]:
					count += 1
			max_count = min(max_count, k - count)
	return max_count
for i in range(int(input())):
	out = answer()
	print(out)


def solve():
	(l, k) = [int(i) for i in input().split(' ')]
	a = input()
	n = {'R': 'G', 'G': 'B', 'B': 'R'}
	x = 'R'
	y = 'G'
	z = 'B'
	prev_x = 'R'
	prev_y = 'G'
	prev_z = 'B'
	x_c = y_c = z_c = 0
	m = float('inf')
	for i in range(k):
		if a[i] != x:
			x_c += 1
		if a[i] != y:
			y_c += 1
		if a[i] != z:
			z_c += 1
		x = n[x]
		y = n[y]
		z = n[z]
	m = min(m, x_c, y_c, z_c)
	import code
	for i in range(k, l):
		import code
		if a[i - k] != prev_x:
			x_c -= 1
		if a[i - k] != prev_y:
			y_c -= 1
		if a[i - k] != prev_z:
			z_c -= 1
		if a[i] != x:
			x_c += 1
		if a[i] != y:
			y_c += 1
		if a[i] != z:
			z_c += 1
		prev_x = n[prev_x]
		prev_y = n[prev_y]
		prev_z = n[prev_z]
		x = n[x]
		y = n[y]
		z = n[z]
		import code
		m = min(m, x_c, y_c, z_c)
	return m
q = int(input())
o = ''
for _ in range(q):
	o += str(solve()) + '\n'
print(o)


q = int(input())
for _ in range(q):
	s1 = 'RGB'
	s2 = 'GBR'
	s3 = 'BRG'
	(n, k) = map(int, input().split())
	s = input()
	dp1 = [0] * (len(s) + 1)
	dp2 = [0] * (len(s) + 1)
	dp3 = [0] * (len(s) + 1)
	count = 0
	for i in range(1, n + 1):
		if s[i - 1] != s1[count % 3]:
			dp1[i] += 1
		count += 1
		dp1[i] += dp1[i - 1]
	count = 0
	for i in range(1, n + 1):
		if s[i - 1] != s2[count % 3]:
			dp2[i] += 1
		count += 1
		dp2[i] += dp2[i - 1]
	count = 0
	for i in range(1, n + 1):
		if s[i - 1] != s3[count % 3]:
			dp3[i] += 1
		count += 1
		dp3[i] += dp3[i - 1]
	mi = 100000000
	for i in range(1, n - k + 2):
		mi = min(mi, dp1[i + k - 1] - dp1[i - 1])
		mi = min(mi, dp2[i + k - 1] - dp2[i - 1])
		mi = min(mi, dp3[i + k - 1] - dp3[i - 1])
	print(mi)


from sys import stdin, stdout
import sys
import math

def meth(string1, string2):
	res = 0
	for i in range(len(string1)):
		if string1[i] != string2[i]:
			res += 1
	return res
M = sys.maxsize
t = int(stdin.readline())
for i in range(t):
	(n, k) = list(map(int, stdin.readline().split()))
	s = stdin.readline()
	s = s[:-1]
	arr = []
	sss = 'RGB'
	if k == 1:
		arr = ['R', 'G', 'B']
	if k == 2:
		arr = ['RG', 'GB', 'BR']
	arr = ['RGB' * (k // 3), 'GBR' * (k // 3), 'BRG' * (k // 3)]
	val = k - k // 3 * 3
	if val == 1:
		arr[0] += 'R'
		arr[1] += 'G'
		arr[2] += 'B'
	if val == 2:
		arr[0] += 'RG'
		arr[1] += 'GB'
		arr[2] += 'BR'
	mn = sys.maxsize
	for i in range(n - k + 1):
		str1 = s[i:i + k]
		for x in arr:
			mn = min(mn, meth(str1, x))
	print(mn)


q = int(input())

def pr(t, x, k):
	count = 0
	for i in range(k):
		if t[i] != x[i]:
			count += 1
	return count
for i in range(q):
	mi = float('inf')
	(n, k) = map(int, input().split())
	s = input()
	for i in range(n - k + 1):
		t = s[i:i + k]
		x1 = 'RGB' * (k // 3)
		x1 += 'RGB'[:k % 3]
		x2 = 'GBR' * (k // 3)
		x2 += 'GBR'[:k % 3]
		x3 = 'BRG' * (k // 3)
		x3 += 'BRG'[:k % 3]
		mi = min(mi, pr(t, x1, k), pr(t, x2, k), pr(t, x3, k))
	print(mi)


q = int(input())
for i in range(q):
	(n, k) = map(int, input().split())
	s = input()
	a = [0] * n
	for j in range(n):
		if s[j] == 'R':
			p = 0
		elif s[j] == 'G':
			p = 1
		elif s[j] == 'B':
			p = 2
		a[j] = p - j % 3
	counts = [0, 0, 0]
	for j in range(k):
		counts[a[j]] += 1
	best = [counts[0], counts[1], counts[2]]
	for j in range(k, n):
		counts[a[j]] += 1
		counts[a[j - k]] -= 1
		best[0] = max(best[0], counts[0])
		best[1] = max(best[1], counts[1])
		best[2] = max(best[2], counts[2])
	print(k - max(best))


querries = int(input())
for _ in range(querries):
	raw_in = input().split()
	[n, k] = [int(x) for x in raw_in]
	string = input()
	str_len = len(string)
	full = 'RGB' * 668
	all_pos = [full[:str_len], full[1:str_len + 1], full[2:str_len + 2]]
	max_val = k
	if k < n:
		for i in range(3):
			check_dif = []
			for j in range(str_len):
				if string[j] != all_pos[i][j]:
					check_dif.append(1)
				else:
					check_dif.append(0)
			count = 0
			for u in range(k, n + 1):
				check_min = sum(check_dif[count:u])
				if check_min < max_val:
					max_val = check_min
				count += 1
	else:
		for i in range(3):
			check_min = 0
			for j in range(str_len):
				if string[j] != all_pos[i][j]:
					check_min += 1
			if check_min < max_val:
				max_val = check_min
	print(max_val)


from sys import stdin, stdout
from os import path
rd = lambda : stdin.readline().strip()
wr = stdout.write
if path.exists('input.txt'):
	stdin = open('input.txt', 'r')
import time, math
for _ in range(int(rd())):
	(x, y) = map(int, rd().split())
	res = rd()
	cur = ['R', 'G', 'B']
	ans = 1 << 24
	for i in range(x - y + 1):
		for index in range(3):
			counter = 0
			for j in range(y):
				if res[i + j] != cur[(j + index) % 3]:
					counter += 1
			ans = min(ans, counter)
	print(ans)


a = int(input())
ot = ''
for i in range(a):
	(b, c) = map(int, input().split())
	s = input()
	pref = [0] * (b + 1)
	pref1 = [0] * (b + 1)
	pref2 = [0] * (b + 1)
	n = 'RGB'
	n1 = 'GBR'
	n2 = 'BRG'
	for j in range(b):
		pref[j + 1] = pref[j]
		if s[j] != n[j % 3]:
			pref[j + 1] += 1
		pref1[j + 1] = pref1[j]
		if s[j] != n1[j % 3]:
			pref1[j + 1] += 1
		pref2[j + 1] = pref2[j]
		if s[j] != n2[j % 3]:
			pref2[j + 1] += 1
	mi = 1000000000
	for j in range(c, b + 1):
		mi = min(pref[j] - pref[j - c], pref1[j] - pref1[j - c], pref2[j] - pref2[j - c], mi)
	mi = min(pref[-1], pref1[-1], pref2[-1], mi)
	ot += str(mi) + '\n'
print(ot)


import math
for ii in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	r = 'RGB' * n
	g = 'GBR' * n
	b = 'BRG' * n
	ans = math.inf
	for i in range(n - k + 1):
		(c, c1, c2) = (0, 0, 0)
		ind = 0
		for j in range(i, i + k):
			if s[j] != r[ind]:
				c += 1
			if s[j] != g[ind]:
				c1 += 1
			if s[j] != b[ind]:
				c2 += 1
			ind += 1
		ans = min(ans, min(c, min(c2, c1)))
	print(ans)


for _ in range(int(input())):
	(n, k) = [int(i) for i in input().split()]
	word = input()
	ans = 0
	for col in ['RGB', 'GBR', 'BRG']:
		cnt = 0
		for i in range(k):
			if word[i] == col[i % 3]:
				cnt += 1
		mx = cnt
		for i in range(n - k):
			if word[i + k] == col[(i + k) % 3]:
				cnt += 1
			if word[i] == col[i % 3]:
				cnt -= 1
			if cnt > mx:
				mx = cnt
		ans = max(ans, mx)
	print(k - ans)


sub = []
for i in range(3000):
	z = (i + 1) % 3
	if z == 1:
		sub.append('R')
	elif z == 2:
		sub.append('G')
	else:
		sub.append('B')
su = ''.join(sub)
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	i = 0
	j = i + k - 1
	mini = 10 ** 18
	while j < n:
		x = i
		y = j
		count1 = 0
		count2 = 0
		count3 = 0
		k1 = 0
		k2 = 1
		k3 = 2
		while x <= y:
			if s[x] != su[k1]:
				count1 += 1
			if s[x] != su[k2]:
				count2 += 1
			if s[x] != su[k3]:
				count3 += 1
			x += 1
			k1 += 1
			k2 += 1
			k3 += 1
		i += 1
		j += 1
		z = min(count1, count2, count3)
		mini = min(z, mini)
	print(mini)


import sys

def countR(ip):
	c = 0
	for i in ip:
		if i == 'R':
			c += 1
	return c

def countB(ip):
	c = 0
	for i in ip:
		if i == 'B':
			c += 1
	return c

def countG(ip):
	c = 0
	for i in ip:
		if i == 'G':
			c += 1
	return c
t = int(sys.stdin.readline())
x = 'RGB' * 680
y = 'GBR' * 680
z = 'BRG' * 680
for i in range(t):
	(n, k) = list(map(int, sys.stdin.readline().strip().split()))
	a = sys.stdin.readline().strip()
	xk = x[:k]
	yk = y[:k]
	zk = z[:k]
	op = 2001
	for j in range(n - k + 1):
		b = a[j:j + k]
		xd = 0
		yd = 0
		zd = 0
		for jj in range(len(b)):
			if b[jj] != xk[jj]:
				xd += 1
			if b[jj] != yk[jj]:
				yd += 1
			if b[jj] != zk[jj]:
				zd += 1
		op = min(op, xd, yd, zd)
	print(op)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	sums = [0, 0, 0]
	res = 1 << 24
	for i in range(n):
		sums[(333 - ord(s[i]) + i) % 3] -= 1
		if i < k:
			for j in range(3):
				sums[j] += 1
		else:
			sums[(333 - ord(s[i - k]) + i - k) % 3] += 1
		if i >= k - 1:
			res = min(res, min(sums))
	print(res)


import sys

def main():

	def diff(s):
		ret1 = 0
		for k in range(len(s)):
			if k % 3 == 0 and s[k] != 'R':
				ret1 += 1
			if k % 3 == 1 and s[k] != 'G':
				ret1 += 1
			if k % 3 == 2 and s[k] != 'B':
				ret1 += 1
		ret2 = 0
		for k in range(len(s)):
			if k % 3 == 0 and s[k] != 'G':
				ret2 += 1
			if k % 3 == 1 and s[k] != 'B':
				ret2 += 1
			if k % 3 == 2 and s[k] != 'R':
				ret2 += 1
		ret3 = 0
		for k in range(len(s)):
			if k % 3 == 0 and s[k] != 'B':
				ret3 += 1
			if k % 3 == 1 and s[k] != 'R':
				ret3 += 1
			if k % 3 == 2 and s[k] != 'G':
				ret3 += 1
		return min(ret1, ret2, ret3)
	input = sys.stdin.readline
	q = int(input())
	for query in range(q):
		(n, k) = map(int, input().split())
		s = input()
		ans = 100000
		for l in range(n - k + 1):
			ans = min(ans, diff(s[l:l + k]))
		print(ans)
main()


import sys

def rgb(n):

	def _inner(shift):
		b = ['R', 'G', 'B']
		for _ in range(n):
			yield b[(_ + shift) % 3]
	for i in range(3):
		yield list(_inner(i))

def fun(n, k, string):
	ans = None
	for i in range(n - k + 1):
		substr = string[i:i + k]
		for eta in rgb(k):
			if substr == eta:
				return 0
			else:
				mid_ans = 0
				for (a, b) in zip(eta, substr):
					if a != b:
						mid_ans += 1
				if ans is None or mid_ans < ans:
					ans = mid_ans
	return ans
q = int(next(sys.stdin))
for _ in range(q):
	(n, k) = map(int, next(sys.stdin).split(' '))
	string = next(sys.stdin)
	print(fun(n, k, string))


def c2i(color):
	if color == 'R':
		return 0
	if color == 'G':
		return 1
	if color == 'B':
		return 2

def bijiao(a, b):
	if a == b:
		return 0
	else:
		return 1
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	l = list(map(c2i, list(input())))
	su = k
	kk = [[i % 3 for i in range(0, 0 + k)], [i % 3 for i in range(1, 1 + k)], [i % 3 for i in range(2, 2 + k)]]
	for i in range(n - k + 1):
		for z in range(3):
			su0 = 0
			for j in range(0, k):
				su0 += bijiao(l[i + j], kk[z][j])
			if su0 < su:
				su = su0
	print(su)


from sys import stdin, stdout
input = stdin.readline
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = str(input())
	s1 = 'RGB'
	s2 = 'GBR'
	s3 = 'BRG'
	ans = 200000
	(cnt1, cnt2, cnt3) = (0, 0, 0)
	dp = [[0 for i in range(3)] for j in range(n + 1)]
	dp[0][0] = 0
	dp[0][1] = 0
	dp[0][2] = 0
	for i in range(n):
		if s[i] != s1[i % 3]:
			cnt1 += 1
		if s[i] != s2[i % 3]:
			cnt2 += 1
		if s[i] != s3[i % 3]:
			cnt3 += 1
		dp[i + 1][0] = cnt1
		dp[i + 1][1] = cnt2
		dp[i + 1][2] = cnt3
	for i in range(k, n + 1):
		ans = min(ans, dp[i][0] - dp[i - k][0], dp[i][1] - dp[i - k][1], dp[i][2] - dp[i - k][2])
	print(ans)


import sys
input = sys.stdin.readline
RI = lambda : [int(x) for x in sys.stdin.readline().strip().split()]
rw = lambda : input().strip().split()
infinite = float('inf')
t = int(input())
for _ in range(t):
	(n, k) = RI()
	s = input()
	mini = n
	count = 0
	test = 'RGB' * (k // 3 + 5)
	for j in range(k):
		if s[j] != test[j]:
			count += 1
	mini = min(count, mini)
	count1 = count
	count = 0
	test = 'GBR' * (k // 3 + 5)
	for j in range(k):
		if s[j] != test[j]:
			count += 1
	mini = min(count, mini)
	count2 = count
	count = 0
	test = 'BRG' * (k // 3 + 5)
	for j in range(k):
		if s[j] != test[j]:
			count += 1
	mini = min(count, mini)
	count3 = count
	count = 0
	for i in range(1, n - k + 1):
		if s[i - 1] == 'B':
			count = count3
			temp = 'RGB' * (k // 3 + 5)
			if s[i + k - 1] != temp[k - 1]:
				count += 1
			mini = min(count, mini)
			count1_new = count
			count = count1 - 1
			temp = 'GBR' * (k // 3 + 5)
			if s[i + k - 1] != temp[k - 1]:
				count += 1
			mini = min(count, mini)
			count2_new = count
			temp = 'BRG' * (k // 3 + 5)
			count = count2 - 1
			if s[i + k - 1] != temp[k - 1]:
				count += 1
			mini = min(count, mini)
			count3_new = count
			count1 = count1_new
			count2 = count2_new
			count3 = count3_new
		elif s[i - 1] == 'R':
			temp = 'GBR' * (k // 3 + 5)
			count = count1
			if s[i + k - 1] != temp[k - 1]:
				count += 1
			mini = min(count, mini)
			count2_new = count
			temp = 'BRG' * (k // 3 + 5)
			count = count2 - 1
			if s[i + k - 1] != temp[k - 1]:
				count += 1
			mini = min(count, mini)
			count3_new = count
			temp = 'RGB' * (k // 3 + 5)
			count = count3 - 1
			if s[i + k - 1] != temp[k - 1]:
				count += 1
			mini = min(count, mini)
			count1_new = count
			count1 = count1_new
			count2 = count2_new
			count3 = count3_new
		else:
			count = count2
			temp = 'BRG' * (k // 3 + 5)
			if s[i + k - 1] != temp[k - 1]:
				count += 1
			mini = min(count, mini)
			count3_new = count
			temp = 'RGB' * (k // 3 + 5)
			count = count3 - 1
			if s[i + k - 1] != temp[k - 1]:
				count += 1
			mini = min(count, mini)
			count1_new = count
			temp = 'GBR' * (k // 3 + 5)
			count = count1 - 1
			if s[i + k - 1] != temp[k - 1]:
				count += 1
			mini = min(count, mini)
			count2_new = count
			count1 = count1_new
			count2 = count2_new
			count3 = count3_new
	print(mini)


t = int(input())
for i in range(0, t):
	(n, k) = map(int, input().split())
	l1 = ['RGB', 'BRG', 'GBR']
	s = input()
	mi = n * k
	for j in range(0, n - k + 1):
		s2 = s[j:j + k]
		for s1 in l1:
			s1 = s1 * k
			s1 = s1[:k]
			if len(s1) == k and len(s2) == k:
				c = 0
				for p in range(0, k):
					if s1[p] != s2[p]:
						c = c + 1
						if c > mi:
							break
				mi = min(c, mi)
	if mi == n * k:
		for s1 in l1:
			s1 = s1 * k
			s1 = s1[:k]
			for p in range(0, k):
				if s1[p] != s[p]:
					c = c + 1
					if c > mi:
						break
			mi = min(c, mi)
	print(mi)


from sys import stdin, stdout
import cProfile, math
from collections import Counter, defaultdict, deque
from bisect import bisect_left, bisect, bisect_right
import itertools
from copy import deepcopy
from fractions import Fraction
import sys, threading
import operator as op
from functools import reduce
sys.setrecursionlimit(10 ** 6)
threading.stack_size(2 ** 27)
fac_warmup = False
printHeap = str()
memory_constrained = False
P = 10 ** 9 + 7
import sys

class merge_find:

	def __init__(self, n):
		self.parent = list(range(n))
		self.size = [1] * n
		self.num_sets = n
		self.lista = [[_] for _ in range(n)]

	def find(self, a):
		to_update = []
		while a != self.parent[a]:
			to_update.append(a)
			a = self.parent[a]
		for b in to_update:
			self.parent[b] = a
		return self.parent[a]

	def merge(self, a, b):
		a = self.find(a)
		b = self.find(b)
		if a == b:
			return
		if self.size[a] < self.size[b]:
			(a, b) = (b, a)
		self.num_sets -= 1
		self.parent[b] = a
		self.size[a] += self.size[b]
		self.lista[a] += self.lista[b]

	def set_size(self, a):
		return self.size[self.find(a)]

	def __len__(self):
		return self.num_sets

def display(string_to_print):
	stdout.write(str(string_to_print) + '\n')

def primeFactors(n):
	factors = dict()
	for i in range(2, math.ceil(math.sqrt(n)) + 1):
		while n % i == 0:
			if i in factors:
				factors[i] += 1
			else:
				factors[i] = 1
			n = n // i
	if n > 2:
		factors[n] = 1
	return factors

def all_factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def fibonacci_modP(n, MOD):
	if n < 2:
		return 1
	return (cached_fn(fibonacci_modP, (n + 1) // 2, MOD) * cached_fn(fibonacci_modP, n // 2, MOD) + cached_fn(fibonacci_modP, (n - 1) // 2, MOD) * cached_fn(fibonacci_modP, (n - 2) // 2, MOD)) % MOD

def factorial_modP_Wilson(n, p):
	if p <= n:
		return 0
	res = p - 1
	for i in range(n + 1, p):
		res = res * cached_fn(InverseEuler, i, p) % p
	return res

def binary(n, digits=20):
	b = bin(n)[2:]
	b = '0' * (digits - len(b)) + b
	return b

def isprime(n):
	if n < 4:
		return True
	if n % 2 == 0:
		return False
	if n % 3 == 0:
		return False
	i = 5
	w = 2
	while i * i <= n:
		if n % i == 0:
			return False
		i += w
		w = 6 - w
	return True

def generate_primes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 1
	return prime
factorial_modP = []

def warm_up_fac(MOD):
	global factorial_modP, fac_warmup
	if fac_warmup:
		return
	factorial_modP = [1 for _ in range(fac_warmup_size + 1)]
	for i in range(2, fac_warmup_size):
		factorial_modP[i] = factorial_modP[i - 1] * i % MOD
	fac_warmup = True

def InverseEuler(n, MOD):
	return pow(n, MOD - 2, MOD)

def nCr(n, r, MOD):
	global fac_warmup, factorial_modP
	if not fac_warmup:
		warm_up_fac(MOD)
		fac_warmup = True
	return factorial_modP[n] * (pow(factorial_modP[r], MOD - 2, MOD) * pow(factorial_modP[n - r], MOD - 2, MOD) % MOD) % MOD

def test_print(*args):
	if testingMode:
		print(args)

def display_list(list1, sep=' '):
	stdout.write(sep.join(map(str, list1)) + '\n')

def display_2D_list(li):
	for i in li:
		print(i)

def prefix_sum(li):
	sm = 0
	res = []
	for i in li:
		sm += i
		res.append(sm)
	return res

def get_int():
	return int(stdin.readline().strip())

def get_tuple():
	return map(int, stdin.readline().split())

def get_list():
	return list(map(int, stdin.readline().split()))
import heapq, itertools
pq = []
entry_finder = {}
REMOVED = '<removed-task>'

def add_task(task, priority=0):
	if task in entry_finder:
		remove_task(task)
	count = next(counter)
	entry = [priority, count, task]
	entry_finder[task] = entry
	heapq.heappush(pq, entry)

def remove_task(task):
	entry = entry_finder.pop(task)
	entry[-1] = REMOVED

def pop_task():
	while pq:
		(priority, count, task) = heapq.heappop(pq)
		if task is not REMOVED:
			del entry_finder[task]
			return task
	raise KeyError('pop from an empty priority queue')
memory = dict()

def clear_cache():
	global memory
	memory = dict()

def cached_fn(fn, *args):
	global memory
	if args in memory:
		return memory[args]
	else:
		result = fn(*args)
		memory[args] = result
		return result

def ncr(n, r):
	return math.factorial(n) / (math.factorial(n - r) * math.factorial(r))

def binary_serach(i, li):
	fn = lambda x: li[x] - x // i
	x = -1
	b = len(li)
	while b >= 1:
		while b + x < len(li) and fn(b + x) > 0:
			x += b
		b = b // 2
	return x
TestCases = True
testingMode = False
fac_warmup_size = 10 ** 5 + 100
optimiseForReccursion = False
from math import factorial

def main():
	(n, k) = get_tuple()
	st = stdin.readline()
	li = ['R', 'G', 'B']
	for offset in range(3):
		cnt = 0
		for i in range(k):
			if st[i] != li[(i + offset) % 3]:
				cnt += 1
		if offset == 0:
			min_cnt = cnt
		min_cnt = min(min_cnt, cnt)
		for i in range(k, n):
			if st[i - k] != li[(i - k + offset) % 3]:
				cnt -= 1
			if st[i] != li[(i + offset) % 3]:
				cnt += 1
			min_cnt = min(min_cnt, cnt)
	print(min_cnt)
if TestCases:
	for i in range(get_int()):
		cProfile.run('main()') if testingMode else main()
else:
	(cProfile.run('main()') if testingMode else main()) if not optimiseForReccursion else threading.Thread(target=main).start()


from sys import stdin
from math import ceil
input = stdin.readline
for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	s = input().rstrip()
	series = 'RGB' * (ceil(k / 3) + 1)
	m = k
	for i in range(3):
		for j in range(n - k + 1):
			c = 0
			for (ch, ser) in zip(s[j:j + k], series[i:][:k]):
				if ch == ser:
					c += 1
			c = k - c
			if c < m:
				m = c
	print(m)


from sys import stdin, stdout
from collections import Counter
import math

def rsingle_int():
	return int(stdin.readline().rstrip())

def rmult_int():
	return [int(x) for x in stdin.readline().rstrip().split()]

def rmult_str():
	return stdin.readline().rstrip().split()

def r_str():
	return stdin.readline().rstrip()

def rsingle_char():
	return stdin.read(1)
rgbs = {'R': 'RGB', 'G': 'GBR', 'B': 'BRG'}

def create_rgb(start, k):
	temp = rgbs[start]
	return temp * (k // 3) + temp[:k % 3]

def main():
	q = rsingle_int()
	rgb = 'RGB'
	for i in range(q):
		(n, k) = rmult_int()
		s = r_str()
		all_min = k
		for shift in range(3):
			rgb_temp = create_rgb(rgb[shift], n)
			diff = []
			for i in range(n):
				if s[i] == rgb_temp[i]:
					diff.append(0)
				else:
					diff.append(1)
			min = 0
			for i in range(n):
				min += diff[i]
				if i >= k:
					min -= diff[i - k]
				if i >= k - 1:
					if all_min > min:
						all_min = min
		print(all_min)
main()


import math
import sys
import collections

def getdict(n):
	d = {}
	if type(n) is list:
		for i in n:
			if i in d:
				d[i] += 1
			else:
				d[i] = 1
	else:
		for i in range(n):
			t = ii()
			if t in d:
				d[t] += 1
			else:
				d[t] = 1
	return d

def cdiv(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))
a = 'RGB' * 2000
b = a[1:]
c = a[2:]

def minSum(arr, n, k):
	res = 0
	for i in range(k):
		res += arr[i]
	curr_sum = res
	for i in range(k, n):
		curr_sum += arr[i] - arr[i - k]
		res = min(res, curr_sum)
	return res
t = ii()
for i in range(t):
	(n, k) = mi()
	s = input()
	da = []
	db = []
	dc = []
	for i in range(n):
		da.append(0) if a[i] == s[i] else da.append(1)
		db.append(0) if b[i] == s[i] else db.append(1)
		dc.append(0) if c[i] == s[i] else dc.append(1)
	print(min(minSum(da, n, k), minSum(db, n, k), minSum(dc, n, k)))


t = int(input())
v = 'RGB' * 800
for i in range(t):
	(n, k) = map(int, input().split())
	s = input()
	p = 0
	pr = ''
	l = v[0:k]
	maxp = 100000000000
	for i in range(n - k + 1):
		j = i
		p = 0
		x = 0
		while j - k < i:
			if s[j] != l[x]:
				p += 1
			j += 1
			x += 1
		if p < maxp:
			maxp = p
	l = v[1:k + 1]
	p = 0
	for i in range(n - k + 1):
		j = i
		p = 0
		x = 0
		while j - k < i:
			if s[j] != l[x]:
				p += 1
			j += 1
			x += 1
		if p < maxp:
			maxp = p
	l = v[2:k + 2]
	p = 0
	for i in range(n - k + 1):
		j = i
		p = 0
		x = 0
		while j - k < i:
			if s[j] != l[x]:
				p += 1
			j += 1
			x += 1
		if p < maxp:
			maxp = p
	print(maxp)


import os
import sys
from math import ceil, floor, sqrt, gcd, factorial
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		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')

def main():
	for _ in range(int(input())):
		(n, k) = map(int, input().split())
		s = input()
		t = ceil(n / 3) + 1
		rgb = 'RGB' * t
		r = rgb[:n]
		g = rgb[1:n + 1]
		b = rgb[2:n + 2]
		r1 = []
		g1 = []
		b1 = []
		rr = 0
		gg = 0
		bb = 0
		for i in range(n):
			if s[i] == r[i]:
				rr += 1
				r1.append(True)
			else:
				r1.append(False)
			if s[i] == g[i]:
				gg += 1
				g1.append(True)
			else:
				g1.append(False)
			if s[i] == b[i]:
				bb += 1
				b1.append(True)
			else:
				b1.append(False)
		diff1 = []
		diff2 = []
		diff3 = []
		for i in range(0, n - k + 1):
			diff1.append(r1[i:i + k].count(False))
			diff2.append(g1[i:i + k].count(False))
			diff3.append(b1[i:i + k].count(False))
		print(min(min(diff1), min(diff2), min(diff3)))
main()


def main():
	Q = int(input())
	data = []
	for _ in range(Q):
		(N, K) = map(int, input().split())
		S = input()
		data.append([N, K, S])
	RGB = []
	for i in range(3000):
		if i % 3 == 0:
			RGB.append('R')
		elif i % 3 == 1:
			RGB.append('G')
		else:
			RGB.append('B')
	for (N, K, S) in data:
		ans = N
		for j in range(3):
			for i in range(N + 1 - K):
				c = 0
				for k in range(K):
					if S[i + k] != RGB[i + j + k]:
						c += 1
				ans = min(ans, c)
		print(ans)
main()


import sys
input = sys.stdin.readline
pure = 'RGB' * 100006
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	ans = 0
	Rc = 0
	Gc = 0
	Bc = 0
	count = 0
	for i in range(k):
		if pure[i] == s[i]:
			Rc += 1
		elif pure[i - 1] == s[i]:
			Bc += 1
		elif pure[i - 2] == s[i]:
			Gc += 1
	ans = max(ans, Rc, Gc, Bc)
	for i in range(k, n):
		if pure[i] == s[i]:
			Rc += 1
		if pure[i - k] == s[i - k]:
			Rc -= 1
		if pure[i - 1] == s[i]:
			Bc += 1
		if pure[i - k - 1] == s[i - k]:
			Bc -= 1
		if pure[i - 2] == s[i]:
			Gc += 1
		if pure[i - k - 2] == s[i - k]:
			Gc -= 1
		ans = max(ans, Rc, Gc, Bc)
	print(k - ans)


q = int(input())
dic = {'R': 0, 'G': 1, 'B': 2}
board = []
board.append('RGB' * 700)
board.extend(['GBR' * 700, 'BRG' * 700])

def hamming(t, model):
	l = len(t)
	cou = 0
	for i in range(l):
		if t[i] != model[i]:
			cou += 1
	return cou
for _ in range(q):
	(n, k) = map(int, input().split())
	s = str(input())
	mi = 2000
	for i in range(n - k + 1):
		mi = min(mi, hamming(s[i:i + k], board[0]))
		mi = min(mi, hamming(s[i:i + k], board[1]))
		mi = min(mi, hamming(s[i:i + k], board[2]))
	print(mi)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	doosraarr = [0] * n
	s = input()
	r = [0] * (n + 2)
	g = [0] * (n + 2)
	b = [0] * (n + 2)
	array = [1] * n
	array[0] = 0
	hfjv = 'RGB'
	klkn = 'GBR'
	uiok = 'BRG'
	array[0] = 0
	for i in range(1, n + 1):
		array[0] = 0
		r[i] = r[i - 1]
		g[i] = g[i - 1]
		b[i] = b[i - 1]
		if s[i - 1] != hfjv[(i - 1) % 3]:
			r[i] += 1
		if s[i - 1] != klkn[(i - 1) % 3]:
			g[i] += 1
			array[0] = 0
		if s[i - 1] != uiok[(i - 1) % 3]:
			b[i] += 1
			array[0] = 0
		array[0] = 0
	mi = []
	for i in range(k, n + 1):
		mi.append(min(r[i] - r[i - k], g[i] - g[i - k], b[i] - b[i - k]))
	print(min(mi))


def main():
	q = int(input())
	for i in range(q):
		(n, k) = map(int, input().split())
		s = input()
		min_ans = 10 ** 9
		for i in range(n - k + 1):
			count1 = 0
			count2 = 0
			count3 = 0
			for j in range(k):
				if (i + j) % 3 == 0:
					if s[i + j] != 'R':
						count1 += 1
					if s[i + j] != 'G':
						count2 += 1
					if s[i + j] != 'B':
						count3 += 1
				if (i + j) % 3 == 1:
					if s[i + j] != 'G':
						count1 += 1
					if s[i + j] != 'B':
						count2 += 1
					if s[i + j] != 'R':
						count3 += 1
				if (i + j) % 3 == 2:
					if s[i + j] != 'B':
						count1 += 1
					if s[i + j] != 'R':
						count2 += 1
					if s[i + j] != 'G':
						count3 += 1
			min_ans = min(min_ans, count1, count2, count3)
		print(min_ans)
main()


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	co = 'RGB'
	ans = 2000
	for i in range(n - k + 1):
		for j in range(3):
			t = cur = 0
			for jj in range(i, i + k):
				if s[jj] != co[(j + t) % 3]:
					cur += 1
				t += 1
			ans = min(ans, cur)
	print(ans)


import sys

def subset(l, n, k):
	p = 0
	m = 1000000
	p = l[k - 1]
	m = min(m, p)
	for i in range(1, n - k + 1):
		p = l[i + k - 1] - l[i - 1]
		m = min(m, p)
	return m
p = 'RGB' * 100000
for _ in range(int(sys.stdin.readline())):
	(n, k) = [int(i) for i in sys.stdin.readline().split()]
	s = sys.stdin.readline()
	if n == 1:
		sys.stdout.write(str(0) + '\n')
	else:
		ans = 1000000
		for i in range(3):
			b = p[i:i + n]
			l = []
			a = 0
			for j in range(n):
				if s[j] != b[j]:
					a = a + 1
				l.append(a)
			v = subset(l, n, k)
			ans = min(ans, v)
		sys.stdout.write(str(ans) + '\n')


from sys import stdin
input = stdin.readline
R = lambda : map(int, input().split())
(q,) = R()
a = 'RGB'
an = [0] * 3
for q1 in range(q):
	(n, k) = R()
	s = input()
	for i2 in range(3):
		ans = [0] * n
		for i in range(n):
			ans[i] = s[i] == a[(i + i2) % 3]
		m = ans[0:k].count(0)
		m1 = m
		for i in range(k, n):
			m1 += -ans[i] + ans[i - k]
			m = min(m, m1)
		an[i2] = m
	print(min(an))


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	c = 'RGB'
	ret = 10 ** 9
	for i in range(n - k + 1):
		for j in range(3):
			cu = 0
			for x in range(k):
				cu += s[i + x] != c[(x + j) % 3]
			ret = min(cu, ret)
	print(ret)


import atexit, io, sys
input_lines = sys.stdin.read().splitlines()
input = iter(input_lines).__next__
out_buffer = io.StringIO()
sys.stdout = out_buffer

@atexit.register
def write():
	sys.__stdout__.write(out_buffer.getvalue())

def line():
	inp = input().split()
	if len(inp) == 1:
		return int(inp[0])
	return map(int, inp)
from itertools import repeat, cycle
q = line()
for _ in repeat(None, q):
	(n, k) = line()
	s = input()
	w = []
	for i in range(3):
		rgb = cycle('RGB')
		for _ in repeat(None, i):
			next(rgb)
		e = [c != next(rgb) for c in s]
		r = sum(e[:k])
		m = r
		for j in range(n - k):
			r += e[j + k] - e[j]
			m = min(m, r)
		w.append(m)
	print(min(w))


q = int(input())
f = []
t = 'RGB' * (2000 // 3 + 1)
for i in range(q):
	(n, k) = map(int, input().split())
	s = input()
	s1 = t[:k]
	s2 = t[1:k + 1]
	s3 = t[2:k + 2]
	mi = k
	for i in range(n + 1 - k):
		num = [0, 0, 0]
		for j in range(k):
			if s[i + j] == s1[j]:
				num[1] += 1
				num[2] += 1
			elif s[i + j] == s2[j]:
				num[0] += 1
				num[2] += 1
			else:
				num[0] += 1
				num[1] += 1
		u = min(num)
		if u < mi:
			mi = u
	f += [mi]
for i in f:
	print(i)


st = ''.join(['RGB'] * 10000)
ins = ['R', 'G', 'B']
for i in range(int(input())):
	(n, k) = list(map(int, input().split()))
	data = input()
	min_ans = 999999999999
	for j in range(3):
		ans = 0
		for i in range(k):
			if ins[(i + j) % 3] != data[i]:
				ans += 1
		if min_ans > ans:
			min_ans = ans
		for i in range(k, n):
			if ins[(i + j) % 3] != data[i]:
				ans += 1
			if ins[(i - k + j) % 3] != data[i - k]:
				ans -= 1
			if min_ans > ans:
				min_ans = ans
	print(min_ans)


import sys
from collections import defaultdict as dd
mod = 10 ** 9 + 7

def ri(flag=0):
	if flag == 0:
		return [int(i) for i in sys.stdin.readline().split()]
	else:
		return int(sys.stdin.readline())
for _ in range(int(input())):
	(n, k) = ri()
	a = input()
	rgb = [0 for i in range(n)]
	gbr = [0 for i in range(n)]
	brg = [0 for i in range(n)]
	for i in range(n):
		if i % 3 == 0:
			if a[i] != 'R':
				rgb[i] += 1
		if i % 3 == 1:
			if a[i] != 'G':
				rgb[i] += 1
		if i % 3 == 2:
			if a[i] != 'B':
				rgb[i] += 1
	for i in range(n):
		if i % 3 == 0:
			if a[i] != 'G':
				gbr[i] += 1
		if i % 3 == 1:
			if a[i] != 'B':
				gbr[i] += 1
		if i % 3 == 2:
			if a[i] != 'R':
				gbr[i] += 1
	for i in range(n):
		if i % 3 == 0:
			if a[i] != 'B':
				brg[i] += 1
		if i % 3 == 1:
			if a[i] != 'R':
				brg[i] += 1
		if i % 3 == 2:
			if a[i] != 'G':
				brg[i] += 1
	for i in range(1, n):
		rgb[i] += rgb[i - 1]
		brg[i] += brg[i - 1]
		gbr[i] += gbr[i - 1]
	ans = 999999999
	for i in range(k - 1, n):
		if i - k == -1:
			ans = min(ans, rgb[i], gbr[i], brg[i])
		else:
			ans = min(ans, rgb[i] - rgb[i - k], gbr[i] - gbr[i - k], brg[i] - brg[i - k])
	print(ans)


rgb = 'RGB'
gbr = 'GBR'
brg = 'BRG'
for case in range(int(input())):
	(n, k) = [int(x) for x in input().split()]
	string = input()
	R = [not c == rgb[i % 3] for (i, c) in enumerate(string)]
	G = [not c == gbr[i % 3] for (i, c) in enumerate(string)]
	B = [not c == brg[i % 3] for (i, c) in enumerate(string)]
	red = sum(R[:k])
	green = sum(G[:k])
	blue = sum(B[:k])
	best = min(red, green, blue)
	for i in range(n - k):
		red = red - R[i] + R[i + k]
		green = green - G[i] + G[i + k]
		blue = blue - B[i] + B[i + k]
		best = min(best, red, green, blue)
	print(best)


from sys import stdin, stdout
riga = stdin.readline()
query = int(riga)
for i in range(query):
	riga = stdin.readline()
	test = riga.split()
	n = int(test[0])
	k = int(test[1])
	check = [['R', 'G', 'B'], ['G', 'B', 'R'], ['B', 'R', 'G']]
	dp = [[], [], []]
	sol = [0, 0, 0]
	for i in range(n):
		for j in range(3):
			dp[j].append(0)
	stringa = stdin.readline()
	risultato = -1
	for i in range(n):
		if i >= k:
			for j in range(3):
				sol[j] = sol[j] - dp[j][i - k]
		for j in range(3):
			if check[j][i % 3] != stringa[i]:
				dp[j][i] = 1
			sol[j] = sol[j] + dp[j][i]
			if i >= k - 1:
				if risultato == -1:
					risultato = sol[j]
				else:
					risultato = min(risultato, sol[j])
	stdout.write(str(risultato) + '\n')


q = int(input())
while q:
	(n, k) = map(int, input().split())
	s = input()
	b = []
	for i in range(n - k + 1):
		a = s[i:k + i]
		count = 0
		for j in range(k):
			if j % 3 == 0:
				if a[j] != 'R':
					count += 1
			if j % 3 == 1:
				if a[j] != 'G':
					count += 1
			if j % 3 == 2:
				if a[j] != 'B':
					count += 1
		b.append(count)
		count = 0
		for j in range(k):
			if j % 3 == 0:
				if a[j] != 'G':
					count += 1
			if j % 3 == 1:
				if a[j] != 'B':
					count += 1
			if j % 3 == 2:
				if a[j] != 'R':
					count += 1
		b.append(count)
		count = 0
		for j in range(k):
			if j % 3 == 0:
				if a[j] != 'B':
					count += 1
			if j % 3 == 1:
				if a[j] != 'R':
					count += 1
			if j % 3 == 2:
				if a[j] != 'G':
					count += 1
		b.append(count)
	print(min(b))
	q -= 1


import sys
input = sys.stdin.readline
for nt in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	s1 = 'RGB'
	dp = [[0, 0, 0] for i in range(n)]
	ans = 10 ** 18
	for i in range(3):
		for j in range(k):
			if s[j] != s1[(i + j) % 3]:
				dp[0][i] += 1
		ans = min(ans, dp[0][i])
		for j in range(1, n - k + 1):
			if s[j - 1] != s1[(i + j - 1) % 3]:
				dp[j][i] += dp[j - 1][i] - 1
			else:
				dp[j][i] += dp[j - 1][i]
			if s[j + k - 1] != s1[(i + j + k - 1) % 3]:
				dp[j][i] += 1
			ans = min(ans, dp[j][i])
	print(ans)


import sys
input = sys.stdin.readline
q = int(input())
aux = 'RGB'

def fun(k, s, ini):
	tam = 0
	ans = int(1e+20)
	change = 0
	i = 0
	j = 0
	while len(s) > i:
		while len(s) > i and k > tam:
			change += 1 if aux[(ini + i) % 3] != s[i] else 0
			tam += 1
			i += 1
		if tam == k:
			ans = min(ans, change)
		change -= 1 if s[j] != aux[(ini + j) % 3] else 0
		j += 1
		tam -= 1
	return ans
for _ in range(q):
	(n, k) = map(int, input().split(' '))
	s = input()
	print(min(fun(k, s, 0), min(fun(k, s, 1), fun(k, s, 2))))


import math

class Read:

	@staticmethod
	def int():
		return int(input())

	@staticmethod
	def list(sep=' '):
		return input().split(sep)

	@staticmethod
	def list_int(sep=' '):
		return list(map(int, input().split(sep)))

def solve():
	(n, k) = Read.list_int()
	s = input()
	sf = 'RGB' * (k + 2)
	max_s = 0
	for i in range(n - k + 1):
		for j in range(3):
			count = 0
			for b in range(k):
				if sf[j + b] == s[i + b]:
					count += 1
			if count > max_s:
				max_s = count
	print(k - max_s)
query_count = Read.int()
for j in range(query_count):
	solve()


import sys
from itertools import accumulate

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7

def check(l, r):
	res = 0
	for i in range(3):
		cnt = 0
		for a in range(3):
			cnt += acc[a][(i + a) % 3][r] - acc[a][(i + a) % 3][l]
		res = max(res, cnt)
	return res
for _ in range(INT()):
	(N, K) = MAP()
	S = input()
	A = [0] * N
	for (i, s) in enumerate(S):
		if s == 'G':
			A[i] = 1
		elif s == 'B':
			A[i] = 2
	acc = list3d(3, 3, N + 1, 0)
	for (i, a) in enumerate(A):
		acc[a][i % 3][i + 1] = 1
	for i in range(3):
		for j in range(3):
			acc[i][j] = list(accumulate(acc[i][j]))
	ans = 0
	for i in range(N - K + 1):
		j = i + K
		ans = max(ans, check(i, j))
	print(K - ans)


from sys import stdin
from collections import deque
c = int(stdin.readline().strip())
for cas in range(c):
	(n, m) = map(int, stdin.readline().strip().split())
	s = deque(stdin.readline().strip())
	arr = ['R', 'G', 'B']
	ans = n + 3
	for k in range(1):
		for i in range(3):
			x = i
			dp = [0 for i in range(n + 1)]
			for j in range(n):
				if s[j] != arr[x]:
					dp[j + 1] += 1
				dp[j + 1] += dp[j]
				if j + 1 >= m:
					ans = min(ans, dp[j + 1] - dp[j + 1 - m])
				x += 1
				x = x % 3
	print(ans)
1


import math
import sys
input = sys.stdin.readline
q = int(input())
for j in range(q):
	(n, k) = map(int, input().split())
	s = input()
	cycle = ['R', 'G', 'B']
	mina = math.inf
	for i in range(3):
		ans = [0] * n
		c = 0
		for j in range(n):
			if s[j] != cycle[(i + j) % 3]:
				ans[j] += 1
			c += ans[j]
			if j >= k:
				c -= ans[j - k]
			if j >= k - 1:
				mina = min(mina, c)
	sys.stdout.write(str(mina) + '\n')


import sys
input = sys.stdin.readline
a = int(input())
for i in range(a):
	(n, k) = map(int, input().split())
	dp = [[0 for i in range(n - k + 1)] for i in range(3)]
	s = input()
	c1 = 0
	c2 = 0
	c3 = 0
	for i in range(k):
		if i % 3 == 0:
			if s[i] == 'R':
				c2 += 1
				c3 += 1
			elif s[i] == 'G':
				c1 += 1
				c3 += 1
			else:
				c1 += 1
				c2 += 1
		if i % 3 == 1:
			if s[i] == 'R':
				c1 += 1
				c2 += 1
			if s[i] == 'G':
				c2 += 1
				c3 += 1
			if s[i] == 'B':
				c1 += 1
				c3 += 1
		if i % 3 == 2:
			if s[i] == 'R':
				c1 += 1
				c3 += 1
			elif s[i] == 'G':
				c1 += 1
				c2 += 1
			else:
				c2 += 1
				c3 += 1
	dp[0][0] = c1
	dp[1][0] = c2
	dp[2][0] = c3
	mini = min(c1, c2, c3)
	for i in range(1, n - k + 1):
		dp[0][i] = dp[2][i - 1] - int(s[i - 1] != 'B')
		dp[1][i] = dp[0][i - 1] - int(s[i - 1] != 'R')
		dp[2][i] = dp[1][i - 1] - int(s[i - 1] != 'G')
		t = i + k - 1
		if s[t] == 'R':
			if k % 3 == 0:
				dp[0][i] += 1
				dp[2][i] += 1
			if k % 3 == 1:
				dp[2][i] += 1
				dp[1][i] += 1
			if k % 3 == 2:
				dp[0][i] += 1
				dp[1][i] += 1
		if s[t] == 'G':
			if k % 3 == 0:
				dp[0][i] += 1
				dp[1][i] += 1
			if k % 3 == 1:
				dp[0][i] += 1
				dp[2][i] += 1
			if k % 3 == 2:
				dp[1][i] += 1
				dp[2][i] += 1
		if s[t] == 'B':
			if k % 3 == 0:
				dp[1][i] += 1
				dp[2][i] += 1
			if k % 3 == 1:
				dp[0][i] += 1
				dp[1][i] += 1
			if k % 3 == 2:
				dp[0][i] += 1
				dp[2][i] += 1
		mini = min(mini, dp[0][i], dp[1][i], dp[2][i])
	print(mini)


q = int(input())
for _ in range(q):
	(n, k) = map(int, input().split())
	s = str(input())
	pre1 = []
	for i in range(k):
		if i % 3 == 0:
			pre1.append('R')
		elif i % 3 == 1:
			pre1.append('G')
		else:
			pre1.append('B')
	pre2 = []
	for i in range(k):
		if i % 3 == 0:
			pre2.append('G')
		elif i % 3 == 1:
			pre2.append('B')
		else:
			pre2.append('R')
	pre3 = []
	for i in range(k):
		if i % 3 == 0:
			pre3.append('B')
		elif i % 3 == 1:
			pre3.append('R')
		else:
			pre3.append('G')
	pre1 = ''.join(pre1)
	pre2 = ''.join(pre2)
	pre3 = ''.join(pre3)
	ans = 10 ** 18
	for i in range(n - k + 1):
		c1 = 0
		c2 = 0
		c3 = 0
		for j in range(i, i + k):
			x = s[j]
			if pre1[j - i] != x:
				c1 += 1
			if pre2[j - i] != x:
				c2 += 1
			if pre3[j - i] != x:
				c3 += 1
		ans = min(ans, min([c1, c2, c3]))
	print(ans)


import sys
t = int(input())
for _ in range(t):
	(n, k) = map(int, sys.stdin.readline().split())
	s = sys.stdin.readline()
	d = {}
	d['R'] = [0, 0, 0]
	d['G'] = [0, 0, 0]
	d['B'] = [0, 0, 0]
	res = 0
	for i in range(k):
		d[s[i]][i % 3] += 1
	res = max(d['R'][0] + d['G'][1] + d['B'][2], d['R'][1] + d['G'][2] + d['B'][0], d['R'][2] + d['G'][0] + d['B'][1])
	for i in range(k, n):
		d[s[i]][i % 3] += 1
		d[s[i - k]][(i - k) % 3] -= 1
		res = max(res, d['R'][0] + d['G'][1] + d['B'][2], d['R'][1] + d['G'][2] + d['B'][0], d['R'][2] + d['G'][0] + d['B'][1])
		if res >= k:
			break
	print(k - res)


for i in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	mindiff = 10000
	for u in range(n - k + 1):
		diffRGB = 0
		diffGBR = 0
		diffBRG = 0
		for t in range(k):
			if s[u + t] != 'RGB'[t % 3]:
				diffRGB += 1
			if s[u + t] != 'GBR'[t % 3]:
				diffGBR += 1
			if s[u + t] != 'BRG'[t % 3]:
				diffBRG += 1
		mindiff = min(mindiff, diffBRG, diffGBR, diffRGB)
	print(mindiff)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	n = len(s)
	dp = [[0] * (n + 1) for j in range(3)]
	a = ['R', 'G', 'B']
	i = 0
	for q in range(3):
		i = q
		for j in range(1, n + 1):
			i = i % 3
			if s[j - 1] != a[i]:
				dp[q][j] = 1 + dp[q][j - 1]
			else:
				dp[q][j] = dp[q][j - 1]
			i += 1
	mi = 10 ** 6
	for i in range(3):
		for j in range(k, n + 1):
			mi = min(mi, dp[i][j] - dp[i][j - k])
	print(mi)


import sys
input = lambda : sys.stdin.readline().strip()
from math import ceil

def mismatch(s1, s2):
	cnt = 0
	for i in range(len(s1)):
		if s1[i] != s2[i]:
			cnt += 1
	return cnt
T = int(input())
for _ in range(T):
	(n, k) = map(int, input().split())
	check = ''
	for i in range(ceil((k + 2) / 3)):
		check += 'RGB'
	ls = []
	for i in range(3):
		ls.append(check[i:i + k])
	s = input()
	m = n
	for i in range(n - k + 1):
		for j in ls:
			m = min(m, mismatch(s[i:i + k], j))
	print(m)


import sys
q = int(sys.stdin.readline())
for _ in range(q):
	(n, k) = map(int, sys.stdin.readline().split())
	string = list(sys.stdin.readline().strip())
	sub = []
	for i in range(n):
		sub.append(string[i:i + k])
		if i + k == len(string):
			break
	res = []
	for i in range(len(sub)):
		cnt1 = 0
		cnt2 = 0
		cnt3 = 0
		for j in range(len(sub[i])):
			if sub[i][j] == 'R':
				if j % 3 == 0:
					cnt2 += 1
					cnt3 += 1
				elif j % 3 == 1:
					cnt1 += 1
					cnt2 += 1
				elif j % 3 == 2:
					cnt1 += 1
					cnt3 += 1
			elif sub[i][j] == 'G':
				if j % 3 == 0:
					cnt1 += 1
					cnt3 += 1
				elif j % 3 == 1:
					cnt2 += 1
					cnt3 += 1
				elif j % 3 == 2:
					cnt1 += 1
					cnt2 += 1
			elif sub[i][j] == 'B':
				if j % 3 == 0:
					cnt1 += 1
					cnt2 += 1
				elif j % 3 == 1:
					cnt1 += 1
					cnt3 += 1
				elif j % 3 == 2:
					cnt2 += 1
					cnt3 += 1
		res.append(cnt1)
		res.append(cnt2)
		res.append(cnt3)
	print(min(res))


import sys
from collections import deque
input = sys.stdin.readline
q = int(input())
sample = 'RGB'
for _ in range(q):
	(n, k) = map(int, input().split())
	s = input()
	ans = k
	for t in range(3):
		cnt = 0
		x = t
		d = deque()
		for i in range(k):
			e = s[i]
			if e != sample[x]:
				d.append(1)
				cnt += 1
			else:
				d.append(0)
			x = (x + 1) % 3
		ans = min(ans, cnt)
		for i in range(k, n):
			a = d.popleft()
			e = s[i]
			if e != sample[x]:
				d.append(1)
				cnt += 1 - a
			else:
				d.append(0)
				cnt -= a
			x = (x + 1) % 3
			ans = min(ans, cnt)
	print(ans)


import sys
input = sys.stdin.readline

def solve(d, n, k):
	mv = sum(d[0:k])
	v = mv
	for i in range(1, n - k + 1):
		mv = mv + d[i + k - 1] - d[i - 1]
		v = min(v, mv)
	return v
for _ in range(int(input())):
	(n, k) = tuple(map(int, input().split()))
	s = input()
	st = 'RGB' * (n // 3 + 3)
	(diff1, diff2, diff3) = ([0 for _ in range(n)], [0 for _ in range(n)], [0 for _ in range(n)])
	for i in range(n):
		if s[i] != st[i]:
			diff1[i] = 1
		if s[i] != st[i + 1]:
			diff2[i] = 1
		if s[i] != st[i + 2]:
			diff3[i] = 1
	print(min(solve(diff1, n, k), solve(diff2, n, k), solve(diff3, n, k)))


t = 'RGB'
for i in range(int(input())):
	(n, k) = map(int, input().split())
	a = str(input())
	ans = 10 ** 9
	for j in range(0, n - k + 1):
		for off in range(0, 3):
			cur = 0
			for p in range(0, k):
				if a[j + p] != t[(p + off) % 3]:
					cur += 1
			ans = min(ans, cur)
	print(ans)


nm = int(input())
for o in range(nm):
	(n, k) = map(int, input().split())
	s = input()
	t = 'RGB'
	q = []
	for i in range(n - k + 1):
		for j in range(3):
			count = 0
			for pos in range(k):
				if s[i + pos] != t[(pos + j) % 3]:
					count += 1
			q.append(count)
	print(min(q))


global main_string
main_string = list('RGB' * 3000)

def diff(s, k):
	diffs = []
	r = 0
	copy_s = main_string[:]
	for i in range(k):
		if s[i] != copy_s[i]:
			r += 1
	diffs.append(r)
	r = 0
	copy_s.pop(0)
	for i in range(k):
		if s[i] != copy_s[i]:
			r += 1
	diffs.append(r)
	copy_s.pop(0)
	r = 0
	for i in range(k):
		if s[i] != copy_s[i]:
			r += 1
	diffs.append(r)
	return min(diffs)
q = int(input())
for i in range(q):
	(n, k) = map(int, input().split())
	s = input()
	ans = 999999999999
	for i in range(0, n - k + 1):
		d = diff(s[i:i + k], k)
		if ans > d:
			ans = d
	print(ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	rgb = 'RGB'
	least1 = 20000000
	for j in range(3):
		ans = [0 for i in range(n)]
		for i in range(n):
			if s[i] != rgb[(i + j) % 3]:
				ans[i] = 1
		least = sum(ans[:k])
		total = sum(ans[:k])
		for i in range(k, n):
			total = total + ans[i] - ans[i - k]
			least = min(least, total)
		least1 = min(least1, least)
	print(least1)


import sys
q = int(sys.stdin.readline())
ans_arr = []
for x in range(q):
	[n, k] = [int(i) for i in sys.stdin.readline().split()]
	s = sys.stdin.readline()
	arr = []
	for i in range(n + 1):
		tmp = []
		for j in range(3):
			tmp.append(0)
		arr.append(tmp)
	arr[0][0] = 0
	arr[0][1] = 0
	arr[0][2] = 0
	for x in range(1, n + 1):
		if s[x - 1] == 'R':
			arr[x][0] = arr[x - 1][2]
			arr[x][1] = arr[x - 1][0] + 1
			arr[x][2] = arr[x - 1][1] + 1
		elif s[x - 1] == 'G':
			arr[x][0] = arr[x - 1][2] + 1
			arr[x][1] = arr[x - 1][0]
			arr[x][2] = arr[x - 1][1] + 1
		elif s[x - 1] == 'B':
			arr[x][0] = arr[x - 1][2] + 1
			arr[x][1] = arr[x - 1][0] + 1
			arr[x][2] = arr[x - 1][1]
	rev = ['R', 'B', 'G']
	d = {}
	d['R'] = 0
	d['G'] = 1
	d['B'] = 2
	mini = k + 1
	for w in range(k, n + 1):
		mini = min(arr[w][0] - arr[w - k][d[rev[k % 3]]], mini)
		mini = min(arr[w][1] - arr[w - k][d[rev[(k + 2) % 3]]], mini)
		mini = min(arr[w][2] - arr[w - k][d[rev[(k + 1) % 3]]], mini)
	ans_arr.append(str(mini))
print('\n'.join(ans_arr))


import sys
for _ in range(int(sys.stdin.readline())):
	(n, k) = [int(i) for i in sys.stdin.readline().split()]
	st = sys.stdin.readline().strip()
	stri = ['R', 'G', 'B']
	best = k
	for i in range(3):
		errcount = 0
		for j in range(k):
			if st[j] != stri[(j + i) % 3]:
				errcount += 1
		best = min(best, errcount)
		for a in range(k, n):
			if st[a] != stri[(a + i) % 3]:
				errcount += 1
			if st[a - k] != stri[(a + i - k) % 3]:
				errcount -= 1
			best = min(best, errcount)
	print(best)


import math
pattern = 'RGB'
q = int(input())
for _ in range(q):
	(N, K) = map(int, input().split())
	S = input()
	ans = math.inf
	for i in range(N - K + 1):
		for offset in range(3):
			change = 0
			for k in range(K):
				if S[i + k] != pattern[(k + offset) % 3]:
					change += 1
			ans = min(ans, change)
	print(ans)


q = int(input())
t = 'RGB' * (2000 // 3 + 1)
for _ in range(q):
	(n, k) = map(int, input().split())
	s = input()
	mi = k
	for i in range(n - k + 1):
		subs = s[i:i + k]
		for j in range(3):
			subt = t[j:k + j]
			diff = 0
			for l in range(k):
				if subs[l] != subt[l]:
					diff += 1
			mi = min(mi, diff)
	print(mi)


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	ans = 10 ** 9
	a = ['RGB' * k, 'GBR' * k, 'BRG' * k]
	for i in range(n - k + 1):
		for x in a:
			cnt = 0
			for j in range(k):
				if s[i + j] != x[j]:
					cnt += 1
			ans = min(ans, cnt)
	print(ans)


test = 'RGB' * 100000
for i in range(int(input())):
	(a, m) = map(int, input().split())
	s = input()
	f = 100000000
	ans = [[0] * a for i in range(3)]
	for i in range(3):
		for j in range(a):
			ans[i][j] += s[j] != test[i + j]
	for i in range(3):
		mm = sum(ans[i][:m])
		f = min(f, mm)
		for j in range(m, a):
			mm -= ans[i][j - m]
			mm += ans[i][j]
			f = min(f, mm)
	print(f)


import os
import sys
from io import BytesIO, IOBase

def main():
	cmpare = 'RGB' * 5000
	for _ in range(int(input())):
		(n, k) = map(int, input().split(' '))
		s = input()
		realans = int(10000000000.0)
		for x in range(n - k + 1):
			for start in range(3):
				cnt = x
				ans = 0
				for z in range(start, start + k):
					if cmpare[z] != s[cnt]:
						ans += 1
					cnt += 1
				realans = min(ans, realans)
		print(realans)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		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')
main()


s = 'RGB' * 666
rgb = ['GB' + s]
rgb.append('RGB' + s)
rgb.append('BRGB' + s)
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	t = input()
	l = len(t)
	ans = 2001
	a = [0] * l
	for i in range(3):
		a[0] = 1 if rgb[i][0] != t[0] else 0
		for j in range(1, l):
			a[j] = a[j - 1] + (1 if rgb[i][j] != t[j] else 0)
		ans = min(ans, a[k - 1])
		for j in range(k, l):
			ans = min(ans, a[j] - a[j - k])
	print(ans)


import sys

def search_for_the_largest_number_of_zeros(character_seq):
	length_of_subseq = 0
	num_of_zeros = 0
	largest_num_of_zeros = 0
	pos = 0
	while pos < k:
		num_of_zeros += 1 if s[pos] == character_seq[pos % 3] else 0
		length_of_subseq += 1
		largest_num_of_zeros = max(largest_num_of_zeros, num_of_zeros)
		pos += 1
	for pos in range(k, n):
		num_of_zeros -= 1 if s[pos - k] == character_seq[(pos - k) % 3] else 0
		num_of_zeros += 1 if s[pos] == character_seq[pos % 3] else 0
		largest_num_of_zeros = max(largest_num_of_zeros, num_of_zeros)
	return largest_num_of_zeros
for _ in range(int(input())):
	(n, k) = map(int, sys.stdin.readline().split())
	s = sys.stdin.readline()
	print(k - max(search_for_the_largest_number_of_zeros('RGB'), search_for_the_largest_number_of_zeros('GBR'), search_for_the_largest_number_of_zeros('BRG')))


from sys import stdin, stdout
input = stdin.readline
for _ in range(int(input())):
	x = 10 ** 5
	(n, k) = map(int, input().split())
	s = input()
	ans = 10 ** 9
	for i in range(n - k + 1):
		x = s[i:i + k]
		m = 0
		curr = ['R', 'G', 'B']
		for l in range(3):
			m = 0
			z = l
			for j in x:
				if j != curr[z]:
					m += 1
				z += 1
				z %= 3
			ans = min(ans, m)
	print(ans)


t = int(input())
for z in range(t):
	(n, k) = [int(x) for x in input().split()]
	s = input()
	a = []
	for i in s:
		if i == 'R':
			a.append(0)
		elif i == 'G':
			a.append(1)
		else:
			a.append(2)
	flag = 0
	ans = 10000000
	for i in range(n - k + 1):
		q = a[i:i + k]
		l = 1000000
		for x in range(3):
			p = ''
			d = 0
			for y in range(x, x + k):
				p += str(y % 3)
			for j in range(k):
				if str(q[j]) != p[j]:
					d += 1
			l = min(l, d)
		ans = min(ans, l)
	print(ans)


from __future__ import print_function
from collections import Counter, OrderedDict
from itertools import permutations as perm
from fractions import Fraction
from collections import deque
from sys import stdin
from bisect import *
from heapq import *
from math import *
g = lambda : stdin.readline().strip()
gl = lambda : g().split()
gil = lambda : [int(var) for var in gl()]
gfl = lambda : [float(var) for var in gl()]
gcl = lambda : list(g())
gbs = lambda : [int(var) for var in g()]
mod = int(1000000000.0) + 7
inf = float('inf')
(t,) = gil()
base = ['R']
while len(base) < 2010:
	if base[-1] == 'R':
		base.append('G')
	elif base[-1] == 'G':
		base.append('B')
	else:
		base.append('R')

def solve(s, n, k, off):
	cv = 0
	for i in range(k):
		if s[i] != base[i + off]:
			cv += 1
	ans = cv
	for i in range(k, n):
		if s[i - k] != base[i - k + off]:
			cv -= 1
		if s[i] != base[i + off]:
			cv += 1
		ans = min(ans, cv)
	return ans
for _ in range(t):
	(n, k) = gil()
	s = g()
	print(min(solve(s, n, k, 0), solve(s, n, k, 1), solve(s, n, k, 2)))


from sys import stdin, stdout
import collections
Q = int(input())
ans = [0] * Q
for q in range(Q):
	(N, K) = [int(x) for x in stdin.readline().split()]
	s = input()
	res = N + 1
	if N == 1 or K == 1:
		ans[q] = 0
		continue
	prefix = [0] * (N + 1)
	for k in range(3):
		if k == 0:
			RGB = 'RGB'
		if k == 1:
			RGB = 'GBR'
		if k == 2:
			RGB = 'BRG'
		ssum = 0
		for i in range(N):
			letter = RGB[i % 3]
			if s[i] != letter:
				ssum += 1
			prefix[i + 1] = ssum
			if i >= K - 1:
				res = min(res, prefix[i + 1] - prefix[i + 1 - K])
	ans[q] = res
print(*ans, sep='\n')


import sys
for _ in range(int(sys.stdin.readline())):
	(n, k) = [int(i) for i in sys.stdin.readline().split()]
	s = sys.stdin.readline().strip()
	ans = 0
	for x in ['RGB', 'GBR', 'BRG']:
		same = 0
		for i in range(k):
			if s[i] == x[i % 3]:
				same += 1
		maxm = same
		for i in range(n - k):
			if s[i + k] == x[(i + k) % 3]:
				same += 1
			if s[i] == x[i % 3]:
				same -= 1
			if same > maxm:
				maxm = same
		ans = max(ans, maxm)
	sys.stdout.write(str(k - ans) + '\n')


ll = lambda : list(map(int, input().split()))
testcases = 1
testcases = int(input())
for testcase in range(testcases):
	[n, k] = ll()
	s = input()
	nc = {}
	nc['R'] = 'G'
	nc['G'] = 'B'
	nc['B'] = 'R'
	ans = 999999999
	for x in range(0, n - k + 1):
		(dif1, dif2, dif3) = (0, 0, 0)
		(cn1, cn2, cn3) = ('R', 'G', 'B')
		for i in range(x, x + k):
			if cn1 != s[i]:
				dif1 += 1
			cn1 = nc[cn1]
			if cn2 != s[i]:
				dif2 += 1
			cn2 = nc[cn2]
			if cn3 != s[i]:
				dif3 += 1
			cn3 = nc[cn3]
		ans = min(dif1, dif2, dif3, ans)
	print(ans)


for g in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	t = 2000
	for i in range(n - k + 1):
		s1 = 'RGB'
		for h in range(3):
			var = h
			count = 0
			for j in range(k):
				if s[i + j] == s1[var]:
					var += 1
					var = var % 3
				else:
					count += 1
					var += 1
					var = var % 3
			t = min(count, t)
	print(t)


q = int(input())
for i in range(q):
	(n, k) = list(map(int, input().split()))
	s = str(input())
	str1 = ''
	str2 = ''
	str3 = ''
	for j in range(k):
		if j % 3 == 0:
			str1 += 'R'
		elif j % 3 == 1:
			str1 += 'G'
		else:
			str1 += 'B'
	for j in range(k):
		if j % 3 == 0:
			str2 += 'G'
		elif j % 3 == 1:
			str2 += 'B'
		else:
			str2 += 'R'
	for j in range(k):
		if j % 3 == 0:
			str3 += 'B'
		elif j % 3 == 1:
			str3 += 'R'
		else:
			str3 += 'G'
	ans = k
	for j in range(n - k + 1):
		p = s[j:j + k]
		temp1 = 0
		temp2 = 0
		temp3 = 0
		for l in range(k):
			if p[l] != str1[l]:
				temp1 += 1
		for l in range(k):
			if p[l] != str2[l]:
				temp2 += 1
		for l in range(k):
			if p[l] != str3[l]:
				temp3 += 1
		ans = min(ans, temp1, temp2, temp3)
	print(ans)


def f(s, prev):
	for i in range(1, k):
		if prev == 'R':
			s = s + 'G'
			prev = 'G'
		elif prev == 'G':
			s = s + 'B'
			prev = 'B'
		elif prev == 'B':
			s = s + 'R'
			prev = 'R'
	return s
q = int(input())
while q > 0:
	q = q - 1
	(n, k) = map(int, input().split())
	s = input()
	S = list(s)
	d = {}
	d['R'] = f('R', 'R')
	d['G'] = f('G', 'G')
	d['B'] = f('B', 'B')
	ans = []
	for i in range(0, n - k + 1):
		a = 10 ** 6
		for m in d:
			c = 0
			for j in range(k):
				e = d[m]
				if e[j] != S[i + j]:
					c = c + 1
			a = min(a, c)
		ans.append(a)
	print(min(ans))


import sys
import math
q = int(input())

def minimum(sub):
	(x, y, z) = (0, 0, 0)
	for i in range(0, len(sub)):
		if rgb[i] == sub[i]:
			x += 1
	for i in range(0, len(sub)):
		if rgb[i + 1] == sub[i]:
			y += 1
	for i in range(0, len(sub)):
		if rgb[i + 2] == sub[i]:
			z += 1
	x1 = k - x
	y1 = k - y
	z1 = k - z
	return min(x1, y1, z1)

def check(li, k):
	mi = sys.maxsize
	for i in range(0, len(li) - k + 1):
		temp = minimum(li[i:i + k])
		if mi > temp:
			mi = temp
	return mi
for i in range(0, q):
	(n, k) = list((int(x) for x in input().split()[:2]))
	str = input()[:n]
	li = list(str)
	times = math.ceil(k / 3) * 2
	rgb = 'RGB' * times
	rgb = list(rgb)
	print(check(li, k))


from sys import stdin, stdout, exit
inf = 10 ** 6
q = int(input())
for it in range(q):
	(n, k) = map(int, stdin.readline().split())
	s = stdin.readline().strip()
	ans = inf
	tgt = 'RGB'
	for offset in range(3):
		matching = [1 if c == tgt[(offset + i) % 3] else 0 for (i, c) in enumerate(s)]
		partial = 0
		for i in range(n):
			partial += matching[i]
			if i >= k:
				partial -= matching[i - k]
			ans = min(ans, k - partial)
	stdout.write(str(ans) + '\n')


n = int(input())
for i in range(0, n):
	p = input().rstrip().split(' ')
	n = int(p[0])
	kk = int(p[1])
	s = input().rstrip()
	x = list(s)
	L = ['R', 'G', 'B']
	q = []
	w = []
	S = []
	D = []
	for j in range(0, len(x) - kk + 1):
		T = x[j:j + kk]
		SS = 0
		if T[0] == 'R':
			A = 0
			for k in range(0, len(T)):
				if T[k] != L[A % 3]:
					SS += 1
				A += 1
			S.append(SS)
		else:
			A = 0
			for k in range(0, len(T)):
				if T[k] != L[A % 3]:
					SS += 1
				A += 1
			S.append(SS)
		SS = 0
		if T[0] == 'G':
			A = 1
			for k in range(0, len(T)):
				if T[k] != L[A % 3]:
					SS += 1
				A += 1
			S.append(SS)
		else:
			A = 1
			for k in range(0, len(T)):
				if T[k] != L[A % 3]:
					SS += 1
				A += 1
			S.append(SS)
		SS = 0
		if T[0] == 'B':
			A = 2
			for k in range(0, len(T)):
				if T[k] != L[A % 3]:
					SS += 1
				A += 1
			S.append(SS)
		else:
			A = 2
			for k in range(0, len(T)):
				if T[k] != L[A % 3]:
					SS += 1
				A += 1
			S.append(SS)
	print(min(S))


def answer(n, k, A):
	d = {'R': 0, 'G': 1, 'B': 2}
	dp = [[0, 0, 0] for i in range(n + 1)]
	for i in range(0, 3):
		for j in range(i, i + n):
			if j % 3 != d[s[j - i]]:
				dp[j - i][i] = dp[j - i - 1][i] + 1
			else:
				dp[j - i][i] = dp[j - i - 1][i]
	ans = 10 ** 9
	for i in range(0, n - k + 1):
		x = dp[i + k - 1][0] - dp[i - 1][0]
		y = dp[i + k - 1][1] - dp[i - 1][1]
		z = dp[i + k - 1][2] - dp[i - 1][2]
		ans = min(ans, x, y, z)
	return ans
t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	s = input()
	print(answer(n, k, s))


import math as mt
import sys, string
input = sys.stdin.readline
import random
from collections import deque, defaultdict
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : map(int, input().split())
I = lambda : int(input())
a = 'RGB'
b = 'GBR'
c = 'BRG'
t = I()
for _ in range(t):
	(n, k) = M()
	s = input()
	ac = 0
	bc = 0
	cc = 0
	for i in range(k):
		if a[i % 3] != s[i]:
			ac += 1
		if b[i % 3] != s[i]:
			bc += 1
		if c[i % 3] != s[i]:
			cc += 1
	aes = ac
	bs = bc
	cs = cc
	for i in range(k, n):
		ac += int(not a[i % 3] == s[i])
		ac -= int(not a[(i - k) % 3] == s[i - k])
		aes = min(aes, ac)
		bc += int(not b[i % 3] == s[i])
		bc -= int(not b[(i - k) % 3] == s[i - k])
		bs = min(bs, bc)
		cc += int(not c[i % 3] == s[i])
		cc -= int(not c[(i - k) % 3] == s[i - k])
		cs = min(cs, cc)
	print(min(aes, bs, cs))


import sys
input = sys.stdin.readline
rgb = ['R', 'G', 'B']

def func(s):
	r = [0, 0, 0]
	i = 0
	for c in s:
		if c == rgb[i]:
			r[1] += 1
			r[2] += 1
		elif c == rgb[(i + 1) % 3]:
			r[2] += 1
			r[0] += 1
		else:
			r[0] += 1
			r[1] += 1
		i = (i + 1) % 3
	return r
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	dp = func(s[:k])
	ans = min(dp)
	(x, y, z) = ((k - 1 + 3) % 3, 0, 1)
	for i in range(k, n):
		for j in range(3):
			if s[i - k] != rgb[y]:
				dp[j] -= 1
			y = (y + 1) % 3
		(dp[1], dp[2], dp[0]) = (dp[0], dp[1], dp[2])
		for j in range(3):
			if s[i] != rgb[x]:
				dp[j] += 1
			x = (x + 1) % 3
		ans = min(ans, min(dp))
	print(ans)


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	s = input()
	s1 = 'RGB'
	s2 = 'GBR'
	s3 = 'BRG'
	mi = 10000000000
	if k == 1:
		print(0)
	else:
		for i in range(0, n - k + 1):
			x = 0
			y = 0
			for j in range(i, i + k):
				if s[j] != s1[x % 3]:
					y += 1
				x += 1
			mi = min(mi, y)
			y = 0
			x = 0
			for j in range(i, i + k):
				if s[j] != s2[x % 3]:
					y += 1
				x += 1
			mi = min(mi, y)
			x = 0
			y = 0
			for j in range(i, i + k):
				if s[j] != s3[x % 3]:
					y += 1
				x += 1
			mi = min(mi, y)
		print(mi)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		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')
from math import factorial
from collections import Counter, defaultdict
from heapq import heapify, heappop, heappush

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def comb(n, m):
	return factorial(n) / (factorial(m) * factorial(n - m)) if n >= m else 0

def perm(n, m):
	return factorial(n) // factorial(n - m) if n >= m else 0

def mdis(x1, y1, x2, y2):
	return abs(x1 - x2) + abs(y1 - y2)
mod = 998244353
INF = float('inf')
string1 = 'RGB'
string2 = 'BRG'
string3 = 'GBR'
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = str(input())
	(cs1, cs2, cs3) = (0, 0, 0)
	dp = [[] for i in range(n)]
	for i in range(n):
		if string1[i % 3] != s[i]:
			cs1 += 1
		if string2[i % 3] != s[i]:
			cs2 += 1
		if string3[i % 3] != s[i]:
			cs3 += 1
		dp[i] = [cs1, cs2, cs3]
	ans = 10 ** 18
	z = 0
	for i in range(k - 1, n):
		if s[z] != string1[z % 3]:
			val1 = dp[i][0] - dp[z][0] + 1
		else:
			val1 = dp[i][0] - dp[z][0]
		if s[z] != string2[z % 3]:
			val2 = dp[i][1] - dp[z][1] + 1
		else:
			val2 = dp[i][1] - dp[z][1]
		if s[z] != string3[z % 3]:
			val3 = dp[i][2] - dp[z][2] + 1
		else:
			val3 = dp[i][2] - dp[z][2]
		ans = min(val1, val2, val3, ans)
		z += 1
	print(ans)


import sys
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
(t,) = I()
for _ in range(t):
	(n, k) = I()
	s = input()
	an = k
	rq = 'RGB'
	for j in range(3):
		cr = 0
		for i in range(k):
			if s[i] != rq[(j + i) % 3]:
				cr += 1
		an = min(cr, an)
		for i in range(k, n):
			if s[i - k] != rq[(j + i - k) % 3]:
				cr -= 1
			if s[i] != rq[(j + i) % 3]:
				cr += 1
			an = min(cr, an)
	print(an)


def charDiff(s1, s2):
	l = len(s1)
	diffCount = 0
	for i in range(l):
		if s1[i] != s2[i]:
			diffCount += 1
	return diffCount
for t in range(int(input())):
	(n, k) = map(int, input().split())
	ip = input()
	R = ['', 'R', 'RG', 'RGB']
	G = ['', 'G', 'GB', 'GBR']
	B = ['', 'B', 'BR', 'BRG']
	possibleSS = []
	possibleSS.append(k // 3 * R[3] + R[k % 3])
	possibleSS.append(k // 3 * G[3] + G[k % 3])
	possibleSS.append(k // 3 * B[3] + B[k % 3])
	SSfromIP = []
	for i in range(n - k + 1):
		SSfromIP.append(ip[i:i + k])
	minChanges = 2000
	minFound = False
	for i in possibleSS:
		for j in SSfromIP:
			diffCount = charDiff(i, j)
			if diffCount < minChanges:
				minChanges = diffCount
				if minChanges == 0:
					print(0)
					minFound = True
					break
		if minFound:
			break
	if not minFound:
		print(minChanges)


import sys
input = sys.stdin.readline
RI = lambda : [int(x) for x in sys.stdin.readline().strip().split()]
rw = lambda : input().strip().split()
infinite = float('inf')
t = int(input())
for _ in range(t):
	(n, k) = RI()
	s = input()
	mini = n
	test = 'RGB' * (k // 3 + 5)
	for i in range(n - k + 1):
		count = 0
		for j in range(k):
			if s[i + j] != test[j]:
				count += 1
		mini = min(count, mini)
	test = 'GBR' * (k // 3 + 5)
	for i in range(n - k + 1):
		count = 0
		for j in range(k):
			if s[i + j] != test[j]:
				count += 1
		mini = min(count, mini)
	test = 'BRG' * (k // 3 + 5)
	for i in range(n - k + 1):
		count = 0
		for j in range(k):
			if s[i + j] != test[j]:
				count += 1
		mini = min(count, mini)
	print(mini)


t = int(input())
for r in range(t):
	(n, k) = [int(x) for x in input().split(' ')]
	s = input()
	T = 'RGB'
	ans = 10 ** 9
	for i in range(0, n - k + 1):
		for offset in range(0, 3):
			c = 0
			for pos in range(0, k):
				if s[i + pos] != T[(offset + pos) % 3]:
					c = c + 1
			ans = min(c, ans)
	print(ans)


from sys import stdin

def iinput():
	return int(stdin.readline())

def minput():
	return map(int, stdin.readline().split())

def linput():
	return list(map(int, stdin.readline().split()))
d = {'R': 'G', 'G': 'B', 'B': 'R'}
t = iinput()
while t:
	t -= 1
	(n, k) = minput()
	s = input()
	mn = k
	for j in range(n - k + 1):
		for e in d:
			i = j + 1
			start = e
			temp = 0 if s[j] == start else 1
			while i < j + k:
				start = d[start]
				if s[i] != start:
					temp += 1
				i += 1
			mn = min(mn, temp)
	print(mn)


import sys
input = sys.stdin.readline
q = int(input())
d = {'R': 0, 'G': 1, 'B': 2}
for _ in range(q):
	(n, k) = map(int, input().split())
	s = list(input().rstrip())
	for i in range(n):
		s[i] = d[s[i]]
	t = [(s[i] - i) % 3 for i in range(n)]
	cnt = [0] * 3
	for i in range(k):
		cnt[t[i]] += 1
	ans = k - max(cnt)
	for i in range(n - k):
		cnt[t[i]] -= 1
		cnt[t[i + k]] += 1
		ans = min(ans, k - max(cnt))
	print(ans)


Q = int(input())
for _ in range(Q):
	(N, K) = list(map(int, input().split()))
	S = input()
	X = [{'R': 0, 'G': 1, 'B': 2}[s] for s in S]
	mi = K
	for i in range(3):
		d = 0
		for j in range(N):
			if X[j] != (i + j) % 3:
				d += 1
			if j >= K and X[j - K] != (i + j - K) % 3:
				d -= 1
			if j >= K - 1:
				mi = min(mi, d)
	print(mi)


import sys
input = sys.stdin.readline
q = int(input())
for _ in range(q):
	(n, k) = map(int, input().split())
	s = input()
	a = [0] * n
	for i in range(n):
		if s[i] == 'R':
			a[i] = 0
		if s[i] == 'G':
			a[i] = 1
		if s[i] == 'B':
			a[i] = 2
	ans = n
	for b in range(3):
		diff = 0
		for i in range(k):
			if a[i] != (i + b) % 3:
				diff += 1
		ans = min(ans, diff)
		for j in range(k, n):
			if a[j - k] != (j - k + b) % 3:
				diff -= 1
			if a[j] != (j + b) % 3:
				diff += 1
			ans = min(ans, diff)
	print(ans)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	pa = ['RGB' * k, 'GBR' * k, 'BRG' * k]
	ans = 10 ** 9
	for i in range(n - k + 1):
		for p in pa:
			count = 0
			for j in range(0, k):
				if s[j + i] != p[j]:
					count += 1
			ans = min(ans, count)
			if ans == 0:
				break
		if ans == 0:
			break
	print(ans)


def edit(str1, str2, m, n):
	if m == 0:
		return n
	if n == 0:
		return m
	if str1[m - 1] == str2[n - 1]:
		return edit(str1, str2, m - 1, n - 1)
	replace = edit(str1, str2, m - 1, n - 1)
	return 1 + replace
q = int(input())
for i in range(q):
	(n, k) = map(int, input().split(' '))
	s = input()
	rgb = ['R', 'G', 'B']
	possible = []
	for i in range(0, 3):
		p = ''
		for j in range(i, k + i):
			p += rgb[j % 3]
		possible.append(p)
	s = list(s)
	possible = [list(f) for f in possible]
	changes = 1000000000
	for p in possible:
		for i in range(n - k + 1):
			sub = s[i:i + k]
			changes = min(changes, edit(sub, p, k, k))
	print(changes)


from itertools import chain
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	ans = [k, k, k, 1, 1]
	fin_ans = k
	inp = tuple(((ord('r') + i - ord(c)) % 3 for (i, c) in enumerate(input().lower())))
	for (inp1, inp2) in zip(inp, chain((4,) * k, inp)):
		ans[inp2] += 1
		ans[inp1] -= 1
		fin_ans = min(fin_ans, ans[inp1])
	print(fin_ans)


import sys
from math import log2
import bisect
import heapq
Ri = lambda : [int(x) for x in sys.stdin.readline().split()]
ri = lambda : sys.stdin.readline().strip()

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7
for _ in range(int(ri())):
	(n, k) = Ri()
	st = ri()
	B = 'BRG' * (k // 3 + 2)
	R = 'RGB' * (k // 3 + 2)
	G = 'GBR' * (k // 3 + 2)
	dpb = [0] * n
	dpr = [0] * n
	dpg = [0] * n
	cnt = 0
	for i in range(k):
		if st[i] != B[i]:
			cnt += 1
	dpb[k - 1] = cnt
	cnt = 0
	for i in range(k):
		if st[i] != G[i]:
			cnt += 1
	dpg[k - 1] = cnt
	cnt = 0
	for i in range(k):
		if st[i] != R[i]:
			cnt += 1
	dpr[k - 1] = cnt
	for i in range(k, n):
		dpb[i] = dpg[i - 1] + (1 if B[k - 1] != st[i] else 0)
		if st[i - k] != 'G':
			dpb[i] -= 1
		dpr[i] = dpb[i - 1] + (1 if R[k - 1] != st[i] else 0)
		if st[i - k] != 'B':
			dpr[i] -= 1
		dpg[i] = dpr[i - 1] + (1 if G[k - 1] != st[i] else 0)
		if st[i - k] != 'R':
			dpg[i] -= 1
	minn = INF
	for i in range(k - 1, n):
		minn = min(minn, dpb[i], dpg[i], dpr[i])
	print(minn)


q = int(input())
for _ in range(q):
	(n, k) = map(int, input().split())
	l = list(input())
	changes = k + 1
	for i in range(n - k + 1):
		substring = l[i:i + k]
		x = 0
		for i in range(k):
			if i % 3 == 0 and substring[i] != 'R':
				x += 1
			if i % 3 == 1 and substring[i] != 'G':
				x += 1
			if i % 3 == 2 and substring[i] != 'B':
				x += 1
		changes = min(changes, x)
		x = 0
		for i in range(k):
			if i % 3 == 0 and substring[i] != 'B':
				x += 1
			if i % 3 == 1 and substring[i] != 'R':
				x += 1
			if i % 3 == 2 and substring[i] != 'G':
				x += 1
		changes = min(changes, x)
		x = 0
		for i in range(k):
			if i % 3 == 0 and substring[i] != 'G':
				x += 1
			if i % 3 == 1 and substring[i] != 'B':
				x += 1
			if i % 3 == 2 and substring[i] != 'R':
				x += 1
		changes = min(changes, x)
	print(changes)


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	(n, m) = map(int, sys.stdin.readline().split())
	a = sys.stdin.readline()
	b = 1
	c = [0] * 3
	a1 = 'RGB' * (n // 3 + 1)
	a1 = a1[:n]
	a2 = 'GBR' * (n // 3 + 1)
	a2 = a2[:n]
	a3 = 'BRG' * (n // 3 + 1)
	a3 = a3[:n]
	c1 = c2 = c3 = 0
	for i in range(m):
		if a[i] != a1[i]:
			c1 += 1
		if a[i] != a2[i]:
			c2 += 1
		if a[i] != a3[i]:
			c3 += 1
	c[0] = c1
	c[1] = c2
	c[2] = c3
	for i in range(m, n):
		if a[i] != a1[i]:
			c1 += 1
		if a[i] != a2[i]:
			c2 += 1
		if a[i] != a3[i]:
			c3 += 1
		if a[i - m] != a1[i - m]:
			c1 -= 1
		if a[i - m] != a2[i - m]:
			c2 -= 1
		if a[i - m] != a3[i - m]:
			c3 -= 1
		c[0] = min(c1, c[0])
		c[1] = min(c2, c[1])
		c[2] = min(c3, c[2])
	print(min(c))


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import ceil, floor
from copy import *
from collections import deque, defaultdict
from collections import Counter as counter
from itertools import combinations
from itertools import permutations as permutate
from bisect import bisect_left as bl
from operator import *
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192
from sys import stderr

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		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')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def fsep():
	return map(float, inp().split())

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def pow(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res
from functools import reduce

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

def gcd(a, b):
	if a == b:
		return a
	while b > 0:
		(a, b) = (b, a % b)
	return a

def prefix_sum(ar):
	return list(accumulate(ar))

def suffix_sum(ar):
	return list(accumulate(ar[::-1]))[::-1]

def N():
	return int(inp())
dx = [0, 0, 1, -1]
dy = [1, -1, 0, 0]
from collections import defaultdict

def numberOfSetBits(i):
	i = i - (i >> 1 & 1431655765)
	i = (i & 858993459) + (i >> 2 & 858993459)
	return ((i + (i >> 4) & 252645135) * 16843009 & 4294967295) >> 24
from decimal import *
from math import *

def solve():
	(n, k) = sep()
	s = inp()
	inf = float('inf')
	ar = [[0] * (n - k + 1) for _ in range(3)]
	temp = 'RGB'
	temp1 = []
	for i in range(n):
		if s[i] == temp[i % 3]:
			temp1.append(0)
		else:
			temp1.append(1)
	temp1.append(0)
	j = 0
	su = 0
	for i in range(k):
		su += temp1[i]
	for j in range(n - k + 1):
		ar[j % 3][j] = su
		su += temp1[k + j]
		su -= temp1[j]
	temp = 'GBR'
	temp1 = []
	for i in range(n):
		if s[i] == temp[i % 3]:
			temp1.append(0)
		else:
			temp1.append(1)
	temp1.append(0)
	su = 0
	for i in range(k):
		su += temp1[i]
	for j in range(n - k + 1):
		ar[(j + 1) % 3][j] = su
		su += temp1[k + j]
		su -= temp1[j]
	temp = 'BRG'
	temp1 = []
	for i in range(n):
		if s[i] == temp[i % 3]:
			temp1.append(0)
		else:
			temp1.append(1)
	temp1.append(0)
	su = 0
	for i in range(k):
		su += temp1[i]
	for j in range(n - k + 1):
		ar[(j + 2) % 3][j] = su
		su += temp1[k + j]
		su -= temp1[j]
	m = 9999999999999999
	for i in ar:
		m = min(m, min(i))
	print(m)
testcase(int(inp()))


import sys, os
from io import BytesIO, IOBase
import collections, itertools, bisect, heapq, math, string
from decimal import *
BUFSIZE = 8192
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		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')

def main():
	t = int(input())
	for i in range(t):
		(n, k) = map(int, input().split())
		s = str(input())
		rgb = 'RGB' * (k // 3 + 2)
		sub = ['R' + rgb[1:k], 'G' + rgb[2:k + 1], 'B' + rgb[3:k + 2]]
		x = 0
		ans = n + 1
		while x <= n - k:
			temp = s[x:x + k]
			for j in sub:
				mn = 0
				for y in range(len(temp)):
					if temp[y] != j[y]:
						mn += 1
				ans = min(mn, ans)
			x += 1
		print(ans)
main()


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	arr = list(str(input())[:-1])
	s = 'RGB' * (n // 3 + 2)
	res = float('inf')
	for i in range(3):
		st = s[i:n + i]
		s0 = [0 if arr[j] == st[j] else 1 for j in range(n)]
		temp = sum(s0[:k])
		res = min(res, temp)
		for j in range(k, n):
			temp += s0[j] - s0[j - k]
			res = min(res, temp)
	print(res)


q = int(input())
for _ in range(q):
	(n, k) = map(int, input().split())
	s = input()
	t1 = ('RGB' * ((k + 2) // 3))[:k]
	t2 = ('GBR' * ((k + 2) // 3))[:k]
	t3 = ('BRG' * ((k + 2) // 3))[:k]
	m = k
	for i in range(n - k + 1):
		(m1, m2, m3) = (0, 0, 0)
		for j in range(k):
			if s[i + j] != t1[j]:
				m1 += 1
			if s[i + j] != t2[j]:
				m2 += 1
			if s[i + j] != t3[j]:
				m3 += 1
		m = min(m, min([m1, m2, m3]))
	print(m)


import sys
import math
q = int(sys.stdin.readline())
for _ in range(q):
	(n, k) = map(int, sys.stdin.readline().split())
	sub = sys.stdin.readline().strip()
	ori = ['RGB', 'GBR', 'BRG']
	minVal = sys.maxsize
	for i in range(3):
		cnt = 0
		for j in range(k):
			if sub[j] != ori[i][j % 3]:
				cnt += 1
		st = 1
		end = k
		minVal = min(minVal, cnt)
		if n == k:
			pass
		else:
			for j in range(n):
				if sub[st - 1] != ori[i][(st - 1) % 3]:
					cnt -= 1
				if sub[end] != ori[i][end % 3]:
					cnt += 1
				minVal = min(minVal, cnt)
				if end == len(sub) - 1:
					break
				st += 1
				end += 1
	print(minVal)


import sys
t = sys.stdin.buffer.readline()
for g in range(int(t)):
	(n, k) = map(int, input().split())
	s = input()
	t = 2000
	for i in range(n - k + 1):
		s1 = 'RGB'
		for h in range(3):
			var = h
			count = 0
			for j in range(k):
				if s[i + j] == s1[var]:
					var += 1
					var = var % 3
				else:
					count += 1
					var += 1
					var = var % 3
			t = min(count, t)
	print(t)


for _ in range(int(input())):
	(n, k) = map(int, input().split(' '))
	s = input()
	c1 = c2 = c3 = 0
	s1 = 'RGB'
	s2 = 'GBR'
	s3 = 'BRG'
	ans = n
	for i in range(k):
		if s[i] != s1[i % 3]:
			c1 += 1
		if s[i] != s2[i % 3]:
			c2 += 1
		if s[i] != s3[i % 3]:
			c3 += 1
	ans = min(ans, min(c1, c2, c3))
	for i in range(k, n):
		if s[i] != s1[i % 3]:
			c1 += 1
		if s[i - k] != s1[(i - k) % 3]:
			c1 -= 1
		if s[i] != s2[i % 3]:
			c2 += 1
		if s[i - k] != s2[(i - k) % 3]:
			c2 -= 1
		if s[i] != s3[i % 3]:
			c3 += 1
		if s[i - k] != s3[(i - k) % 3]:
			c3 -= 1
		ans = min(ans, min(c1, c2, c3))
	print(ans)


q = int(input())
for _ in range(q):
	(n, k) = map(int, input().split())
	s = input()
	ans = k
	sample = 'RGB'
	for i in range(n - k + 1):
		cnt = 0
		x = 0
		for j in range(i, i + k):
			if s[j] != sample[x]:
				cnt += 1
			x = (x + 1) % 3
		ans = min(ans, cnt)
		cnt = 0
		x = 1
		for j in range(i, i + k):
			if s[j] != sample[x]:
				cnt += 1
			x = (x + 1) % 3
		ans = min(ans, cnt)
		cnt = 0
		x = 2
		for j in range(i, i + k):
			if s[j] != sample[x]:
				cnt += 1
			x = (x + 1) % 3
		ans = min(ans, cnt)
	print(ans)


t = 'RGB'
q = int(input())
while q > 0:
	l = list(map(int, input().split(' ')))
	n = l[0]
	k = l[1]
	s = str(input())
	ans = k
	for i in range(0, n - k + 1):
		for j in range(0, 3):
			cnt = 0
			for p in range(0, k):
				if s[i + p] != t[(j + p) % 3]:
					cnt += 1
			ans = min(ans, cnt)
	print(ans)
	q -= 1


import math

def add(dp, s, si, i):
	if not dp:
		if si[i] == s[i]:
			dp.append(0)
		else:
			dp.append(1)
	elif si[i] == s[i]:
		dp.append(dp[-1])
	else:
		dp.append(dp[-1] + 1)

def moves(dp, k):
	min_moves = float('inf')
	for i in range(k - 1, len(dp)):
		if i == k - 1:
			min_moves = min(min_moves, dp[i])
		else:
			min_moves = min(min_moves, dp[i] - dp[i - k])
	return min_moves

def solve(s, n, k, ans):
	l = math.ceil(n / 3)
	s1 = 'RGB' * l
	s2 = 'GBR' * l
	s3 = 'BRG' * l
	dp1 = []
	dp2 = []
	dp3 = []
	for i in range(min(n, len(s1))):
		add(dp1, s, s1, i)
		add(dp2, s, s2, i)
		add(dp3, s, s3, i)
	min_moves = moves(dp1, k)
	min_moves = min(min_moves, moves(dp2, k))
	min_moves = min(min_moves, moves(dp3, k))
	ans.append(min_moves)

def main():
	ans = []
	q = int(input())
	for i in range(q):
		(n, k) = map(int, input().split())
		s = input()
		solve(s, n, k, ans)
	for i in ans:
		print(i)
main()


q = int(input())
for w in range(q):
	iarr = list(map(int, input().split()))
	n = iarr[0]
	k = iarr[1]
	s = input()
	rgb = [0 for i in range(n)]
	gbr = [0 for i in range(n)]
	brg = [0 for i in range(n)]
	if s[0] == 'R':
		rgb[0] = 1
	elif s[0] == 'G':
		gbr[0] = 1
	else:
		brg[0] = 1
	for i in range(1, n):
		rgb[i] = rgb[i - 1]
		gbr[i] = gbr[i - 1]
		brg[i] = brg[i - 1]
		curr = i % 3
		if s[i] == 'R':
			if curr == 0:
				rgb[i] += 1
			elif curr == 1:
				brg[i] += 1
			else:
				gbr[i] += 1
		elif s[i] == 'G':
			if curr == 0:
				gbr[i] += 1
			elif curr == 1:
				rgb[i] += 1
			else:
				brg[i] += 1
		elif curr == 0:
			brg[i] += 1
		elif curr == 1:
			gbr[i] += 1
		else:
			rgb[i] += 1
	curr = max(rgb[k - 1], max(gbr[k - 1], brg[k - 1]))
	maxa = curr
	for i in range(k, n):
		curr1 = rgb[i] - rgb[i - k]
		curr2 = gbr[i] - gbr[i - k]
		curr3 = brg[i] - brg[i - k]
		curr = max(curr1, max(curr2, curr3))
		maxa = max(curr, maxa)
	ans = k - maxa
	print(ans)


def info(sk):
	barr = [False] * l
	i = 0
	while i < l:
		if s[i] == sk[i % 3]:
			barr[i] = True
		i += 1
	max = 0
	curr = 0
	i = 0
	while i < l:
		if barr[i]:
			curr += 1
		if i - k >= 0:
			if barr[i - k]:
				curr -= 1
		if curr > max:
			max = curr
		i += 1
	return max
q = int(input())
s1 = 'RGB'
s2 = 'GBR'
s3 = 'BRG'
for c in range(q):
	(l, k) = map(int, input().split())
	s = input()
	arr = []
	arr.append(int(info(s1)))
	arr.append(int(info(s2)))
	arr.append(int(info(s3)))
	arr.sort()
	print(k - arr[2])


q = int(input())

def func(a, k):
	t = 0
	for i in range(k):
		t += a[i]
	r = t
	for i in range(1, n - k + 1, 1):
		if a[i - 1] == 1:
			if a[i + k - 1] == 0:
				t -= 1
		elif a[i + k - 1] == 1:
			t += 1
		r = min(t, r)
	return r
for p in range(q):
	(n, k) = input().split()
	n = int(n)
	k = int(k)
	s = str(input())
	a = 'RGB'
	b = 'GBR'
	c = 'BRG'
	x = a * (n // 3) + a[:n % 3]
	y = b * (n // 3) + b[:n % 3]
	z = c * (n // 3) + c[:n % 3]
	a = []
	b = []
	c = []
	for i in range(n):
		if x[i] != s[i]:
			a.append(1)
		else:
			a.append(0)
		if y[i] != s[i]:
			b.append(1)
		else:
			b.append(0)
		if z[i] != s[i]:
			c.append(1)
		else:
			c.append(0)
	if n == k:
		print(min(sum(a), min(sum(b), sum(c))))
		continue
	print(min(func(a, k), min(func(b, k), func(c, k))))


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	(n, k) = map(int, input().split(' '))
	s = input()
	c1 = c2 = c3 = 0
	s1 = 'RGB'
	s2 = 'GBR'
	s3 = 'BRG'
	ans = n
	for i in range(k):
		if s[i] != s1[i % 3]:
			c1 += 1
		if s[i] != s2[i % 3]:
			c2 += 1
		if s[i] != s3[i % 3]:
			c3 += 1
	ans = min(ans, min(c1, c2, c3))
	for i in range(k, n):
		if s[i] != s1[i % 3]:
			c1 += 1
		if s[i - k] != s1[(i - k) % 3]:
			c1 -= 1
		if s[i] != s2[i % 3]:
			c2 += 1
		if s[i - k] != s2[(i - k) % 3]:
			c2 -= 1
		if s[i] != s3[i % 3]:
			c3 += 1
		if s[i - k] != s3[(i - k) % 3]:
			c3 -= 1
		ans = min(ans, min(c1, c2, c3))
	print(ans)


import sys
input = sys.stdin.readline
Q = int(input())
D = {'R': 0, 'G': 1, 'B': 2}
for _ in range(Q):
	(N, K) = map(int, input().split())
	S = input()
	mi = K
	for i in range(3):
		d = 0
		for j in range(N):
			if D[S[j]] != (i + j) % 3:
				d += 1
			if j >= K and D[S[j - K]] != (i + j - K) % 3:
				d -= 1
			if j >= K - 1:
				mi = min(mi, d)
	print(mi)


chrs = ['R', 'G', 'B']
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	mn = 1e+18
	for i in range(1 + n - k):
		for st in range(3):
			ct = 0
			pos = st
			for j in range(k):
				if s[i + j] != chrs[pos]:
					ct += 1
				pos = (pos + 1) % 3
			mn = min(mn, ct)
	print(mn)


queries = int(input())
rgb = ['R', 'G', 'B']
(r_idx, g_idx, b_idx) = (0, 1, 2)
for q in range(queries):
	(n, k) = list(map(int, input().split()))
	s = input()
	num_windows = n - k + 1
	min_cost = [0] * num_windows
	for i in range(num_windows):
		r_start = g_start = b_start = 0
		for j in range(k):
			if s[i + j] != rgb[(j + r_idx) % 3]:
				r_start += 1
			if s[i + j] != rgb[(j + g_idx) % 3]:
				g_start += 1
			if s[i + j] != rgb[(j + b_idx) % 3]:
				b_start += 1
		min_cost[i] = min(r_start, g_start, b_start)
	print(min(min_cost))


import sys, os, io

def rs():
	return sys.stdin.readline().rstrip()

def ri():
	return int(sys.stdin.readline())

def ria():
	return list(map(int, sys.stdin.readline().split()))

def ws(s):
	sys.stdout.write(s + '\n')

def wi(n):
	sys.stdout.write(str(n) + '\n')

def wia(a):
	sys.stdout.write(' '.join([str(x) for x in a]) + '\n')
import math, datetime, functools, itertools, operator, bisect, fractions, statistics
from collections import deque, defaultdict, OrderedDict, Counter
from fractions import Fraction
from decimal import Decimal
from sys import stdout
from heapq import heappush, heappop, heapify, _heapify_max, _heappop_max, nsmallest, nlargest

def main():
	starttime = datetime.datetime.now()
	if os.path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	tc = ri()
	for _ in range(tc):
		(n, k) = ria()
		s = list(rs())
		a = 'RGB' * n
		b = 'BRG' * n
		c = 'GBR' * n
		a = a[:n]
		b = b[:n]
		c = c[:n]
		z = [a, b, c]
		da = [0] * n
		db = [0] * n
		dc = [0] * n
		for i in range(n):
			if s[i] == a[i]:
				da[i] = 1
			elif s[i] == b[i]:
				db[i] = 1
			else:
				dc[i] = 1
		pda = [0]
		pdb = [0]
		pdc = [0]
		for i in range(n):
			pda.append(pda[-1] + da[i])
			pdb.append(pdb[-1] + db[i])
			pdc.append(pdc[-1] + dc[i])
		maxs = 0
		for i in range(k, n + 1):
			sa = pda[i] - pda[i - k]
			sb = pdb[i] - pdb[i - k]
			sc = pdc[i] - pdc[i - k]
			maxs = max(maxs, sa, sb, sc)
		print(k - maxs)
	endtime = datetime.datetime.now()
	time = (endtime - starttime).total_seconds() * 1000
	if os.path.exists('input.txt'):
		print('Time:', time, 'ms')

class FastReader(io.IOBase):
	newlines = 0

	def __init__(self, fd, chunk_size=1024 * 8):
		self._fd = fd
		self._chunk_size = chunk_size
		self.buffer = io.BytesIO()

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self, size=-1):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, self._chunk_size if size == -1 else size))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

class FastWriter(io.IOBase):

	def __init__(self, fd):
		self._fd = fd
		self.buffer = io.BytesIO()
		self.write = self.buffer.write

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

class FastStdin(io.IOBase):

	def __init__(self, fd=0):
		self.buffer = FastReader(fd)
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

class FastStdout(io.IOBase):

	def __init__(self, fd=1):
		self.buffer = FastWriter(fd)
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.flush = self.buffer.flush
sys.stdin = FastStdin()
sys.stdout = FastStdout()
main()


import sys
input = sys.stdin.readline
mod = 998244353
INF = float('inf')
string1 = 'RGB'
string2 = 'BRG'
string3 = 'GBR'
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = str(input())
	(cs1, cs2, cs3) = (0, 0, 0)
	dp = [[] for i in range(n)]
	for i in range(n):
		if string1[i % 3] != s[i]:
			cs1 += 1
		if string2[i % 3] != s[i]:
			cs2 += 1
		if string3[i % 3] != s[i]:
			cs3 += 1
		dp[i] = [cs1, cs2, cs3]
	ans = 10 ** 18
	z = 0
	for i in range(k - 1, n):
		if s[z] != string1[z % 3]:
			val1 = dp[i][0] - dp[z][0] + 1
		else:
			val1 = dp[i][0] - dp[z][0]
		if s[z] != string2[z % 3]:
			val2 = dp[i][1] - dp[z][1] + 1
		else:
			val2 = dp[i][1] - dp[z][1]
		if s[z] != string3[z % 3]:
			val3 = dp[i][2] - dp[z][2] + 1
		else:
			val3 = dp[i][2] - dp[z][2]
		ans = min(val1, val2, val3, ans)
		z += 1
	print(ans)


IN = input
rint = lambda : int(IN())
rmint = lambda : map(int, IN().split())
rlist = lambda : list(rmint())
q = rint()
u = [0] * 228228
while q:
	q -= 1
	(n, k) = rmint()
	t = IN()
	ans = n
	for i in range(3):
		j = i
		for p in range(n):
			if p:
				u[p] = u[p - 1]
			else:
				u[p] = 0
			if t[p] != 'RGB'[j]:
				u[p] += 1
			j = (j + 1) % 3
			if p + 2 > k:
				if p < k:
					ans = min(ans, u[p])
				else:
					ans = min(ans, u[p] - u[p - k])
	print(ans)


import sys
sss = 'RGB' * 700

def check(ss, p):
	i = 0
	m = 10 ** 5
	ans = 0
	while i < len(p):
		if p[i] != sss[i]:
			ans += 1
		i += 1
	m = min(m, ans)
	ans = 0
	i = 1
	while i < len(p) + 1:
		if p[i - 1] != sss[i]:
			ans += 1
		i += 1
	m = min(m, ans)
	ans = 0
	i = 2
	while i < len(p) + 2:
		if p[i - 2] != sss[i]:
			ans += 1
		i += 1
	m = min(m, ans)
	return m
for _ in range(int(input())):
	(n, k) = list(map(int, sys.stdin.readline().split()))
	s = input()
	m = 10 ** 5
	for i in range(n - k + 1):
		m = min(m, check(sss, s[i:i + k]))
	print(m)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	s1 = 'RGB'
	ans = []
	for i in range(n - k + 1):
		for j in range(3):
			val = 0
			for x in range(k):
				if s[i + x] != s1[(x + j) % 3]:
					val += 1
			ans.append(val)
	print(min(ans))


q = int(input())
for Q in range(q):
	(n, k) = list(map(int, input().split()))
	s = input()
	max_count = 0
	ans = ''
	for i in range(n - k + 1):
		count1 = 0
		count2 = 0
		count3 = 0
		sq = s[i:i + k]
		for i in range(k):
			if i % 3 == 0 and sq[i] == 'R':
				count1 += 1
			elif i % 3 == 1 and sq[i] == 'G':
				count1 += 1
			elif i % 3 == 2 and sq[i] == 'B':
				count1 += 1
		for i in range(k):
			if i % 3 == 0 and sq[i] == 'G':
				count2 += 1
			elif i % 3 == 1 and sq[i] == 'B':
				count2 += 1
			elif i % 3 == 2 and sq[i] == 'R':
				count2 += 1
		for i in range(k):
			if i % 3 == 0 and sq[i] == 'B':
				count3 += 1
			elif i % 3 == 1 and sq[i] == 'R':
				count3 += 1
			elif i % 3 == 2 and sq[i] == 'G':
				count3 += 1
		c = max(count1, count2, count3)
		max_count = max(c, max_count)
	print(k - max_count)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	string = input()
	s = ['RGB', 'GBR', 'BRG']
	ans = float('inf')
	for ss in s:
		dp = [0] * (n + 1)
		for i in range(n):
			if ss[i % 3] != string[i]:
				dp[i + 1] = 1
		for i in range(1, n + 1):
			dp[i] += dp[i - 1]
		for i in range(k, n + 1):
			ans = dp[i] - dp[i - k] if dp[i] - dp[i - k] < ans else ans
	print(ans)


from sys import stdin
t = int(stdin.readline())
for _ in range(t):
	(n, k) = map(int, stdin.readline().split())
	l = stdin.readline()
	m = float('inf')
	for q in range(3):
		if q == 0:
			s = 'RGB'
		elif q == 1:
			s = 'GBR'
		else:
			s = 'BRG'
		(i, count, c) = (0, 0, 0)
		while i < k:
			if l[i] != s[c]:
				count += 1
			i += 1
			c += 1
			if c == 3:
				c = 0
		m = min(m, count)
		last = s[0]
		while i < n:
			if l[i - k] != last:
				count -= 1
			if l[i] != s[c]:
				count += 1
			i += 1
			c += 1
			if c == 3:
				c = 0
			m = min(m, count)
			if last == s[0]:
				last = s[1]
			elif last == s[1]:
				last = s[2]
			else:
				last = s[0]
	print(m)


from sys import stdin, stdout
import collections
Q = int(input())
for i in range(Q):
	(N, K) = [int(x) for x in stdin.readline().split()]
	s = input()
	res = N + 1
	for k in range(3):
		if k == 0:
			RGB = ['R', 'G', 'B']
		if k == 1:
			RGB = ['G', 'B', 'R']
		if k == 2:
			RGB = ['B', 'R', 'G']
		prefix = [0] * N
		ssum = 0
		for i in range(N):
			letter = RGB[i % 3]
			if s[i] != letter:
				ssum += 1
			prefix[i] = ssum
		for i in range(K - 1, N):
			if i == K - 1:
				d = prefix[i]
			else:
				d = prefix[i] - prefix[i - K]
			res = min(res, d)
	print(res)


def gen(n, b):
	a = [(x + b) % 3 for x in range(n)]
	s = ''
	for i in range(n):
		if a[i] == 0:
			s += 'R'
		if a[i] == 1:
			s += 'G'
		if a[i] == 2:
			s += 'B'
	return s
q = int(input())
for _ in range(q):
	(n, k) = map(int, input().split())
	s = input()
	ans = n
	for xi in range(3):
		t = gen(n, xi)
		diff = 0
		for i in range(k):
			if s[i] != t[i]:
				diff += 1
		ans = min(ans, diff)
		for j in range(k, n):
			if s[j - k] != t[j - k]:
				diff -= 1
			if s[j] != t[j]:
				diff += 1
			ans = min(ans, diff)
	print(ans)


import sys
input = sys.stdin.readline

def generateRecString(s):
	t = s * (10 ** 5 + 7)
	return t
start = {}
start['R'] = generateRecString('RGB')
start['G'] = generateRecString('GBR')
start['B'] = generateRecString('BRG')
enum = ['R', 'G', 'B']

def findDiff(s, sc):
	diff = 0
	for i in range(len(s)):
		if s[i] != start[sc][i]:
			diff += 1
	return diff
T = int(input())
for t in range(T):
	(n, k) = list(map(int, input().split()))
	s = input()
	ans = float('inf')
	for e in range(3):
		tmp = findDiff(s[0:k], enum[e])
		ans = min(ans, tmp)
		for i in range(1, n - k + 1):
			if s[i - 1] != start[enum[e]][i - 1]:
				tmp -= 1
			if s[i + k - 1] != start[enum[e]][i + k - 1]:
				tmp += 1
			ans = min(ans, tmp)
	print(ans)


import atexit
import io
import sys
R = lambda : map(int, input().split())
MAXC = int(100000.0)
for _ in range(int(input())):
	(n, k) = R()
	S = input()
	col = 'RGB'
	ans = int(1000000000.0)
	for offset in range(3):
		lst = []
		cnt = 0
		for j in range(n):
			lst.append(S[j] != col[(j + offset) % 3])
			cnt += lst[j]
			if j >= k:
				cnt -= lst[j - k]
			if j >= k - 1:
				ans = min(cnt, ans)
	print(ans)


import os
import sys
from io import BytesIO, IOBase
from math import inf

def main():
	for _ in range(int(input())):
		(n, k) = map(int, input().split())
		s = list(input().rstrip())
		a = ['RGB', 'GBR', 'BRG']
		dp = [[0 for _ in range(n + 1)] for _ in range(3)]
		for j in range(3):
			for i in range(n):
				dp[j][i + 1] = (s[i] != a[j][i % 3]) + dp[j][i]
		mi = inf
		for i in range(n - k + 1):
			for j in range(3):
				mi = min(mi, dp[j][i + k] - dp[j][i])
		print(mi)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		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')
main()


from itertools import accumulate
import sys
readline = sys.stdin.readline
q = int(readline())
for query in range(q):
	(n, k) = map(int, readline().split())
	s = readline()[:-1]
	RGB = 'RGB'
	sum = [[0] * n] * 3
	res = n
	for i in range(3):
		for j in range(n):
			sum[i][j] = int(s[j] != RGB[(i + j) % 3])
		sum[i] = list(accumulate(sum[i]))
		tmp = sum[i][k - 1]
		for j in range(k, n):
			tmp = min(tmp, sum[i][j] - sum[i][j - k])
		res = min(res, tmp)
	print(res)


from sys import stdin
input = stdin.readline
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = [i for i in input().strip()]
	if k == 1:
		print(0)
	else:
		seq = ['R', 'G', 'B']
		dp = [[0] * n for i in range(3)]
		dp[0][0] = int(s[0] != 'R')
		dp[1][0] = int(s[0] != 'G')
		dp[2][0] = int(s[0] != 'B')
		ct = 10 ** 6
		for i in range(1, n):
			dp[0][i] = dp[2][i - 1] + (s[i] != 'R')
			dp[1][i] = dp[0][i - 1] + (s[i] != 'G')
			dp[2][i] = dp[1][i - 1] + (s[i] != 'B')
			if i >= k - 1:
				ct = min(ct, (s[i + 1 - k] != seq[(1 - k) % 3]) + dp[0][i] - dp[(1 - k) % 3][1 + i - k])
				ct = min(ct, (s[i + 1 - k] != seq[(2 - k) % 3]) + dp[1][i] - dp[(2 - k) % 3][1 + i - k])
				ct = min(ct, (s[i + 1 - k] != seq[(3 - k) % 3]) + dp[2][i] - dp[(3 - k) % 3][1 + i - k])
		print(ct)


def generate(n, start):
	ans = '' + start
	added = start
	for i in range(n - 1):
		if added == 'B':
			added = 'R'
		elif added == 'G':
			added = 'B'
		else:
			added = 'G'
		ans += added
	return ans
q = int(input())
for i in range(q):
	nk = input().split(' ')
	(n, k) = (int(nk[0]), int(nk[1]))
	(s1, s2, s3) = (generate(n, 'R'), generate(n, 'G'), generate(n, 'B'))
	s = input()
	a1 = [0] if s[0] != s1[0] else [1]
	a2 = [0] if s[0] != s2[0] else [1]
	a3 = [0] if s[0] != s3[0] else [1]
	for j in range(1, n):
		if s[j] != s1[j]:
			a1.append(a1[-1] + 1)
		else:
			a1.append(a1[-1])
		if s[j] != s2[j]:
			a2.append(a2[-1] + 1)
		else:
			a2.append(a2[-1])
		if s[j] != s3[j]:
			a3.append(a3[-1] + 1)
		else:
			a3.append(a3[-1])
	ans = n
	for j in range(n):
		if j + k - 1 >= n:
			break
		changes = a1[j + k - 1] - a1[j]
		if s[j] != s1[j]:
			changes += 1
		if changes < ans:
			ans = changes
	for j in range(n):
		if j + k - 1 >= n:
			break
		changes = a2[j + k - 1] - a2[j]
		if s[j] != s2[j]:
			changes += 1
		if changes < ans:
			ans = changes
	for j in range(n):
		if j + k - 1 >= n:
			break
		changes = a3[j + k - 1] - a3[j]
		if s[j] != s3[j]:
			changes += 1
		if changes < ans:
			ans = changes
	print(ans)


T = int(input())
for t in range(T):
	(N, K) = map(int, input().split())
	target1 = 'RGB' * (K // 3 + 1)
	target1 = target1[:K]
	target2 = 'GBR' * (K // 3 + 1)
	target2 = target2[:K]
	target3 = 'BRG' * (K // 3 + 1)
	target3 = target3[:K]
	target = [target1, target2, target3]
	s = input()
	D = K
	for n in range(N - K + 1):
		a = s[n:n + K]
		for t in target:
			d = 0
			for i in range(K):
				if a[i] != t[i]:
					d += 1
			if d < D:
				D = d
				if D == 0:
					break
		if D == 0:
			break
	print(D)


def change(d):
	for i in range(0, len(d)):
		if d[i] == 'R':
			d[i] = 1
		elif d[i] == 'G':
			d[i] = 2
		else:
			d[i] = 3
	return d
a = int(input())
if a < 200000:
	for i in range(0, a):
		(n, k) = map(int, input().split())
		c = list(input())
		c = change(c)
		e = 0
		f = 0
		g = 0
		h = [0] * (3 * (n - k + 1))
		mod3 = [0] * n
		for i in range(0, n):
			mod3[i] = (c[i] - i) % 3
		for i in range(0, k):
			if mod3[i] == 0:
				h[0] = h[0] + 1
			elif mod3[i] == 1:
				h[1] = h[1] + 1
			elif mod3[i] == 2:
				h[2] = h[2] + 1
		for i in range(0, n - k):
			if mod3[i] == 1:
				if mod3[i + k] == 1:
					h[3 * (i + 1)] = h[3 * i]
					h[3 * (i + 1) + 1] = h[3 * i + 1]
					h[3 * (i + 1) + 2] = h[3 * i + 2]
				elif mod3[i + k] == 2:
					h[3 * (i + 1)] = h[3 * i]
					h[3 * (i + 1) + 1] = h[3 * i + 1] - 1
					h[3 * (i + 1) + 2] = h[3 * i + 2] + 1
				elif mod3[i + k] == 0:
					h[3 * (i + 1)] = h[3 * i] + 1
					h[3 * (i + 1) + 1] = h[3 * i + 1] - 1
					h[3 * (i + 1) + 2] = h[3 * i + 2]
			elif mod3[i] == 2:
				if mod3[i + k] == 1:
					h[3 * (i + 1)] = h[3 * i]
					h[3 * (i + 1) + 1] = h[3 * i + 1] + 1
					h[3 * (i + 1) + 2] = h[3 * i + 2] - 1
				elif mod3[i + k] == 2:
					h[3 * (i + 1)] = h[3 * i]
					h[3 * (i + 1) + 1] = h[3 * i + 1]
					h[3 * (i + 1) + 2] = h[3 * i + 2]
				elif mod3[i + k] == 0:
					h[3 * (i + 1)] = h[3 * i] + 1
					h[3 * (i + 1) + 1] = h[3 * i + 1]
					h[3 * (i + 1) + 2] = h[3 * i + 2] - 1
			elif mod3[i] == 0:
				if mod3[i + k] == 1:
					h[3 * (i + 1)] = h[3 * i] - 1
					h[3 * (i + 1) + 1] = h[3 * i + 1] + 1
					h[3 * (i + 1) + 2] = h[3 * i + 2]
				elif mod3[i + k] == 2:
					h[3 * (i + 1)] = h[3 * i] - 1
					h[3 * (i + 1) + 1] = h[3 * i + 1]
					h[3 * (i + 1) + 2] = h[3 * i + 2] + 1
				elif mod3[i + k] == 0:
					h[3 * (i + 1)] = h[3 * i]
					h[3 * (i + 1) + 1] = h[3 * i + 1]
					h[3 * (i + 1) + 2] = h[3 * i + 2]
		print(k - max(h))
else:
	for i in range(0, a):
		print(0)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	st = input()
	li = [['R', 'G', 'B'], ['G', 'B', 'R'], ['B', 'R', 'G']]
	ans = 1000000
	for i in range(3):
		ar = [0] * n
		for j in range(n):
			rem = j % 3
			if li[i][rem] != st[j]:
				ar[j] = 1
		su = 0
		for j in range(k):
			su += ar[j]
		ans = min(ans, su)
		for j in range(k, n):
			su -= ar[j - k]
			su += ar[j]
			ans = min(ans, su)
	print(ans)


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from itertools import *
import bisect
from heapq import *
from math import *
from collections import deque
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		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')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
mod = 1000000007

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def lcm(a, b):
	return a * b // gcd(a, b)

def power(a, b):
	ans = 1
	while b > 0:
		if b % 2 == 1:
			ans *= a
		a *= a
		b //= 2
	return ans

def ncr(n, r):
	return factorial(n) // (factorial(r) * factorial(max(n - r, 1)))

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def bfs(g, st, lcatable, level):
	visited = [0] * len(g)
	visited[st] = 1
	queue = []
	queue.append(st)
	new = []
	while len(queue) != 0:
		s = queue.pop()
		new.append(s)
		for i in g[s]:
			if visited[i] == 0:
				level[i] = level[s] + 1
				lcatable[i][0] = s
				visited[i] = 1
				queue.append(i)
	return new

def lcatable(g, n, level):
	mx = 0
	while 2 ** mx <= n:
		mx += 1
	lcatable = [[-1] * mx for i in range(n + 1)]
	bfs(g, 1, lcatable, level)
	for j in range(1, mx):
		for i in range(1, n + 1):
			if lcatable[i][j - 1] != -1:
				lcatable[i][j] = lcatable[lcatable[i][j - 1]][j - 1]
	return lcatable

def dist(x0, y0, x1, y1):
	return abs(x0 - x1) + abs(y0 - y1)

def solve():
	(n, k) = sep()
	s = list(inp())
	for i in range(n):
		if s[i] == 'B':
			s[i] = 2
		if s[i] == 'G':
			s[i] = 1
		if s[i] == 'R':
			s[i] = 0
	ans = 10 ** 10
	for i in range(0, n - (k - 1)):
		curr = s[i:i + k]
		cnt1 = 0
		ind = i
		for j in range(0, k):
			if s[ind] != j % 3:
				cnt1 += 1
			ind += 1
		cnt2 = 0
		ind = i
		for j in range(1, 1 + k):
			if s[ind] != j % 3:
				cnt2 += 1
			ind += 1
		cnt3 = 0
		ind = i
		for j in range(2, 2 + k):
			if s[ind] != j % 3:
				cnt3 += 1
			ind += 1
		ans = min(ans, cnt1, cnt2, cnt3)
	print(ans)
testcase(int(inp()))


q = int(input())
for _ in range(q):
	(n, k) = list(map(int, input().split()))
	s = input()
	rgb = 0
	gbr = 0
	brg = 0
	RGB = 'RGB'
	for i in range(k):
		if i % 3 == 0:
			rgb += int(s[i] != 'R')
			gbr += int(s[i] != 'G')
			brg += int(s[i] != 'B')
		if i % 3 == 1:
			rgb += int(s[i] != 'G')
			gbr += int(s[i] != 'B')
			brg += int(s[i] != 'R')
		if i % 3 == 2:
			rgb += int(s[i] != 'B')
			gbr += int(s[i] != 'R')
			brg += int(s[i] != 'G')
	minimum = min(rgb, brg, gbr)
	for i in range(k, n):
		if (i - k) % 3 == 0:
			rgb -= int(s[i - k] != 'R')
			gbr -= int(s[i - k] != 'G')
			brg -= int(s[i - k] != 'B')
		if (i - k) % 3 == 1:
			rgb -= int(s[i - k] != 'G')
			gbr -= int(s[i - k] != 'B')
			brg -= int(s[i - k] != 'R')
		if (i - k) % 3 == 2:
			rgb -= int(s[i - k] != 'B')
			gbr -= int(s[i - k] != 'R')
			brg -= int(s[i - k] != 'G')
		if i % 3 == 0:
			rgb += int(s[i] != 'R')
			gbr += int(s[i] != 'G')
			brg += int(s[i] != 'B')
		if i % 3 == 1:
			rgb += int(s[i] != 'G')
			gbr += int(s[i] != 'B')
			brg += int(s[i] != 'R')
		if i % 3 == 2:
			rgb += int(s[i] != 'B')
			gbr += int(s[i] != 'R')
			brg += int(s[i] != 'G')
		minimum = min(minimum, rgb, brg, gbr)
	print(minimum)


def f(a, b):
	c = 0
	for i in range(len(a)):
		if a[i] != b[i]:
			c += 1
	return c
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	inf = 'RGB' * ((k + 2) // 3)
	inf += 'RGB'[:(k + 2) % 3]
	a1 = inf[:k]
	a2 = inf[1:k + 1]
	a3 = inf[2:k + 2]
	l = []
	for i in range(n - k + 1):
		l.append(s[i:k + i])
	ans = 10000
	for i in l:
		if f(a1, i) < ans:
			ans = f(a1, i)
		if f(a2, i) < ans:
			ans = f(a2, i)
		if f(a3, i) < ans:
			ans = f(a3, i)
	print(ans)


dct = ['R', 'G', 'B']

def check(a):
	mn = list()
	for i in range(3):
		lmn = 0
		for j in range(len(a)):
			if a[j] != dct[(j + i) % 3]:
				lmn += 1
		mn.append(lmn)
	return min(mn)
qs = int(input())
for i in range(qs):
	mn = 0
	(ls, la) = map(lambda x: int(x), input().split())
	s = input()
	k = la
	mn = ls
	while k <= ls:
		a = s[k - la:k]
		if check(a) < mn:
			mn = check(a)
		k += 1
	print(mn)


def matches(pos, c, case):
	if case == 0:
		return pos % 3 == 'RGB'.index(c)
	elif case == 1:
		return pos % 3 == 'GBR'.index(c)
	else:
		return pos % 3 == 'BRG'.index(c)
q = int(input())
for _ in range(q):
	(n, k) = map(int, input().split())
	s = input()
	mglobal = k
	r = g = b = 0
	for (i, c) in enumerate(s[:k]):
		r += not matches(i, c, 0)
		g += not matches(i, c, 1)
		b += not matches(i, c, 2)
	mglobal = min([mglobal, r, g, b])
	for (i, c) in enumerate(s[k:]):
		i += k
		r += -(not matches(i - k, s[i - k], 0)) + (not matches(i, c, 0))
		g += -(not matches(i - k, s[i - k], 1)) + (not matches(i, c, 1))
		b += -(not matches(i - k, s[i - k], 2)) + (not matches(i, c, 2))
		mglobal = min([mglobal, r, g, b])
	print(mglobal)


import math
inf = 10 ** 5
import sys
for _ in range(int(input())):
	(n, k) = list(map(int, sys.stdin.readline().split()))
	s = '$' + sys.stdin.readline().split('\n')[0]
	r = [0 for i in range(n + 1)]
	g = [0 for i in range(n + 1)]
	b = [0 for i in range(n + 1)]
	for i in range(1, n + 1):
		if s[i] == 'R':
			r[i] = r[max(0, i - 3)] + 1
		else:
			r[i] = r[max(0, i - 3)]
		if s[i] == 'G':
			g[i] = g[max(0, i - 3)] + 1
		else:
			g[i] = g[max(0, i - 3)]
		if s[i] == 'B':
			b[i] = b[max(0, i - 3)] + 1
		else:
			b[i] = b[max(0, i - 3)]

	def check(i, k):
		m = 10 ** 12
		rr = 0
		gg = 0
		bb = 0
		if k % 3 == 1:
			rr = 1
		if k % 3 == 2:
			rr = 1
			gg = 1
		kk = k
		m = min(m, k // 3 + rr - (r[i + (k - 1) // 3 * 3] - r[max(i - 3, 0)]) + k // 3 + gg - (g[i + 1 + (k - 2) // 3 * 3] - g[max(i - 2, 0)]) + k // 3 - (b[i + 2 + (k - 3) // 3 * 3] - b[max(i - 1, 0)]))
		m = min(m, k // 3 + rr - (g[i + (k - 1) // 3 * 3] - g[max(i - 3, 0)]) + k // 3 + gg - (b[i + 1 + (k - 2) // 3 * 3] - b[max(i - 2, 0)]) + k // 3 - (r[i + 2 + (k - 3) // 3 * 3] - r[max(i - 1, 0)]))
		m = min(m, k // 3 + rr - (b[i + (k - 1) // 3 * 3] - b[max(i - 3, 0)]) + k // 3 + gg - (r[i + 1 + (k - 2) // 3 * 3] - r[max(i - 2, 0)]) + k // 3 - (g[i + 2 + (k - 3) // 3 * 3] - g[max(i - 1, 0)]))
		return m
	m = 10 ** 13
	for i in range(n - k + 1):
		m = min(m, check(i + 1, k))
	print(max(m, 0))


import sys
input = sys.stdin.readline
q = int(input())
for i in range(q):
	(n, k) = map(int, input().split())
	s = input()[:n]
	if k == 1:
		print(0)
		continue
	rgb = [0, 0, 0]
	a = 1
	for (j, c) in enumerate(s):
		if c == 'R':
			rgb[j % 3] += 1
		elif c == 'G':
			rgb[(j - 1) % 3] += 1
		else:
			rgb[(j - 2) % 3] += 1
		if j + 1 >= k:
			a = max(a, max(rgb))
			t = s[j - k + 1]
			if t == 'R':
				rgb[(j - k + 1) % 3] -= 1
			elif t == 'G':
				rgb[(j - k) % 3] -= 1
			else:
				rgb[(j - k - 1) % 3] -= 1
			if a == k:
				print(0)
				break
	else:
		print(k - a)


queries = int(input())
rgb = ['R', 'G', 'B']
(r_idx, g_idx, b_idx) = (0, 1, 2)
ret = ''
for q in range(queries):
	(n, k) = list(map(int, input().split()))
	s = input()
	if n == 1 or k == 1:
		ret += '0\n'
		continue
	num_windows = n - k + 1
	r_arr = [0] * num_windows
	g_arr = [0] * num_windows
	b_arr = [0] * num_windows
	min_cost = [0] * num_windows
	for i in range(num_windows):
		r_start = g_start = b_start = 0
		if i == 0:
			for j in range(k):
				if s[i + j] != rgb[(j + r_idx) % 3]:
					r_start += 1
				if s[i + j] != rgb[(j + g_idx) % 3]:
					g_start += 1
				if s[i + j] != rgb[(j + b_idx) % 3]:
					b_start += 1
		else:
			j = k - 1
			r_start = b_arr[i - 1] - (1 if s[i - 1] != 'B' else 0) + (1 if s[i + j] != rgb[(j + r_idx) % 3] else 0)
			g_start = r_arr[i - 1] - (1 if s[i - 1] != 'R' else 0) + (1 if s[i + j] != rgb[(j + g_idx) % 3] else 0)
			b_start = g_arr[i - 1] - (1 if s[i - 1] != 'G' else 0) + (1 if s[i + j] != rgb[(j + b_idx) % 3] else 0)
		r_arr[i] = r_start
		g_arr[i] = g_start
		b_arr[i] = b_start
		min_cost[i] = min(r_start, g_start, b_start)
		if min_cost[i] == 0:
			break
	ret += str(min(min_cost)) + '\n'
print(ret)


from sys import stdin, stdout
readline = stdin.readline
write = stdout.write

def check(a, b):
	res = len(a)
	for i in range(len(a) - len(b) + 1):
		tmp = 0
		for (x, y) in zip(a[i:], b):
			tmp += x != y
		res = min(res, tmp)
	return res
q = int(readline())
for query in range(q):
	(n, k) = map(int, readline().split())
	RGB = 'RGB' * k
	s = readline()
	s = s[:-1]
	print(min(check(s, RGB[0:k]), check(s, RGB[1:k + 1]), check(s, RGB[2:k + 2])))


from sys import stdin, stdout
from math import gcd, sqrt, factorial, pi, inf
from collections import deque, defaultdict
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
P = lambda x: stdout.write(str(x) + '\n')
lcm = lambda x, y: x * y // gcd(x, y)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 1 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
sm = lambda x: (x ** 2 + x) // 2
N = 10 ** 6 + 6

def solve(a, n, k):
	val = sum(a[:k])
	ans = val
	for i in range(k, n):
		val += a[i]
		val -= a[i - k]
		ans = min(val, ans)
	return ans
for _ in range(I()):
	(n, k) = R()
	s = S()
	P = [0] * n
	G = [0] * n
	B = [0] * n
	x = 'RGB'
	for i in range(n):
		if x[i % 3] != s[i]:
			P[i] = 1
		if x[(i + 1) % 3] != s[i]:
			G[i] = 1
		if x[(i + 2) % 3] != s[i]:
			B[i] = 1
	print(min(solve(P, n, k), solve(G, n, k), solve(B, n, k)))


q = int(input())
for i in range(q):
	(n, k) = map(int, input().split())
	s = input()
	m = 10 ** 4
	for j in range(n):
		if j + k <= n:
			l1 = ['R', 'G', 'B']
			(m1, m2, m3) = (0, 0, 0)
			for i in range(j, j + k):
				if l1[(i - j) % 3] != s[i]:
					m1 += 1
			for i in range(j, j + k):
				if l1[(i + 1 - j) % 3] != s[i]:
					m2 += 1
			for i in range(j, j + k):
				if l1[(i + 2 - j) % 3] != s[i]:
					m3 += 1
			m = min(m, m1, m2, m3)
		else:
			break
	print(m)


import os
import gc
import sys
from io import BytesIO, IOBase
from collections import Counter
from collections import deque
import heapq
import math
import statistics

def sin():
	return input()

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

def sain():
	return input().split()

def iin():
	return int(sin())
MAX = float('inf')
MIN = float('-inf')
MOD = 10 ** 9 + 7

def readTree(n, m):
	adj = [deque([]) for _ in range(n + 1)]
	for _ in range(m):
		(u, v) = ain()
		adj[u].append(v)
		adj[v].append(u)
	return adj

def solve(n):
	pass

def main():
	for _ in range(iin()):
		(n, k) = ain()
		s = sin()
		ss1 = 'RGB'
		ss2 = 'GBR'
		ss3 = 'BRG'
		ss1 = ss1 * (k // 3) + ss1[0:k % 3]
		ss2 = ss2 * (k // 3) + ss2[0:k % 3]
		ss3 = ss3 * (k // 3) + ss3[0:k % 3]
		mini = MAX
		for i in range(0, n - k + 1):
			x = s[i:i + k]
			c1 = c2 = c3 = 0
			for j in range(k):
				if ss1[j] != x[j]:
					c1 += 1
				if ss2[j] != x[j]:
					c2 += 1
				if ss3[j] != x[j]:
					c3 += 1
			mini = min(mini, c1, c2, c3)
		print(mini)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		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')
main()


x = int(input())
y = []

def kukareku(st, k):
	ans0 = 0
	ans1 = 0
	ans2 = 0
	a = 1
	sp = []
	spans = []
	for i in range(0, len(st)):
		f = []
		if i % 3 == 0:
			if st[i] == 'R':
				f = [0, 1, 1]
			elif st[i] == 'G':
				f = [1, 0, 1]
			else:
				f = [1, 1, 0]
		elif i % 3 == 1:
			if st[i] == 'G':
				f = [0, 1, 1]
			elif st[i] == 'B':
				f = [1, 0, 1]
			else:
				f = [1, 1, 0]
		elif st[i] == 'B':
			f = [0, 1, 1]
		elif st[i] == 'R':
			f = [1, 0, 1]
		else:
			f = [1, 1, 0]
		sp.append(f)
		if a <= k:
			a += 1
			ans0 += sp[i][0]
			ans1 += sp[i][1]
			ans2 += sp[i][2]
		else:
			ans0 += sp[i][0]
			ans1 += sp[i][1]
			ans2 += sp[i][2]
			ans0 -= sp[i - k][0]
			ans1 -= sp[i - k][1]
			ans2 -= sp[i - k][2]
		if a > k:
			spans.append(min(ans0, ans1, ans2))
	return min(spans)
for i in range(0, x):
	(n, k) = input().split()
	k = int(k)
	st = input()
	x = kukareku(st, k)
	y.append(x)
for i in y:
	print(i)


import sys
input = sys.stdin.readline
q = int(input())
for i in range(q):
	(n, k) = map(int, input().split())
	s = input()
	a = k
	for j in range(n - k + 1):
		(a1, a2, a3) = (0, 0, 0)
		for jj in range(k):
			if jj % 3 == 0:
				if s[j + jj] == 'R':
					a2 += 1
					a3 += 1
				elif s[j + jj] == 'G':
					a1 += 1
					a3 += 1
				else:
					a1 += 1
					a2 += 1
			elif jj % 3 == 1:
				if s[j + jj] == 'R':
					a1 += 1
					a2 += 1
				elif s[j + jj] == 'G':
					a2 += 1
					a3 += 1
				else:
					a3 += 1
					a1 += 1
			elif s[j + jj] == 'R':
				a1 += 1
				a3 += 1
			elif s[j + jj] == 'G':
				a1 += 1
				a2 += 1
			else:
				a3 += 1
				a2 += 1
		a = min(a, a1, a2, a3)
	print(a)


t = 'RGB'
q = int(input())
for _ in range(q):
	(n, k) = map(int, input().split())
	s = input()
	ans = 10 ** 9
	for i in range(n - k + 1):
		for off in range(3):
			cnt = 0
			for j in range(k):
				if t[(off + j) % 3] != s[i + j]:
					cnt += 1
			ans = min(ans, cnt)
	print(ans)


import math

def solve(s, n, k):
	new = 'RGB' * math.ceil(n / 3) + 'RG'
	mn = float('inf')
	for j in range(3):
		temp = []
		for i in range(n):
			if new[j + i] == s[i]:
				temp.append(1)
			else:
				temp.append(0)
		one = 0
		for p in range(k):
			if temp[p] == 1:
				one += 1
		mn = min(k - one, mn)
		for p in range(k, len(temp)):
			if temp[p] == 1:
				one += 1
			if temp[p - k] == 1:
				one -= 1
			mn = min(k - one, mn)
	return max(0, mn)
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	print(solve(s, n, k))


q = int(input())
rgb = 'RGB'
answer = []
for req in range(q):
	(n, k) = map(int, input().split())
	s = input()
	last1 = rgb[(k - 1) % 3]
	last2 = rgb[k % 3]
	last3 = rgb[(k + 1) % 3]
	dp = [0, 0, 0]
	for i in range(k):
		if s[i] != rgb[i % 3]:
			dp[0] += 1
		if s[i] != rgb[(i + 1) % 3]:
			dp[1] += 1
		if s[i] != rgb[(i + 2) % 3]:
			dp[2] += 1
	count = min(dp)
	for i in range(k, n):
		first = s[i - k]
		if first != 'R':
			dp[0] -= 1
		if first != 'G':
			dp[1] -= 1
		if first != 'B':
			dp[2] -= 1
		dp.insert(0, dp.pop())
		last = s[i]
		if last != last1:
			dp[0] += 1
		if last != last2:
			dp[1] += 1
		if last != last3:
			dp[2] += 1
		count = min(min(dp), count)
	answer.append(str(count))
print('\n'.join(answer))


T = int(input())
for _ in range(T):
	(n, k) = map(int, input().split())
	s = input()
	d = [[0] * (k + 1), [0] * (k + 1), [0] * (k + 1)]
	res = k
	for (i, v) in enumerate(s):
		p = i % (k + 1)
		pp = (i - 1) % (k + 1)
		z = i > k - 2
		if z:
			q = (i - k) % (k + 1)
		for j in range(3):
			x = v != 'RGB'[(i + j) % 3]
			d[j][p] = d[j][pp] + x
			if z:
				res = min(res, d[j][p] - d[j][q])
	print(res)


from math import inf

def compare(s, start, end):
	ans = inf
	A = 'RGB'
	for j in [0, 1, 2]:
		case = end - start
		for i in range(start, end):
			if A[j] == s[i]:
				case -= 1
			j = (j + 1) % 3
		ans = min(ans, case)
	return ans

def solve(n, k, s):
	ans = inf
	for i in range(n - k + 1):
		ans = min(compare(s, i, i + k), ans)
	return ans
for nt in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	print(solve(n, k, s))


import sys
from collections import deque
input = sys.stdin.readline

def f(l):
	(count1, count2, count3) = (0, 0, 0)
	start = 'R'
	prev = 'B'
	for i in range(len(l)):
		if l[i] != start:
			count1 += 1
		prev = start
		if start == 'R':
			start = 'G'
		elif start == 'G':
			start = 'B'
		else:
			start = 'R'
	start = 'G'
	prev = 'R'
	for i in range(len(l)):
		if l[i] != start:
			count2 += 1
		prev = start
		if start == 'R':
			start = 'G'
		elif start == 'G':
			start = 'B'
		else:
			start = 'R'
	start = 'B'
	prev = 'G'
	for i in range(len(l)):
		if l[i] != start:
			count3 += 1
		prev = start
		if start == 'R':
			start = 'G'
		elif start == 'G':
			start = 'B'
		else:
			start = 'R'
	return min(count1, count2, count3)
t = int(input())
for nt in range(t):
	(n, k) = map(int, input().split())
	s = input()
	temp = list(s[0:k])
	temp = deque(temp)
	m = 1000000
	temp2 = f(temp)
	if temp2 < m:
		m = temp2
	for i in range(k, n):
		temp.popleft()
		temp.append(s[i])
		temp2 = f(temp)
		if temp2 < m:
			m = temp2
	print(m)


from sys import stdin
q = int(stdin.readline().strip())
for _ in range(q):
	(n, k) = [int(i) for i in stdin.readline().strip().split()]
	s = stdin.readline().strip()
	codes = {'R': 0, 'G': 1, 'B': 2}
	res = len(s)
	(a, b, c) = (0, 0, 0)
	for i in range(k):
		letter = s[i]
		a += codes[letter] != i % 3
		b += codes[letter] != (i + 1) % 3
		c += codes[letter] != (i + 2) % 3
	res = min(res, a, b, c)
	for j in range(k, len(s)):
		i = j - k
		x = s[j - k]
		y = s[j]
		a += -(codes[x] != i % 3) + (codes[y] != j % 3)
		b += -(codes[x] != (i + 1) % 3) + (codes[y] != (j + 1) % 3)
		c += -(codes[x] != (i + 2) % 3) + (codes[y] != (j + 2) % 3)
		res = min(a, b, c, res)
	print(res)


from sys import stdin, stdout
input = stdin.readline
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = str(input())
	(s1, s2, s3) = ('', '', '')
	for i in range(1, n + 1):
		if i % 3 == 0:
			s1 += 'R'
			s2 += 'G'
			s3 += 'B'
		elif i % 3 == 1:
			s1 += 'G'
			s2 += 'B'
			s3 += 'R'
		elif i % 3 == 2:
			s1 += 'B'
			s2 += 'R'
			s3 += 'G'
	ans = 200000
	for i in range(n - k + 1):
		ii = i
		(cnt1, cnt2, cnt3) = (0, 0, 0)
		for j in range(ii, ii + k):
			if s[j] != s1[j]:
				cnt1 += 1
			if s[j] != s2[j]:
				cnt2 += 1
			if s[j] != s3[j]:
				cnt3 += 1
		ans = min(ans, cnt1, cnt2, cnt3)
	print(ans)


import sys
t = int(sys.stdin.readline())
x = 'RGB'
y = 'GBR'
z = 'BRG'
for i in range(t):
	(n, k) = list(map(int, sys.stdin.readline().strip().split()))
	a = sys.stdin.readline().strip()
	xk = x
	yk = y
	zk = z
	op = 2001
	xd = []
	yd = []
	zd = []
	xdc = 0
	ydc = 0
	zdc = 0
	b = a
	for j in range(k):
		if b[j] != xk[j % 3]:
			xd.append(1)
			xdc += 1
		else:
			xd.append(0)
		if b[j] != yk[j % 3]:
			yd.append(1)
			ydc += 1
		else:
			yd.append(0)
		if b[j] != zk[j % 3]:
			zdc += 1
			zd.append(1)
		else:
			zd.append(0)
	op = min(xdc, ydc, zdc)
	for j in range(k, n):
		if b[j] != xk[j % 3]:
			xd.append(1)
			xdc += 1
		else:
			xd.append(0)
		if b[j] != yk[j % 3]:
			yd.append(1)
			ydc += 1
		else:
			yd.append(0)
		if b[j] != zk[j % 3]:
			zdc += 1
			zd.append(1)
		else:
			zd.append(0)
		xdc -= xd[j - k]
		ydc -= yd[j - k]
		zdc -= zd[j - k]
		op = min(op, xdc, ydc, zdc)
	print(op)


q = int(input())
b = []
for m in range(q):
	(n, k) = map(int, input().split())
	l = input()
	k1 = 'R'
	k2 = 'G'
	k3 = 'B'
	for i in range(1, k):
		if k1[i - 1] == 'R':
			k1 = k1 + 'G'
		if k1[i - 1] == 'G':
			k1 = k1 + 'B'
		if k1[i - 1] == 'B':
			k1 = k1 + 'R'
		if k2[i - 1] == 'R':
			k2 = k2 + 'G'
		if k2[i - 1] == 'G':
			k2 = k2 + 'B'
		if k2[i - 1] == 'B':
			k2 = k2 + 'R'
		if k3[i - 1] == 'R':
			k3 = k3 + 'G'
		if k3[i - 1] == 'G':
			k3 = k3 + 'B'
		if k3[i - 1] == 'B':
			k3 = k3 + 'R'
	minn = n
	for i in range(n - k + 1):
		tec = 0
		for j in range(k):
			if l[i + j] != k1[j]:
				tec += 1
		if tec < minn:
			minn = tec
	for i in range(n - k + 1):
		tec = 0
		for j in range(k):
			if l[i + j] != k2[j]:
				tec += 1
		if tec < minn:
			minn = tec
	for i in range(n - k + 1):
		tec = 0
		for j in range(k):
			if l[i + j] != k3[j]:
				tec += 1
		if tec < minn:
			minn = tec
	b.append(minn)
for i in range(q):
	print(b[i])


def check(s, k):
	a = 'RGB'
	s1 = a * (k // 3) + a[:k % 3]
	k -= 1
	s2 = 'B' + a * (k // 3) + a[:k % 3]
	k += 1
	s3 = 'GB'[:min(k, 2)]
	k -= 2
	s3 = s3 + a * (k // 3) + a[:k % 3]
	count = 0
	k += 2
	for i in range(k):
		if s[i] != s1[i]:
			count += 1
	count1 = 0
	for i in range(k):
		if s[i] != s2[i]:
			count1 += 1
	count = min(count, count1)
	count1 = 0
	for i in range(k):
		if s[i] != s3[i]:
			count1 += 1
	return min(count, count1)
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	minn = k
	for i in range(n - k + 1):
		minn = min(check(s[i:i + k], k), minn)
	print(minn)


R = lambda : map(int, input().split())
for _ in range(int(input())):
	(n, k) = R()
	s = input()
	p = (k + 2) // 2
	l = 'RGB' * p
	res = n
	for i in range(n - k + 1):
		c = 0
		for j in range(0, k):
			c += s[i + j] != l[j]
		res = min(res, c)
		c = 0
		for j in range(1, k + 1):
			c += s[i + j - 1] != l[j]
		res = min(res, c)
		c = 0
		for j in range(2, k + 2):
			c += s[i + j - 2] != l[j]
		res = min(res, c)
	print(res)


def c(a1, s, a, k):
	for i in range(n):
		if s[i] != a1[i % 3]:
			if i == 0:
				a[i] = 1
			else:
				a[i] = a[i - 1] + 1
		elif i == 0:
			a[i] = a[i]
		else:
			a[i] = a[i - 1]
	rslt = k
	for i in range(k - 1, n):
		if i == k - 1:
			rslt = min(a[i], rslt)
		else:
			rslt = min(rslt, a[i] - a[i - k])
	return rslt
for i in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	a1 = 'RGB'
	a2 = 'GBR'
	a3 = 'BRG'
	ans = []
	a = [0] * n
	b = [0] * n
	ci = [0] * n
	print(min(c(a1, s, a, k), c(a2, s, b, k), c(a3, s, ci, k)))


import sys
Input = lambda : map(int, sys.stdin.readline().split())
for i in range(int(sys.stdin.readline())):
	(n, k) = Input()
	s = sys.stdin.readline()
	if k == 1:
		sys.stdout.write('0\n')
	else:
		SUB = 'RGB' * k
		res = float('inf')
		for j in range(n - k + 1):
			yy1 = s[j:k + j]
			for q in range(3):
				yy2 = SUB[q:k + q]
				t = 0
				for (w, e) in zip(yy1, yy2):
					if e != w:
						t += 1
				res = min(t, res)
		sys.stdout.write(str(res))
		sys.stdout.write('\n')


from sys import stdin
import math
rgb = 'RGB'
for query in range(int(stdin.readline())):
	(n, k) = map(int, stdin.readline().split())
	s = stdin.readline()
	ans = math.inf
	for start in range(3):
		dp = [0 for i in range(n + 1)]
		for i in range(n):
			cur = rgb[(start + i) % len(rgb)]
			dp[i + 1] = dp[i] + int(s[i] != cur)
		for i in range(n - k + 1):
			ans = min(ans, dp[i + k] - dp[i])
	print(ans)


p = 'RGB'
q = 'GBR'
r = 'BRG'
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = input()
	x = [0]
	y = [0]
	z = [0]
	for i in range(n):
		if a[i] != p[i % 3]:
			x.append(x[i] + 1)
		else:
			x.append(x[i])
		if a[i] != q[i % 3]:
			y.append(y[i] + 1)
		else:
			y.append(y[i])
		if a[i] != r[i % 3]:
			z.append(z[i] + 1)
		else:
			z.append(z[i])
	j = 0
	m = s = k
	if m:
		while s <= n:
			m = min(m, x[s] - x[j], y[s] - y[j], z[s] - z[j])
			j += 1
			s += 1
	print(m)


import math
import os
import sys
from math import ceil, floor, sqrt, gcd, factorial
from io import BytesIO, IOBase
from collections import Counter
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		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')

def prog_name():
	(n, k) = map(int, input().split())
	s = input()
	ss = 'RGB' * 1000
	need1 = ss[:k]
	need2 = ss[1:k + 1]
	need3 = ss[2:k + 2]
	change = 0
	if need2 in s or need3 in s or need1 in s:
		print(change)
	else:
		M = math.inf
		for i in range(n - k + 1):
			(c, c1, c2) = (0, 0, 0)
			ind = 0
			for j in range(i, i + k):
				if s[j] != need1[ind]:
					c += 1
				if s[j] != need2[ind]:
					c1 += 1
				if s[j] != need3[ind]:
					c2 += 1
				ind += 1
			M = min(M, min(c, min(c2, c1)))
		print(M)

def main():
	T = int(input())
	for unique in range(T):
		prog_name()
main()


q = int(input())

def slide(s, l):
	ans = len(l)
	for i in range(len(s) - len(l) + 1):
		cnt = 0
		for j in range(len(l)):
			if s[i + j] != l[j]:
				cnt += 1
		ans = min(ans, cnt)
	return ans
for i in range(q):
	(n, k) = map(int, input().split())
	p = input()
	L = []
	s = 'RGB' * 2000
	for i in range(3):
		L.append(s[i:i + k])
	ans = k
	for i in range(3):
		ans = min(slide(p, L[i]), ans)
	print(ans)


import sys
input = sys.stdin.readline
q = int(input())
for i in range(q):
	(n, k) = map(int, input().split())
	s = input()
	(R, G, B) = (0, 0, 0)
	ans = float('inf')
	for j in range(n):
		if j % 3 == 0:
			if s[j] == 'R':
				G += 1
				B += 1
			elif s[j] == 'G':
				R += 1
				B += 1
			else:
				R += 1
				G += 1
		elif j % 3 == 1:
			if s[j] == 'R':
				G += 1
				R += 1
			elif s[j] == 'G':
				G += 1
				B += 1
			else:
				R += 1
				B += 1
		elif s[j] == 'R':
			R += 1
			B += 1
		elif s[j] == 'G':
			R += 1
			G += 1
		else:
			G += 1
			B += 1
		if j >= k - 1:
			ans = min(ans, R, G, B)
			if (j - k + 1) % 3 == 0:
				if s[j - k + 1] == 'R':
					G -= 1
					B -= 1
				elif s[j - k + 1] == 'G':
					R -= 1
					B -= 1
				else:
					R -= 1
					G -= 1
			elif (j - k + 1) % 3 == 1:
				if s[j - k + 1] == 'R':
					G -= 1
					R -= 1
				elif s[j - k + 1] == 'G':
					G -= 1
					B -= 1
				else:
					R -= 1
					B -= 1
			elif s[j - k + 1] == 'R':
				R -= 1
				B -= 1
			elif s[j - k + 1] == 'G':
				R -= 1
				G -= 1
			else:
				G -= 1
				B -= 1
	print(ans)


def maxSum(arr, n, k):
	if n < k:
		print('Invalid')
		return -1
	res = 0
	for i in range(k):
		res += arr[i]
	curr_sum = res
	for i in range(k, n):
		curr_sum += arr[i] - arr[i - k]
		res = max(res, curr_sum)
	return res
q = int(input())
for _ in range(q):
	(n, k) = map(int, input().split())
	s = list(input())
	arr1 = [0] * n
	arr2 = [0] * n
	arr3 = [0] * n
	flag = 0
	for i in range(n):
		if flag == 0:
			if s[i] == 'R':
				arr1[i] = 1
			else:
				arr1[i] = 0
		elif flag == 1:
			if s[i] == 'G':
				arr1[i] = 1
			else:
				arr1[i] = 0
		elif flag == 2:
			if s[i] == 'B':
				arr1[i] = 1
			else:
				arr1[i] = 0
		flag = (flag + 1) % 3
	flag = 0
	for i in range(n):
		if flag == 0:
			if s[i] == 'G':
				arr2[i] = 1
			else:
				arr2[i] = 0
		elif flag == 1:
			if s[i] == 'B':
				arr2[i] = 1
			else:
				arr2[i] = 0
		elif flag == 2:
			if s[i] == 'R':
				arr2[i] = 1
			else:
				arr2[i] = 0
		flag = (flag + 1) % 3
	flag = 0
	for i in range(n):
		if flag == 0:
			if s[i] == 'B':
				arr3[i] = 1
			else:
				arr3[i] = 0
		elif flag == 1:
			if s[i] == 'R':
				arr3[i] = 1
			else:
				arr3[i] = 0
		elif flag == 2:
			if s[i] == 'G':
				arr3[i] = 1
			else:
				arr3[i] = 0
		flag = (flag + 1) % 3
	a = maxSum(arr1, n, k)
	b = maxSum(arr2, n, k)
	c = maxSum(arr3, n, k)
	ans = max(max(a, b), c)
	print(k - ans)


rgb = 'RGB' * 667
gbr = 'GBR' * 667
brg = 'BRG' * 667
q = int(input())
for i in range(0, q):
	exp = list(map(int, input().split(' ')))
	n = exp[0]
	k = exp[1]
	line = input()
	min_ = int(22000)
	for pos in range(0, n - k + 1):
		temp = line[pos:pos + k]
		diff_rgb = 0
		diff_gbr = 0
		diff_brg = 0
		for s in range(0, k):
			if temp[s] != rgb[s]:
				diff_rgb += 1
			if temp[s] != gbr[s]:
				diff_gbr += 1
			if temp[s] != brg[s]:
				diff_brg += 1
		temp_min = min(diff_rgb, diff_gbr, diff_brg)
		if temp_min < min_:
			min_ = temp_min
	print(min_)


def ceil(a, b):
	return (a + b - 1) // b

def changes(s, want, k):
	first = want[:k]
	second = want[1:k + 1]
	third = want[2:k + 2]
	moves = float('inf')
	temp = 0
	for i in range(k):
		if s[i] != first[i]:
			temp += 1
	moves = min(temp, moves)
	temp = 0
	for i in range(k):
		if s[i] != second[i]:
			temp += 1
	moves = min(temp, moves)
	temp = 0
	for i in range(k):
		if s[i] != third[i]:
			temp += 1
	moves = min(temp, moves)
	return moves
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	want = 'RGB' * (ceil(k, 3) + 3)
	ans = float('inf')
	for i in range(n - k + 1):
		ans = min(ans, changes(s[i:i + k], want, k))
	print(ans)


from sys import stdin, stdout
import math, queue, heapq
fastinput = stdin.readline
fastout = stdout.write
t = int(fastinput())
while t:
	t -= 1
	(n, m) = map(int, fastinput().split())
	x = 'RGB'
	i = 0
	dp = [[0, 0, 0] for _ in range(n + 1)]
	j = 1
	s = input()
	for k in s:
		if k == x[(i + 0) % 3]:
			dp[j][0] = 1
		elif k == x[(i + 1) % 3]:
			dp[j][1] = 1
		else:
			dp[j][2] = 1
		i = (i + 1) % 3
		dp[j][0] += dp[j - 1][0]
		dp[j][1] += dp[j - 1][1]
		dp[j][2] += dp[j - 1][2]
		j += 1
	ans = 0
	for i in range(m, n + 1):
		ans = max(ans, dp[i][0] - dp[i - m][0], dp[i][1] - dp[i - m][1], dp[i][2] - dp[i - m][2])
	print(m - ans)


def int_list():
	return [int(c) for c in input().split()]

def int1():
	return int(input())

def str_list():
	return [c for c in input().split()]

def str1():
	return input()
s1 = ('RGB' * 66667)[:-1]
s2 = ('GBR' * 66667)[:-1]
s3 = ('BRG' * 66667)[:-1]
q = int1()
res = []
for j in range(q):
	(n, k) = int_list()
	s = str1()
	c1 = []
	c2 = []
	c3 = []
	for i in range(n):
		if s[i] != s1[i]:
			c1.append(1)
		else:
			c1.append(0)
		if s[i] != s2[i]:
			c2.append(1)
		else:
			c2.append(0)
		if s[i] != s3[i]:
			c3.append(1)
		else:
			c3.append(0)
	min1 = sum(c1[:k])
	min2 = sum(c2[:k])
	min3 = sum(c3[:k])
	cost1 = min1
	cost2 = min2
	cost3 = min3
	for i in range(k, n):
		cost1 += c1[i] - c1[i - k]
		cost2 += c2[i] - c2[i - k]
		cost3 += c3[i] - c3[i - k]
		min1 = cost1 if cost1 < min1 else min1
		min2 = cost2 if cost2 < min2 else min2
		min3 = cost3 if cost3 < min3 else min3
	res.append(min(min1, min2, min3))
for result in res:
	print(result)


import copy
import sys
from collections import defaultdict, Counter

def input():
	return sys.stdin.readline()

def mapi():
	return map(int, input().split())

def maps():
	return map(str, input().split())

def print(arg, *argv, end=None):
	sys.stdout.write(str(arg))
	for i in argv:
		sys.stdout.write(' ' + str(i))
	sys.stdout.write(end) if end else sys.stdout.write('\n')

def GCD(x, y):
	return GCD(y, x % y) if y else x

def modPow(x, y, p):
	(res, x) = (1, x % p)
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		(y, x) = (y >> 1, x * x % p)
	return res

def modInv(s, mod):
	return modPow(s, mod - 2, mod)

def solveVer1():
	t = 1
	t = int(input())
	while t:
		t -= 1
		(n, k) = mapi()
		s = input()
		st = 'RGB'
		res = float('inf')
		for i in range(n - k + 1):
			for offset in range(3):
				curr = 0
				for l in range(k):
					if s[i + l] != st[(offset + l) % 3]:
						curr += 1
				res = min(res, curr)
		print(res)

def solveVer2():
	t = 1
	t = int(input())
	while t:
		t -= 1
		(n, k) = mapi()
		s = input()
		st = 'RGB'
		res = float('inf')
		dp = [[0] * 3 for i in range(n + 1)]
		for i in range(n):
			for offset in range(3):
				if s[i] != st[(offset + i) % 3]:
					dp[i + 1][offset] += 1
				dp[i + 1][offset] += dp[i][offset]
		for i in range(k, n + 1):
			for offset in range(3):
				res = min(res, dp[i][offset] - dp[i - k][offset])
		print(res)
solveVer2()


q = int(input())
for t in range(q):
	(n, k) = list(map(int, input().split()))
	rgb = input()
	dp = [0] * 3
	dp[0] = [0] * (n + 1)
	dp[1] = [0] * (n + 1)
	dp[2] = [0] * (n + 1)
	for i in range(0, n):
		if rgb[i] == 'R':
			dp[0][i + 1] = dp[2][i]
			dp[1][i + 1] = dp[0][i] + 1
			dp[2][i + 1] = dp[1][i] + 1
		if rgb[i] == 'G':
			dp[0][i + 1] = dp[2][i] + 1
			dp[1][i + 1] = dp[0][i]
			dp[2][i + 1] = dp[1][i] + 1
		if rgb[i] == 'B':
			dp[0][i + 1] = dp[2][i] + 1
			dp[1][i + 1] = dp[0][i] + 1
			dp[2][i + 1] = dp[1][i]
	repl = k
	dif = k % 3
	for j in range(3):
		for i in range(1, n - k + 2):
			repl = min(repl, -dp[j][i - 1] + dp[(j + dif) % 3][i + k - 1])
	print(repl)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	temp = list(str(input())[:-1])
	d = {'R': 0, 'G': 1, 'B': 2}
	arr = [d[temp[i]] for i in range(n)]
	res = float('inf')
	for i in range(n - k + 1):
		a = arr[i:i + k]
		for j in range(3):
			temp = [0 if a[l] == (l + j) % 3 else 1 for l in range(k)]
			res = min(sum(temp), res)
	print(res)


def give(val):
	strg = ''
	for i in range(val):
		strg += 'RGB'
	a = []
	for i in range(3):
		a.append(strg[i:val + i])
	return a

def diff(s1, s2):
	count = 0
	for i in range(len(s1)):
		if s1[i] != s2[i]:
			count += 1
	return count
t = int(input())
for i in range(t):
	(n, k) = input().split(' ')
	s = input()
	res = give(int(k))
	ans = 1000000
	for j in res:
		for i in range(len(s) - len(j) + 1):
			m = diff(j, s[i:len(j) + i])
			if m < ans:
				ans = m
	print(ans)


for i in range(int(input())):
	(n, k) = map(int, input().split(' '))
	x = input()
	y = []
	for j in range(n - k + 1):
		a = x[j:j + k]
		count1 = 0
		count2 = 0
		count3 = 0
		r = 'RGB' * (k // 3) + 'RGB'[:k % 3]
		g = 'GBR' * (k // 3) + 'GBR'[:k % 3]
		b = 'BRG' * (k // 3) + 'BRG'[:k % 3]
		for m in range(k):
			if a[m] != r[m]:
				count1 += 1
			if a[m] != g[m]:
				count2 += 1
			if a[m] != b[m]:
				count3 += 1
		y.append(min(count1, count2, count3))
	print(min(y))


from sys import stdin
input = stdin.readline
test = 'RGB'
for i in range(int(input())):
	(a, m) = map(int, input().split())
	s = input()
	f = 100000000
	ans = [[0] * a for i in range(3)]
	for i in range(3):
		for j in range(a):
			ans[i][j] += s[j] != test[(i + j) % 3]
	for i in range(3):
		mm = sum(ans[i][:m])
		f = min(f, mm)
		for j in range(m, a):
			mm -= ans[i][j - m]
			mm += ans[i][j]
			f = min(f, mm)
	print(f)


from sys import stdin
input = stdin.readline
from sys import stdin
input = stdin.readline
t = int(input())
for i in range(t):
	(n, k) = map(int, input().split(' '))
	str_1 = input()
	str_2 = ['GBR', 'BRG', 'RGB']
	do = []
	for h in str_2:
		count = 0
		for j in range(k):
			if str_1[j] != h[j % 3]:
				count += 1
		do.append(count)
		for j in range(k, n):
			if str_1[j - k] != h[(j - k) % 3]:
				count -= 1
			if str_1[j] != h[j % 3]:
				count += 1
			do.append(count)
	print(min(do))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		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')
t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	s = input()
	s1 = 'RGB' * (n // 3 + 2)
	l1 = []
	l2 = []
	l3 = []
	(c1, c2, c3) = (0, 0, 0)
	for j in range(n):
		if s[j] == s1[j]:
			c1 += 1
		elif s[j] == s1[j + 1]:
			c2 += 1
		else:
			c3 += 1
		l1.append(c1)
		l2.append(c2)
		l3.append(c3)
	(a, b, c) = (l1[k - 1], l2[k - 1], l3[k - 1])
	ans = max(a, b, c)
	for j in range(k, n):
		a = max(a, l1[j] - l1[j - k])
		b = max(b, l2[j] - l2[j - k])
		c = max(c, l3[j] - l3[j - k])
		ans = max(ans, a, b, c)
	print(k - ans)


from sys import stdin, stdout
input = stdin.readline
d = ['R', 'G', 'B']
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input().strip()
	ans = float('inf')
	for i in range(0, n - k + 1):
		temp = s[i:i + k]
		a = b = c = 0
		p = 0
		q = 1
		r = 2
		for j in temp:
			if j != d[p]:
				a += 1
			if j != d[q]:
				b += 1
			if j != d[r]:
				c += 1
			p += 1
			p %= 3
			q += 1
			q %= 3
			r += 1
			r %= 3
		ans = min(ans, a, b, c)
	print(ans)


from sys import stdin, stdout

def check_diff(s, rgb):
	n = len(s)
	arr = []
	for i in range(n):
		if s[i] == rgb[i]:
			arr.append(0)
		else:
			arr.append(1)
	return arr
q = int(stdin.readline().rstrip())
for _ in range(q):
	(n, k) = map(int, stdin.readline().rstrip().split())
	s = stdin.readline().rstrip()
	rgb = ('RGB' * n)[:n]
	gbr = ('GBR' * n)[:n]
	brg = ('BRG' * n)[:n]
	diff1 = check_diff(s, rgb)
	diff2 = check_diff(s, gbr)
	diff3 = check_diff(s, brg)
	cum1 = [0] * (n + 1)
	cum2 = [0] * (n + 1)
	cum3 = [0] * (n + 1)
	for i in range(1, n + 1):
		cum1[i] = cum1[i - 1] + diff1[i - 1]
		cum2[i] = cum2[i - 1] + diff2[i - 1]
		cum3[i] = cum3[i - 1] + diff3[i - 1]
	sol = cum1[k] - cum1[0]
	for i in range(1, n - k + 2):
		sol = min(sol, cum1[i + k - 1] - cum1[i - 1])
		sol = min(sol, cum2[i + k - 1] - cum2[i - 1])
		sol = min(sol, cum3[i + k - 1] - cum3[i - 1])
	print(sol)


R = 'RGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGBRGB'

def dif(s):
	k = [0, 0, 0]
	for (n, e) in enumerate(trip):
		for (i, sign) in enumerate(e):
			if sign != s[i]:
				k[n] += 1
	return min(k)

def for_vr(n, k, s):
	var = []
	for i in range(n - k + 1):
		var.append(s[i:k + i])
	mb = []
	for v in var:
		mb.append(dif(v))
	return min(mb)
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	trip = [R[:k], R[1:k + 1], R[2:k + 2]]
	s = input()
	print(for_vr(n, k, s))


rgb_a = 'RGB' * 1000
q = int(input())
for i in range(q):
	(n, k) = [int(w) for w in input().split()]
	s = input()
	ans = 300000
	for w in range(3):
		for i in range(n - k + 1):
			temp = 0
			for j in range(k):
				if s[i + j] != rgb_a[w + j]:
					temp = temp + 1
			ans = min(ans, temp)
	print(ans)


from sys import stdin
input = stdin.readline
q = int(input())
for ppppp in range(q):
	[n, k] = [int(item) for item in input().split(' ')]
	x = input()
	inf_seq = 'RGB'
	ptr = 0
	min_changes = n + 1
	for i in range(n - k + 1):
		changes = [0] * 3
		for d in range(3):
			ind = 0
			for item in x[i:i + k]:
				if item != inf_seq[(d + ind) % 3]:
					changes[d] += 1
				ind += 1
		min_changes = min(min_changes, min(changes))
	print(min_changes)


import sys
t = int(input())
for _ in range(t):
	(n, k) = map(int, sys.stdin.readline().split())
	s = input()
	(r, g, b) = ([0, 0, 0], [0, 0, 0], [0, 0, 0])
	res = 0
	for i in range(n):
		if s[i] == 'R':
			r[i % 3] += 1
		if s[i] == 'G':
			g[i % 3] += 1
		if s[i] == 'B':
			b[i % 3] += 1
		if i >= k:
			if s[i - k] == 'R':
				r[(i - k) % 3] -= 1
			if s[i - k] == 'G':
				g[(i - k) % 3] -= 1
			if s[i - k] == 'B':
				b[(i - k) % 3] -= 1
		res = max(r[0] + g[1] + b[2], r[1] + g[2] + b[0], r[2] + g[0] + b[1], res)
	print(k - res)


import sys
q = int(sys.stdin.readline())
for Q in range(q):
	(n, k) = map(int, sys.stdin.readline().split())
	s = sys.stdin.readline()
	dp = [0] * 3
	if n == 1 or k == 1:
		print(0)
	else:
		s1 = 0
		for i in range(k):
			if i % 3 == 0 and s[i] != 'R':
				s1 += 1
			elif i % 3 == 1 and s[i] != 'G':
				s1 += 1
			elif i % 3 == 2 and s[i] != 'B':
				s1 += 1
		dp[0] = s1
		s2 = 0
		for i in range(k):
			if i % 3 == 0 and s[i] != 'B':
				s2 += 1
			elif i % 3 == 1 and s[i] != 'R':
				s2 += 1
			elif i % 3 == 2 and s[i] != 'G':
				s2 += 1
		dp[1] = s2
		s3 = 0
		for i in range(k):
			if i % 3 == 0 and s[i] != 'G':
				s3 += 1
			elif i % 3 == 1 and s[i] != 'B':
				s3 += 1
			elif i % 3 == 2 and s[i] != 'R':
				s3 += 1
		dp[2] = s3
		for i in range(k, n):
			if i % 3 == 0 and s[i] != 'R':
				s1 += 1
			elif i % 3 == 1 and s[i] != 'G':
				s1 += 1
			elif i % 3 == 2 and s[i] != 'B':
				s1 += 1
			j = i - k
			if j % 3 == 0 and s[j] != 'R':
				s1 -= 1
			elif j % 3 == 1 and s[j] != 'G':
				s1 -= 1
			elif j % 3 == 2 and s[j] != 'B':
				s1 -= 1
			dp[0] = min(s1, dp[0])
			if i % 3 == 0 and s[i] != 'B':
				s2 += 1
			elif i % 3 == 1 and s[i] != 'R':
				s2 += 1
			elif i % 3 == 2 and s[i] != 'G':
				s2 += 1
			j = i - k
			if j % 3 == 0 and s[j] != 'B':
				s2 -= 1
			elif j % 3 == 1 and s[j] != 'R':
				s2 -= 1
			elif j % 3 == 2 and s[j] != 'G':
				s2 -= 1
			dp[1] = min(s2, dp[1])
			if i % 3 == 0 and s[i] != 'G':
				s3 += 1
			elif i % 3 == 1 and s[i] != 'B':
				s3 += 1
			elif i % 3 == 2 and s[i] != 'R':
				s3 += 1
			j = i - k
			if j % 3 == 0 and s[j] != 'G':
				s3 -= 1
			elif j % 3 == 1 and s[j] != 'B':
				s3 -= 1
			elif j % 3 == 2 and s[j] != 'R':
				s3 -= 1
			dp[2] = min(s3, dp[2])
		print(min(dp))


import sys

def change_seq(S, start):
	R = []
	for s in S:
		if s != start:
			R.append(1)
		else:
			R.append(0)
		start = (start + 1) % 3
	return R

def minimum(C, k):
	r = sum(C[:k])
	t = r
	for i in range(k, len(C)):
		t = t - C[i - k] + C[i]
		if t < r:
			r = t
	return r

def solve(S, k):
	m = len(S)
	for i in range(3):
		m = min(m, minimum(change_seq(S, i), k))
	return m
tr = {'R': 0, 'G': 1, 'B': 2}
q = int(sys.stdin.readline())
R = []
for _ in range(q):
	(n, k) = map(int, sys.stdin.readline().split())
	S = list(map(tr.get, sys.stdin.readline()))
	sys.stdout.write(str(solve(S, k)) + '\n')
sys.stdout.flush()


import sys
for _ in range(int(input())):
	(n, k) = map(int, sys.stdin.readline().split())
	st = sys.stdin.readline()
	dp = [[1, 1, 1] for i in range(n + 1)]
	dp[0][0] = dp[0][1] = dp[0][2] = 0
	for i in range(n):
		if st[i] == 'R':
			dp[i + 1][0] = 0
		elif st[i] == 'G':
			dp[i + 1][1] = 0
		else:
			dp[i + 1][2] = 0
	for i in range(1, n):
		for j in range(3):
			if dp[i + 1][(j + 1) % 3] == 1:
				dp[i + 1][(j + 1) % 3] = dp[i][j] + 1
			else:
				dp[i + 1][(j + 1) % 3] = dp[i][j]
	mini = 99999999999
	j = k
	i = 0
	store = 0
	stores = 1
	while j <= n:
		for l in range(3):
			if dp[j][(l + k) % 3] - dp[i][l] < mini:
				mini = dp[j][(l + k) % 3] - dp[i][l]
				store = j
				stores = i
		j += 1
		i += 1
	print(mini)


from sys import stdin
input = stdin.readline

def get_carr(s, c):
	carr = []
	cs = 0
	for i in range(n):
		if s[i] != c:
			cs += 1
		c = (c + 1) % 3
		carr.append(cs)
	return carr
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(map(lambda o: 0 if o == 'R' else 1 if o == 'G' else 2, input().strip()))
	cs = (get_carr(s, 0), get_carr(s, 1), get_carr(s, 2))
	mc = n
	for c in cs:
		for i in range(k - 1, n):
			mc = min(mc, c[i] - (c[i - k] if i - k >= 0 else 0))
	print(mc)


nb_Test = int(input())
T = 'RGB' * (2000 // 3 + 1)
for _ in range(nb_Test):
	(lens, limit) = [int(x) for x in input().split()]
	given = input()
	needs = limit
	for i in range(lens - limit + 1):
		currs = given[i:i + limit]
		for j in range(3):
			maybe = T[j:limit + j]
			diff = sum([1 for (x, y) in zip(currs, maybe) if x != y])
			needs = min(needs, diff)
	print(needs)


def main():
	q = int(input())
	ans = []
	for i in range(q):
		(n, k) = map(int, input().split())
		s = input()
		min_ans = 10 ** 9
		pr1 = [0]
		pr2 = [0]
		pr3 = [0]
		for i in range(n):
			count1 = 0
			count2 = 0
			count3 = 0
			if i % 3 == 0:
				if s[i] != 'R':
					count1 += 1
				if s[i] != 'G':
					count2 += 1
				if s[i] != 'B':
					count3 += 1
			if i % 3 == 1:
				if s[i] != 'G':
					count1 += 1
				if s[i] != 'B':
					count2 += 1
				if s[i] != 'R':
					count3 += 1
			if i % 3 == 2:
				if s[i] != 'B':
					count1 += 1
				if s[i] != 'R':
					count2 += 1
				if s[i] != 'G':
					count3 += 1
			pr1.append(pr1[-1] + count1)
			pr2.append(pr2[-1] + count2)
			pr3.append(pr3[-1] + count3)
			j = i + 1
			if j >= k:
				count1 = pr1[j] - pr1[j - k]
				count2 = pr2[j] - pr2[j - k]
				count3 = pr3[j] - pr3[j - k]
				min_ans = min(min_ans, count1, count2, count3)
		ans.append(min_ans)
	print(*ans, sep='\n')
main()


from collections import deque, defaultdict
from math import sqrt, ceil, factorial, floor, inf, log2, sqrt, gcd
import bisect
import sys
import copy

def get_array():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def input():
	return sys.stdin.readline().strip()
for _ in range(int(input())):
	(n, k) = get_ints()
	s = input()
	R = [0] * n
	G = [0] * n
	B = [0] * n
	st = 'RGBRGB'
	d = {'R': 3, 'G': 4, 'B': 5}
	if s[0] == 'R':
		R[0] = 0
		G[0] = 1
		B[0] = 1
	if s[0] == 'B':
		B[0] = 0
		G[0] = 1
		R[0] = 1
	if s[0] == 'G':
		G[0] = 0
		R[0] = 1
		B[0] = 1
	for i in range(1, n):
		if s[i] == 'R':
			R[i] = B[i - 1]
			G[i] = 1 + R[i - 1]
			B[i] = 1 + G[i - 1]
		if s[i] == 'B':
			B[i] = G[i - 1]
			G[i] = 1 + R[i - 1]
			R[i] = 1 + B[i - 1]
		if s[i] == 'G':
			G[i] = R[i - 1]
			B[i] = 1 + G[i - 1]
			R[i] = 1 + B[i - 1]
	mini = inf
	for i in range(len(s)):
		if i >= k - 1:
			if i == k - 1:
				mini = min(mini, G[i])
				mini = min(mini, R[i])
				mini = min(mini, B[i])
			else:
				ans = k % 3
				ele_G = st[d['G'] - ans]
				ele_R = st[d['R'] - ans]
				ele_B = st[d['B'] - ans]
				if ele_G == 'G':
					mini = min(mini, G[i] - G[i - k])
				if ele_G == 'R':
					mini = min(mini, G[i] - R[i - k])
				if ele_G == 'B':
					mini = min(mini, G[i] - B[i - k])
				if ele_R == 'G':
					mini = min(mini, R[i] - G[i - k])
				if ele_R == 'R':
					mini = min(mini, R[i] - R[i - k])
				if ele_R == 'B':
					mini = min(mini, R[i] - B[i - k])
				if ele_B == 'G':
					mini = min(mini, B[i] - G[i - k])
				if ele_B == 'R':
					mini = min(mini, B[i] - R[i - k])
				if ele_B == 'B':
					mini = min(mini, B[i] - B[i - k])
	print(mini)


t = int(input().strip())
for i in range(t):
	(n, k) = map(int, input().strip().split())
	string = input().strip()
	sample = ['RGB' * (n // 3) + 'RGB'[:n % 3], 'GBR' * (n // 3) + 'GBR'[:n % 3], 'BRG' * (n // 3) + 'BRG'[:n % 3]]
	ans = 10000000
	for each_sample in sample:
		cost = [0 for i in range(n)]
		total_cost = [0 for i in range(n + 1 - k)]
		for i in range(n):
			if string[i] == each_sample[i]:
				cost[i] = 0
			else:
				cost[i] = 1
		cur_cost = 0
		for i in range(k):
			cur_cost += cost[i]
		total_cost[0] = cur_cost
		for i in range(k, n):
			total_cost[i - k + 1] = total_cost[i - k] - cost[i - k] + cost[i]
			cur_cost = min(cur_cost, total_cost[i - k + 1])
		if cur_cost < ans:
			ans = cur_cost
	print(ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	r = [0] * (n + 2)
	g = [0] * (n + 2)
	b = [0] * (n + 2)
	rr = 'RGB'
	bb = 'BRG'
	gg = 'GBR'
	for i in range(0, n):
		if s[i] != rr[i % 3]:
			r[i + 1] += 1
		if s[i] != gg[i % 3]:
			g[i + 1] += 1
		if s[i] != bb[i % 3]:
			b[i + 1] += 1
		b[i + 2] = b[i + 1]
		g[i + 2] = g[i + 1]
		r[i + 2] = r[i + 1]
	mn = []
	for i in range(k, n + 1):
		mn.append(g[i] - g[i - k])
		mn.append(b[i] - b[i - k])
		mn.append(r[i] - r[i - k])
	print(min(mn))


from fractions import gcd
from itertools import combinations, permutations, accumulate, product
from collections import deque, defaultdict, Counter
import decimal
import re
import math
import bisect
import sys
sys.setrecursionlimit(10000000)
mod = 10 ** 9 + 7
from sys import stdin
readline = stdin.readline

def readInts():
	return list(map(int, readline().split()))

def readTuples():
	return tuple(map(int, readline().split()))

def I():
	return int(readline())
base1 = 'RGB'
base2 = 'GBR'
base3 = 'BRG'
for _ in range(I()):
	(n, k) = readInts()
	s = input()
	div = k // 3
	rem = k % 3
	p1 = base1 * div + base1[:rem]
	p2 = base2 * div + base2[:rem]
	p3 = base3 * div + base3[:rem]
	nya = float('inf')
	for i in range(n - k + 1):
		cnt = 0
		for j in range(i, i + k):
			if p1[j - i] == s[j]:
				pass
			else:
				cnt += 1
		nya = min(nya, cnt)
		cnt = 0
		for j in range(i, i + k):
			if p2[j - i] == s[j]:
				pass
			else:
				cnt += 1
		nya = min(nya, cnt)
		cnt = 0
		for j in range(i, i + k):
			if p3[j - i] == s[j]:
				pass
			else:
				cnt += 1
		nya = min(nya, cnt)
	print(nya)


from sys import stdin
input = stdin.readline
t = int(input())

def foo(seq, index):
	if index == None:
		return seq[0]
	index = index % 3
	return seq[index]
for j in range(t):
	(n, k) = list(map(int, input().split(' ')))
	chars = list(input())
	chars.insert(0, None)
	diff = [[] for i in range(n + 1)]
	diff[0] = [0, 0, 0]
	for i in range(1, n + 1):
		diff[i].append(foo('RGB', i - 1) != chars[i])
		diff[i].append(foo('GBR', i - 1) != chars[i])
		diff[i].append(foo('BRG', i - 1) != chars[i])
	for i in range(2, n + 1):
		diff[i][0] += diff[i - 1][0]
		diff[i][1] += diff[i - 1][1]
		diff[i][2] += diff[i - 1][2]
	res = float('inf')
	for i in range(k, n + 1):
		res = min(res, diff[i][0] - diff[i - k][0])
		res = min(res, diff[i][1] - diff[i - k][1])
		res = min(res, diff[i][2] - diff[i - k][2])
	print(res)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	r1 = []
	e1 = 'R'
	e2 = 'G'
	e3 = 'B'
	i = 1
	while i <= k:
		if i % 3 == 1:
			r1.append(e1)
		if i % 3 == 2:
			r1.append(e2)
		if i % 3 == 0:
			r1.append(e3)
		i = i + 1
	r2 = r1.copy()
	r2.pop(0)
	if i % 3 == 1:
		r2.append(e1)
	if i % 3 == 2:
		r2.append(e2)
	if i % 3 == 0:
		r2.append(e3)
	i = i + 1
	r3 = r2.copy()
	r3.pop(0)
	if i % 3 == 1:
		r3.append(e1)
	if i % 3 == 2:
		r3.append(e2)
	if i % 3 == 0:
		r3.append(e3)
	m = k
	l = []
	i = 0
	while i < k:
		l.append(s[i])
		i = i + 1
	m1 = k
	m2 = k
	m3 = k
	for j in range(k):
		if l[j] == r1[j]:
			m1 = m1 - 1
		if l[j] == r2[j]:
			m2 = m2 - 1
		if l[j] == r3[j]:
			m3 = m3 - 1
	if min(m1, m2, m3) < m:
		m = min(m1, m2, m3)
	while i < len(s):
		l.pop(0)
		l.append(s[i])
		m1 = k
		m2 = k
		m3 = k
		for j in range(k):
			if l[j] == r1[j]:
				m1 = m1 - 1
			if l[j] == r2[j]:
				m2 = m2 - 1
			if l[j] == r3[j]:
				m3 = m3 - 1
		if min(m1, m2, m3) < m:
			m = min(m1, m2, m3)
		i = i + 1
	print(m)


import sys
input = sys.stdin.readline
q = int(input())
for testcases in range(q):
	(n, k) = map(int, input().split())
	S = list(input().strip())
	for i in range(n):
		if S[i] == 'R':
			S[i] = 0
		elif S[i] == 'G':
			S[i] = 1
		else:
			S[i] = 2
	ANS = 1 << 50
	for mod in range(3):
		SUM = 0
		for i in range(k):
			if S[i] % 3 != (mod + i) % 3:
				SUM += 1
		ANS = min(ANS, SUM)
		for i in range(k, n):
			if S[i - k] != (mod + (i - k)) % 3:
				SUM -= 1
			if S[i] != (mod + i) % 3:
				SUM += 1
			ANS = min(ANS, SUM)
	print(ANS)


for _ in range(int(input())):
	(n, k) = map(int, input().split(' '))
	s = input()
	t = 'RGB'
	result = 99999999
	for i in range(n - k + 1):
		for j in range(3):
			c = 0
			for q in range(k):
				if s[i + q] != t[(j + q) % 3]:
					c += 1
			result = min(result, c)
	print(result)


t = int(input())
INF = 2 * 10 ** 4
A = ['R', 'G', 'B'] * INF
for _ in range(t):
	(n, k) = map(int, input().split())
	u = list(input())
	d = [INF] * (n - k + 1)
	a1 = A[:k]
	a2 = A[1:k + 1]
	a3 = A[2:k + 2]
	gl_ans = INF
	for i in range(n - k + 1):
		ans1 = 0
		ans2 = 0
		ans3 = 0
		for j in range(k):
			if u[i + j] != a1[j]:
				ans1 += 1
			if u[i + j] != a2[j]:
				ans2 += 1
			if u[i + j] != a3[j]:
				ans3 += 1
		gl_ans = min(gl_ans, ans1, ans2, ans3)
	print(gl_ans)


def two(x):
	for i in range(len(x) - 1):
		if x[i] == 'R' and x[i + 1] == 'G':
			return 0
		elif x[i] == 'G' and x[i + 1] == 'B':
			return 0
		elif x[i] == 'B' and x[i + 1] == 'R':
			return 0
	return 1
t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	x = input()
	if k == 1:
		print(0)
		continue
	elif k == 2:
		print(two(x))
		continue
	d = [['R', 'G', 'B'], ['G', 'B', 'R'], ['B', 'R', 'G']]
	ans = 10 ** 10
	for j in range(3):
		count = 0
		for i in range(k):
			if x[i] != d[j][i % 3]:
				count += 1
		ans = min(ans, count)
		for i in range(k, n):
			if x[i] != d[j][i % 3]:
				count += 1
			if x[i - k] != d[j][(i - k) % 3]:
				count -= 1
			ans = min(ans, count)
	print(ans)


from sys import setcheckinterval, stdin
setcheckinterval(1000)
iin = lambda : int(stdin.readline())
lin = lambda : list(map(int, stdin.readline().split()))

def check2(a, b):
	return 0 if a == b else 1

def check1(a, b, c):
	ans = 3
	for i in ['RGB', 'GBR', 'BRG']:
		ans = min(ans, check2(a, i[0]) + check2(b, i[1]) + check2(c, i[2]))
	return ans
for _ in range(iin()):
	(n, k) = lin()
	a = input()
	a1 = [[0], [0], [0]]
	ch = ['RGB', 'GBR', 'BRG']
	for i in range(3):
		for j in range(n):
			a1[i].append(check2(ch[i][j % 3], a[j]))
	for i in range(3):
		for j in range(1, n + 1):
			a1[i][j] += a1[i][j - 1]
	sol = 9999
	for i in range(3):
		ch = 0
		for j in range(k, n + 1):
			sol = min(sol, a1[i][j] - a1[i][ch])
			ch += 1
	print(sol)


def diff(a, b):
	c = 10000000
	for i in range(len(b) - len(a) + 1):
		d = 0
		for j in range(len(a)):
			if a[j] != b[i + j]:
				d += 1
		c = min(c, d)
	return c
for ii in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	a = 'RGB' * (k // 3)
	b = 'BRG' * (k // 3)
	c = 'GBR' * (k // 3)
	if k % 3 == 2:
		a = a + 'RG'
		b = b + 'BR'
		c = c + 'GB'
	elif k % 3 == 1:
		a = a + 'R'
		b = b + 'B'
		c = c + 'G'
	cx = 10000000000
	for i in [a, b, c]:
		cx = min(cx, diff(i, s))
	print(cx)


from sys import stdin
q = int(stdin.readline())
for i in range(q):
	(n, k) = map(int, stdin.readline().split())
	s = stdin.readline().strip()
	ans = []
	for i in range(n - k + 1):
		ans.append(s[i:i + k])
	ans = list(set(ans))
	changes = []
	for ele in ans:
		(r, g, b) = ([], [], [])
		for i in range(k):
			if ele[i] == 'B':
				b.append(i)
			elif ele[i] == 'G':
				g.append(i)
			else:
				r.append(i)
		a = len(list(filter(lambda x: x % 3 == 0, b))) + len(list(filter(lambda x: x % 3 == 1, r))) + len(list(filter(lambda x: x % 3 == 2, g)))
		x = len(list(filter(lambda x: x % 3 == 0, r))) + len(list(filter(lambda x: x % 3 == 1, g))) + len(list(filter(lambda x: x % 3 == 2, b)))
		y = len(list(filter(lambda x: x % 3 == 0, g))) + len(list(filter(lambda x: x % 3 == 1, b))) + len(list(filter(lambda x: x % 3 == 2, r)))
		changes.append(min([k - a, k - x, k - y]))
	print(min(changes))


from sys import stdin, stdout
Pi = lambda x: stdout.write(str(x) + '\n')
Ps = lambda x: stdout.write(str(x))
S = lambda x: x * (x + 1) // 2
I = lambda x: 1 + 2 * x
R = lambda : stdin.readline()
Ri = lambda x: map(int, x.split())
Rs = lambda x: map(str, x.split())
Rf = lambda x: map(float, x.split())
MaxN = int(100000.0) + 5

def f(s, x, k):
	dp = []
	if s[0] != x[0]:
		dp.append(1)
	else:
		dp.append(0)
	for i in range(1, len(s)):
		if s[i] != x[i % 3]:
			dp.append(dp[-1] + 1)
		else:
			dp.append(dp[-1])
	ans = dp[k - 1]
	for i in range(k, len(s)):
		ans = min(ans, dp[i] - dp[i - k])
	return ans

def main():
	test = int(R())
	for i in range(test):
		(n, k) = Ri(R())
		s = R().strip('\n')
		ans = f(s, 'RGB', k)
		ans = min(ans, f(s, 'GBR', k))
		ans = min(ans, f(s, 'BRG', k))
		print(ans)
main()


from sys import stdin
input = lambda : stdin.readline().strip()
from math import ceil, sqrt, factorial, gcd
from collections import deque
from bisect import bisect_left, bisect_right
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	mi = k
	s = input()
	l = deque()
	d = {'R': 0, 'G': 1, 'B': 2}
	for i in range(3):
		l.append([0, 0, 0])
	for i in range(k - 1):
		l[i % 3][d[s[i]]] += 1
	a = 0
	for i in range(k - 1, n):
		l[i % 3][d[s[i]]] += 1
		t = l[0][1] + l[0][2] + l[1][0] + l[1][2] + l[2][0] + l[2][1]
		t1 = l[0][1] + l[0][0] + l[1][1] + l[1][2] + l[2][0] + l[2][2]
		t2 = l[0][0] + l[0][2] + l[1][0] + l[1][1] + l[2][2] + l[2][1]
		mi = min(mi, t, t1, t2)
		l[a % 3][d[s[a]]] -= 1
		a += 1
	print(mi)


def check(s, k, n):
	t = 'RGB'
	ans = int(1000000000.0)
	for i in range(n - k + 1):
		for off in range(0, 3):
			cur = 0
			for pos in range(k):
				if s[i + pos] != t[(pos + off) % 3]:
					cur += 1
			ans = min(ans, cur)
	return ans
t = int(input())
while t:
	nk = list(map(int, input().rstrip().split()))
	n = nk[0]
	k = nk[1]
	s = input().rstrip().lstrip()
	print(check(s, k, n))
	t = t - 1


q = int(input())
ans = []
rgb_row = 'RGB' * 200000
for a in range(q):
	(n, k) = map(int, input().split())
	s = str(input())
	if len(s) == 1:
		ans.append(0)
		continue
	str_1 = [0] * n
	str_2 = [0] * n
	str_3 = [0] * n
	for i in range(n):
		if s[i] == rgb_row[i]:
			str_1[i] += 1
		if s[i] == rgb_row[i + 1]:
			str_2[i] += 1
		if s[i] == rgb_row[i + 2]:
			str_3[i] += 1
	counter_1 = 0
	counter_2 = 0
	counter_3 = 0
	k1 = []
	k2 = []
	k3 = []
	min1 = n + 1
	min2 = n + 1
	min3 = n + 1
	for i in range(k):
		counter_1 += str_1[i]
	k1.append(counter_1)
	for i in range(k, n):
		counter_1 = counter_1 + str_1[i] - str_1[i - k]
		k1.append(counter_1)
	max1 = max(k1)
	for i in range(k):
		counter_2 += str_2[i]
	k2.append(counter_2)
	for i in range(k, n):
		counter_2 = counter_2 + str_2[i] - str_2[i - k]
		k2.append(counter_2)
	max2 = max(k2)
	for i in range(k):
		counter_3 += str_3[i]
	k3.append(counter_3)
	for i in range(k, n):
		counter_3 = counter_3 + str_3[i] - str_3[i - k]
		k3.append(counter_3)
	max3 = max(k3)
	final_max = max(max1, max2, max3)
	if final_max >= k:
		ans.append(0)
	else:
		ans.append(k - final_max)
for i in range(q):
	print(ans[i])


a = int(input())
b = [0] * a

def change(d):
	for i in range(0, len(d)):
		if d[i] == 'R':
			d[i] = 1
		elif d[i] == 'G':
			d[i] = 2
		else:
			d[i] = 3
	return d
for i in range(0, a):
	b[i] = list(map(int, input().split()))
	n = b[i][0]
	k = b[i][1]
	c = list(input())
	c = change(c)
	e = [0] * (3 * (n - k + 1))
	for i in range(0, n - k + 1):
		for j in range(i, i + k):
			if (c[j] - c[i]) % 3 != (j - i) % 3:
				e[3 * i] = e[3 * i] + 1
			if (c[j] - c[i]) % 3 != (j - i - 1) % 3:
				e[3 * i + 1] = e[3 * i + 1] + 1
			if (c[j] - c[i]) % 3 != (j - i - 2) % 3:
				e[3 * i + 2] = e[3 * i + 2] + 1
	e.sort()
	print(e[0])


from sys import stdin
input = stdin.readline
q = int(input())
for ppppp in range(q):
	[n, k] = [int(item) for item in input().split(' ')]
	x = input()
	inf_seq = 'RGB'
	ptr = 0
	weights = []
	cost = [0, 0, 0]
	for i in range(k):
		dd = [0, 0, 0]
		for d in range(3):
			if x[i] != inf_seq[(i + d) % 3]:
				dd[d] = 1
				cost[d] += 1
		weights.append(dd)
	min_changes = min(cost)
	for i in range(k, n):
		dd = [0, 0, 0]
		for d in range(3):
			cost[d] -= weights[i - k][d]
			if x[i] != inf_seq[(i + d) % 3]:
				dd[d] = 1
				cost[d] += 1
		weights.append(dd)
		min_changes = min(min_changes, min(cost))
	print(min_changes)


q = int(input())
answer = []
rgb_string = 'RGB' * 2000
for a in range(q):
	(n, k) = map(int, input().split())
	s = str(input())
	len_s = len(s)
	string_1 = [False] * len_s
	string_2 = [False] * len_s
	string_3 = [False] * len_s
	max_true = []
	for i in range(len_s):
		if s[i] == rgb_string[i]:
			string_1[i] = True
		if s[i] == rgb_string[i + 1]:
			string_2[i] = True
		if s[i] == rgb_string[i + 2]:
			string_3[i] = True
	if n != k:
		for i in range(0, len_s - k + 1):
			k1 = k2 = k3 = 0
			for j in range(i, i + k):
				if string_1[j]:
					k1 += 1
				if string_2[j]:
					k2 += 1
				if string_3[j]:
					k3 += 1
			max_true.append(k1)
			max_true.append(k2)
			max_true.append(k3)
	else:
		k1 = k2 = k3 = 0
		for i in range(len_s):
			if string_1[i]:
				k1 += 1
			if string_2[i]:
				k2 += 1
			if string_3[i]:
				k3 += 1
		max_true.append(k1)
		max_true.append(k2)
		max_true.append(k3)
	max_string = max(max_true)
	if max_string < k:
		answer.append(k - max_string)
	else:
		answer.append(0)
for i in range(q):
	print(answer[i])


cache = {}

def generateRecString(s, k):
	t = s
	while len(t) < k:
		t += s
	return t

def findMinDiff(s):
	start_r = generateRecString('RGB', len(s))
	start_g = generateRecString('GBR', len(s))
	start_b = generateRecString('BRG', len(s))
	diff_r = diff_g = diff_b = 0
	for i in range(len(s)):
		if s[i] != start_r[i]:
			diff_r += 1
		if s[i] != start_g[i]:
			diff_g += 1
		if s[i] != start_b[i]:
			diff_b += 1
	return min(min(diff_r, diff_g), diff_b)
T = int(input())
for t in range(T):
	(n, k) = list(map(int, input().split()))
	s = input()
	ans = float('inf')
	for i in range(n - k + 1):
		substring = s[i:i + k]
		if substring in cache:
			tmp = cache[substring]
		else:
			tmp = findMinDiff(substring)
			cache[substring] = tmp
		ans = min(ans, tmp)
	print(ans)


d = {'R': 0, 'G': 1, 'B': 2, 0: 'R', 1: 'G', 2: 'B'}

def cost(i):
	if i:
		return 0
	else:
		return 1
for _ in range(int(input())):
	(n, k) = (int(s) for s in input().split())
	s = input().strip()
	(c1, c2, c3, i1, i2, i3) = (0, 0, 0, 'R', 'G', 'B')
	for i in range(k):
		if s[i] != i1:
			c1 += 1
		if s[i] != i2:
			c2 += 1
		if s[i] != i3:
			c3 += 1
		if i != k - 1:
			i2 = d[(d[i2] + 1) % 3]
			i3 = d[(d[i3] + 1) % 3]
			i1 = d[(d[i1] + 1) % 3]
	ans = min(c1, c2, c3)
	for i in range(k, n):
		(c1, c2, c3) = (c3 - cost(s[i - k] == 'B') + cost(s[i] == i1), c1 - cost(s[i - k] == 'R') + cost(s[i] == i2), c2 - cost(s[i - k] == 'G') + cost(s[i] == i3))
		ans = min(ans, c1, c2, c3)
	print(ans)


import sys
input = lambda : sys.stdin.readline().strip('\r\n')
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	ans = 0
	for color in ['RGB', 'GBR', 'BRG']:
		cnt = 0
		for i in range(k):
			if s[i] == color[i % 3]:
				cnt += 1
		maxi = cnt
		for i in range(n - k):
			if s[i + k] == color[(i + k) % 3]:
				cnt += 1
			if s[i] == color[i % 3]:
				cnt -= 1
			if cnt > maxi:
				maxi = cnt
		ans = max(ans, maxi)
	print(k - ans)


def c(a, k):
	cnt = 0
	for j in range(1, k):
		if a[j - 1] == 'R':
			if a[j] != 'G':
				a[j] = 'G'
				cnt += 1
		elif a[j - 1] == 'G':
			if a[j] != 'B':
				a[j] = 'B'
				cnt += 1
		elif a[j - 1] == 'B':
			if a[j] != 'R':
				a[j] = 'R'
				cnt += 1
	return cnt
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	min_cnt = k
	s = input()
	for i in range(n - k + 1):
		a = list(s[i:i + k])
		min_cnt = min(1 + min(c(['R'] + a[1:], k) - int(bool(a[0] == 'R')), c(['G'] + a[1:], k) - int(bool(a[0] == 'G')), c(['B'] + a[1:], k) - int(bool(a[0] == 'B'))), min_cnt)
	print(min_cnt)


from heapq import heappush, heappop
from collections import deque, Counter, defaultdict
import itertools
import sys
sys.setrecursionlimit(10 ** 6)

def MI():
	return map(int, input().split())

def I():
	return int(input())

def LI():
	return [int(i) for i in input().split()]
YN = ['YES', 'NO']
input = sys.stdin.readline
q = I()
for _ in range(q):
	(n, k) = LI()
	ans = k
	s = list(input())
	l = 'RGB'
	c = [[0], [0], [0]]
	for i in range(n):
		for j in range(3):
			if s[i] != l[(i + j) % 3]:
				c[j].append(c[j][-1] + 1)
			else:
				c[j].append(c[j][-1])
	for i in range(n - k + 1):
		for j in range(3):
			ans = min(c[j][i + k] - c[j][i], ans)
	print(ans)


q = int(input())
rgb = 'RGB'
for _ in range(q):
	(n, k) = map(int, input().split())
	s = input()
	ans = 1000000000.0
	dp = [0 for x in range(n)]
	for offset in range(0, 3):
		cnt = 0
		for i in range(0, n):
			dp[i] = s[i] != rgb[(i + offset) % 3]
			cnt += dp[i]
			if i >= k:
				cnt -= dp[i - k]
			if i >= k - 1:
				ans = min(ans, cnt)
	print(ans)


def givestringsk(k):
	t = ['R', 'G', 'B']
	ans = []
	for i in range(3):
		temp = ''
		for j in range(i, i + k):
			temp += t[j % 3]
		ans.append(temp)
	return ans

def countdifferences(a, b):
	cnt = 0
	for i in range(len(a)):
		if a[i] != b[i]:
			cnt += 1
	return cnt
for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	s = input()
	temp = givestringsk(k)
	ans = 10000000000000
	for i in range(k, n + 1):
		for j in range(3):
			ans = min(ans, countdifferences(s[i - k:i], temp[j]))
	print(ans)


import sys
import math
from collections import defaultdict
input = sys.stdin.readline

def min_to_change(s, n, k):
	strings = ['RGB', 'GBR', 'BRG']
	to_change = [0, 0, 0]
	pos = [0, 0, 0]
	for i in range(k):
		for x in range(3):
			if s[i] != strings[x][pos[x]]:
				to_change[x] += 1
			pos[x] = (pos[x] + 1) % 3
	res = min(to_change)
	curr = 1
	while curr + k - 1 < n:
		to_change_new = [0, 0, 0]
		for x in range(3):
			similar = (x + 2) % 3
			val = to_change[similar]
			if s[curr - 1] != strings[similar][0]:
				val -= 1
			if s[curr + k - 1] != strings[x][(k - 1) % 3]:
				val += 1
			to_change_new[x] = val
		to_change = to_change_new
		res = min(res, *to_change)
		curr += 1
	return res

def main():
	q = int(input())
	for _ in range(q):
		(n, k) = map(int, input().split())
		s = input()
		print(min_to_change(s, n, k))
main()


from sys import stdin
t = int(stdin.readline())
for _ in range(t):
	(n, k) = map(int, stdin.readline().split())
	l = stdin.readline()
	if n == 1:
		print(0)
	else:
		s = 'RGB'
		m = float('inf')
		for q in range(3):
			count = 0
			dp = [0 for i in range(n + 1)]
			for i in range(n):
				dp[i + 1] = dp[i] + int(l[i] != s[(q + i) % 3])
			for i in range(n - k + 1):
				m = min(m, dp[i + k] - dp[i])
		print(m)


q = int(input())
for w in range(q):
	(n, k) = map(int, input().split())
	s = [i for i in input()]
	c1 = []
	c2 = []
	c3 = []
	c = 0
	for i in range(n):
		if c == 0:
			c1.append('R')
		elif c == 1:
			c1.append('G')
		else:
			c1.append('B')
		c += 1
		if c >= 3:
			c = c - 3
	c = 0
	for i in range(n):
		if c == 0:
			c2.append('G')
		elif c == 1:
			c2.append('B')
		else:
			c2.append('R')
		c += 1
		if c >= 3:
			c = c - 3
	c = 0
	for i in range(n):
		if c == 0:
			c3.append('B')
		elif c == 1:
			c3.append('R')
		else:
			c3.append('G')
		c += 1
		if c >= 3:
			c = c - 3
	n1 = []
	n2 = []
	n3 = []
	for i in range(n):
		if c1[i] == s[i]:
			n1.append(1)
		else:
			n1.append(0)
		if c2[i] == s[i]:
			n2.append(1)
		else:
			n2.append(0)
		if c3[i] == s[i]:
			n3.append(1)
		else:
			n3.append(0)
	s1 = sum(n1[0:k])
	s2 = sum(n2[0:k])
	s3 = sum(n3[0:k])
	m = max(s1, s2, s3)
	j = 0
	for i in range(k, n):
		s1 = s1 - n1[j] + n1[i]
		s2 = s2 - n2[j] + n2[i]
		s3 = s3 - n3[j] + n3[i]
		if s1 > m:
			m = s1
		if s2 > m:
			m = s2
		if s3 > m:
			m = s3
		j += 1
	print(k - m)


def check(a, b):
	c = 0
	for i in range(len(a)):
		if a[i] != b[i]:
			c += 1
	return c
p = 'RGB' * 1000
for _ in range(int(input())):
	(n, k) = [int(i) for i in input().split()]
	s = str(input())
	ans = 10000
	for i in range(3):
		b = p[i:i + k]
		for j in range(n - k + 1):
			a = s[j:j + k]
			ans = min(ans, check(a, b))
	print(ans)


import sys
input = sys.stdin.readline
rgb = 'RGB'
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input().rstrip()
	costs = [[0] * (n + 1) for i in range(3)]
	for i in range(3):
		for j in range(n):
			costs[i][j + 1] = costs[i][j] + (1 if rgb[(i + j) % 3] != s[j] else 0)
	ans = 10 ** 18
	for (i, j) in zip(range(n), range(k, n + 1)):
		for k in range(3):
			if ans > costs[k][j] - costs[k][i]:
				ans = costs[k][j] - costs[k][i]
	print(ans)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	l = ['R', 'G', 'B']
	t1 = l * (k // 3) + l[:k % 3]
	l.insert(0, l.pop())
	t2 = l * (k // 3) + l[:k % 3]
	l.insert(0, l.pop())
	t3 = l * (k // 3) + l[:k % 3]
	m = 7 + 10 ** 9
	for i in range(n - k + 1):
		(a, b, c) = (0, 0, 0)
		for j in range(k):
			if s[i + j] != t1[j]:
				a += 1
			if s[i + j] != t2[j]:
				b += 1
			if s[i + j] != t3[j]:
				c += 1
		m = min(m, a, b, c)
	print(m)


li = ['R', 'G', 'B']
r = ''
g = ''
b = ''
for i in range(2001):
	r += li[i % 3]
	g += li[(i + 1) % 3]
	b += li[(i + 2) % 3]
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	dp = [0] * (n - k + 1)
	for i in range(n - k + 1):
		(a_c, b_c, c_c) = (0, 0, 0)
		for j in range(i, i + k):
			if s[j] != r[j - i]:
				a_c += 1
			if s[j] != g[j - i]:
				b_c += 1
			if s[j] != b[j - i]:
				c_c += 1
		dp[i] = min(a_c, b_c, c_c)
	print(min(dp))


q = int(input())
for query in range(q):
	(n, k) = map(int, input().split())
	s = list(input())
	g_min = 200000
	A = []
	for j in range(k):
		if j % 3 == 0:
			A.append('R')
		elif j % 3 == 1:
			A.append('G')
		else:
			A.append('B')
	B = []
	for j in range(k):
		if j % 3 == 0:
			B.append('G')
		elif j % 3 == 1:
			B.append('B')
		else:
			B.append('R')
	C = []
	for j in range(k):
		if j % 3 == 0:
			C.append('B')
		elif j % 3 == 1:
			C.append('R')
		else:
			C.append('G')
	for i in range(n - k + 1):
		temp = s[i:i + k]
		d = 0
		e = 0
		f = 0
		for x in range(k):
			if A[x] != temp[x]:
				d += 1
		for x in range(k):
			if B[x] != temp[x]:
				e += 1
		for x in range(k):
			if C[x] != temp[x]:
				f += 1
		l_min = min(d, e, f)
		if l_min < g_min:
			g_min = l_min
	print(g_min)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		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')

def conv(let):
	if let == 'R':
		return 0
	elif let == 'G':
		return 1
	else:
		return 2
for j in range(int(input())):
	(n, k) = map(int, input().split())
	vals = [conv(i) for i in input()]
	store = [[0, (k - 1) % 3], [1, k % 3], [2, (k + 1) % 3]]
	cost = [0, 0, 0]
	mincost = 10 ** 9
	for s in range(3):
		for i in range(k):
			if vals[i] != (s + i) % 3:
				cost[s] += 1
	mincost = min(cost)
	for i in range(1, n + 1 - k):
		for s in range(3):
			if vals[i - 1] != store[s][0]:
				cost[s] -= 1
			store[s][0] += 1
			store[s][1] += 1
			store[s][0] = store[s][0] % 3
			store[s][1] = store[s][1] % 3
			if vals[i + k - 1] != store[s][1]:
				cost[s] += 1
		mincost = min(mincost, min(cost))
	print(mincost)


t = int(input())
while t:
	(n, m) = map(int, input().split())
	s = input()
	s1 = 'RGB' * (m // 3) + ''.join(['R', 'G', 'B'][:m % 3])
	s2 = 'GBR' * (m // 3) + ''.join(['G', 'B', 'R'][:m % 3])
	s3 = 'BRG' * (m // 3) + ''.join(['B', 'R', 'G'][:m % 3])
	mi = 10 ** 9
	for i in range(0, n - m + 1):
		(c1, c2, c3) = (0, 0, 0)
		for j in range(i, i + m):
			if s[j] != s1[j - i]:
				c1 += 1
			if s[j] != s2[j - i]:
				c2 += 1
			if s[j] != s3[j - i]:
				c3 += 1
		mi = min(mi, c1, c2, c3)
	print(mi)
	t -= 1


from copy import copy

def db(*arg):
	print(*arg)
Q = int(input())
for _ in range(Q):
	(N, K) = map(int, input().split())
	s = list(input())
	s_mod = [0] * N
	for i in range(N):
		if s[i] == 'R':
			num = 0
		elif s[i] == 'G':
			num = 1
		else:
			num = 2
		s_mod[i] = (i % 3 - num) % 3
	(count_0, count_1, count_2) = (0, 0, 0)
	s_mod_csum = [[0] * (N + 1) for _ in range(3)]
	for i in range(N):
		(s_mod_csum[0][i], s_mod_csum[1][i], s_mod_csum[2][i]) = (count_0, count_1, count_2)
		count_0 += s_mod[i] == 0
		count_1 += s_mod[i] == 1
		count_2 += s_mod[i] == 2
	(s_mod_csum[0][-1], s_mod_csum[1][-1], s_mod_csum[2][-1]) = (count_0, count_1, count_2)
	ans = float('inf')
	for start in range(N):
		if not start + K < N + 1:
			break
		for mod in range(3):
			ans = min(ans, K - (s_mod_csum[mod][start + K] - s_mod_csum[mod][start]))
	print(ans)


from itertools import chain
from sys import stdin, stdout

def main():
	input = stdin.readline
	for _ in range(int(input())):
		(n, k) = map(int, input().split())
		ans = [k, k, k, 1, 1]
		fin_ans = k
		inp = tuple(((ord('r') + i - ord(c)) % 3 for (i, c) in enumerate(input().rstrip().lower())))
		for (inp1, inp2) in zip(inp, chain((4,) * k, inp)):
			ans[inp2] += 1
			ans[inp1] -= 1
			fin_ans = min(fin_ans, ans[inp1])
		stdout.write(f'{fin_ans}\n')
main()


import sys
q = int(input())
for x in range(q):
	(n, k) = map(int, input().split())
	s = list(input())
	R = 'R'
	G = 'G'
	B = 'B'
	D = []
	L = []
	if k == 1:
		print(0)
	else:
		for i in range(k - 1):
			if R[i] == 'R':
				R = R + 'G'
			if R[i] == 'G':
				R = R + 'B'
			if R[i] == 'B':
				R = R + 'R'
			if G[i] == 'R':
				G = G + 'G'
			if G[i] == 'G':
				G = G + 'B'
			if G[i] == 'B':
				G = G + 'R'
			if B[i] == 'R':
				B = B + 'G'
			if B[i] == 'G':
				B = B + 'B'
			if B[i] == 'B':
				B = B + 'R'
		L.append(R)
		L.append(G)
		L.append(B)
		for i in range(n - k + 1):
			for g in range(3):
				d = 0
				for y in range(i, i + k):
					if L[g][y - i] != s[y]:
						d += 1
				D.append(d)
		D.sort()
		print(D[0])


def sovp(s, s1, s2, s3):
	c = 0
	v = 0
	q = 0
	for i in range(len(s1)):
		if s[i] == s1[i]:
			c = c + 1
		if s[i] == s2[i]:
			v = v + 1
		if s[i] == s3[i]:
			q = q + 1
	return max(c, q, v)
q = int(input())
for j in range(0, q):
	(n, k) = map(int, input().split())
	s = str(input())
	s1 = ''
	s2 = ''
	s3 = ''
	q = ['R', 'G', 'B']
	for i in range(0, k):
		s1 = s1 + q[i % 3]
		s2 = s2 + q[(i + 1) % 3]
		s3 = s3 + q[(i + 2) % 3]
	v = 0
	for i in range(0, n - k + 1):
		if sovp(s[i:n], s1, s2, s3) > v:
			v = sovp(s[i:n], s1, s2, s3)
	print(k - v)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	s1 = 'RGB'
	mini = 10 ** 9
	for i in range(n - k + 1):
		for j in range(3):
			curr = 0
			for x in range(k):
				curr += s[i + x] != s1[(x + j) % 3]
			mini = min(mini, curr)
	print(mini)


tc = int(input())
for t in range(tc):
	(n, k) = [int(x) for x in input().split(' ')]
	given = input()
	string = 'RGB'
	strlis = []
	for m in range(3):
		j = m
		l = 0
		r = ''
		while l < k:
			r = r + string[j]
			j = j + 1
			if j == 3:
				j = 0
			l = l + 1
		strlis.append(r)
	j = 0
	mini = 30000
	while j + k <= n:
		temp = given[j:j + k]
		for st in strlis:
			c = 0
			for i in range(k):
				if st[i] != temp[i]:
					c = c + 1
			if c < mini:
				mini = c
		j = j + 1
	print(mini)


from __future__ import division, print_function

def main():
	t = int(input())
	s1 = 'RGB'
	s2 = 'BRG'
	s3 = 'GBR'
	for test_case in range(t):
		(n, k) = map(int, input().split())
		s = input()
		ans = 1000000000.0
		diff = [0] * n
		cur = 0
		for i in range(n):
			if s[i] != s1[i % 3]:
				diff[i] = 1
			cur += diff[i]
			if i >= k:
				cur -= diff[i - k]
			if i >= k - 1:
				ans = min(ans, cur)
		diff = [0] * n
		cur = 0
		for i in range(n):
			if s[i] != s2[i % 3]:
				diff[i] = 1
			cur += diff[i]
			if i >= k:
				cur -= diff[i - k]
			if i >= k - 1:
				ans = min(ans, cur)
		diff = [0] * n
		cur = 0
		for i in range(n):
			if s[i] != s3[i % 3]:
				diff[i] = 1
			cur += diff[i]
			if i >= k:
				cur -= diff[i - k]
			if i >= k - 1:
				ans = min(ans, cur)
		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'0'[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'0'[0]:
		A.append(sign * numb)
	return A
main()


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	l = list(input())
	q = ['R', 'G', 'B'] * (k + 3)
	l1 = []
	for y1 in q[0:k]:
		l1.append(y1)
	l2 = []
	for y2 in q[1:k + 1]:
		l2.append(y2)
	l3 = []
	for y3 in q[2:k + 2]:
		l3.append(y3)
	ans = []
	c = 0
	for t1 in range(len(l) - (k - 1)):
		te = 0
		for tt in range(t1, t1 + k):
			if l1[te] != l[tt]:
				c += 1
			te += 1
		ans.append(c)
		c = 0
	for t1 in range(len(l) - (k - 1)):
		tq = 0
		for tt in range(t1, t1 + k):
			if l2[tq] != l[tt]:
				c += 1
			tq += 1
		ans.append(c)
		c = 0
	for t1 in range(len(l) - (k - 1)):
		tw = 0
		for tt in range(t1, t1 + k):
			if l3[tw] != l[tt]:
				c += 1
			tw += 1
		ans.append(c)
		c = 0
	print(min(ans))


from sys import stdin, stdout
input = stdin.readline
print = stdout.write
st = 'RGB'
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	ans = 1000000000.0
	for offset in range(3):
		cur = 0
		diff = [0] * n
		for i in range(n):
			if s[i] != st[(i + offset) % 3]:
				diff[i] = 1
				cur += 1
			if i >= k:
				cur -= diff[i - k]
			if i >= k - 1:
				ans = min(ans, cur)
	print('%d\n' % ans)


q = int(input())
f = []
for i in range(q):
	(n, k) = map(int, input().split())
	s = input()
	mi = 0
	num = [0, 0, 0]
	for i in range(n):
		if i % 3 == 0:
			if s[i] == 'R':
				num[0] += 1
			elif s[i] == 'G':
				num[1] += 1
			else:
				num[2] += 1
		elif i % 3 == 1:
			if s[i] == 'G':
				num[0] += 1
			elif s[i] == 'B':
				num[1] += 1
			else:
				num[2] += 1
		elif s[i] == 'B':
			num[0] += 1
		elif s[i] == 'R':
			num[1] += 1
		else:
			num[2] += 1
		if i >= k:
			if (i - k) % 3 == 0:
				if s[i - k] == 'R':
					num[0] -= 1
				elif s[i - k] == 'G':
					num[1] -= 1
				else:
					num[2] -= 1
			elif (i - k) % 3 == 1:
				if s[i - k] == 'G':
					num[0] -= 1
				elif s[i - k] == 'B':
					num[1] -= 1
				else:
					num[2] -= 1
			elif s[i - k] == 'B':
				num[0] -= 1
			elif s[i - k] == 'R':
				num[1] -= 1
			else:
				num[2] -= 1
		e = max(num)
		if mi < e:
			mi = e
	f += [k - mi]
for i in f:
	print(i)


q = int(input())
for _ in range(q):
	(n, k) = [int(i) for i in input().split()]
	s = input()
	start = [0] * n
	for i in range(n):
		if s[i] == 'R':
			start[i] = i % 3
		elif s[i] == 'G':
			start[i] = (i - 1) % 3
		else:
			start[i] = (i - 2) % 3
	act = [0] * 3
	res = [0] * 3
	for i in range(k):
		act[start[i]] += 1
		res[start[i]] += 1
	for i in range(k, n):
		act[start[i]] += 1
		act[start[i - k]] -= 1
		if act[start[i]] > res[start[i]]:
			res[start[i]] = act[start[i]]
	print(k - max(res))


import sys
input = sys.stdin.readline
q = int(input())
rgb = 'RGB'
for _ in range(q):
	(n, k) = map(int, input().split())
	s = input()
	ans = n
	for i in range(3):
		r = [0]
		l = i
		for c in s:
			r.append(r[-1] + (1 if c != rgb[l] else 0))
			l = (l + 1) % 3
			if len(r) > k:
				ans = min(ans, r[-1] - r[len(r) - 1 - k])
	print(ans)


rgb = 'RGB' * 1000
for q in range(int(input())):
	(n, k) = [int(s) for s in input().split()]
	s = input()
	ans = 3000
	for w in range(3):
		for e in range(n - k + 1):
			temp = 0
			for i in range(k):
				if s[e + i] != rgb[w + i]:
					temp += 1
			ans = min(ans, temp)
	print(ans)


t = 'RGB' * 100000
q = int(input())
for _ in range(q):
	ans = 999999999999999999999999999999999999999999999999999999999999999999999999999
	(n, k) = map(int, input().split())
	s = input()
	for i in range(3):
		to = t[i:i + n + 1]
		dp = [0 for i in range(n)]
		for j in range(n):
			if s[j] != to[j]:
				dp[j] += 1
		for j in range(1, n):
			dp[j] += dp[j - 1]
		ii = 0
		jj = k - 1
		while jj < n:
			if ii == 0:
				cnt = dp[jj]
			else:
				cnt = dp[jj] - dp[ii - 1]
			ans = min(ans, cnt)
			jj += 1
			ii += 1
	print(ans)


def slide(x, k):
	s = sum(x[:k])
	maxsum = s
	for i in range(k, len(x)):
		s += x[i] - x[i - k]
		maxsum = max(maxsum, s)
	return maxsum

def dist(a, b, k):
	x = [1 if a[i] == b[i] else 0 for i in range(len(s))]
	dist = k - slide(x, k)
	return dist
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	print(min([dist(s, ('RGB' * (len(s) // 3 + 1))[:len(s)], k), dist(s, ('GBR' * (len(s) // 3 + 1))[:len(s)], k), dist(s, ('BRG' * (len(s) // 3 + 1))[:len(s)], k)]))


from sys import stdin
q = int(input())
INF = 10 ** 18
for _ in range(q):
	(n, k) = map(int, stdin.readline().split())
	s = stdin.readline()[:-1]
	r = ('RGB' * k)[:k]
	g = ('GBR' * k)[:k]
	b = ('BRG' * k)[:k]
	dp = [[0] * 3 for i in range(n)]
	for i in range(k):
		if s[i] == r[i]:
			dp[k - 1][0] += 1
		elif s[i] == g[i]:
			dp[k - 1][1] += 1
		elif s[i] == b[i]:
			dp[k - 1][2] += 1
	ans = max(dp[k - 1])
	for i in range(k, n):
		dp[i][0] = dp[i - 1][2] - (s[i - k] == b[0]) + (s[i] == r[k - 1])
		dp[i][1] = dp[i - 1][0] - (s[i - k] == r[0]) + (s[i] == g[k - 1])
		dp[i][2] = dp[i - 1][1] - (s[i - k] == g[0]) + (s[i] == b[k - 1])
		ans = max(ans, max(dp[i]))
	print(k - ans)


import sys
for _ in range(int(sys.stdin.readline())):
	(n, k) = [int(i) for i in sys.stdin.readline().split()]
	word = sys.stdin.readline().strip()
	ans = 0
	for col in ['RGB', 'GBR', 'BRG']:
		cnt = 0
		for i in range(k):
			if word[i] == col[i % 3]:
				cnt += 1
		mx = cnt
		for i in range(n - k):
			if word[i + k] == col[(i + k) % 3]:
				cnt += 1
			if word[i] == col[i % 3]:
				cnt -= 1
			if cnt > mx:
				mx = cnt
		ans = max(ans, mx)
	sys.stdout.write(str(k - ans) + '\n')


def find_substring(s1, s2):
	min_val = len(s1)
	for i in range(len(s2) - len(s1) + 1):
		change = 0
		for j in range(i, i + len(s1)):
			if s1[j - i] != s2[j]:
				change += 1
		min_val = min(min_val, change)
	return min_val
query = int(input())
for q in range(query):
	(n, k) = map(int, input().split())
	string = input().strip()
	print(min(find_substring(('RGB' * (k // 3 + 2))[:k], string), find_substring(('GBR' * (k // 3 + 2))[:k], string), find_substring(('BRG' * (k // 3 + 2))[:k], string)))


q = int(input())
for _ in range(q):
	(n, k) = map(int, input().split(' '))
	s = input()
	compare = []
	for i in range(1, k + 3):
		if i % 3 == 1:
			compare.append('R')
		elif i % 3 == 2:
			compare.append('G')
		else:
			compare.append('B')
	i = 0
	final = []
	while i < n - k + 1:
		j = i
		c = []
		while j < k + i:
			c.append(s[j])
			j += 1
		j = 0
		m = 0
		while j < 3:
			j1 = count = 0
			while j1 < k:
				if c[j1] == compare[j + j1]:
					count += 1
				j1 += 1
			if m < count:
				m = count
			j += 1
		final.append(k - m)
		i += 1
	print(min(final))


from sys import stdin, stdout

def check(s, k, n):
	t = 'RGB'
	ans = int(1000000000.0)
	for off in range(0, 3):
		res = [None] * n
		curr = 0
		for j in range(n):
			res[j] = s[j] != t[(j + off) % 3]
			curr += res[j]
			if j >= k:
				curr -= res[j - k]
			if j >= k - 1:
				ans = min(ans, curr)
	return ans
t = int(stdin.readline())
while t:
	nk = list(map(int, stdin.readline().rstrip().split()))
	n = nk[0]
	k = nk[1]
	s = stdin.readline().rstrip().lstrip()
	stdout.write(str(check(s, k, n)) + '\n')
	t = t - 1


import math
Q = int(input())
pattern = 'RGB'
for q in range(Q):
	N_K = input().split(' ')
	(N, K) = (int(N_K[0]), int(N_K[1]))
	S = input()
	ans = math.inf
	for i in range(len(S) - K + 1):
		for offset in range(3):
			change = 0
			for k in range(K):
				if S[i + k] != pattern[(k + offset) % 3]:
					change += 1
			ans = min(ans, change)
	print(ans)


r1 = []
for i in range(10 ** 5):
	r1.append('R')
	r1.append('G')
	r1.append('B')
r2 = []
for i in range(10 ** 5):
	r2.append('B')
	r2.append('R')
	r2.append('G')
r3 = []
for i in range(10 ** 5):
	r3.append('G')
	r3.append('B')
	r3.append('R')
t = int(input())
for _ in range(t):
	(n, k) = [int(i) for i in input().split()]
	a = input()
	ans = 10 ** 9
	b = [0] * n
	tn = 0
	for i in range(k):
		if a[i] != r1[i]:
			tn += 1
			b[i] = tn
		else:
			b[i] = tn
	ans = min(ans, b[k - 1])
	for i in range(k, n):
		if a[i] != r1[i]:
			tn += 1
			b[i] = tn
		else:
			b[i] = tn
		ans = min(ans, b[i] - b[i - k])
	b = [0] * n
	tn = 0
	for i in range(k):
		if a[i] != r2[i]:
			tn += 1
			b[i] = tn
		else:
			b[i] = tn
	ans = min(ans, b[k - 1])
	for i in range(k, n):
		if a[i] != r2[i]:
			tn += 1
			b[i] = tn
		else:
			b[i] = tn
		ans = min(ans, b[i] - b[i - k])
	b = [0] * n
	tn = 0
	for i in range(k):
		if a[i] != r3[i]:
			tn += 1
			b[i] = tn
		else:
			b[i] = tn
	ans = min(ans, b[k - 1])
	for i in range(k, n):
		if a[i] != r3[i]:
			tn += 1
			b[i] = tn
		else:
			b[i] = tn
		ans = min(ans, b[i] - b[i - k])
	print(ans)


def solve():
	(n, k) = [int(i) for i in input().split(' ')]
	a = input()
	x = 'RGB' * (k // 3 + 1)
	y = 'GBR' * (k // 3 + 1)
	z = 'BRG' * (k // 3 + 1)
	m = float('inf')
	for i in range(n - k + 1):
		x_c = y_c = z_c = 0
		for j in range(i, i + k):
			if a[j] != x[j - i]:
				x_c += 1
			if a[j] != y[j - i]:
				y_c += 1
			if a[j] != z[j - i]:
				z_c += 1
		m = min(m, x_c, y_c, z_c)
	return m
q = int(input())
o = ''
for _ in range(q):
	o += str(solve()) + '\n'
print(o)


import os
import sys
import math
from io import BytesIO, IOBase
from fractions import Fraction
import collections
from itertools import permutations
from collections import defaultdict
from collections import deque
import threading
threading.stack_size(10 ** 8)
sys.setrecursionlimit(300000)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		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')

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	a1 = [0] * n
	a2 = [0] * n
	a3 = [0] * n
	c1 = 'RGB' * math.ceil(n / 3)
	c2 = 'GBR' * math.ceil(n / 3)
	c3 = 'BRG' * math.ceil(n / 3)
	for i in range(n):
		if i != 0:
			a1[i] = a1[i - 1]
			a2[i] = a2[i - 1]
			a3[i] = a3[i - 1]
		if s[i] != c1[i]:
			a1[i] += 1
		if s[i] != c2[i]:
			a2[i] += 1
		if s[i] != c3[i]:
			a3[i] += 1
	m = min(a1[k - 1], a2[k - 1], a3[k - 1])
	for i in range(k, n):
		m = min(m, a1[i] - a1[i - k], a2[i] - a2[i - k], a3[i] - a3[i - k])
	print(m)


def user99():
	text = 'RGB' * 2222
	for _ in range(int(input())):
		(n, k) = list(map(int, input().split()))
		s = input()
		ans = 2222
		for i in range(3):
			p = text[i:k + i]
			for j in range(n - k + 1):
				diff = 0
				for l in range(j, j + k):
					if s[l] != p[l - j]:
						diff += 1
				ans = min(ans, diff)
		print(ans)
user99()


q = int(input())
s1 = 'RGB' * 2000
s2 = 'GBR' * 2000
s3 = 'BRG' * 2000
while q > 0:
	inp = list(map(int, input().split()))
	n = inp[0]
	k = inp[1]
	s = input()
	i = 0
	ans = 1000000000
	j = 0
	while j <= n - k:
		t1 = 0
		t2 = 0
		t3 = 0
		for i in range(0, k):
			if s1[i] != s[i + j]:
				t1 = t1 + 1
			if s2[i] != s[i + j]:
				t2 = t2 + 1
			if s3[i] != s[i + j]:
				t3 = t3 + 1
		ans = min(ans, t1, t2, t3)
		j = j + 1
	print(ans)
	q = q - 1


q = int(input())
for _ in range(q):
	s1 = 'RGB'
	s2 = 'GBR'
	s3 = 'BRG'
	(n, k) = map(int, input().split())
	s = input()
	mi = 1000000000
	for i in range(n - k + 1):
		sum1 = 0
		count = 0
		for j in range(i, i + k):
			if s[j] != s1[count % 3]:
				sum1 += 1
			count += 1
		mi = min(mi, sum1)
		sum1 = 0
		count = 0
		for j in range(i, i + k):
			if s[j] != s2[count % 3]:
				sum1 += 1
			count += 1
		mi = min(mi, sum1)
		sum1 = 0
		count = 0
		for j in range(i, i + k):
			if s[j] != s3[count % 3]:
				sum1 += 1
			count += 1
		mi = min(mi, sum1)
	print(mi)


import sys

def input():
	return sys.stdin.readline()
p = 'RGB'
q = 'GBR'
r = 'BRG'
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = input()
	x = [0]
	y = [0]
	z = [0]
	for i in range(n):
		if a[i] != p[i % 3]:
			x.append(x[i] + 1)
		else:
			x.append(x[i])
		if a[i] != q[i % 3]:
			y.append(y[i] + 1)
		else:
			y.append(y[i])
		if a[i] != r[i % 3]:
			z.append(z[i] + 1)
		else:
			z.append(z[i])
	j = 0
	m = s = k
	if m:
		while s <= n:
			d1 = x[s] - x[j]
			d2 = y[s] - y[j]
			d3 = z[s] - z[j]
			if d1 < m:
				m = d1
			if d2 < m:
				m = d2
			if d3 < m:
				m = d3
			j += 1
			s += 1
	print(m)


import math
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	x = 'RGB' * math.ceil(k / 3)
	y = 'GBR' * math.ceil(k / 3)
	z = 'BRG' * math.ceil(k / 3)
	X = x[0:k]
	Y = y[0:k]
	Z = z[0:k]
	mindiff = math.inf
	for i in range(n - k + 1):
		count1 = 0
		count2 = 0
		count3 = 0
		for j in range(k):
			if s[i + j] != X[j]:
				count1 += 1
			if s[i + j] != Y[j]:
				count2 += 1
			if s[i + j] != Z[j]:
				count3 += 1
		mindiff = min(mindiff, count1, count2, count3)
	print(mindiff)


import os, sys, math
from io import BytesIO, IOBase
from collections import defaultdict, deque, OrderedDict
import bisect as bi

def yes():
	print('YES')

def no():
	print('NO')

def I():
	return int(input())

def In():
	return map(int, input().split())

def ln():
	return list(map(int, input().split()))

def Sn():
	return input().strip()
BUFSIZE = 8192

def find_gt(a, x):
	i = bi.bisect_left(a, x)
	if i != len(a):
		return i
	else:
		return len(a)

def solve():
	d = {'R': 0, 'G': 1, 'B': 2}

	def get(ch, le):
		t = (d[ch] + le) % 3
		return t
	(n, k) = In()
	s = Sn()
	dp = [[0, 0, 0]]
	for i in range(n):
		dp.append([0, 0, 0])
		if s[i] == 'R':
			dp[i + 1][0] = dp[i][2]
			dp[i + 1][1] = dp[i][0] + 1
			dp[i + 1][2] = dp[i][1] + 1
		elif s[i] == 'G':
			dp[i + 1][0] = dp[i][2] + 1
			dp[i + 1][1] = dp[i][0]
			dp[i + 1][2] = dp[i][1] + 1
		else:
			dp[i + 1][0] = dp[i][2] + 1
			dp[i + 1][1] = dp[i][0] + 1
			dp[i + 1][2] = dp[i][1]
	ans = float('inf')
	for i in range(1, n - k + 2):
		tr = dp[i + k - 1][get('R', k - 1)] - dp[i][0]
		tg = dp[i + k - 1][get('G', k - 1)] - dp[i][1]
		tb = dp[i + k - 1][get('B', k - 1)] - dp[i][2]
		if s[i - 1] == 'R':
			tb += 1
			tg += 1
		elif s[i - 1] == 'G':
			(tr, tb) = (tr + 1, tb + 1)
		else:
			(tr, tg) = (tr + 1, tg + 1)
		ans = min(tr, tg, tb, ans)
	print(ans)
	pass

def main():
	T = I()
	for i in range(T):
		solve()
M = 998244353
P = 1000000007

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		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')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
input = sys.stdin.readline
for _ in range(int(input().strip())):
	(n, k) = list(map(int, input().strip().split()))
	inp = input().strip()
	st = 'RGB'
	ans = float('inf')
	for offset in range(3):
		cur = 0
		pre = []
		for i in range(n):
			if inp[i] != st[(i + offset) % 3]:
				cur += 1
			pre.append(cur)
		for i in range(k - 1, n):
			if i == k - 1:
				ans = min(ans, pre[k - 1])
			else:
				ans = min(ans, pre[i] - pre[i - k])
	print(ans)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	if n == 1:
		print(0)
		continue
	A1 = 'RGB' * n
	A2 = 'GBR' * n
	A3 = 'BRG' * n
	ans = k
	x1 = 0
	x2 = 0
	x3 = 0
	for j in range(0, k):
		x1 += s[j] != A1[j]
		x2 += s[j] != A2[j]
		x3 += s[j] != A3[j]
	ans = min(ans, x1, x2, x3)
	for i in range(k, n):
		x1 += (s[i] != A1[i]) - (s[i - k] != A1[i - k])
		x2 += (s[i] != A2[i]) - (s[i - k] != A2[i - k])
		x3 += (s[i] != A3[i]) - (s[i - k] != A3[i - k])
		ans = min(ans, x1, x2, x3)
	print(ans)


import sys
quries = int(sys.stdin.readline())
result = []
mapping = {'R': 'G', 'G': 'B', 'B': 'R'}
rev_mapping = {'R': 'B', 'B': 'G', 'G': 'R'}
count = {'R': 0, 'B': 0, 'G': 0}
memory = {}
for i in range(quries):
	(n, k) = [int(x) for x in sys.stdin.readline().split()]
	best = 10000000
	lis = sys.stdin.readline()
	count = {'R': 0, 'B': 0, 'G': 0}
	for cur in ['R', 'G', 'B']:
		current = cur
		for s in range(0, k):
			if lis[s] != current:
				count[cur] += 1
			current = mapping[current]
	for (s, value) in count.items():
		best = min(best, value)
	for s in range(1, n - k + 1):
		count_temp = {'R': 0, 'G': 0, 'B': 0}
		for cur in ['R', 'G', 'B']:
			count_temp[cur] = count[rev_mapping[cur]]
			if lis[s - 1] != rev_mapping[cur]:
				count_temp[cur] -= 1
			needed = 'c'
			if k % 3 == 0:
				needed = mapping[mapping[cur]]
			elif k % 3 == 1:
				needed = cur
			else:
				needed = mapping[cur]
			if lis[s + k - 1] != needed:
				count_temp[cur] += 1
		count = count_temp
		for (s, value) in count.items():
			best = min(best, value)
	print(best, flush=False)


import sys
input = sys.stdin.readline

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def upper_bound(a, x, lo=0):
	hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	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
	return prime
MAXN = int(1000000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
for _ in range(int(input())):
	(n, k) = int_array()
	string = input().strip()
	dp = [[0 for __ in range(3)] for ___ in range(n)]
	a = []
	ans = k
	for i in string:
		a.append(['R', 'G', 'B'].index(i))
	for i in range(3):
		if i != a[0]:
			dp[0][i] = 1
	for i in range(1, n):
		for j in range(3):
			if j != a[i]:
				dp[i][j] = 1 + dp[i - 1][(j + 2) % 3]
			else:
				dp[i][j] = dp[i - 1][(j + 2) % 3]
	for i in range(k - 1, n):
		prev_i = i - k + 1
		for j in range(3):
			if k % 3 == 1:
				ind = j
			elif k % 3 == 2:
				ind = (j + 2) % 3
			else:
				ind = (j + 1) % 3
			if ind == a[prev_i]:
				kkk = dp[i][j] - dp[prev_i][ind]
			else:
				kkk = dp[i][j] - dp[prev_i][ind] + 1
			ans = min(ans, kkk)
	print(ans)


def wrong_sym(character_seq, list_of_positions):
	for (pos, sym) in enumerate(s):
		list_of_positions.append(0 if sym == character_seq[pos % 3] else 1)

def search_for_the_largest_number_of_zeros(list_of_positions):
	length_of_subseq = 0
	num_of_zeros = 0
	largest_num_of_zeros = 0
	for (pos, num) in enumerate(list_of_positions):
		if length_of_subseq != k:
			num_of_zeros += 1 if num == 0 else 0
			length_of_subseq += 1
			largest_num_of_zeros = max(largest_num_of_zeros, num_of_zeros)
		else:
			num_of_zeros -= 1 if list_of_positions[pos - k] == 0 else 0
			num_of_zeros += 1 if num == 0 else 0
			largest_num_of_zeros = max(largest_num_of_zeros, num_of_zeros)
	return largest_num_of_zeros
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	r = []
	wrong_sym('RGB', r)
	g = []
	wrong_sym('GBR', g)
	b = []
	wrong_sym('BRG', b)
	print(k - max(search_for_the_largest_number_of_zeros(r), search_for_the_largest_number_of_zeros(g), search_for_the_largest_number_of_zeros(b)))


import sys
input = lambda : sys.stdin.readline().strip()
nxt = {'R': 'G', 'G': 'B', 'B': 'R'}
T = int(input())
for _ in range(T):
	(n, k) = map(int, input().split())
	s = input()
	res = []
	for start in ['R', 'G', 'B']:
		mis = []
		cur = start
		for j in range(k):
			if s[j] != cur:
				mis.append(1)
			else:
				mis.append(0)
			cur = nxt[cur]
		res.append(sum(mis))
		for j in range(k, n):
			res.append(res[-1] + int(s[j] != cur) - mis[j - k])
			if s[j] != cur:
				mis.append(1)
			else:
				mis.append(0)
			cur = nxt[cur]
	print(min(res))


x = int(input())
y = []

def izmen(st, srav):
	ans = 0
	for i in range(0, len(st)):
		if st[i] != srav[i]:
			ans += 1
	return ans

def glizmen(st, m1, m2, m3):
	x1 = izmen(st, m1)
	x2 = izmen(st, m2)
	x3 = izmen(st, m3)
	m = min(x1, x2, x3)
	return m

def kukareku(st1, m1, m2, m3, k):
	h = set()
	min1 = 100000000000
	for j in range(0, len(st1) - k + 1):
		st = st1[j:j + k]
		if st not in h:
			h.add(st)
			v = glizmen(st, m1, m2, m3)
			if v < min1:
				min1 = v
	return min1
for i in range(0, x):
	(n, k) = input().split()
	k = int(k)
	st = input()
	m1 = ''
	m2 = ''
	m3 = ''
	for j in range(0, k):
		if j % 3 == 0:
			m1 += 'R'
			m2 += 'G'
			m3 += 'B'
		elif j % 3 == 1:
			m3 += 'R'
			m1 += 'G'
			m2 += 'B'
		else:
			m2 += 'R'
			m3 += 'G'
			m1 += 'B'
	x = kukareku(st, m1, m2, m3, k)
	y.append(x)
for i in y:
	print(i)


letters = {'R': 'G', 'G': 'B', 'B': 'R'}
order = 'RGB'

def rgb_substring(s, k):

	def min_characters_changed(i):
		out = k
		for letter in order:
			expected = letter
			changed = 0
			for j in range(i, i + k):
				if s[j] != expected:
					changed += 1
				expected = letters[expected]
			out = min(out, changed)
		return out
	min_num_characters_changed = len(s)
	for i in range(len(s) - k + 1):
		min_num_characters_changed = min(min_characters_changed(i), min_num_characters_changed)
	return min_num_characters_changed
q = int(input())
out = []
for i in range(q):
	(n, k) = list(map(int, input().split(' ')))
	s = input()
	out.append(rgb_substring(s, k))
for s in out:
	print(s)


import sys
input = sys.stdin.readline

def solve(s, temp, n, k):
	l = list()
	l.append(0)
	for i in range(n):
		if temp[i] != s[i]:
			l.append(l[i] + 1)
		else:
			l.append(l[i])
	mini = 1000000
	for i in range(0, n - k + 1):
		if l[i + k] - l[i] < mini:
			mini = l[i + k] - l[i]
	return mini
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	if n == 1:
		print(0)
		continue
	temp = list()
	for i in range(n):
		if i % 3 == 0:
			temp.append('R')
		elif i % 3 == 1:
			temp.append('G')
		else:
			temp.append('B')
	ans = solve(s, temp, n, k)
	for i in range(n):
		if i % 3 == 0:
			temp[i] = 'G'
		elif i % 3 == 1:
			temp[i] = 'B'
		else:
			temp[i] = 'R'
	ans = min(ans, solve(s, temp, n, k))
	for i in range(n):
		if i % 3 == 0:
			temp[i] = 'B'
		elif i % 3 == 1:
			temp[i] = 'R'
		else:
			temp[i] = 'G'
	ans = min(ans, solve(s, temp, n, k))
	print(ans)


q = int(input())
out = ''
for _ in range(q):
	(n, k) = list(map(int, input().split()))
	s = input()
	rgb = 0
	gbr = 0
	brg = 0
	RGB = 'RGB'
	for i in range(k):
		if i % 3 == 0:
			rgb += int(s[i] != 'R')
			gbr += int(s[i] != 'G')
			brg += int(s[i] != 'B')
		elif i % 3 == 1:
			rgb += int(s[i] != 'G')
			gbr += int(s[i] != 'B')
			brg += int(s[i] != 'R')
		else:
			rgb += int(s[i] != 'B')
			gbr += int(s[i] != 'R')
			brg += int(s[i] != 'G')
	minimum = min(rgb, brg, gbr)
	for i in range(k, n):
		if (i - k) % 3 == 0:
			rgb -= int(s[i - k] != 'R')
			gbr -= int(s[i - k] != 'G')
			brg -= int(s[i - k] != 'B')
		elif (i - k) % 3 == 1:
			rgb -= int(s[i - k] != 'G')
			gbr -= int(s[i - k] != 'B')
			brg -= int(s[i - k] != 'R')
		else:
			rgb -= int(s[i - k] != 'B')
			gbr -= int(s[i - k] != 'R')
			brg -= int(s[i - k] != 'G')
		if i % 3 == 0:
			rgb += int(s[i] != 'R')
			gbr += int(s[i] != 'G')
			brg += int(s[i] != 'B')
		elif i % 3 == 1:
			rgb += int(s[i] != 'G')
			gbr += int(s[i] != 'B')
			brg += int(s[i] != 'R')
		else:
			rgb += int(s[i] != 'B')
			gbr += int(s[i] != 'R')
			brg += int(s[i] != 'G')
		minimum = min(minimum, rgb, brg, gbr)
		if minimum == 0:
			break
	out += str(minimum) + '\n'
print(out)


def get_common_len(j):
	cnt = 0
	for i in range(n):
		if s[i] == e[(i + j) % 3]:
			cnt += 1
		cum[i][j] = cnt
	(res, prev) = (0, 0)
	for i in range(n - k + 1):
		res = max(res, cum[i + k - 1][j] - prev)
		prev = cum[i][j]
	return res
N = int(200000.0 + 3)
e = 'RGB'
cum = list(([0, 0, 0] for _ in range(N)))
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	res = 0
	res = max(res, get_common_len(0))
	res = max(res, get_common_len(1))
	res = max(res, get_common_len(2))
	print(max(0, k - res))


import sys
n = int(input())
I = lambda : map(int, sys.stdin.readline().split())
for _ in range(n):
	(n, m) = I()
	minn = float('inf')
	st = input()
	a1 = (m // 3 + 1) * 'RGB'
	a1 = a1[:m]
	a2 = (m // 3 + 1) * 'BRG'
	a2 = a2[:m]
	a3 = (m // 3 + 1) * 'GBR'
	a3 = a3[:m]
	for i in range(n - m + 1):
		c1 = 0
		c2 = 0
		c3 = 0
		for j in range(m):
			if st[i + j] == a1[j]:
				pass
			else:
				c1 += 1
			if st[i + j] == a2[j]:
				pass
			else:
				c2 += 1
			if st[i + j] == a3[j]:
				pass
			else:
				c3 += 1
		minn = min(minn, c1)
		minn = min(minn, c2)
		minn = min(minn, c3)
	print(minn)


from itertools import accumulate as acc
q = int(input())
rgb = 'RGB'
for _ in range(q):
	(n, k) = map(int, input().split())
	s = input()
	ans = n
	for i in range(3):
		r = []
		l = i
		for c in s:
			r.append(1 if c != rgb[l] else 0)
			l += 1
			if l == 3:
				l = 0
		r = [0] + list(acc(r))
		ans = min(ans, min([r[j] - r[j - k] for j in range(k, n + 1)]))
	print(ans)


n = int(input())
for t in range(n):
	(a, b) = map(int, input().split())
	l = input()
	m = float('inf')
	i = 0
	while i <= a - b:
		for _ in range(3):
			if _ == 0:
				s = 'RGB'
			elif _ == 1:
				s = 'GBR'
			else:
				s = 'BRG'
			count = 0
			j = i
			c = 0
			while j < i + b:
				if s[c] != l[j]:
					count += 1
				c += 1
				j += 1
				if c == 3:
					c = 0
			m = min(m, count)
		i += 1
	print(m)


import sys, math, itertools
from collections import Counter, deque, defaultdict
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush, heapify
mod = 10 ** 9 + 7
INF = float('inf')

def inp():
	return int(sys.stdin.readline())

def inpl():
	return list(map(int, sys.stdin.readline().split()))

def inpl_1():
	return list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))

def inps():
	return sys.stdin.readline()

def inpsl(x):
	tmp = sys.stdin.readline()
	return list(tmp[:x])

def err(x):
	print(x)
	exit()
ST = 'RGB' * 10 ** 5
for _ in range(inp()):
	(n, k) = inpl()
	s = inpsl(n)
	res = INF
	for stj in range(3):
		a = [0] * n
		for i in range(n):
			if s[i] != ST[stj + i]:
				a[i] = 1
		ans = sum((a[i] for i in range(k - 1)))
		for i in range(k - 1, n):
			ans += a[i]
			res = min(res, ans)
			ans -= a[i - k + 1]
	print(res)


import os
import sys
from math import *
from collections import *
from bisect import *
from io import BytesIO, IOBase

def vsInput():
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

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

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		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')
ALPHA = 'abcdefghijklmnopqrstuvwxyz'
M = 998244353
EPS = 1e-06

def Ceil(a, b):
	return a // b + int(a % b > 0)

def value():
	return tuple(map(int, input().split()))

def array():
	return [int(i) for i in input().split()]

def Int():
	return int(input())

def Str():
	return input()

def arrayS():
	return [i for i in input().split()]
for _ in range(Int()):
	(n, k) = value()
	s = input()
	s1 = 'RGB' * k
	need = [s1[:k], s1[1:k + 1], s1[2:k + 2]]
	dp = [[0 for i in range(n)] for i in range(3)]
	for i in range(k):
		if s[i] != need[0][i]:
			dp[0][k - 1] += 1
		if s[i] != need[1][i]:
			dp[1][k - 1] += 1
		if s[i] != need[2][i]:
			dp[2][k - 1] += 1
	for i in range(k, n):
		prev = s[i - k]
		now = s[i]
		dp[0][i] = int(now != need[0][-1]) + (dp[2][i - 1] - int(prev != need[2][0]))
		dp[1][i] = int(now != need[1][-1]) + (dp[0][i - 1] - int(prev != need[0][0]))
		dp[2][i] = int(now != need[2][-1]) + (dp[1][i - 1] - int(prev != need[1][0]))
	ans = k
	for i in range(k - 1, n):
		ans = min(ans, dp[0][i], dp[1][i], dp[2][i])
	print(ans)


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	ans = 10 ** 9
	a = ['R', 'G', 'B']
	for l in range(3):
		x = l
		dp = [0] * (n + 1)
		cnt = 10 ** 9
		for i in range(n):
			if s[i] != a[x]:
				dp[i + 1] = dp[i] + 1
			else:
				dp[i + 1] = dp[i]
			x = (x + 1) % 3
		for i in range(1, n - k + 2):
			cnt = min(cnt, dp[i + k - 1] - dp[i - 1])
		ans = min(cnt, ans)
	print(ans)


import sys
from collections import Counter

def input():
	return sys.stdin.readline().strip()

def list2d(a, b, c):
	return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def list4d(a, b, c, d, e):
	return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]

def ceil(x, y=1):
	return int(-(-x // y))

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST(N=None):
	return list(MAP()) if N is None else [INT() for i in range(N)]

def Yes():
	print('Yes')

def No():
	print('No')

def YES():
	print('YES')

def NO():
	print('NO')
INF = 10 ** 18
MOD = 10 ** 9 + 7

def check(S, T):
	res = 0
	for i in range(K):
		if S[i] != T[i]:
			res += 1
	return res
for _ in range(INT()):
	(N, K) = MAP()
	S = input()
	RGB = 'RGB'
	T1 = []
	for i in range(K):
		T1.append(RGB[i % 3])
	T2 = []
	for i in range(K):
		T2.append(RGB[(i + 1) % 3])
	T3 = []
	for i in range(K):
		T3.append(RGB[(i + 2) % 3])
	ans = INF
	for i in range(N - K + 1):
		j = i + K
		S2 = S[i:j]
		ans = min(ans, check(S2, T1))
		ans = min(ans, check(S2, T2))
		ans = min(ans, check(S2, T3))
	print(ans)


import sys

def getCost(st, ch, k):
	dic = {'R': 'G', 'G': 'B', 'B': 'R'}
	cost = 0
	res = ''
	res = res + ch
	for i in range(0, k - 1):
		p = dic[res[i]]
		res = res + p
		if p != st[i + 1]:
			cost += 1
	if ch != st[0]:
		cost += 1
	return cost

def minCost(st, k):
	r_cost = getCost(st, 'R', k)
	g_cost = getCost(st, 'G', k)
	b_cost = getCost(st, 'B', k)
	return min(r_cost, g_cost, b_cost)
t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	st = input()
	min_cost = sys.maxsize
	for i in range(n - k + 1):
		cost = minCost(st[i:k + i], k)
		if cost < min_cost:
			min_cost = cost
	print(min_cost)


import sys
input = sys.stdin.readline

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return res

def upper_bound(a, x, lo=0):
	hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	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
	return prime
MAXN = int(1000000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
main = 'RGB' * 2000
for _ in range(int(input())):
	(n, k) = int_array()
	string = input().strip()
	allsubs = set()
	for i in range(len(main) - k + 1):
		s = main[i:i + k]
		if s not in allsubs:
			allsubs.add(s)
		else:
			break
	hereiswe = []
	for i in range(n - k + 1):
		s = string[i:i + k]
		hereiswe.append(s)
	ans = k
	for i in allsubs:
		for j in hereiswe:
			count = 0
			for p in range(k):
				if i[p] != j[p]:
					count += 1
			ans = min(ans, count)
	print(ans)


from sys import stdin, stdout
inp = stdin.readline
for _ in range(int(inp())):
	(n, k) = map(int, inp().split())
	(a, b, c, ans) = ([0], [0], [0], 200000.0 + 5)
	s = inp()
	for i in range(n):
		a.append(a[-1] + (s[i] != 'RGB'[i % 3]))
		b.append(b[-1] + (s[i] != 'GBR'[i % 3]))
		c.append(c[-1] + (s[i] != 'BRG'[i % 3]))
	for i in range(n - k + 1):
		ans = min(ans, a[i + k] - a[i], b[i + k] - b[i], c[i + k] - c[i])
	stdout.write(str(ans) + '\n')


from sys import stdin
input = stdin.readline
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	l = input()
	s = 'RGB'
	minim = k
	for j in range(3):
		d = [0] * (n + 1)
		for i in range(n):
			d[i + 1] = d[i] if s[(i + j) % 3] == l[i] else d[i] + 1
			if i >= k - 1:
				minim = min(minim, d[i + 1] - d[i + 1 - k])
	print(minim)


x = int(input())
for i in range(x):
	in1 = input()
	info = in1.split(' ')
	leng = int(info[0])
	nbr = int(info[1])
	chR = ['R']
	chG = ['G']
	chB = ['B']
	for k in range(nbr - 1):
		if chR[k] == 'R':
			chR.append('G')
		if chR[k] == 'G':
			chR.append('B')
		if chR[k] == 'B':
			chR.append('R')
		if chB[k] == 'R':
			chB.append('G')
		if chB[k] == 'G':
			chB.append('B')
		if chB[k] == 'B':
			chB.append('R')
		if chG[k] == 'R':
			chG.append('G')
		if chG[k] == 'G':
			chG.append('B')
		if chG[k] == 'B':
			chG.append('R')
	ch = input()
	minn = nbr
	for j in range(len(ch) - nbr + 1):
		subch = ch[j:j + nbr]
		n = 0
		m = 0
		l = 0
		for k in range(nbr):
			if chG[k] != subch[k]:
				n = n + 1
			if chB[k] != subch[k]:
				m = m + 1
			if chR[k] != subch[k]:
				l = l + 1
		minn = min(minn, n, l, m)
	else:
		print(minn)


t = int(input())
for i in range(t):
	inp = [int(j) for j in filter(None, input().split(' '))]
	k = inp[1]
	string = [j for j in input()]
	res = 2001
	for j in range(len(string) + 1 - k):
		cur_str = string.copy()
		cur_res = 0
		cur_cur_res = 0
		if cur_str[j] != 'R':
			cur_str[j] = 'R'
			cur_cur_res += 1
		for q in range(k - 1):
			if cur_str[j + q] == 'R':
				if cur_str[j + q + 1] != 'G':
					cur_cur_res += 1
					cur_str[j + q + 1] = 'G'
			if cur_str[j + q] == 'G':
				if cur_str[j + q + 1] != 'B':
					cur_cur_res += 1
					cur_str[j + q + 1] = 'B'
			if cur_str[j + q] == 'B':
				if cur_str[j + q + 1] != 'R':
					cur_cur_res += 1
					cur_str[j + q + 1] = 'R'
		cur_res = cur_cur_res
		cur_str = string.copy()
		cur_cur_res = 0
		if cur_str[j] != 'G':
			cur_str[j] = 'G'
			cur_cur_res += 1
		for q in range(k - 1):
			if cur_str[j + q] == 'R':
				if cur_str[j + q + 1] != 'G':
					cur_cur_res += 1
					cur_str[j + q + 1] = 'G'
			if cur_str[j + q] == 'G':
				if cur_str[j + q + 1] != 'B':
					cur_cur_res += 1
					cur_str[j + q + 1] = 'B'
			if cur_str[j + q] == 'B':
				if cur_str[j + q + 1] != 'R':
					cur_cur_res += 1
					cur_str[j + q + 1] = 'R'
		cur_res = min(cur_cur_res, cur_res)
		cur_str = string.copy()
		cur_cur_res = 0
		if cur_str[j] != 'B':
			cur_str[j] = 'B'
			cur_cur_res += 1
		for q in range(k - 1):
			if cur_str[j + q] == 'R':
				if cur_str[j + q + 1] != 'G':
					cur_cur_res += 1
					cur_str[j + q + 1] = 'G'
			if cur_str[j + q] == 'G':
				if cur_str[j + q + 1] != 'B':
					cur_cur_res += 1
					cur_str[j + q + 1] = 'B'
			if cur_str[j + q] == 'B':
				if cur_str[j + q + 1] != 'R':
					cur_cur_res += 1
					cur_str[j + q + 1] = 'R'
		cur_res = min(cur_cur_res, cur_res)
		cur_str = string.copy()
		cur_cur_res = 0
		res = min(res, cur_res)
	print(res)