ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 296 ms):
from collections import Counter
def solve(a,n,k):
    a.sort()
    cnt = Counter(a)
    counts = sorted(cnt.values())
    m = len(counts)

    for i in range(m - 1):
        if counts[i] > k:
            print(m - i)
            return
        k -= counts[i]
    
    print(1)
t=int(input())
for i in range(t):
    n,k=map(int,input().split())
    a=list(map(int,input().split()))
    solve(a,n,k)


Solution 2 (runtime: 312 ms):
import bisect
t=int(input())
for i in range(t):
    n,k=map(int,input().split())
    l=list(map(int,input().split()))
    l.sort()
    f=[]
    c=1
    for i in range(1,n):
        if(l[i]==l[i-1]):
            c+=1
        else:
            f.append(c)
            c=1
    f.append(c)
    f.sort()
    x=0

    pf=[]
    for j in f:
        x+=j
        pf.append(x)
    v=bisect.bisect_left(pf,k)
    y=0
    #print(pf,v)
    if(v==n):
        print(1)
    elif(v==0):
        if(k!=0):
            if(k<pf[0]):
                y=1
                print(max(1,len(f)-1-v+y))
            else:
                print(max(1,len(f)-1-v))
        else:
            print(len(f))
    elif(k<pf[v]):
        y=1
        print(len(f)-1-v+y)
    
    else:
        print(max(1,len(f)-1-v))
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 296 ms):
from collections import Counter
def solve(a,n,k):
    a.sort()
    cnt = Counter(a)
    counts = sorted(cnt.values())
    m = len(counts)

    for i in range(m - 1):
        if counts[i] > k:
            print(m - i)
            return
        k -= counts[i]
    
    print(1)
t=int(input())
for i in range(t):
    n,k=map(int,input().split())
    a=list(map(int,input().split()))
    solve(a,n,k)


Solution 2 (runtime: 187 ms):
import sys
from collections import Counter

input = sys.stdin.readline
output = sys.stdout.write
str_input = lambda: input().strip()
int_input = lambda: int(input())
multi_int_input = lambda: map(int, input().split())
list_input = lambda: list(map(int, input().split()))
P1 = lambda X: result_array.append(f"{X}\n")
P2 = lambda X,Y: result_array.append(f"{X} {Y}\n")
P3 = lambda X,Y,Z: result_array.append(f"{X} {Y} {Z}\n")
PA = lambda X: result_array.append(f"{' '.join(map(str,X))}\n")


def solve(test_case):
    N,K = multi_int_input()
    arr = list_input()

    check = Counter()
    for i in arr:
        check[str(i)] += 1
    check = sorted(check.values(),reverse=True)

    while check:
        if check[-1] <= K:
            K -= check.pop()
        else:
            break
    
    P1(max(1,len(check)))


tc = 1
tc = int_input()
result_array = []
for test_case in range(1,tc+1):
    solve(test_case)
output(''.join(result_array))
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 296 ms):
from collections import Counter
def solve(a,n,k):
    a.sort()
    cnt = Counter(a)
    counts = sorted(cnt.values())
    m = len(counts)

    for i in range(m - 1):
        if counts[i] > k:
            print(m - i)
            return
        k -= counts[i]
    
    print(1)
t=int(input())
for i in range(t):
    n,k=map(int,input().split())
    a=list(map(int,input().split()))
    solve(a,n,k)


Solution 2 (runtime: 249 ms):
t = int(input())
while t:
	t -= 1
	n, k =  map(int, input().split())
	ordem = {}
	array = list(map(int, input().split()))
	array.sort()
	freq = []

	prev = array[0]
	count = 0
	for num in array:
		if num != prev:
			freq.append(count)
			count = 0
		count += 1
		prev = num		
	freq.append(count)
	freq.sort()
	
	sum = 0
	count = 0
	for num in freq:
		sum += num
		if k >= sum:
			count += 1
		else:
			break
	
	print(max(1, len(freq)-count))
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 296 ms):
from collections import Counter
def solve(a,n,k):
    a.sort()
    cnt = Counter(a)
    counts = sorted(cnt.values())
    m = len(counts)

    for i in range(m - 1):
        if counts[i] > k:
            print(m - i)
            return
        k -= counts[i]
    
    print(1)
