Chef made $N$ pieces of cakes, numbered them $1$ through $N$ and arranged them in a row in this order. There are $K$ possible types of flavours (numbered $1$ through $K$); for each valid $i$, the $i$-th piece of cake has a flavour $A_i$.
Chef wants to select a contiguous subsegment of the pieces of cake such that there is at least one flavour which does not occur in that subsegment. Find the maximum possible length of such a subsegment of cakes.

-----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 integers $N$ and $K$.
- The second line contains $N$ space-separated integers $A_1, A_2, \ldots, A_N$.

-----Output-----
For each test case, print a single line containing one integer ― the maximum length of a valid subsegment.

-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le N \le 10^5$
- $2 \le K \le 10^5$
- $1 \le A_i \le K$ for each valid $i$
- the sum of $N$ over all test cases does not exceed $10^6$

-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- $K = 2$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints

-----Example Input-----
2
6 2
1 1 1 2 2 1
5 3
1 1 2 2 1

-----Example Output-----
3
5

Difficulty: MEDIUM_HARD

Solutions:
def longest(a, n, k):
	freq = [0] * n
	start = 0
	end = 0
	now = 0
	l = 0
	for i in range(n):
		freq[a[i]] += 1
		if freq[a[i]] == 1:
			now += 1
		while now > k:
			freq[a[l]] -= 1
			if freq[a[l]] == 0:
				now -= 1
			l += 1
		if i - l + 1 >= end - start + 1:
			end = i
			start = l
	return end - start + 1
for _ in range(int(input())):
	(n, k) = map(int, input().split(' '))
	lst = list(map(int, input().split(' ')))
	maxi = 0
	if k == 2:
		ctr = 0
		while True:
			ele = lst[ctr]
			count = 1
			while ctr != len(lst) - 1:
				ctr += 1
				if lst[ctr] != ele:
					break
				else:
					count += 1
			if count > maxi:
				maxi = count
			if ctr >= len(lst) - 1:
				break
		print(maxi)
	else:
		print(longest(lst, n, k - 1))


from sys import stdin, stdout
import math
from itertools import permutations, combinations
from collections import defaultdict
from bisect import bisect_left
import sys
mod = 1000000007

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

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

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

def printIn(ob):
	return stdout.write(str(ob) + '\n')

def powerLL(n, p):
	result = 1
	while p:
		if p & 1:
			result = result * n % mod
		p = int(p / 2)
		n = n * n % mod
	return result
import sys

def myCode():
	(n, k) = In()
	A = L()
	d = defaultdict(int)
	i = 0
	j = 0
	flavour = 0
	ans = 0
	for i in range(n):
		if d[A[i]] == 0:
			flavour += 1
		d[A[i]] += 1
		if flavour < k:
			ans = max(ans, i - j + 1)
		while flavour >= k:
			d[A[j]] -= 1
			if d[A[j]] == 0:
				flavour -= 1
			j += 1
	print(ans)

def main():
	for t in range(I()):
		myCode()
main()


import sys
from collections import defaultdict

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

def fout(s, end='\n'):
	sys.stdout.write(str(s) + end)
t = int(input())
while t > 0:
	t -= 1
	(n, k) = list(map(int, fin().split()))
	a = [int(x) for x in fin().split()]
	freq = defaultdict(int)
	freq[a[0]] += 1
	temp = set([a[0]])
	ans = 1
	prev = 0
	for i in range(1, n):
		freq[a[i]] += 1
		temp.add(a[i])
		if len(temp) < k:
			ans = max(ans, i - prev + 1)
		else:
			while len(temp) >= k:
				freq[a[prev]] -= 1
				if freq[a[prev]] == 0:
					temp.remove(a[prev])
				prev += 1
	print(ans)


for _ in range(int(input())):
	(n, k) = [int(i) for i in input().split()]
	a = [int(i) for i in input().split()]
	ind = [[] for i in range(k)]
	for i in range(n):
		ind[a[i] - 1].append(i)
	maxi = 0
	flag = 0
	for i in ind:
		if len(i) == 0:
			flag = 1
			break
		maxi = max(maxi, i[0])
		for j in range(1, len(i)):
			maxi = max(i[j] - i[j - 1] - 1, maxi)
		maxi = max(n - i[-1] - 1, maxi)
	if flag == 1:
		print(n)
	else:
		print(maxi)


