Chef loves to play with iron (Fe) and magnets (Ma). He took a row of $N$ cells (numbered $1$ through $N$) and placed some objects in some of these cells. You are given a string $S$ with length $N$ describing them; for each valid $i$, the $i$-th character of $S$ is one of the following:
- 'I' if the $i$-th cell contains a piece of iron
- 'M' if the $i$-th cell contains a magnet
- '_' if the $i$-th cell is empty
- ':' if the $i$-th cell contains a conducting sheet
- 'X' if the $i$-th cell is blocked
If there is a magnet in a cell $i$ and iron in a cell $j$, the attraction power between these cells is $P_{i,j} = K+1 - |j-i| - S_{i,j}$, where $S_{i,j}$ is the number of cells containing sheets between cells $i$ and $j$. This magnet can only attract this iron if $P_{i, j} > 0$ and there are no blocked cells between the cells $i$ and $j$.
Chef wants to choose some magnets (possibly none) and to each of these magnets, assign a piece of iron which this magnet should attract. Each piece of iron may only be attracted by at most one magnet and only if the attraction power between them is positive and there are no blocked cells between them. Find the maximum number of magnets Chef can choose.

-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a single string $S$ with length $N$.

-----Output-----
For each test case, print a single line containing one integer ― the maximum number of magnets that can attract iron.

-----Constraints-----
- $1 \le T \le 2,000$
- $1 \le N \le 10^5$
- $0 \le K \le 10^5$
- $S$ contains only characters 'I', 'M', '_', ':' and 'X'
- the sum of $N$ over all test cases does not exceed $5 \cdot 10^6$

-----Subtasks-----
Subtask #1 (30 points): there are no sheets, i.e. $S$ does not contain the character ':'
Subtask #2 (70 points): original constraints

-----Example Input-----
2
4 5
I::M
9 10
MIM_XII:M

-----Example Output-----
1
2

-----Explanation-----
Example case 1: The attraction power between the only magnet and the only piece of iron is $5+1-3-2 = 1$. Note that it decreases with distance and the number of sheets.
Example case 2:
The magnets in cells $1$ and $3$ can attract the piece of iron in cell $2$, since the attraction power is $10$ in both cases. They cannot attract iron in cells $6$ or $7$ because there is a wall between them.
The magnet in cell $9$ can attract the pieces of iron in cells $7$ and $6$; the attraction power is $8$ and $7$ respectively.

Starter code:

Solutions:
# cook your dish here
# cook your dish here
for _ in range(int(input())) :
 n,k=map(int,input().split())
 #reading the string 
 s=input()
 i,j=0,0
 q=0
 while(i<n and j<n) :
  if(s[i]=='M') :
   if(s[j]=='I') :
    cnt=0
    if(i>j) :
     p=s[j:i]
     cnt=p.count(':')
    else :
     p=s[i:j]
     cnt=p.count(':')
    t=k+1-abs(i-j)-cnt
    if(t>0) :
     q+=1
     i+=1
     j+=1
    else:
     if(i<j) :
      i+=1
     else:
      j+=1
   elif(s[j]=='X') :
    j+=1
    i=j
   else:
    j+=1
  elif(s[i]=='X') :
   i+=1
   j=i
  else:
   i+=1
 print(q) 

# cook your dish here

def solve(Str, K):
 S = ''
 ind = -1
 iron, mag = [], []
 for it in Str:
  ind += 1
  if it == ':':
   ind += 1

  if it == 'I':
   iron.append(ind)
  elif it == 'M':
   mag.append(ind)

 ans = 0
 lr = len(iron)
 lm = len(mag)

 i, j = 0, 0
 while i<lr and j<lm:
  ir, mg = iron[i], mag[j]
  if abs(ir - mg) <= K:
   ans += 1
   i+=1
   j+=1
  else:
   if ir > mg:
    j += 1
   else:
    i += 1
 return ans

for case in range(int(input())):
 n, k = list(map(int, input().split()))
 st = input()
 li = [it for it in st.split('X') if len(it)>0]

 ans = sum(solve(string, k) for string in li)
 print(ans)


# cook your dish here
t=int(input())
for t in range(t):
 n,k=list(map(int,input().split()))
 s=input()
 i=0
 j=0
 a=0
 while(i<n and j<n):
  if(s[i]=='M'):
   if(s[j]=='I'):
    c=0
    if(i>j):
     z=s[j:i]
     c=z.count(":")
    else:
     z=s[i:j]
     c=z.count(":")
    p=k+1-abs(i-j)-c
    if(p>0):
     a+=1
     i+=1
     j+=1
    else:
     if(i<j):
      i+=1
     else:
      j+=1
   elif(s[j]=='X'):
    j+=1
    i=j
   else:
    j+=1
  elif(s[i]=='X'):
   i+=1
   j=i
  else:
   i+=1
 print(a)

  
  


