Devu loves to play with binary strings a lot. One day he borrowed a binary string s of size n from his friend Churu. Before starting to play with it, he wants to make sure that string does not contain more than  k consecutive equal characters. For achieving that, only kind of operation he is allowed to perform is to flip any ith character of the string. 

As Devu is always in hurry to meet his girlfriend, he wants you to help him in finding out the minimum number of operations he will need. Also he wants you to print one of the possible modified string too.

-----Input-----
- First line of input contains an integer T denoting the number of test cases. 
- For each test case, there are two lines. 
- First line contains two space separated integers n, k as defined in the problem. 
- Next line contains string s of size n.

-----Output-----
- For each test case, print two lines.
- First line should contain an integer corresponding to minimum number of operations Devu needs.
- In second line, print one of the possible modified strings.

-----Constraints-----
Subtask #1: 20 points
- 1 ≤ T ≤ 100, 1 ≤ n ≤ 20, 1 ≤ k ≤ n

Subtask #2: 35 points
- 1 ≤ T ≤ 102, 1 ≤ n ≤ 103, 1 ≤ k ≤ n

Subtask #3: 45 points
- 1 ≤ T ≤ 105, 1 ≤ n ≤ 105, 1 ≤ k ≤ n
- Sum of n over all the test cases is ≤ 106 

-----Example-----
Input:
3
2 1
11
2 2
11
4 1
1001

Output:
1
10
0
11
2
1010

-----Explanation-----
Example case 1: As 1 is occurring twice consecutively, we can convert 11 to 10 in a single operation.
Example case 2: You don't need to modify the string as it does not have more than 2 equal consecutive character.
Example case 3: As 0 is occurring twice consecutively, we can convert 1001 to 1010 in a two operations (Flip third and fourth character).

Difficulty: MEDIUM_HARD
Tags: Amortized analysis
Skill types: Amortized analysis

Starter code:

Solutions:
def solve(l, k):
	s = [x for x in l]
	n = len(s)
	r = 0
	if k == 1:
		s0 = s[:]
		r0 = r1 = 0
		for (i, c) in enumerate(s):
			if i % 2:
				if c == '0':
					s0[i] = '1'
					r0 += 1
			elif c == '1':
				s0[i] = '0'
				r0 += 1
		s1 = s[:]
		for (i, c) in enumerate(s):
			if i % 2 == 0:
				if c == '0':
					s1[i] = '1'
					r1 += 1
			elif c == '1':
				s1[i] = '0'
				r1 += 1
		if r0 < r1:
			r = r0
			s = s0
		else:
			r = r1
			s = s1
	else:
		i = j = 0
		while j < n:
			while j < n and s[i] == s[j]:
				j += 1
			if j - i > k:
				for l in range(i + k, j, k + 1):
					if l == j - 1:
						l -= 1
					s[l] = '0' if s[l] == '1' else '1'
					r += 1
			i = j
	print(r)
	print(''.join(s))
for t in range(int(input())):
	(n, k) = [int(x) for x in input().split()]
	solve(input(), k)


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

def process(stack):
	if len(stack) < k + 1:
		ans.extend(stack)
		return
	if len(stack) % (k + 1) == 0:
		for i in range(k, len(stack) - 1, k + 1):
			stack[i] = (stack[i] + 1) % 2
		stack[-2] = (stack[-2] + 1) % 2
	else:
		for i in range(k, len(stack), k + 1):
			stack[i] = (stack[i] + 1) % 2
	ans.extend(stack)