t=int(input())
for i in range(t):
    n,k=map(int,input().split())
    a=list(map(int,input().split()))
    solve(a,n,k)


Solution 2 (runtime: 249 ms):
for _ in range(int(input())):
  n, k = list(map(int, input().split()))
  a = list(map(int, input().split()))
  
  a.sort()
  # store the freq of nums
  cnt = [1]
  for i in range(1, n):
    if a[i] == a[i-1]:
      cnt[-1] += 1
    else:
      cnt.append(1)
  
  # sort the cnts to check with k and convert them to min
  cnt.sort()
  m = len(cnt)
  flag = False
  for i in range(m-1):
    if cnt[i] > k:
      # we can't change any ai so
      print(m-i)
      flag = True
      break
    
    k -= cnt[i]
  
  if flag == False:
    print(1)
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 171 ms):
n = int(input())
for _ in range(n):
    l, k = map(int, input().split())
    input_str = input()
    arr = list(map(int, input_str.split()))

    # 先排序
    arr.sort()

    # 存储不同的数字有几组
    count = []
    curnum = 1

    for i in range(1, l):
        if arr[i] == arr[i - 1]:
            curnum += 1
        else:
            count.append(curnum)
            curnum = 1
    count.append(curnum)

    count.sort()
    sum = 0
    ans = len(count)
    for c in count:
        if ans == 1:
            break
        sum += c
        if k >= sum:
            ans -= 1
        else:
            break
    print(ans)


Solution 2 (runtime: 140 ms):
if __name__ == "__main__":
    tc = 0
    tc = int(input())
    lres = []
    for i in range(tc):
        nkRaw = input()
        n = int(nkRaw.split(" ")[0])
        k = int(nkRaw.split(" ")[1])
        minimum = 0
        maximum = 1
        arrayRaw = input()
        array = arrayRaw.split(" ")
        diccionario = {}
        for num in range(int(n)):
            if(array[num] not in diccionario):
                diccionario[array[num]] = 1
            else:
                diccionario[array[num]] += 1;
        if(k >= n):
            lres.append(1)
        else:
            lista_diccionario = sorted(diccionario.values())
            aux = 0
            for num in lista_diccionario:
                if(num <= k):
                    k -= num
                    aux += 1
                else:
                    break
            lres.append(len(lista_diccionario) - aux)
    for res in lres:
        print(res)
#end
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 171 ms):
n = int(input())
for _ in range(n):
    l, k = map(int, input().split())
    input_str = input()
    arr = list(map(int, input_str.split()))

    # 先排序
    arr.sort()

    # 存储不同的数字有几组
    count = []
    curnum = 1

    for i in range(1, l):
        if arr[i] == arr[i - 1]:
            curnum += 1
        else:
            count.append(curnum)
            curnum = 1
    count.append(curnum)

    count.sort()
    sum = 0
    ans = len(count)
    for c in count:
        if ans == 1:
            break
        sum += c
        if k >= sum:
            ans -= 1
        else:
            break
    print(ans)


Solution 2 (runtime: 187 ms):
for q in range(int(input())):
    n,k=list(map(int,input().split(' ')))
    a=list(map(int,input().split(' ')))
    b=set(a)
    d={}
    for i in b:
        d[i]=0
    for i in a:
        d[i]+=1
    l=[]
    for i in d:
        l.append(d[i])
    pre=0
    ans=len(l)
    l.sort()
    for i in range(len(l)):
        if(pre+l[i]<=k):
            pre+=l[i]
            ans-=1
    print(max(ans,1))
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 171 ms):
n = int(input())
for _ in range(n):
    l, k = map(int, input().split())
    input_str = input()
    arr = list(map(int, input_str.split()))

    # 先排序
    arr.sort()

    # 存储不同的数字有几组
    count = []
    curnum = 1

    for i in range(1, l):
        if arr[i] == arr[i - 1]:
            curnum += 1
        else:
            count.append(curnum)
            curnum = 1
    count.append(curnum)

    count.sort()
    sum = 0
    ans = len(count)
    for c in count:
        if ans == 1:
            break
        sum += c
        if k >= sum:
            ans -= 1
        else:
            break
    print(ans)