for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	m = -1
	for i in range(n):
		flag = 0
		count = 1
		s = set()
		s.add(arr[i])
		for j in range(i + 1, n):
			if arr[j] not in s:
				if len(s) == k - 1:
					flag = 1
				else:
					s.add(arr[j])
					count += 1
			else:
				count += 1
			if flag == 1:
				break
		if count > m:
			m = count
	print(m)


for _ in range(int(input())):
	(m, n) = map(int, input().split())
	arr = list(map(int, input().split()))
	(i, j, res) = (0, 0, 0)
	s = set()
	while i < m and j < m:
		s.add(arr[j])
		if len(s) < n:
			res = max(res, j - i + 1)
			j += 1
		else:
			b = arr[i]
			i += 1
			if b not in arr[i:j + 1] and b in s:
				s.remove(b)
	print(res)


t = int(input())
while t > 0:
	x = 0
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	m = [0] * k
	p = 0
	for i in range(n):
		a = l[i]
		q = 0
		for j in range(p, n):
			m[l[j] - 1] += 1
			if 0 not in m:
				x = max(x, j - i)
				q = 1
				p = j
				m[l[j] - 1] -= 1
				break
		m[l[i] - 1] -= 1
		if q == 0:
			x = max(x, j - i + 1)
			p = n
	print(x)
	t -= 1


from sys import stdin
ip = stdin.readline
for _ in range(int(ip())):
	(n, k) = map(int, ip().split())
	a = [*map(int, ip().split())]
	if n == 1:
		print('1')
		continue
	m = [0] * (k + 1)
	k -= 1
	i = 0
	ans = 0
	prev = a[0]
	v = 1
	m[prev] = 1
	for j in range(1, n):
		val = a[j]
		if not prev == val:
			if m[val] == 0:
				v += 1
			m[val] += 1
		prev = a[j]
		if v > k:
			ans = max(ans, j - i)
			v -= 1
			curr = a[i]
			pr = curr
			while True:
				i += 1
				curr = a[i]
				if not pr == curr:
					m[pr] -= 1
				if m[pr] == 0:
					break
				pr = curr
	ans = max(ans, n - i)
	print(ans)


from collections import defaultdict as dd
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	lst = list(map(int, input().split()))
	dk = dd(int)
	cnt = mx = 0
	left = 0
	right = 0
	while right != n:
		dk[lst[right]] += 1
		if dk[lst[right]] == 1:
			cnt += 1
		while cnt == k:
			dk[lst[left]] -= 1
			mx = max(mx, right - left)
			if dk[lst[left]] == 0:
				cnt -= 1
			left += 1
		right += 1
	print(max(mx, right - left))


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	x = list(map(int, input().split()))
	l = 0
	r = 0
	ans = 0
	d = {i + 1: 0 for i in range(k)}
	s = set()
	while l <= r < n:
		if len(s) != k:
			s.add(x[r])
			d[x[r]] += 1
			if len(s) != k:
				ans = max(ans, r - l + 1)
			r += 1
		else:
			d[x[l]] -= 1
			if d[x[l]] == 0:
				s.remove(x[l])
			l += 1
	print(ans)


import sys

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

def Strs():
	return map(str, sys.stdin.readline().strip().split())

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

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

def Int():
	return int(sys.stdin.readline().strip())

def MOD():
	return 1000000007

def notAll(arr, N, k):
	counter = [0] * (N + 5)
	current = 0
	ans = 0
	start = end = 0
	for i in range(N):
		counter[arr[i]] += 1
		if counter[arr[i]] == 1:
			current += 1
		while current > k:
			counter[arr[start]] -= 1
			if counter[arr[start]] == 0:
				current -= 1
			start += 1
		ans = max(ans, abs(i - start + 1))
	return ans
t = Int()
while t:
	(N, K) = Ints()
	arr = Array()
	print(notAll(arr, N, K - 1))
	t -= 1


