Real stupidity beats artificial intelligence every time.

— Terry Pratchett, Hogfather, Discworld

You are given a string $s$ of length $n$ and a number $k$. Let's denote by $rev(s)$ the reversed string $s$ (i.e. $rev(s) = s_n s_{n-1} ... s_1$). You can apply one of the two kinds of operations to the string:

replace the string $s$ with $s + rev(s)$

replace the string $s$ with $rev(s) + s$

How many different strings can you get as a result of performing exactly $k$ operations (possibly of different kinds) on the original string $s$?

In this statement we denoted the concatenation of strings $s$ and $t$ as $s + t$. In other words, $s + t = s_1 s_2 ... s_n t_1 t_2 ... t_m$, where $n$ and $m$ are the lengths of strings $s$ and $t$ respectively.


-----Input-----

The first line contains one integer $t$ ($1 \le t \le 100$) — number of test cases. Next $2 \cdot t$ lines contain $t$ test cases:

The first line of a test case contains two integers $n$ and $k$ ($1 \le n \le 100$, $0 \le k \le 1000$) — the length of the string and the number of operations respectively.

The second string of a test case contains one string $s$ of length $n$ consisting of lowercase Latin letters.


-----Output-----

For each test case, print the answer (that is, the number of different strings that you can get after exactly $k$ operations) on a separate line.

It can be shown that the answer does not exceed $10^9$ under the given constraints.


-----Examples-----

Input
4
3 2
aab
3 3
aab
7 1
abacaba
2 0
ab
Output
2
2
1
1


-----Note-----

In the first test case of the example:

After the first operation the string $s$ can become either aabbaa or baaaab. After the second operation there are 2 possibilities for $s$: aabbaaaabbaa and baaaabbaaaab.

Difficulty: EASY
Tags: String algorithms

Greedy algorithms
Skill types: Greedy algorithms

Starter code:

Solutions:
for _ in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	if b == 0 or s == s[::-1]:
		print(1)
	else:
		print(2)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	if s == s[::-1] or k == 0:
		print(1)
	else:
		print(2)


t = int(input())
for _ in range(t):
	l = list(map(int, input().split()))
	(n, k) = (l[0], l[1])
	s = input()
	o = 1
	if k == 0:
		print(o)
	elif s[::-1] == s:
		print(o)
	else:
		print(2)


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	str = input()
	if str == str[::-1] or n == 1 or k == 0:
		print(1)
	else:
		print(2)


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	s = input()
	if k == 0:
		print(1)
		continue
	if s == s[::-1]:
		print(1)
	else:
		print(2)


t = int(input())
mylist = []
for x in range(0, t):
	(n, k) = input().split()
	s = input()
	n = int(n)
	k = int(k)
	if k == 0:
		mylist.append(1)
	elif s == s[::-1]:
		mylist.append(1)
	else:
		mylist.append(2)
for ans in mylist:
	print(ans)


t = int(input())
for times in range(t):
	(n, k) = input().split()
	s = input()
	revs = s[::-1]
	if s == revs or int(k) == 0:
		print('1')
	else:
		print('2')


t = int(input())
while t > 0:
	input_string = input()
	a = input_string.split()
	for i in range(len(a)):
		a[i] = int(a[i])
	s = input()
	count = 0
	if a[1] == 0:
		count = 1
	elif a[1] >= 1:
		revs = s[::-1]
		if revs == s:
			count = 1
		else:
			count = 2
	print(count)
	t = t - 1


t = int(input())
while t:
	(n, k) = map(int, input().split(' '))
	s = input()
	if k == 0:
		print('1')
	elif s == s[::-1]:
		print('1')
	else:
		print('2')
	t = t - 1


def fun(n, k, s):
	if k == 0:
		return 1
	(i, j) = (0, n - 1)
	while i < j:
		if s[i] != s[j]:
			return 2
		i += 1
		j -= 1
	return 1
test = int(input())
for t in range(test):
	(n, k) = map(int, input().strip().split())
	s = input()
	print(fun(n, k, s))


t = int(input())