Solution 2 (runtime: 312 ms):
import bisect
t=int(input())
for i in range(t):
    n,k=map(int,input().split())
    l=list(map(int,input().split()))
    l.sort()
    f=[]
    c=1
    for i in range(1,n):
        if(l[i]==l[i-1]):
            c+=1
        else:
            f.append(c)
            c=1
    f.append(c)
    f.sort()
    x=0

    pf=[]
    for j in f:
        x+=j
        pf.append(x)
    v=bisect.bisect_left(pf,k)
    y=0
    #print(pf,v)
    if(v==n):
        print(1)
    elif(v==0):
        if(k!=0):
            if(k<pf[0]):
                y=1
                print(max(1,len(f)-1-v+y))
            else:
                print(max(1,len(f)-1-v))
        else:
            print(len(f))
    elif(k<pf[v]):
        y=1
        print(len(f)-1-v+y)
    
    else:
        print(max(1,len(f)-1-v))
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 171 ms):
n = int(input())
for _ in range(n):
    l, k = map(int, input().split())
    input_str = input()
    arr = list(map(int, input_str.split()))

    # 先排序
    arr.sort()

    # 存储不同的数字有几组
    count = []
    curnum = 1

    for i in range(1, l):
        if arr[i] == arr[i - 1]:
            curnum += 1
        else:
            count.append(curnum)
            curnum = 1
    count.append(curnum)

    count.sort()
    sum = 0
    ans = len(count)
    for c in count:
        if ans == 1:
            break
        sum += c
        if k >= sum:
            ans -= 1
        else:
            break
    print(ans)


Solution 2 (runtime: 187 ms):
import sys
from collections import Counter

input = sys.stdin.readline
output = sys.stdout.write
str_input = lambda: input().strip()
int_input = lambda: int(input())
multi_int_input = lambda: map(int, input().split())
list_input = lambda: list(map(int, input().split()))
P1 = lambda X: result_array.append(f"{X}\n")
P2 = lambda X,Y: result_array.append(f"{X} {Y}\n")
P3 = lambda X,Y,Z: result_array.append(f"{X} {Y} {Z}\n")
PA = lambda X: result_array.append(f"{' '.join(map(str,X))}\n")


def solve(test_case):
    N,K = multi_int_input()
    arr = list_input()

    check = Counter()
    for i in arr:
        check[str(i)] += 1
    check = sorted(check.values(),reverse=True)

    while check:
        if check[-1] <= K:
            K -= check.pop()
        else:
            break
    
    P1(max(1,len(check)))


tc = 1
tc = int_input()
result_array = []
for test_case in range(1,tc+1):
    solve(test_case)
output(''.join(result_array))
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 171 ms):
n = int(input())
for _ in range(n):
    l, k = map(int, input().split())
    input_str = input()
    arr = list(map(int, input_str.split()))

    # 先排序
    arr.sort()

    # 存储不同的数字有几组
    count = []
    curnum = 1

    for i in range(1, l):
        if arr[i] == arr[i - 1]:
            curnum += 1
        else:
            count.append(curnum)
            curnum = 1
    count.append(curnum)

    count.sort()
    sum = 0
    ans = len(count)
    for c in count:
        if ans == 1:
            break
        sum += c
        if k >= sum:
            ans -= 1
        else:
            break
    print(ans)


Solution 2 (runtime: 249 ms):
t = int(input())
while t:
	t -= 1
	n, k =  map(int, input().split())
	ordem = {}
	array = list(map(int, input().split()))
	array.sort()
	freq = []

	prev = array[0]
	count = 0
	for num in array:
		if num != prev:
			freq.append(count)
			count = 0
		count += 1
		prev = num		
	freq.append(count)
	freq.sort()
	
	sum = 0
	count = 0
	for num in freq:
		sum += num
		if k >= sum:
			count += 1
		else:
			break
	
	print(max(1, len(freq)-count))
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 171 ms):
n = int(input())
for _ in range(n):
    l, k = map(int, input().split())
    input_str = input()
    arr = list(map(int, input_str.split()))

    # 先排序
    arr.sort()

    # 存储不同的数字有几组
    count = []
    curnum = 1

    for i in range(1, l):
        if arr[i] == arr[i - 1]:
            curnum += 1
        else:
            count.append(curnum)
            curnum = 1
    count.append(curnum)

    count.sort()
    sum = 0
    ans = len(count)
    for c in count:
        if ans == 1:
            break
        sum += c
        if k >= sum:
            ans -= 1
        else:
            break
    print(ans)