t=int(input())
for _ in range(t):
 n,k=list(map(int,input().split()))
 s=list(input())
 ma,ir=[],[]
 total=0
 for i in range(n):
  k1=s[i]
  if k1=='M':
   ma.append(i)
  elif k1=='I':
   ir.append(i)
  if k1=='X' or i==n-1:    
   alt=ir.copy()
   el,l=0,len(ir)
   for mag in ma:
    for j in range(l):
     iron=ir[j]
     if iron>mag:
      #sheet=s[mag:iron].count(":")
      sheet=0
      p=k+1-(iron-mag)-sheet 
      if p>0:
       el+=1
       alt.pop(j)
       l-=1
       break 
     else:
      #sheet=s[iron:mag].count(":")
      sheet=0
      #print(iron,mag,sheet)
      p=k+1-(mag-iron)-sheet 
      if p>0: 
       el+=1
       alt.pop(j)
       l-=1
       break 
    ir=alt.copy() 
   total+=el
   #for k,v in attr.items():
   #    total-=v-1 
   #print(ma,ir,total)   
   ma,ir=[],[]
 print(total) 




t=int(input())
for _ in range(t):
 n,k=list(map(int,input().split()))
 s=list(input())
 ma,ir=[],[]
 total=0
 for i in range(n):
  k1=s[i]
  if k1=='M':
   ma.append(i)
  elif k1=='I':
   ir.append(i)
  if k1=='X' or i==n-1:    
   alt=ir.copy()
   el=0
   for mag in ma:
    for iron in ir:
     if iron>mag:
      #sheet=s[mag:iron].count(":")
      sheet=0
      p=k+1-(iron-mag)-sheet 
      if p>0:
       el+=1
       alt.remove(iron)
       break 
     else:
      #sheet=s[iron:mag].count(":")
      sheet=0
      #print(iron,mag,sheet)
      p=k+1-(mag-iron)-sheet 
      if p>0: 
       el+=1
       alt.remove(iron)
       break 
    ir=alt.copy() 
   total+=el
   #for k,v in attr.items():
   #    total-=v-1 
   #print(ma,ir,total)   
   s1=set() 
   ma,ir=[],[]
 print(total) 




for _ in range(int(input())):
 n,k = map(int,input().split())
 s = input()
 l = ''
 for i in s:
  if i == ':':
   l += ':'
  l += i
 magnet = []
 iron = []
 n = len(l)
 ans = 0
 for i in range(n):
  if l[i] == 'I':
   while magnet != [] and abs(magnet[0]-i) > k:
    magnet.pop(0)
   if len(magnet)>0:
    ans += 1
    magnet.pop(0)
   else:
    iron.append(i)
  elif l[i] == 'M':
   while iron!=[] and abs(iron[0]-i)>k:
    iron.pop(0)
   if len(iron) > 0:
    ans += 1
    iron.pop(0)
   else:
    magnet.append(i)
  elif l[i] == 'X':
   while iron!=[]:
    iron.pop(0)
   while magnet != []:
    magnet.pop(0)
 print(ans)

t=int(input()) #TESTCASES

for y in range(t):
 inptstr=input().split()

 n=int(inptstr[0]) #N TOTAL ELEMENTS

 K=int(inptstr[1]) #K CONSTaNT IN Pij

 s=input() #STRING SEQUENCE OF CELLS ELEMENTS

 
 noblock=s.split('X')
 cnt=[]
 cntiron=0
 for ele in noblock: #PARSING ELEMENTS WITHOUT BLOCK
  d={}
  d1={}
  iron=[]
  magnet=[]
  for x in range(len(ele)):#TRACKING LOCATION OF ELEMENTS IN NOBLOCK
   if ele[x]=='I':
    iron.append(x)
   elif ele[x]=='M':
    magnet.append(x)
  #print('part',ele)
  for i in iron:

   #print('IRON - ',i)
   for j in magnet:
    #print('MAGNET - ',j)
    
    if i in d or j in d1:
     continue
    if i<j:
     Sij=(ele[i:j]).count(':')

    if j<i:
     Sij=(ele[j:i]).count(':')
    Pij=(K+1)-(abs(j-i))-Sij
    if Pij > 0:
     cntiron=cntiron+1
     #print('CONNECTION DONE')
     d[i]=j
     d1[j]=i
     break
 
 print(cntiron)

    
    
    
  
  




 
 