t = int(input())
for _ in range(t):
	(n, k) = list(map(int, input().split()))
	f = list(map(int, input().split()))
	hashmap = {i: 0 for i in range(1, k + 1)}
	l = 0
	r = 0
	res = 1
	c = 0
	while r < len(f):
		hashmap[f[r]] += 1
		while not 0 in hashmap.values():
			hashmap[f[l]] -= 1
			l += 1
		c = r - l + 1
		res = max(res, c)
		r += 1
	print(res)


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	freq = {}
	set1 = set(l)
	for i in list(set1):
		freq[i] = 0
	diff = 0
	ans = []
	i = 0
	j = 0
	while i < n and j < n:
		freq[l[i]] += 1
		if freq[l[i]] == 1:
			diff += 1
		while diff >= k:
			freq[l[j]] -= 1
			if freq[l[j]] == 0:
				diff -= 1
			j += 1
		ans.append(i - j + 1)
		i += 1
	if len(ans) > 0:
		print(max(ans))
	else:
		print(n)


for _ in range(int(input())):
	(n_31809, k_31809) = map(int, input().split())
	l = list(map(int, input().split()))
	d = [0] * k_31809
	(s_31809, m_31809, j_31809) = (0, 0, 0)
	for i in range(n_31809):
		if d[l[i] - 1] == 0:
			s_31809 += 1
		d[l[i] - 1] += 1
		while s_31809 >= k_31809:
			d[l[j_31809] - 1] -= 1
			if d[l[j_31809] - 1] == 0:
				s_31809 -= 1
			j_31809 += 1
		m_31809 = max(i - j_31809 + 1, m_31809)
	print(m_31809)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	se = set()
	(i, j, mx) = (0, 0, 0)
	while i < n and j < n:
		se.add(a[j])
		if len(se) < k:
			mx = max(mx, j - i + 1)
			j = j + 1
		else:
			d = a[i]
			i = i + 1
			if d not in a[i:j + 1]:
				se.remove(d)
	print(mx)


from sys import stdin, stdout
t = int(stdin.readline())
for _ in range(t):
	(n, k) = map(int, stdin.readline().split())
	cakes = list(map(int, stdin.readline().split()))
	last = [-1] * k
	max_ans = -1
	last_min_ind = 0
	for r in range(n):
		last[cakes[r] - 1] = r
		if cakes[r] - 1 == last_min_ind:
			last_min_ind = last.index(min(last))
		ans = r - last[last_min_ind]
		max_ans = max(max_ans, ans)
	print(max_ans)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	(i, j) = (0, 0)
	res = 0
	s = set()
	while j < n and i < n:
		s.add(arr[j])
		if len(s) >= k:
			ele = arr[i]
			i += 1
			if ele not in arr[i:j + 1]:
				s.remove(ele)
		else:
			res = max(res, j + 1 - i)
			j += 1
	print(res)


for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	p = 0
	sm = 0
	ans = 0
	d = dict()
	for i in range(n):
		d[a[i]] = d.get(a[i], 0) + 1
		if d[a[i]] == 1:
			sm += 1
		if sm == k:
			ans = max(ans, i - p)
			while sm == k:
				d[a[p]] = d[a[p]] - 1
				p += 1
				if d[a[p - 1]] == 0:
					sm -= 1
					break
	ans = max(ans, n - p)
	if ans == 0:
		ans = n
	print(ans)


from collections import defaultdict
t = int(input())
while t > 0:
	(n, k) = map(int, input().split())
	array = list(map(int, input().split()))
	d = defaultdict(int)
	ans = 1
	(i, j) = (0, 0)
	while j < n:
		if d[array[j]] == 0:
			k -= 1
		d[array[j]] += 1
		while k == 0:
			d[array[i]] -= 1
			if d[array[i]] == 0:
				k += 1
			i += 1
		ans = max(ans, j - i + 1)
		j += 1
	print(ans)
	t -= 1


t = int(input())
while t:
	(d, j, ans) = ({}, 0, 0)
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	for i in range(n):
		if arr[i] in d:
			d[arr[i]] = i
		elif len(d) == k - 1:
			(m, mk) = (1000000000, 0)
			for (key, val) in d.items():
				if val < m:
					m = val
					mk = key
			j = d[mk] + 1
			del d[mk]
			d[arr[i]] = i
		elif len(d) < k - 1:
			d[arr[i]] = i
		ans = max(ans, i - j + 1)
	print(ans)
	t -= 1