for i in range(eval(input())):
	(n, k) = r()
	arr = list(map(int, list(input())))
	if k == 1:
		s = []
		c = 0
		for i in range(n):
			s.append((c + 1) % 2)
			c += 1
		s1 = []
		c = 1
		for i in range(n):
			s1.append((c + 1) % 2)
			c += 1
		(ans1, ans2) = (0, 0)
		for i in range(n):
			if arr[i] != s1[i]:
				ans2 += 1
			if arr[i] != s[i]:
				ans1 += 1
		s1 = list(map(str, s1))
		s = list(map(str, s))
		if ans1 < ans2:
			print(ans1)
			print(''.join(s))
		else:
			print(ans2)
			print(''.join(s1))
	else:
		ans = []
		c = 1
		count = 0
		stack = [arr[0]]
		for i in range(1, n):
			if arr[i] == arr[i - 1]:
				stack.append(arr[i])
			else:
				process(stack)
				stack = [arr[i]]
		process(stack)
		count = 0
		for i in range(n):
			if ans[i] != arr[i]:
				count += 1
		print(count)
		ans = list(map(str, ans))
		print(''.join(ans))


def is_prime(n):
	if n == 2 or n == 3:
		return True
	if n < 2 or n % 2 == 0:
		return False
	if n < 9:
		return True
	if n % 3 == 0:
		return False
	r = int(n ** 0.5)
	f = 5
	while f <= r:
		if n % f == 0:
			return False
		if n % (f + 2) == 0:
			return False
		f += 6
	return True