t=int(input()) #TESTCASES

for y in range(t):
 inptstr=input().split()

 n=int(inptstr[0]) #N TOTAL ELEMENTS

 K=int(inptstr[1]) #K CONSTaNT IN Pij

 s=input() #STRING SEQUENCE OF CELLS ELEMENTS

 
 noblock=s.split('X')
 cnt=[]
 cntiron=0
 for ele in noblock: #PARSING ELEMENTS WITHOUT BLOCK
  d={}
  d1={}
  iron=[]
  magnet=[]
  for x in range(len(ele)):#TRACKING LOCATION OF ELEMENTS IN NOBLOCK
   if ele[x]=='I':
    iron.append(x)
   elif ele[x]=='M':
    magnet.append(x)
  #print('part',ele)
  for i in iron:

   #print('IRON - ',i)
   for j in magnet:
    #print('MAGNET - ',j)
    
    if i in d or j in d1:
     continue
    if i<j:
     Sij=(ele[i:j]).count(':')

    if j<i:
     Sij=(ele[j:i]).count(':')
    Pij=(K+1)-(abs(j-i))-Sij
    if Pij > 0:
     cntiron=cntiron+1
     #print('CONNECTION DONE')
     d[i]=j
     d1[j]=i
     break
 
 print(cntiron)

    
    
    
  
  




 
 



# cook your dish here
from collections import deque
for _ in range(int(input())):

 n, k = list(map(int, input().split()))
 s = input()

 new_s = ""
 for obj in s:
  if obj == ":":
   new_s+=obj
  new_s+=obj

 qi = deque()
 qm = deque()

 ans = 0; j = 0
 for i in new_s:

  if i == 'I':
   while (len(qm)!=0 and qm[0]+k < j):
    qm.popleft()

   if len(qm) != 0:
    ans+=1
    qm.popleft()

   else:
    qi.append(j)

  elif i == 'M':
   while (len(qi) != 0 and qi[0]+k < j):
    qi.popleft()

   if len(qi)!=0:
    ans+=1
    qi.popleft()

   else:
    qm.append(j)

  elif i == 'X':
   while len(qm)!=0:
    qm.popleft()
   while len(qi)!=0:
    qi.popleft()

  j+=1
 print(ans)


def fema(s,k):
 iron=[]
 mag=[]
 sheet=0
 ind=-1
 for i in s:
  ind+=1
  if i==':':
   sheet+=1
   ind+=1
  if i=='I':
   iron.append(ind)
  if i=='M':
   mag.append(ind)

 count=0
 i,j=0,0
 while i<len(iron) and j<len(mag):
  if abs(iron[i]-mag[j])<=k:
   count+=1
   i+=1
   j+=1
  else:
   if(iron[i]>mag[j]):
    j+=1
   else:
    i+=1
 return count

test=int(input())
while(test!=0):
 test-=1
 n,k=list(map(int,input().split()))
 s=input()
 li=[st for st in s.split('X') if len(st)>0]
 ans=sum(fema(st,k) for st in li)
 print(ans)


def fema(s,k):
 iron=[]
 mag=[]
 sheet=0
 ind=-1
 for i in s:
  ind+=1
  if i==':':
   sheet+=1
  if i=='I':
   iron.append(ind)
  if i=='M':
   mag.append(ind)

 count=0
 i,j=0,0
 while i<len(iron) and j<len(mag):
  if abs(iron[i]-mag[j])<=k:
   count+=1
   i+=1
   j+=1
  else:
   if(iron[i]>mag[j]):
    j+=1
   else:
    i+=1
 return count

test=int(input())
while(test!=0):
 test-=1
 n,k=list(map(int,input().split()))
 s=input()
 li=[st for st in s.split('X') if len(st)>0]
 ans=sum(fema(st,k) for st in li)
 print(ans)


test=int(input())
while(test!=0):
 test=test-1
 n,k= list(map(int,input().split()))
 s=input()
 i=0
 j=0
 mag=0
 sheet=0
 while(i<n and j<n):
  if(s[i]=='I'):
   if(s[j]=='M'):
    if(i<j):
     for a in range(i,j+1):
      if s[a]==':':
       sheet=sheet+1
    if(j<i):
     for a in range(j,i+1):
      if s[a]==':':
       sheet=sheet+1

    power=(k+1)-abs(j-i)+sheet
    sheet=0
    if(power>0):
     mag+=1
     i+=1
     j+=1
    else:
     if(i>j):
      j+=1
     else:
      i+=1
   elif(s[j]=='X'):
    i=j
    i+=1
    j+=1
   else:
    j+=1
  elif(s[i]=='X'):
   j=i
   j+=1
   i+=1
  else:
   i+=1
 print(mag)
       