Solution 2 (runtime: 249 ms):
for _ in range(int(input())):
  n, k = list(map(int, input().split()))
  a = list(map(int, input().split()))
  
  a.sort()
  # store the freq of nums
  cnt = [1]
  for i in range(1, n):
    if a[i] == a[i-1]:
      cnt[-1] += 1
    else:
      cnt.append(1)
  
  # sort the cnts to check with k and convert them to min
  cnt.sort()
  m = len(cnt)
  flag = False
  for i in range(m-1):
    if cnt[i] > k:
      # we can't change any ai so
      print(m-i)
      flag = True
      break
    
    k -= cnt[i]
  
  if flag == False:
    print(1)
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 140 ms):
if __name__ == "__main__":
    tc = 0
    tc = int(input())
    lres = []
    for i in range(tc):
        nkRaw = input()
        n = int(nkRaw.split(" ")[0])
        k = int(nkRaw.split(" ")[1])
        minimum = 0
        maximum = 1
        arrayRaw = input()
        array = arrayRaw.split(" ")
        diccionario = {}
        for num in range(int(n)):
            if(array[num] not in diccionario):
                diccionario[array[num]] = 1
            else:
                diccionario[array[num]] += 1;
        if(k >= n):
            lres.append(1)
        else:
            lista_diccionario = sorted(diccionario.values())
            aux = 0
            for num in lista_diccionario:
                if(num <= k):
                    k -= num
                    aux += 1
                else:
                    break
            lres.append(len(lista_diccionario) - aux)
    for res in lres:
        print(res)
#end

Solution 2 (runtime: 187 ms):
for q in range(int(input())):
    n,k=list(map(int,input().split(' ')))
    a=list(map(int,input().split(' ')))
    b=set(a)
    d={}
    for i in b:
        d[i]=0
    for i in a:
        d[i]+=1
    l=[]
    for i in d:
        l.append(d[i])
    pre=0
    ans=len(l)
    l.sort()
    for i in range(len(l)):
        if(pre+l[i]<=k):
            pre+=l[i]
            ans-=1
    print(max(ans,1))
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 140 ms):
if __name__ == "__main__":
    tc = 0
    tc = int(input())
    lres = []
    for i in range(tc):
        nkRaw = input()
        n = int(nkRaw.split(" ")[0])
        k = int(nkRaw.split(" ")[1])
        minimum = 0
        maximum = 1
        arrayRaw = input()
        array = arrayRaw.split(" ")
        diccionario = {}
        for num in range(int(n)):
            if(array[num] not in diccionario):
                diccionario[array[num]] = 1
            else:
                diccionario[array[num]] += 1;
        if(k >= n):
            lres.append(1)
        else:
            lista_diccionario = sorted(diccionario.values())
            aux = 0
            for num in lista_diccionario:
                if(num <= k):
                    k -= num
                    aux += 1
                else:
                    break
            lres.append(len(lista_diccionario) - aux)
    for res in lres:
        print(res)
#end