def isPal(s):
	j = len(s) - 1
	for i in range(0, int(len(s) / 2 + 1)):
		if s[i] != s[j]:
			return False
		j -= 1
	return True
for i in range(t):
	(n, k) = map(int, input().split())
	s = list(input())
	pal = isPal(s)
	if k < 1:
		print(1)
	elif pal:
		print(1)
	else:
		print(2)


def solve(s, k):
	res = 0
	n = len(s)
	check = True
	for i in range(0, int(n / 2) + 1):
		if s[i] != s[n - 1 - i]:
			check = False
			break
	if check:
		res = 1
	else:
		res = 1 if k < 1 else 2
	return res
test = int(input())
for t in range(0, test):
	(n, k) = list(map(int, input().split()))
	s = input()
	print(solve(s, k))


for i in range(int(input())):
	(n, K) = map(int, input().split())
	s = input()
	d = []
	if K == 0:
		print(1)
	elif s == s[::-1]:
		print(1)
	else:
		print(2)


def main():
	(n, k) = map(int, input().split())
	s = input()
	print(1 if s == s[::-1] or k == 0 else 2)
t = int(input())
for i in range(t):
	main()


from collections import defaultdict
import sys, math
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	if s == s[::-1]:
		print(1)
	elif k == 0:
		print(1)
	else:
		print(2)


a = int(input())
for i in range(a):
	(b, c) = list(map(int, input().split()))
	d = input()
	if c == 0:
		print(1)
	elif d == d[::-1]:
		print(1)
	else:
		print(2)


for _ in range(int(input())):
	(a, b) = map(int, input().split(' '))
	s = input()
	t = ''
	for i in range(1, a + 1):
		t += s[-i]
	s_1 = s + t * b
	s_2 = t * b + s
	if s_1 == s_2:
		print(1)
	else:
		print(2)


t = int(input())
for _ in range(t):
	(n, k) = [int(i) for i in input().split()]
	s = input()
	if s != s[::-1] and k >= 1:
		print(2)
	else:
		print(1)


from sys import stdin, exit, setrecursionlimit
from math import ceil
input = stdin.readline
lmi = lambda : list(map(int, input().split()))
mi = lambda : map(int, input().split())
si = lambda : input().strip('\n')
ssi = lambda : input().strip('\n').split()
for _ in range(int(input())):
	(n, k) = mi()
	s = si()
	if s == s[::-1]:
		print(1)
	elif k >= 1:
		print(2)
	else:
		print(1)


import sys
input = sys.stdin.readline