t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	c = [0] * (k + 1)
	c2 = 0
	res = 0
	r = 0
	for j in range(n):
		while r < n and (c[a[r]] > 0 or c2 < k - 1):
			if c[a[r]] == 0:
				c2 += 1
			c[a[r]] += 1
			r += 1
		res = max(res, r - j)
		c[a[j]] -= 1
		if c[a[j]] == 0:
			c2 -= 1
	print(res)


for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	(i, j) = (0, 0)
	c_set = set()
	ans = 0
	while i < n and j < n:
		c_set.add(arr[j])
		if len(c_set) <= k - 1:
			ans = max(ans, j - i + 1)
			j += 1
		else:
			crnt = arr[i]
			i += 1
			if crnt not in arr[i:j + 1] and crnt in c_set:
				c_set.remove(crnt)
	print(ans)


n = int(input())
for i in range(n):
	(a, b) = [int(i) for i in input().split()]
	l = [int(i) for i in input().split()]
	start = 0
	end = 0
	d = []
	answer = []
	for j in range(0, len(l)):
		d.append(l[j])
		end = end + 1
		if len(set(d)) < b:
			pass
		else:
			end = end - 1
			start = end
			while len(set(d)) >= b:
				d.pop(0)
		answer.append(len(d))
	print(max(answer))


t = int(input())
for item in range(t):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	a = [0 for i in range(k)]
	count = 0
	i = 0
	j = 0
	max_length = 0
	while i < n:
		if count == k - 1 and a[arr[i] - 1] == 0:
			a[arr[j] - 1] -= 1
			if a[arr[j] - 1] == 0:
				count -= 1
			j += 1
		else:
			if a[arr[i] - 1] > 0:
				a[arr[i] - 1] += 1
			else:
				a[arr[i] - 1] += 1
				count += 1
			i += 1
		cl = i - j
		max_length = max(max_length, cl)
	print(max_length)


def answer():
	c = [0] * (k + 1)
	(res, d, length) = (0, 0, 0)
	ind = 0
	for v in a:
		if c[v] == 0:
			d += 1
			c[v] += 1
			if d == k:
				while 1:
					c[a[ind]] -= 1
					length -= 1
					if c[a[ind]] == 0:
						ind += 1
						break
					else:
						ind += 1
				d -= 1
			c[v] -= 1
		length += 1
		res = max(res, length)
		c[v] += 1
	return res
for T in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	print(answer())


for T in range(int(input())):
	(n, k) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	if len(set(a)) < k:
		print(len(a))
	else:
		(f, t, c) = ([], [], 1)
		key = a[0]
		for i in range(1, n):
			if a[i] == key:
				c += 1
			else:
				f.append(c)
				t.append(key)
				key = a[i]
				c = 1
		f.append(c)
		t.append(key)
		(check, uni, start, i) = ([], 0, 0, 0)
		(l, m) = (0, 0)
		while i < len(t) and start < len(t):
			if t[i] not in check:
				uni += 1
			if uni == k:
				m = max(m, l)
				check.remove(t[start])
				if t[start] not in check:
					uni -= 1
				uni -= 1
				l -= f[start]
				start += 1
				continue
			check.append(t[i])
			l += f[i]
			i += 1
		m = max(m, l)
		print(m)


def main():
	t = int(input())
	while t > 0:
		(n, k) = map(int, input().split())
		arr = list(map(int, input().split()))
		freq = [0] * 100001
		k = k - 1
		st = 0
		end = 0
		currentcount = 0
		prevelement = 0
		for i in range(n):
			freq[arr[i]] += 1
			if freq[arr[i]] == 1:
				currentcount += 1
			while currentcount > k:
				freq[arr[prevelement]] -= 1
				if freq[arr[prevelement]] == 0:
					currentcount -= 1
				prevelement += 1
			if i - prevelement + 1 >= end - st + 1:
				end = i
				st = prevelement
		print(end - st + 1)
		t = t - 1
