for cas in xrange(input()):
    n, m = map(int, raw_input().strip().split())
    a = map(int, raw_input().strip().split())
    a.sort()
    total = 0
    for i in xrange(m):
        k = m - i - total - 1
        if k <= a[i]:
            ans = total + k
            break
        total += a[i]    
    print ans