def is_pal(s):
	for i in range(len(s) // 2):
		if s[i] != s[len(s) - 1 - i]:
			return 0
	return 1
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input().strip()
	if is_pal(s):
		print(1)
	elif k >= 1:
		print(2)
	else:
		print(1)


for z in range(int(input())):
	(n, k) = map(int, input().strip().split())
	s = input()
	pal = 1
	for i in range(0, n // 2):
		if s[i] != s[n - i - 1]:
			pal = 0
	if k == 0 or n == 1:
		print(1)
	elif pal == 0:
		print(2)
	else:
		print(1)


for i in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	s1 = s[::-1]
	if k == 0:
		print(1)
	elif s == s1:
		print(1)
	else:
		print(2)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	if k == 0:
		print(1)
		continue
	flag = False
	for i in range(n // 2):
		if s[i] != s[n - 1 - i]:
			flag = True
			break
	if flag:
		print(2)
	else:
		print(1)


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	s = input()
	ans = 1
	while k and s != s[::-1]:
		s += s[::-1]
		ans += 1
		k -= 1
	print(ans)


import sys
input = sys.stdin.readline

def solution():
	is_palindrome = True
	for i in range(n // 2):
		if s[i] != s[-(i + 1)]:
			is_palindrome = False
			break
	if is_palindrome:
		print(1)
	elif k == 0:
		print(1)
	else:
		print(2)
t = int(input())
for tc in range(t):
	(n, k) = map(int, input().split())
	s = input().rstrip()
	solution()


def main():
	(n, k) = map(int, input().split())
	s = input()
	cur = {s}
	k = min(k, 3)
	for _ in range(k):
		news = set()
		for s in cur:
			news.add(s + s[::-1])
			news.add(s[::-1] + s)
		cur = news
	print(len(cur))
for _ in range(int(input())):
	main()


for i in range(int(input())):
	(a, b) = map(int, input().split())
	s = input()
	if s != s[::-1] and b > 0:
		print(2)
	else:
		print(1)


n = int(input())
for i in range(n):
	(m, n) = map(int, input().split())
	s = input()
	f = True
	if n == 0:
		print(1)
	else:
		for j in range(int(m / 2)):
			if s[j] != s[m - 1 - j]:
				f = False
		if f == True:
			print(1)
		else:
			print(2)


def isPalindrome(n):
	return n == n[::-1]
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	if k == 0:
		print(1)
	elif isPalindrome(s):
		print(1)
	else:
		print(2)


for _ in range(int(input())):
	(a, k) = list(map(int, input().split()))
	s = input()
	if k == 0:
		print(1)
	elif s == s[::-1]:
		print(1)
	else:
		print(2)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	x = list(input())
	if k > 0:
		if x == list(reversed(x)):
			print(1)
		else:
			print(2)
	else:
		print(1)


def solve(n, k, s):
	if k == 0:
		return 1
	if s[::-1] == s:
		return 1
	return 2
t = int(input())
for _ in range(t):
	(n, k) = (int(component) for component in input().split(' '))
	s = input()
	print(solve(n, k, s))


n = int(input().strip())
for j in range(n):
	(s, k) = input().strip().split()
	k = int(k)
	s = input().strip()
	flag = True
	for i in range(len(s) // 2):
		if s[i] != s[len(s) - 1 - i]:
			flag = False
	if flag or k == 0:
		print(1)
	else:
		print(2)


def pal(s):
	a = s[::-1]
	if s != a:
		return 1
	return 0
for t in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	ans = 1
	c = pal(s)
	if k != 0:
		print(c + ans)
	else:
		print(ans)


n = int(input())

def checkp(s):
	return s == s[::-1]
for i in range(n):
	x = input().split()
	a = int(x[0])
	b = int(x[1])
	s = input()
	if checkp(s):
		print('1')
		continue
	if b == 0:
		print('1')
		continue
	else:
		print('2')
		continue


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	if k == 0:
		print(1)
		continue
	is_pal = True
	for i in range(n // 2):
		if s[i] != s[n - i - 1]:
			is_pal = False
	if is_pal:
		print(1)
	else:
		print(2)


def concatRev(s, k):
	r = s[::-1]
	count = 1
	if s == r:
		return count
	else:
		return count + 1 if k >= 1 else count
t = int(input())
for i in range(t):
	(n, k) = list(map(int, input().split()))
	s = input()
	print(concatRev(s, k))


def function(n, k, s):
	res = 1
	if s != s[::-1]:
		res += 1
	if k == 0:
		return 1
	return res
t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	print(function(n, k, s))


t = int(input())

def rev(s: str) -> str:
	s1 = ''
	for i in range(len(s)):
		s1 += s[len(s) - 1 - i]
	return s1
for _ in range(t):
	x = str(input()).split()
	n = int(x[0])
	k = int(x[1])
	string = str(input())
	string1 = rev(string)
	if k == 0:
		print(1)
		continue
	elif k > 0:
		if string == string1:
			print(1)
			continue
		else:
			print(2)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = input()
	rev = ''
	for x in s:
		rev = x + rev
	if k == 0:
		print(1)
	elif rev == s:
		print(1)
	else:
		print(2)


t = int(input())
ans = []
for x in range(t):
	m = list(map(int, input().split()))
	n = m[0]
	k = m[1]
	s = list(input())
	s2 = s[:]
	s2.reverse()
	if s == s2 or k == 0:
		ans.append(1)
	else:
		ans.append(2)
for a in ans:
	print(a)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = [i for i in input()]
	rev = s[::-1]
	if k >= 1:
		s1 = s + rev
		s2 = rev + s
		if s1 == s2:
			print(1)
		else:
			print(2)
	else:
		print(1)


def rev(s1, k, l, h):
	if s1 == s1[::-1]:
		return 1
	if k == l:
		return 1
	if s1 in h:
		return h[s1]
	s = s1 + s1[::-1]
	s2 = s1[::-1] + s1
	h[s1] = rev(s, k + 1, l, h) + rev(s2, k + 1, l, h)
	return h[s1]
n = int(input())
for i in range(n):
	a = list(map(int, input().split()))
	l = a[1]
	s = input()
	h = {}
	print(rev(s, 0, l, h))


t = int(input())
while t:
	(a, b) = map(int, input().split())
	s = input()
	if b == 0:
		print(1)
	elif s == s[::-1]:
		print(1)
	else:
		print(2)
	t -= 1


tests_no = int(input())
for i in range(tests_no):
	user_input = input().split(' ')
	length_str = int(user_input[0])
	operate = int(user_input[1])
	inp_str = str(input())
	if inp_str == inp_str[::-1] or operate < 1:
		print(1)
	else:
		print(2)


def isPalindrome(s):
	return s == s[::-1]
for i in range(int(input())):
	(n, k) = map(int, input().split())
	st = input()
	ans = isPalindrome(st)
	if k == 0:
		print('1')
	elif ans:
		print('1')
	else:
		print('2')


def rev(S):
	return S[::-1]
for _ in range(int(input())):
	(n, k) = list(map(int, input().split(' ')))
	s = input()
	if k == 0 or rev(s) == s:
		print(1)
	else:
		print(2)


n = int(input())
for i in range(n):
	kt_list = list(map(int, input().split()))
	s1 = input()
	if kt_list[1] == 0:
		print('1')
	else:
		count = 0
		s2 = s1[::-1]
		if s1 == s2:
			print('1')
		else:
			print('2')


def main():
	t = int(input())
	for _ in range(t):
		(n, k) = map(int, input().split())
		s = input()
		i = 0
		while i < k:
			if s == s[::-1]:
				break
			s += s[::-1]
			i += 1
		print(i + 1)
main()


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	s = input()
	ans = True
	if k == 0:
		print(1)
	else:
		for w in range(n // 2):
			if s[w] != s[n - w - 1]:
				ans = False
				break
		if ans:
			print(1)
		else:
			print(2)


from audioop import reverse
for _ in range(int(input())):
	(n, k) = map(int, input().split(' '))
	s = input()
	revs = s[::-1]
	if s == revs or k == 0:
		print('1')
	else:
		print('2')


def isPalin(s):
	k = s[::-1]
	if s == k:
		return True
	return False
t = int(input())
while t != 0:
	(n, k) = map(int, input().split())
	s = input()
	if isPalin(s):
		print(1)
		t -= 1
		continue
	if k == 0:
		print(1)
	else:
		print(2)
	t -= 1


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = input()
	if k < 1:
		print('1')
	else:
		b = a[::-1]
		if a == b:
			print('1')
		else:
			print('2')


def solve(s, n, k):
	rev = s[::-1]
	if s == rev or k == 0:
		return 1
	else:
		return 2
t = int(input())
while t:
	(n, k) = map(int, input().split())
	s = input()
	print(solve(s, n, k))
	t -= 1


import sys
t = int(sys.stdin.readline())
for _ in range(t):
	(n, k) = map(int, sys.stdin.readline().split())
	s = sys.stdin.readline().strip()
	if k == 0 or s == s[::-1]:
		print(1)
	else:
		print(2)


tc = range(int(input()))
func = lambda : [int(q) for q in input().split()]

def ChkRev(str):
	if str == str[::-1]:
		return True
	else:
		return False
ans = []
for _ in tc:
	(n, k) = func()
	str = input().strip()
	if k == 0:
		ans.append(1)
		continue
	elif ChkRev(str):
		ans.append(1)
		continue
	else:
		ans.append(2)
		continue
for q in ans:
	print(q)


def isPalindrome(s: str):
	if s == s[::-1]:
		return True
	else:
		return False
test_cases = int(input())
for _ in range(test_cases):
	(strLen, operations) = map(int, input().split())
	string = input()
	if isPalindrome(string) or operations == 0:
		print(1)
	else:
		print(2)


testcases = int(input())
for _ in range(testcases):
	args = list(map(int, input().split()))
	(n, k) = (args[0], args[1])
	s = input()
	if k == 0:
		print('1')
	else:
		isPalin = True
		for i in range(len(s) // 2):
			if s[i] != s[n - i - 1]:
				isPalin = False
				break
		if isPalin:
			print(1)
		else:
			print(2)


t = int(input())
for i in range(t):
	z = input()
	a = int(z.split()[0])
	b = int(z.split()[1])
	s = input()
	if s == s[::-1] or b < 1:
		print(1)
	else:
		print(2)


def main():
	import sys
	inp = sys.stdin.readline
	test = int(input())
	while test:
		(n, m) = map(int, inp().split())
		s = inp().strip()
		if s == s[::-1] or m == 0:
			print(1)
		else:
			print(2)
		test -= 1
main()


n = int(input())
for i in range(n):
	aa = input().split()
	k = int(aa[1])
	a = input()
	if k == 0:
		print(1)
	elif a == a[::-1]:
		print(1)
	else:
		print(2)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	s = list(input())
	s1 = s[::-1]
	if s1 + s == s + s1:
		print(1)
		continue
	if k == 0:
		print(1)
		continue
	print(2)


l = int(input())
for i in range(l):
	c = 0
	(n, k) = map(int, input().split())
	s = input()
	t = s[::-1]
	f = (s + t) * k
	p = (t + s) * k
	if f != p:
		c += 2
	else:
		c += 1
	print(c)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	s = input()
	if k == 0:
		print(1)
	elif k == 1 and s == ''.join(list(reversed(s))):
		print(1)
	elif s == ''.join(list(reversed(s))):
		print(1)
	else:
		print(2)


test = int(input())

def reverse(s):
	str = ''
	for i in s:
		str = i + str
	return str
while test > 0:
	(x, y) = input().split()
	y = int(y)
	string = str(input())
	string1 = reverse(string)
	if y == 0:
		print('1')
	elif string1 == string:
		print('1')
	else:
		print('2')
	test = test - 1


def pallindrome(s: str):
	i = 0
	j = len(s) - 1
	while i <= j:
		if s[i] != s[j]:
			return False
		i += 1
		j -= 1
	return True
T = input()
for t in range(int(T)):
	l1 = input()
	n = int(l1.split()[0])
	k = int(l1.split()[1])
	s = input()
	if k == 0:
		print(1)
		continue
	if pallindrome(s):
		print(1)
	else:
		print(2)


for j in range(int(input())):
	alf = set()
	(n, k) = [int(i) for i in input().split()]
	inp = input()
	if k == 0:
		print(1)
		continue
	flag = True
	for i in range(len(inp) // 2):
		if inp[i] != inp[-1 - i]:
			flag = False
			break
	if flag:
		print(1)
	else:
		print(2)


t = int(input())
l = []
for i in range(t):
	(n1, k1) = input().split()
	n = int(n1)
	k = int(k1)
	q = input()
	if k == 0:
		l.append(1)
	elif q == q[::-1] and k >= 1:
		l.append(1)
	elif q != q[::-1] and k >= 1:
		l.append(2)
for j in l:
	print(j)


t = int(input())
for _ in range(t):
	comb = set()
	nk = str.split(input(), ' ')
	n = int(nk[0])
	k = int(nk[1])
	inp = input()
	if k == 0:
		print(1)
		continue
	if n % 2 == 1:
		upper = inp[len(inp) // 2 + 1:]
		lower = inp[0:len(inp) // 2]
		if upper[::-1] == lower:
			print(1)
		else:
			print(2)
	else:
		upper = inp[len(inp) // 2:]
		lower = inp[0:len(inp) // 2]
		if upper[::-1] == lower:
			print(1)
		else:
			print(2)


t = int(input())
while t != 0:
	t = t - 1
	(n, k) = map(int, input().split())
	str1 = input()
	if k == 0:
		print('1')
		continue
	if str1 == str1[::-1]:
		print('1')
	else:
		print('2')