main()


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	s = set()
	ans = 0
	start = 0
	dic = dict()
	for i in range(n):
		if arr[i] in s:
			dic[arr[i]] += 1
		else:
			s.add(arr[i])
			dic[arr[i]] = 1
		if len(s) == k:
			while len(s) == k:
				dic[arr[start]] -= 1
				if dic[arr[start]] == 0:
					s.remove(arr[start])
				start += 1
		ans = max(ans, i - start + 1)
	print(ans)


t = int(input())
for i in range(t):
	(n, k) = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	count = 0
	unique = 0
	path = 1
	d = {}
	j = 0
	for i in range(n):
		if arr[i] in d:
			if d[arr[i]] == 0:
				unique = unique + 1
			count = count + 1
			d[arr[i]] = d[arr[i]] + 1
		else:
			unique = unique + 1
			count = count + 1
			d[arr[i]] = 1
		if unique == k:
			path = max(path, count - 1)
			while unique == k and j < i:
				if d[arr[j]] == 1:
					unique = unique - 1
				d[arr[j]] = d[arr[j]] - 1
				count = count - 1
				j = j + 1
	if unique == k:
		path = max(path, count - 1)
	else:
		path = max(path, count)
	print(path)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	s = []
	for _ in range(k):
		s.append(0)
	res = 0
	r = 1
	t = 1
	f = 0
	for i in range(1, n):
		if l[i] != l[i - 1]:
			s[l[i - 1] - 1] = i
			if s[l[i] - 1] == f:
				t = t + 1
			s[l[i] - 1] = i + 1
			if t == k:
				if r > res:
					res = r
				r = r - min(s) + f
				t = t - 1
				f = min(s)
			r = r + 1
		else:
			r = r + 1
	if r > res:
		res = r
	print(res)


def main():
	t = int(input())
	while t != 0:
		(n, k) = map(int, input().split())
		arr = list(map(int, input().split()))
		freq = [0] * 100001
		k = k - 1
		st = 0
		end = 0
		currentCount = 0
		previousElement = 0
		for i in range(n):
			freq[arr[i]] += 1
			if freq[arr[i]] == 1:
				currentCount += 1
			while currentCount > k:
				freq[arr[previousElement]] -= 1
				if freq[arr[previousElement]] == 0:
					currentCount -= 1
				previousElement += 1
			if i - previousElement + 1 >= end - st + 1:
				end = i
				st = previousElement
		print(end - st + 1)
		t = t - 1

def __starting_point():
	main()
__starting_point()


for i in range(int(input())):
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	(i, j, ans) = (0, 0, 0)
	s = set()
	while j < n:
		s.add(l[j])
		if len(s) < k:
			ans = max(ans, j + 1 - i)
			j += 1
		else:
			x = l[i]
			while i < n and l[i] == x:
				i += 1
			if x not in l[i:j + 1]:
				s.remove(x)
	print(ans)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	arr = [int(x) for x in input().split()]
	freq = [0] * (k + 1)
	(i, j) = (0, 0)
	(max_seg, flav) = (0, 0)
	while i < n:
		if freq[arr[i]] == 0:
			flav += 1
		freq[arr[i]] += 1
		if flav == k:
			if i - j > max_seg:
				max_seg = i - j
			while j <= i:
				freq[arr[j]] -= 1
				j += 1
				if freq[arr[j - 1]] == 0:
					flav -= 1
					break
		i += 1
	if i - j > max_seg:
		max_seg = i - j
	print(max_seg)


from collections import defaultdict
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	lst = list(map(int, input().split()))
	d = defaultdict(int)
	(i, j) = (0, 0)
	ans = 0
	while j < n:
		d[lst[j]] += 1
		while len(d) == k:
			d[lst[i]] -= 1
			if d[lst[i]] == 0:
				del d[lst[i]]
			i += 1
		j += 1
		ans = max(ans, j - i)
	print(ans)


def sol(arr, n, k):
	m = -1
	for i in range(n):
		flag = 0
		count = 1
		s = set()
		s.add(arr[i])
		temp = i
		for j in range(i + 1, n):
			if arr[j] not in s:
				if len(s) == k - 1:
					flag = 1
				else:
					s.add(arr[j])
					count += 1
			else:
				count += 1
			if flag == 1:
				break
		if count > m:
			m = count
	return m
