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

Difficulty: introductory

Starter code:

Solutions:
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
# sys.stdin.readline()
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]
    # print(k,xk,zk)
    # xc=[]
    # yc=[]
    # zc=[]
    # xc.append(countR(xk))
    # xc.append(countG(xk))
    # xc.append(countB(xk))
   
    # yc.append(countR(yk))
    # yc.append(countG(yk))
    # yc.append(countB(yk))
    
    # zc.append(countR(zk))
    # zc.append(countG(zk))
    # zc.append(countB(zk))
    op=2001
    for j in range(n-k+1):
        b=a[j:j+k]
        # print(len(b),xc,zc)
        # bc=[]
        
        # bc.append(countR(b))
        # bc.append(countG(b))
        # bc.append(countB(b))
        xd=0
        yd=0
        zd=0
        # print(a,b,xc,yc,zc,bc)
        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
         # print(a,b,xd,yd,zd,z)
        op=min(op,xd,yd,zd)
    print(op)


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)


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)

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
        #print(ans, 7)
        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)
        #print(ans, 8)
        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, 9)       
    print(ans)

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

q = int(input())
b = []
for m in range(q):
    n, k = list(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
    #print(k1)
    #print(k2)
    #print(k3)
    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
                #print(l[i+j], k3[j])
        if tec < minn: minn = tec
    b.append(minn)
for i in range(q):
    print(b[i])


from sys import stdin
from collections import deque
c=int(stdin.readline().strip())
for cas in range(c):
    n,m=list(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


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]
            #print("p = ", p)
            for j in range(n - k + 1):
                diff = 0
                #print(s[j: j + k], "vs", p)
                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())
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)


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
        #print(l[0:k])
        for j in range(0,k):
            c += (s[i+j] != l[j])
        res = min(res,c)
        #print(c)
        c = 0
        #print(l[1:k+1])
        for j in range(1,k+1):
            c += (s[i+j-1] != l[j])
        res = min(res,c)
        #print(c)
        c = 0
        #print(l[2:k+2])
        for j in range(2,k+2):
            c += (s[i+j-2] != l[j])
        res = min(res,c)
        #print(c)
    print(res)

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
        #print(l[0:k])
        for j in range(0,k):
            c += (s[i+j] != l[j])
        res = min(res,c)
        #print(c)
        c = 0
        #print(l[1:k+1])
        for j in range(1,k+1):
            c += (s[i+j-1] != l[j])
        res = min(res,c)
        #print(c)
        c = 0
        #print(l[2:k+2])
        for j in range(2,k+2):
            c += (s[i+j-2] != l[j])
        res = min(res,c)
        #print(c)
    print(res)

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


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

'''input
3
5 2
BGGGG
5 3
RBRGR
5 5
BBBRR
'''
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
	#print(rgb,gbr,brg)
	for i in range(k-1,n):
		#print(i,i-k)
		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)








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)


######################################################################
# Write your code here
import sys
input = sys.stdin.readline
#import resource
#resource.setrlimit(resource.RLIMIT_STACK, [0x10000000, resource.RLIM_INFINITY])
#sys.setrecursionlimit(0x100000)
# Write your code here
RI = lambda : [int(x) for x in sys.stdin.readline().strip().split()]
rw = lambda : input().strip().split()
from collections import defaultdict as df
#import heapq 
#heapq.heapify(li) heappush(li,4) heappop(li)
import random
#random.shuffle(list)
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)


from sys import stdin,stdout
input=stdin.readline
for _ in range(int(input())):
    x=10**5
    n,k=list(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)


######################################################################
# Write your code here
import sys
input = sys.stdin.readline
#import resource
#resource.setrlimit(resource.RLIMIT_STACK, [0x10000000, resource.RLIM_INFINITY])
#sys.setrecursionlimit(0x100000)
# Write your code here
RI = lambda : [int(x) for x in sys.stdin.readline().strip().split()]
rw = lambda : input().strip().split()
#from collections import defaultdict as df
#import heapq 
#heapq.heapify(li) heappush(li,4) heappop(li)
#import random
#random.shuffle(list)
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)


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 = list(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)


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
        #print(a1,a2,a3,rq1,rq2,rq3)

        ans=min(ans,min(a1,a2,a3))

    print(ans)
            
            
    
            
        


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 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
        else:
            if 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
            else:
                if 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)