T = input()
while T:
    T-=1
    N,M = [int(x) for x in raw_input().split()]
    A = [int(x) for x in raw_input().split()]
    A = sorted(A,reverse = False)
    Reqd = M-1
    k = 0
    Total = 0
    while True:
        if A[k] + 1 < Reqd:
            Reqd -= (A[k] + 1)
            Total += A[k]
        elif A[k] + 1 == Reqd or A[k] == Reqd:
            Reqd = 0
            Total += A[k]
            break
        else:
            Total += Reqd
            break
        k+=1
    print Total