for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	arr = list(map(int, input().split()))
	if k == 2:
		mi = sol(arr, n, k)
		print(mi)
		continue
	freq = [0] * n
	i = 0
	j = 0
	c = 0
	l = 0
	mx = -1
	k -= 1
	for x in range(n):
		freq[arr[x]] += 1
		if freq[arr[x]] == 1:
			c += 1
		while c > k:
			freq[arr[l]] -= 1
			if freq[arr[l]] == 0:
				c -= 1
			l += 1
		if x - l + 1 >= j - i + 1:
			j = x
			i = l
	print(j - i + 1)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	c = [0] * (k + 1)
	c2 = 0
	ans = 0
	r = 0
	for l in range(n):
		while r < n and (c[a[r]] > 0 or c2 < k - 1):
			if c[a[r]] == 0:
				c2 += 1
			c[a[r]] += 1
			r += 1
		ans = max(ans, r - l)
		c[a[l]] -= 1
		if c[a[l]] == 0:
			c2 -= 1
	print(ans)


for i in range(int(input())):
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	(j, i, mx) = (0, 0, 0)
	d = dict()
	while j < n:
		if l[j] in d:
			d[l[j]] += 1
		else:
			d[l[j]] = 1
		if len(d) <= k - 1:
			mx = max(mx, j - i + 1)
			j += 1
		else:
			while len(d) > k - 1:
				d[l[i]] -= 1
				if d[l[i]] == 0:
					d.pop(l[i])
				i += 1
			j += 1
	print(mx)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().strip().split())
	arr = list(map(int, input().strip().split()))[:n]
	used_arr = [0 for i in range(k)]
	count = 0
	i = 0
	j = 0
	max_length = 0
	while i < n:
		if count == k - 1 and used_arr[arr[i] - 1] == 0:
			used_arr[arr[j] - 1] -= 1
			if used_arr[arr[j] - 1] == 0:
				count -= 1
			j += 1
		else:
			if used_arr[arr[i] - 1] > 0:
				used_arr[arr[i] - 1] += 1
			else:
				used_arr[arr[i] - 1] += 1
				count += 1
			i += 1
		curr_length = i - j
		max_length = max(max_length, curr_length)
	print(max_length)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	lis = list(map(int, input().split()))
	se = set()
	ma = max(lis)
	co = [0 for _ in range(ma + 1)]
	se.add(lis[0])
	co[lis[0]] += 1
	i1 = 0
	mal = 1
	for i2 in range(1, n):
		se.add(lis[i2])
		co[lis[i2]] += 1
		while len(se) == k:
			if co[lis[i1]] == 1:
				se.remove(lis[i1])
				co[lis[i1]] -= 1
				i1 += 1
			else:
				co[lis[i1]] -= 1
				i1 += 1
		if i2 - i1 > mal - 1:
			mal = i2 - i1 + 1
	print(mal)


t = int(input())
for _ in range(t):
	(n, k) = map(int, input().split())
	arr = list(map(int, input().split()))
	i = 0
	maxi = 0
	visited = set()
	count = 0
	sec = -1
	f = 0
	while i < n:
		visited.add(arr[i])
		if len(visited) == 2 and f == 0:
			sec = i
			f = 1
		if len(visited) == k:
			maxi = max(count, maxi)
			count = 1
			i = sec
			visited = {arr[i]}
			f = 0
		else:
			count = count + 1
		i = i + 1
	maxi = max(count, maxi)
	print(maxi)


from collections import Counter
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	c = Counter()
	i = t = ans = 0
	for j in range(n):
		if c[a[j]] == 0:
			t += 1
		c[a[j]] += 1
		while t == k and i <= j:
			c[a[i]] -= 1
			if c[a[i]] == 0:
				t -= 1
			i += 1
		ans = max(ans, j - i + 1)
	print(ans)