for t in range(int(input())):
 n, k = list(map(int, input().split()))
 s = input()
 i = 0
 j = 0
 ans = 0
 while i < n and j < n:
  if s[i] == "M":
   if s[j] == "I":
    c = 0
    if i > j:
     o = s[j:i]
     c = o.count(':')
    else:
     o = s[i:j]
     c = o.count(':')
    p = k+1-abs(i-j)-c
    if p > 0:
     ans = ans+1
     i = i+1
     j = j+1
    else:
     if i < j:
      i += 1
     else:
      j += 1
   elif s[j] == "X":
    j += 1
    i = j
   else:
    j += 1
  elif s[i] == 'X':
   i += 1
   j = i
  else:
   i += 1
 print(ans)


def solve(string,k):
 ind = 0
 iron,mag = [],[]
 for i in string:
  if i==':':
   ind+=1
  elif i=='I':
   iron.append(ind)
  elif i=="M":
   mag.append(ind)
  ind+=1
 i,j = 0,0
 li = len(iron)
 lm = len(mag)
 ans = 0
 while i<li and j<lm:
  if abs(iron[i]-mag[j])<=k:
   ans+=1
   i+=1
   j+=1
  else:
   if iron[i]>mag[j]:
    j+=1
   else:
    i+=1
 return ans
for _ in range(int(input())):
 ans = 0
 n,k = list(map(int,input().split()))
 s = input()
 real = [i for i in s.split("X") if len(i)>0]
 ans+=sum(solve(string,k)for string in real)
 print(ans)


class check:
 def __init__(self, k, s):
  self.sl = s.split("X")
  self.k = k

 def ans(self):
  answer = 0
  for strings in self.sl:
   irons = []
   magnets = []
   sheets = 0
   for i in range(len(strings)):
    if strings[i] == "I":
     if not magnets:
      irons.append(i + sheets)
     else:
      index_i = i + sheets
      for x in magnets:
       if (self.k + 1 - abs(x - index_i)) > 0:
        answer += 1
        magnets.remove(x)
        break
      else:
       irons.append(index_i)
    elif strings[i] == "M":
     if not irons:
      magnets.append(i + sheets)
     else:
      index_m = i + sheets
      for x in irons:
       if (self.k + 1 - abs(x - index_m)) > 0:
        answer += 1
        irons.remove(x)
        break
      else:
       magnets.append(index_m)
    elif strings[i] == ":":
     sheets += 1
  return answer


for _ in range(int(input())):
 n, k = list(map(int, input().split()))
 s = input()
 st = check(k, s)
 print(st.ans())


from collections import deque

t = int(input())

for _ in range(t):
 n, k = map(int, input().split())
 s = input()
 l = str()
 for i in range(n):
  if s[i]==":":
   l+=":"
  l+=s[i]
  
 
 fe = deque()
 mg = deque()
 tot = 0
 for i in range(len(l)):
  if l[i] == "I":
   while len(mg)>0 and abs(mg[0]-i)>k:
    mg.popleft()
   if len(mg)>0:
    tot += 1
    mg.popleft()
   else:
    fe.append(i)
  elif l[i] == "M":
   while len(fe)>0 and abs(fe[0]-i)>k:
    fe.popleft()
   if len(fe) >0:
    tot+=1
    fe.popleft()
   else:
    mg.append(i)
  elif l[i] == "X":
   while len(fe)>0:
    fe.popleft()
   while len(mg)>0:
    mg.popleft()
 print(tot)

from collections import deque

t = int(input())

for _ in range(t):
 n, k = map(int, input().split())
 s = input()
 l = str()
 for i in range(n):
  if s[i]==":":
   l+=":"
  l+=s[i]
  
 
 fe = deque()
 mg = deque()
 tot = 0
 for i in range(len(s)):
  if s[i] == "I":
   while len(mg)>0 and abs(mg[0]-i)>k:
    mg.popleft()
   if len(mg)>0:
    tot += 1
    mg.popleft()
   else:
    fe.append(i)
  elif s[i] == "M":
   while len(fe)>0 and abs(fe[0]-i)>k:
    fe.popleft()
   if len(fe) >0:
    tot+=1
    fe.popleft()
   else:
    mg.append(i)
  elif s[i] == "X":
   while len(fe)>0:
    fe.popleft()
   while len(mg)>0:
    mg.popleft()
 print(tot)