Solution 2 (runtime: 312 ms):
import bisect
t=int(input())
for i in range(t):
    n,k=map(int,input().split())
    l=list(map(int,input().split()))
    l.sort()
    f=[]
    c=1
    for i in range(1,n):
        if(l[i]==l[i-1]):
            c+=1
        else:
            f.append(c)
            c=1
    f.append(c)
    f.sort()
    x=0

    pf=[]
    for j in f:
        x+=j
        pf.append(x)
    v=bisect.bisect_left(pf,k)
    y=0
    #print(pf,v)
    if(v==n):
        print(1)
    elif(v==0):
        if(k!=0):
            if(k<pf[0]):
                y=1
                print(max(1,len(f)-1-v+y))
            else:
                print(max(1,len(f)-1-v))
        else:
            print(len(f))
    elif(k<pf[v]):
        y=1
        print(len(f)-1-v+y)
    
    else:
        print(max(1,len(f)-1-v))
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 140 ms):
if __name__ == "__main__":
    tc = 0
    tc = int(input())
    lres = []
    for i in range(tc):
        nkRaw = input()
        n = int(nkRaw.split(" ")[0])
        k = int(nkRaw.split(" ")[1])
        minimum = 0
        maximum = 1
        arrayRaw = input()
        array = arrayRaw.split(" ")
        diccionario = {}
        for num in range(int(n)):
            if(array[num] not in diccionario):
                diccionario[array[num]] = 1
            else:
                diccionario[array[num]] += 1;
        if(k >= n):
            lres.append(1)
        else:
            lista_diccionario = sorted(diccionario.values())
            aux = 0
            for num in lista_diccionario:
                if(num <= k):
                    k -= num
                    aux += 1
                else:
                    break
            lres.append(len(lista_diccionario) - aux)
    for res in lres:
        print(res)
#end

Solution 2 (runtime: 187 ms):
import sys
from collections import Counter

input = sys.stdin.readline
output = sys.stdout.write
str_input = lambda: input().strip()
int_input = lambda: int(input())
multi_int_input = lambda: map(int, input().split())
list_input = lambda: list(map(int, input().split()))
P1 = lambda X: result_array.append(f"{X}\n")
P2 = lambda X,Y: result_array.append(f"{X} {Y}\n")
P3 = lambda X,Y,Z: result_array.append(f"{X} {Y} {Z}\n")
PA = lambda X: result_array.append(f"{' '.join(map(str,X))}\n")


def solve(test_case):
    N,K = multi_int_input()
    arr = list_input()

    check = Counter()
    for i in arr:
        check[str(i)] += 1
    check = sorted(check.values(),reverse=True)

    while check:
        if check[-1] <= K:
            K -= check.pop()
        else:
            break
    
    P1(max(1,len(check)))


tc = 1
tc = int_input()
result_array = []
for test_case in range(1,tc+1):
    solve(test_case)
output(''.join(result_array))
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 140 ms):
if __name__ == "__main__":
    tc = 0
    tc = int(input())
    lres = []
    for i in range(tc):
        nkRaw = input()
        n = int(nkRaw.split(" ")[0])
        k = int(nkRaw.split(" ")[1])
        minimum = 0
        maximum = 1
        arrayRaw = input()
        array = arrayRaw.split(" ")
        diccionario = {}
        for num in range(int(n)):
            if(array[num] not in diccionario):
                diccionario[array[num]] = 1
            else:
                diccionario[array[num]] += 1;
        if(k >= n):
            lres.append(1)
        else:
            lista_diccionario = sorted(diccionario.values())
            aux = 0
            for num in lista_diccionario:
                if(num <= k):
                    k -= num
                    aux += 1
                else:
                    break
            lres.append(len(lista_diccionario) - aux)
    for res in lres:
        print(res)
#end

Solution 2 (runtime: 249 ms):
t = int(input())
while t:
	t -= 1
	n, k =  map(int, input().split())
	ordem = {}
	array = list(map(int, input().split()))
	array.sort()
	freq = []

	prev = array[0]
	count = 0
	for num in array:
		if num != prev:
			freq.append(count)
			count = 0
		count += 1
		prev = num		
	freq.append(count)
	freq.sort()
	
	sum = 0
	count = 0
	for num in freq:
		sum += num
		if k >= sum:
			count += 1
		else:
			break
	
	print(max(1, len(freq)-count))
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 140 ms):
if __name__ == "__main__":
    tc = 0
    tc = int(input())
    lres = []
    for i in range(tc):
        nkRaw = input()
        n = int(nkRaw.split(" ")[0])
        k = int(nkRaw.split(" ")[1])
        minimum = 0
        maximum = 1
        arrayRaw = input()
        array = arrayRaw.split(" ")
        diccionario = {}
        for num in range(int(n)):
            if(array[num] not in diccionario):
                diccionario[array[num]] = 1
            else:
                diccionario[array[num]] += 1;
        if(k >= n):
            lres.append(1)
        else:
            lista_diccionario = sorted(diccionario.values())
            aux = 0
            for num in lista_diccionario:
                if(num <= k):
                    k -= num
                    aux += 1
                else:
                    break
            lres.append(len(lista_diccionario) - aux)
    for res in lres:
        print(res)