t = int(input())
while t > 0:
	(n, k) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	l = []
	mx = -1000000000
	start = 0
	for end in range(n):
		l.append(a[end])
		while len(set(l)) >= k:
			l.pop(0)
			start += 1
		if end - start + 1 > mx:
			mx = end - start + 1
	print(mx)
	t = t - 1


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	lst = list(map(int, input().split()))
	ptr1 = 0
	ptr2 = 0
	dt = dict()
	maxlen = 0
	while ptr2 < n:
		if len(dt) < k and ptr2 == n - 1:
			if lst[ptr2] in dt:
				dt[lst[ptr2]] += 1
			else:
				dt[lst[ptr2]] = 1
			if len(dt) == k:
				maxlen = max(maxlen, ptr2 - ptr1)
			else:
				maxlen = max(maxlen, ptr2 - ptr1 + 1)
			break
		elif len(dt) < k:
			if lst[ptr2] in dt:
				dt[lst[ptr2]] += 1
			else:
				dt[lst[ptr2]] = 1
			ptr2 += 1
		else:
			maxlen = max(maxlen, ptr2 - ptr1 - 1)
			dt[lst[ptr1]] -= 1
			if dt[lst[ptr1]] <= 0:
				dt.pop(lst[ptr1])
			ptr1 += 1
	print(maxlen)


def search(l, n, k):
	r = []
	o = []
	p = []
	t = []
	e = []
	for i in range(k):
		o.append(-1)
	for i in range(n):
		r.append(i)
		for j in range(1, k + 1, 1):
			if l[i] == j:
				o.append(i)
			else:
				o.append(o[len(o) - k])
	for i in range(k, len(o), k):
		for j in range(k):
			p.append(o[i + j])
		q = min(p)
		t.append(q)
		p.clear()
	for i in range(len(r)):
		y = r[i] - t[i]
		e.append(y)
	print(max(e))
t = int(input())
for i in range(t):
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	search(l, n, k)


for u in range(int(input())):
	(n, k) = map(int, input().split())
	l = list(map(int, input().split()))
	d = [0] * k
	(s, m, j) = (0, 0, 0)
	for i in range(n):
		if d[l[i] - 1] == 0:
			s += 1
		d[l[i] - 1] += 1
		while s >= k:
			d[l[j] - 1] -= 1
			if d[l[j] - 1] == 0:
				s -= 1
			j += 1
		m = max(i - j + 1, m)
	print(m)


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	l = [int(x) for x in input().split()]
	t = set()
	(i, j, a) = (0, 0, 0)
	while i < n and j < n:
		t.add(l[j])
		if len(t) < k:
			a = max(a, j + 1 - i)
			j = j + 1
		else:
			d = l[i]
			i = i + 1
			if d not in l[i:j + 1]:
				t.remove(d)
	print(a)


from collections import Counter
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = [*map(int, input().split())]
	j = 0
	r = 0
	ans = 0
	x = Counter([])
	for i in range(n):
		if x[a[i]] == 0:
			r += 1
		x[a[i]] += 1
		while r == k:
			x[a[j]] -= 1
			if x[a[j]] == 0:
				r -= 1
			j += 1
		ans = max(i - j + 1, ans)
	print(ans)


for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	l = list(map(int, input().split()))
	(ans, temp) = (0, 0)
	if k == 2:
		l1 = [0, 0]
		for i in l:
			l1[i - 1] = 1
			temp += 1
			if l1 == [1, 1]:
				ans = max(ans, temp - 1)
				l1 = [0, 0]
				l1[i - 1] = 1
				temp = 1
	else:
		l1 = set()
		for i in range(n):
			l1.add(l[i])
			temp = 1
			for j in range(i + 1, n):
				if l[j] in l1:
					temp += 1
				elif len(l1) == k - 1:
					l1 = set()
					ans = max(ans, temp)
					break
				else:
					l1.add(l[j])
					temp += 1
			ans = max(ans, temp)
	print(max(ans, temp))


n = int(input())
for i in range(n):
	(a, b) = [int(i) for i in input().split()]
	l = [int(i) for i in input().split()]
	start = 0
	end = 0
	d = []
	min = -10 ** 9
	for j in range(0, len(l)):
		d.append(l[j])
		end = end + 1
		if len(set(d)) < b:
			pass
		else:
			end = end - 1
			start = end
			while len(set(d)) >= b:
				d.pop(0)
		if len(d) > min:
			min = len(d)
	print(min)