from collections import deque

t = int(input())

for _ in range(t):
 n, k = map(int, input().split())
 s = input()
 
 l = " "
 for i in range(n):
  if s[i]==":":
   l+=":"
  l+=s[i]
  
 
 fe = deque()
 mg = deque()
 tot = 0
 for i in range(len(s)):
  if s[i] == "I":
   while len(mg)>0 and abs(mg[0]-i)>k:
    mg.popleft()
   if len(mg)>0:
    tot += 1
    mg.popleft()
   else:
    fe.append(i)
  elif s[i] == "M":
   while len(fe)>0 and abs(fe[0]-i)>k:
    fe.popleft()
   if len(fe) >0:
    tot+=1
    fe.popleft()
   else:
    mg.append(i)
  elif s[i] == "X":
   while len(fe)>0:
    fe.popleft()
   while len(mg)>0:
    mg.popleft()
 print(tot)

from collections import deque

t = int(input())

for _ in range(t):
 n, k = map(int, input().split())
 s = input()
 
 l = ""
 for i in range(n):
  if s[i]==":":
   l+=":"
  l+=s[i]
  
 
 fe = deque()
 mg = deque()
 tot = 0
 for i in range(len(s)):
  if s[i] == "I":
   while len(mg)>0 and abs(mg[0]-i)>k:
    mg.popleft()
   if len(mg)>0:
    tot += 1
    mg.popleft()
   else:
    fe.append(i)
  elif s[i] == "M":
   while len(fe)>0 and abs(fe[0]-i)>k:
    fe.popleft()
   if len(fe) >0:
    tot+=1
    fe.popleft()
   else:
    mg.append(i)
  elif s[i] == "X":
   while len(fe)>0:
    fe.popleft()
   while len(mg)>0:
    mg.popleft()
 print(tot)

# cook your dish here
t=int(input())
for _ in range(t):
 n,k=map(int,input().split())
 l=input()
 count=0
 i=0
 j=0
 while i<n and j<n:
  if l[i]=="M":
   if l[j]=="I":
    if i<j:
     sij=l[i:j].count(":")
    else:
     sij=l[j:i].count(":")
    p=k+1-abs(i-j)-sij
    if p>0:
     count+=1
     i+=1
     j+=1
    else:
     if i<j:
      i+=1
     else:
      j+=1
   elif l[j]=="X":
    j+=1
    i=j
   else:
    j+=1
  elif l[i]=="X":
   i+=1
   j=i
  else:
   i+=1
 print(count)

t=int(input())
for t in range(t):
 n,k=list(map(int,input().split()))
 s=input()
 i=0
 j=0
 a=0
 while(i<n and j<n):
  if(s[i]=='M'):
   if(s[j]=='I'):
    c=0
    if(i>j):
     z=s[j:i]
     c=z.count(":")
    else:
     z=s[i:j]
     c=z.count(":")
    p=k+1-abs(i-j)-c
    if(p>0):
     a+=1
     i+=1
     j+=1
    else:
     if(i<j):
      i+=1
     else:
      j+=1
   elif(s[j]=='X'):
    j+=1
    i=j
   else:
    j+=1
  elif(s[i]=='X'):
   i+=1
   j=i
  else:
   i+=1
 print(a)




t=int(input()) #TESTCASES

for y in range(t):
 inptstr=input().split()

 n=int(inptstr[0]) #N TOTAL ELEMENTS

 K=int(inptstr[1]) #K CONSTaNT IN Pij

 s=input() #STRING SEQUENCE OF CELLS ELEMENTS

 
 noblock=s.split('X')
 cnt=[]
 cntiron=0
 for ele in noblock: #PARSING ELEMENTS WITHOUT BLOCK
  d={}
  d1={}
  iron=[]
  magnet=[]
  for x in range(len(ele)):#TRACKING LOCATION OF ELEMENTS IN NOBLOCK
   if ele[x]=='I':
    iron.append(x)
   elif ele[x]=='M':
    magnet.append(x)
  #print('part',ele)
  for i in iron:

   #print('IRON - ',i)
   for j in magnet:
    #print('MAGNET - ',j)
    
    if i in d or j in d1:
     continue
    if i<j:
     Sij=(ele[i:j]).count(':')

    if j<i:
     Sij=(ele[j:i]).count(':')
    Pij=(K+1)-(abs(j-i))-Sij
    if Pij > 0:
     cntiron=cntiron+1
     #print('CONNECTION DONE')
     d[i]=j
     d1[j]=i
     break
 
 print(cntiron)