import math
import sys
t=int(raw_input(""))
while t:
    n,m=map(int,sys.stdin.readline().split())
    x=list(map(int,sys.stdin.readline().split()))
    x.sort()
    sum1=0
    for i in range(m):
        sum1+=x[i]
        if sum1>m-i-2:
            print m-i-1
            break
        elif sum1==m-i-2:
            print m-i-2
            break
    t-=1