for _ in range(int(input())):
	(n, k) = list(map(int, input().split()))
	l = list(map(int, input().split()))
	l1 = [0] * k
	l2 = [1] * k
	(ans, temp) = (0, 0)
	for i in l:
		l1[i - 1] = 1
		temp += 1
		if l1 == l2:
			ans = max(ans, temp - 1)
			l1 = [0] * k
			l1[i - 1] = 1
			temp = 1
	print(max(ans, temp))


for _ in range(int(input())):
	(n, k) = map(int, input().split())
	a = list(map(int, input().split()))
	if len(set(a)) < k:
		print(len(a))
	else:
		d = {}
		l = []
		for i in range(1, k + 1):
			d[i] = []
		for i in range(n):
			d[a[i]].append(i)
		for i in range(1, k + 1):
			d1 = []
			d1.append(d[i][0])
			d1.append(n - d[i][-1] - 1)
			for j in range(len(d[i]) - 1):
				x = d[i][j + 1] - d[i][j] - 1
				d1.append(x)
			l.append(max(d1))
		print(max(l))


for _ in range(int(input())):
	ip = list(map(int, input().split()))
	n = ip[0]
	k = ip[1]
	l = list(map(int, input().split()))
	d = {}
	i = 0
	start = 0
	end = 0
	j = 0
	m = 0
	while j < n:
		d.setdefault(l[j], 0)
		d[l[j]] += 1
		if len(d) >= k:
			start = i
			end = j
			if d[l[i]] > 1:
				d[l[i]] -= 1
			else:
				d.pop(l[i])
			if d[l[j]] > 1:
				d[l[j]] -= 1
			else:
				d.pop(l[j])
			j -= 1
			i += 1
		start = i
		end = j + 1
		if m <= end - start:
			m = end - start
		j += 1
	print(m)


t = int(input())
for k in range(t):
	(a, b) = map(int, input().split())
	li = list(map(int, input().split()))
	(i, j, re) = (0, 0, 0)
	st = set()
	while i < a and j < a:
		st.add(li[j])
		if len(st) < b:
			re = max(re, j - i + 1)
			j += 1
		else:
			m = li[i]
			i += 1
			if m not in li[i:j + 1] and m in st:
				st.remove(m)
	print(re)


t = int(input())
while t:
	(n, k) = map(int, input().split())
	freq = [0] * (k + 1)
	l = list(map(int, input().split()))
	i = 0
	j = 0
	max_seg = 0
	flavours = 0
	seg = 0
	while i < n:
		if freq[l[i]] == 0:
			flavours += 1
		freq[l[i]] += 1
		if flavours == k:
			if i - j > max_seg:
				max_seg = i - j
			while j <= i:
				freq[l[j]] -= 1
				j += 1
				if freq[l[j - 1]] == 0:
					flavours -= 1
					break
		i += 1
	if i - j > max_seg:
		max_seg = i - j
	print(max_seg)
	t -= 1


from collections import defaultdict
for _ in range(int(input())):
	(n, k) = map(int, input().split())
	li = list(map(int, input().split()))
	(maxl, e, s) = (0, 0, 0)
	d = defaultdict(int)
	while e < n:
		d[li[e]] += 1
		while len(d) == k:
			d[li[s]] -= 1
			if d[li[s]] == 0:
				del d[li[s]]
			s += 1
		e += 1
		maxl = max(maxl, e - s)
	print(maxl)


import sys

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

def Strs():
	return map(str, sys.stdin.readline().strip().split())

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

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

def Int():
	return int(sys.stdin.readline().strip())

def MOD():
	return 1000000007

def notAll(a, N, K):
	K -= 1
	freq = [0] * (N + 100)
	start = 0
	end = 0
	now = 0
	l = 0
	for i in range(N):
		freq[a[i]] += 1
		if freq[a[i]] == 1:
			now += 1
		while now > K:
			freq[a[l]] -= 1
			if freq[a[l]] == 0:
				now -= 1
			l += 1
		if i - l + 1 >= end - start + 1:
			end = i
			start = l
	ans = 0
	for i in range(start, end + 1):
		ans += 1
	return ans
t = Int()
while t:
	(N, K) = Ints()
	arr = Array()
	print(notAll(arr, N, K))
	t -= 1