def decomp(n):
	return [n] if n < 10 else decomp(n // 10) + [n % 10]

def compose(lst):
	return 0 if len(lst) < 1 else lst[len(lst) - 1] + compose(lst[0:len(lst) - 1]) * 10
n_test = int(input())

def find_next_chunk(bin_str, start_pos, k):
	len_str = len(bin_str)
	if start_pos >= len_str:
		return (-1, -1)
	cur_pos = start_pos
	cur_val = bin_str[start_pos]
	while cur_pos < len_str:
		if bin_str[cur_pos] != cur_val:
			if cur_pos - start_pos >= k:
				return (start_pos, cur_pos - 1)
			else:
				start_pos = cur_pos
				cur_val = bin_str[cur_pos]
		cur_pos += 1
	if cur_pos - start_pos >= k:
		return (start_pos, cur_pos - 1)
	else:
		return (-1, -1)

def fill_chunk(bin_str, start_pos, end_pos, k):
	count = 1
	list_bin_str = list(bin_str)
	if bin_str[start_pos] == '0':
		val = '1'
	else:
		val = '0'
	while end_pos - start_pos - k * count + 1 > 0:
		list_bin_str[start_pos + k * count - 1] = val
		count += 1
	if end_pos - start_pos - k * count + 1 == 0:
		list_bin_str[start_pos + k * (count - 1) + 1] = val
		count += 1
	return (count - 1, ''.join(list_bin_str))

def fill_chunk_zero_one(bin_str):
	list_bin_str = list(bin_str)
	len_str = len(bin_str)
	one_first_count = 0
	zero_first_count = 0
	for k in range(len_str):
		if k % 2 == 0:
			if list_bin_str[k] == '0':
				one_first_count += 1
			else:
				zero_first_count += 1
		elif list_bin_str[k] == '1':
			one_first_count += 1
		else:
			zero_first_count += 1
	if one_first_count <= zero_first_count:
		for k in range(len_str):
			if k % 2 == 0:
				list_bin_str[k] = '1'
			else:
				list_bin_str[k] = '0'
		return (one_first_count, ''.join(list_bin_str))
	else:
		for k in range(len_str):
			if k % 2 == 0:
				list_bin_str[k] = '0'
			else:
				list_bin_str[k] = '1'
		return (zero_first_count, ''.join(list_bin_str))
for t in range(n_test):
	(n, k) = list(map(int, input().split()))
	bin_str = input()
	bin_str = bin_str[0:n]
	n_cnt = 0
	if k == 1:
		(n_cnt, bin_str) = fill_chunk_zero_one(bin_str)
	else:
		start_pos = 0
		while True:
			(start_pos, end_pos) = find_next_chunk(bin_str, start_pos, k + 1)
			if end_pos == -1:
				break
			(count, bin_str) = fill_chunk(bin_str, start_pos, end_pos, k + 1)
			start_pos = end_pos + 1
			n_cnt += count
	print(n_cnt)
	print(bin_str)


t = int(input())
for i in range(t):
	x = input().split()
	n = int(x[0])
	k = int(x[1])
	y = [int(i) for i in input()]
	z = [1 - i for i in y]
	ck = 1
	res = 0
	last = 0
	for i in range(len(y)):
		c = y[i]
		if i != 0:
			if c == y[i - 1]:
				ck += 1
			else:
				last = ck
				ck = 1
		if ck > k:
			if i == len(y) - 1:
				y[i] = 1 - y[i]
			elif y[i] == 1 - y[i + 1] and (last < k or k > 1):
				y[i - 1] = 1 - y[i - 1]
				ck = 1
				last = 1
			else:
				y[i] = 1 - y[i]
				last = ck - 1
				ck = 1
			res += 1
	s = ''
	for i in y:
		s += str(i)
	res1 = 0
	ck = 1
	last = 0
	for i in range(len(z)):
		c = z[i]
		if i != 0:
			if c == z[i - 1]:
				ck += 1
			else:
				last = ck
				ck = 1
		if ck > k:
			if i == len(z) - 1:
				z[i] = 1 - z[i]
			elif z[i] == 1 - z[i + 1] and (last < k or k > 1):
				z[i - 1] = 1 - z[i - 1]
				ck = 1
				last = 1
			else:
				z[i] = 1 - z[i]
				last = ck - 1
				ck = 1
			res1 += 1
	s1 = ''
	for i in z:
		s1 += str(i)
	res1 = n - res1
	if res1 < res:
		print(res1)
		print(s1)
	else:
		print(res)
		print(s)


T = int(input())
for i in range(T):
	(n, k) = list(map(int, input().split()))
	s = list(map(int, input()))
	if k == 1:
		s_0 = {}
		s_1 = {}
		c0 = 0
		c1 = 0
		for j in range(n):
			if j % 2 == 0:
				s_1[j] = 1
				s_0[j] = 0
			else:
				s_1[j] = 0
				s_0[j] = 1
		for j in range(n):
			if s_0[j] - s[j] != 0:
				c0 += 1
			if s_1[j] - s[j] != 0:
				c1 += 1
		if c0 < c1:
			print(c0)
			p_s = ''
			for j in range(n):
				p_s += str(s_0[j])
			print(p_s)
		else:
			print(c1)
			p_s = ''
			for j in range(n):
				p_s += str(s_1[j])
			print(p_s)
	else:
		count = 1
		c = s[0]
		flips = 0
		for j in range(1, n):
			if s[j] == c:
				count += 1
				if count > k:
					if j + 1 < n and s[j] == s[j + 1]:
						if s[j] == 1:
							s[j] = 0
						else:
							s[j] = 1
					elif s[j - 1] == 1:
						s[j - 1] = 0
					else:
						s[j - 1] = 1
					flips += 1
					count = 1
			elif s[j] != c:
				count = 1
			c = s[j]
		print(flips)
		p_s = ''
		for j in range(n):
			p_s += str(s[j])
		print(p_s)


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	a = input()
	if k == 1:
		cnt = 0
		str1 = ''
		str2 = ''
		while cnt < n:
			str1 += '0'
			str2 += '1'
			cnt += 1
			if cnt == n:
				break
			str1 += '1'
			str2 += '0'
			cnt += 1
		flip1 = 0
		flip2 = 0
		for j in range(n):
			if a[j] != str1[j]:
				flip1 += 1
			if a[j] != str2[j]:
				flip2 += 1
		if flip1 < flip2:
			print(flip1)
			print(str1)
		else:
			print(flip2)
			print(str2)
	else:
		flip = 0
		con = 1
		past = a[0]
		for j in range(1, n, 1):
			if a[j] == past:
				con += 1
			else:
				con = 1
			if con > k:
				idx = j
				if j + 1 < n and a[j] != a[j + 1]:
					idx = j - 1
				temp = list(a)
				if temp[idx] == '1':
					temp[idx] = '0'
				else:
					temp[idx] = '1'
				a = ''.join(temp)
				con = 1
				flip += 1
			past = a[j]
		print(flip)
		print(a)