#end

Solution 2 (runtime: 249 ms):
for _ in range(int(input())):
  n, k = list(map(int, input().split()))
  a = list(map(int, input().split()))
  
  a.sort()
  # store the freq of nums
  cnt = [1]
  for i in range(1, n):
    if a[i] == a[i-1]:
      cnt[-1] += 1
    else:
      cnt.append(1)
  
  # sort the cnts to check with k and convert them to min
  cnt.sort()
  m = len(cnt)
  flag = False
  for i in range(m-1):
    if cnt[i] > k:
      # we can't change any ai so
      print(m-i)
      flag = True
      break
    
    k -= cnt[i]
  
  if flag == False:
    print(1)
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 187 ms):
for q in range(int(input())):
    n,k=list(map(int,input().split(' ')))
    a=list(map(int,input().split(' ')))
    b=set(a)
    d={}
    for i in b:
        d[i]=0
    for i in a:
        d[i]+=1
    l=[]
    for i in d:
        l.append(d[i])
    pre=0
    ans=len(l)
    l.sort()
    for i in range(len(l)):
        if(pre+l[i]<=k):
            pre+=l[i]
            ans-=1
    print(max(ans,1))

Solution 2 (runtime: 312 ms):
import bisect
t=int(input())
for i in range(t):
    n,k=map(int,input().split())
    l=list(map(int,input().split()))
    l.sort()
    f=[]
    c=1
    for i in range(1,n):
        if(l[i]==l[i-1]):
            c+=1
        else:
            f.append(c)
            c=1
    f.append(c)
    f.sort()
    x=0

    pf=[]
    for j in f:
        x+=j
        pf.append(x)
    v=bisect.bisect_left(pf,k)
    y=0
    #print(pf,v)
    if(v==n):
        print(1)
    elif(v==0):
        if(k!=0):
            if(k<pf[0]):
                y=1
                print(max(1,len(f)-1-v+y))
            else:
                print(max(1,len(f)-1-v))
        else:
            print(len(f))
    elif(k<pf[v]):
        y=1
        print(len(f)-1-v+y)
    
    else:
        print(max(1,len(f)-1-v))
Problem: 2057B - Gorilla and the Exam
Language: python

Solution 1 (runtime: 187 ms):
for q in range(int(input())):
    n,k=list(map(int,input().split(' ')))
    a=list(map(int,input().split(' ')))
    b=set(a)
    d={}
    for i in b:
        d[i]=0
    for i in a:
        d[i]+=1
    l=[]
    for i in d:
        l.append(d[i])
    pre=0
    ans=len(l)
    l.sort()
    for i in range(len(l)):
        if(pre+l[i]<=k):
            pre+=l[i]
            ans-=1
    print(max(ans,1))

Solution 2 (runtime: 187 ms):
import sys
from collections import Counter

input = sys.stdin.readline
output = sys.stdout.write
str_input = lambda: input().strip()
int_input = lambda: int(input())
multi_int_input = lambda: map(int, input().split())
list_input = lambda: list(map(int, input().split()))
P1 = lambda X: result_array.append(f"{X}\n")
P2 = lambda X,Y: result_array.append(f"{X} {Y}\n")
P3 = lambda X,Y,Z: result_array.append(f"{X} {Y} {Z}\n")
PA = lambda X: result_array.append(f"{' '.join(map(str,X))}\n")


def solve(test_case):
    N,K = multi_int_input()
    arr = list_input()

    check = Counter()
    for i in arr:
        check[str(i)] += 1
    check = sorted(check.values(),reverse=True)

    while check:
        if check[-1] <= K:
            K -= check.pop()
        else:
            break
    
    P1(max(1,len(check)))


tc = 1
tc = int_input()
result_array = []
for test_case in range(1,tc+1):
    solve(test_case)
output(''.join(result_array))