Let's denote a $k$-step ladder as the following structure: exactly $k + 2$ wooden planks, of which

  two planks of length at least $k+1$ — the base of the ladder;  $k$ planks of length at least $1$ — the steps of the ladder; 

Note that neither the base planks, nor the steps planks are required to be equal.

For example, ladders $1$ and $3$ are correct $2$-step ladders and ladder $2$ is a correct $1$-step ladder. On the first picture the lengths of planks are $[3, 3]$ for the base and $[1]$ for the step. On the second picture lengths are $[3, 3]$ for the base and $[2]$ for the step. On the third picture lengths are $[3, 4]$ for the base and $[2, 3]$ for the steps. 

 $H - 1 =$ 

You have $n$ planks. The length of the $i$-th planks is $a_i$. You don't have a saw, so you can't cut the planks you have. Though you have a hammer and nails, so you can assemble the improvised "ladder" from the planks.

The question is: what is the maximum number $k$ such that you can choose some subset of the given planks and assemble a $k$-step ladder using them?


-----Input-----

The first line contains a single integer $T$ ($1 \le T \le 100$) — the number of queries. The queries are independent.

Each query consists of two lines. The first line contains a single integer $n$ ($2 \le n \le 10^5$) — the number of planks you have.

The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^5$) — the lengths of the corresponding planks.

It's guaranteed that the total number of planks from all queries doesn't exceed $10^5$.


-----Output-----

Print $T$ integers — one per query. The $i$-th integer is the maximum number $k$, such that you can choose some subset of the planks given in the $i$-th query and assemble a $k$-step ladder using them.

Print $0$ if you can't make even $1$-step ladder from the given set of planks.


-----Example-----
Input
4
4
1 3 1 3
3
3 3 2
5
2 3 3 4 2
3
1 1 2

Output
2
1
2
0



-----Note-----

Examples for the queries $1-3$ are shown at the image in the legend section.

The Russian meme to express the quality of the ladders:

 [Image]

Difficulty: interview

Starter code:

Solutions:
t = int(input())
for i in range(t):
    n = int(input())
    arr = [int(x) for x in input().split()]
    arr.sort(reverse=True)
    print(min(arr[1] - 1, len(arr) - 2))

import sys

T = int(sys.stdin.readline().strip())
for t in range (0, T):
    n = int(sys.stdin.readline().strip())
    a = list(map(int, sys.stdin.readline().strip().split()))
    a.sort()
    print(min([len(a)-2, a[-2]-1]))

t = int(input())
for _ in range(t):
    n = int(input())
    a = sorted(map(int, input().split()), reverse=True)
    print(min(n - 2, a[1] - 1))


t=int(input())
for i in range(t):
    n=int(input())
    ar=list(map(int,input().split()))
    if(n<=2):
        print(0)
        continue
    ar=sorted(ar)[::-1]
    ans=0
    for i in range(1,n-1):
        if(ar[0]>i and ar[1]>i):
            ans=i
    print(ans)


t = int(input())
for i in range(t):
    n = int(input())
    ai = list(map(int,input().split()))
    ai.sort()
    print(min(n-2,ai[-2]-1))
    


# @author 

import sys

class ADIYWoodenLadder:
    def solve(self):
        t = int(input())
        for _ in range(t):
            n = int(input())
            a = [int(_) for _ in input().split()]
            a.sort()
            print(min(a[-2] - 1, n - 2))

solver = ADIYWoodenLadder()
input = sys.stdin.readline

solver.solve()


n = int(input())
for i in range(n):
    p = int(input())
    a = list(map(int,input().split()))
    a = sorted(a)
    if p == 2:
        print(0)
        continue
    k = a[-2] - 1
    print(min(k, p - 2))

n = int(input())
for i in range(n):
    k = int(input())
    s = list(map(int, input().split()))
    s.sort()
    print(min(k-2,s[k-2]-1))


t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int,input().split()))
    a.sort()
    if a[-2]>n-2:
        print(n-2)
    else:
        print(a[-2]-1)

def main():
    def solve():
        n = int(input())
        arr = sorted(map(int, input().split()))
        a = arr[-2]
        print(min(n - 2, a - 1))
    
    import sys
    input = sys.stdin.readline
    
    for _ in range(int(input())):
        solve()
    
    return 0

main()

t=int(input())
for l in range(t):
	n=int(input())
	arr=list(map(int,input().split()))
	arr.sort()
	a=arr[-2]
	print(min(a-1,n-2))


for TT in range(1, int(input()) + 1):
    n = int(input())
    l = sorted(map(int, input().split()))
    k = max(0, min(n - 2, l[-2] - 1))
    print(k)

t = int(input())
for i in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    a = sorted(a)
    print(min(a[-2]-1, n-2))


t = int(input())
for i in range(t):
    n = int(input())

    lens = sorted(map(int, input().split()))
    cnt = len(list(filter(lambda x: x> 0, lens[:-2])))

    print(min(cnt, lens[-2]-1))

import math
import sys
import collections

# imgur.com/Pkt7iIf.png

def getdict(n):
    d = {}
    if type(n) is list:
        for i in n:
            if i in d:
                d[i] += 1
            else:
                d[i] = 1
    else:
        for i in range(n):
            t = ii()
            if t in d:
                d[t] += 1
            else:
                d[t] = 1
    return d
def cdiv(n, k): return n // k + (n % k != 0)
def ii(): return int(input())
def mi(): return list(map(int, input().split()))
def li(): return list(map(int, input().split()))

t = ii()
for i in range(t):
    n = ii()
    d = sorted(li())
    print(min(d[-2] - 1, n - 2))


from sys import stdin
c=int(stdin.readline().strip())
for i in range(c):
    n=int(stdin.readline().strip())
    #n,m=map(int,stdin.readline().strip().split())
    s=list(map(int,stdin.readline().strip().split()))
    s.sort()
    l=min(s[-1],s[-2])
    ans=min(l-1,n-2)
    print(ans)


T = int(input())
for i in range(T):
	n = int(input())
	a = list(map(int, input().split()))
	a.sort()
	print(min(len(a) - 2, max(a[-2] - 1, 0)))



for _ in range(int(input())):
    n = int(input())
    li = sorted(list(map(int, input().split())))
    print(min(li[n - 2] - 1, n - 2))


for _ in range(int(input())):
    n = int(input())
    a = sorted([int(i) for i in input().split()])
    k = min(a[-1]-1,a[-2]-1,len(a)-2)
    print(k)



t = int(input())
for _ in range(t):
    n = int(input())
    a = [int(x) for x in input().split(' ')]
    a.sort()
    maxK = a[-2]-1
    ans = min(maxK,len(a)-2)
    print(ans)

t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    if n == 1:
        print(0)
    else:
        max1 = max2 = -1
        for q in a:
            if q > max1:
                max1, max2 = q, max1
            elif q > max2:
                max2 = q
        print(max(0, min(max2-1, len(a)-2)))


q = int(input())
for i in range(q):
    n = int(input())
    l = list(map(int, input().split()))
    l.sort()
    ans = 0
    for i in range(n - 2):
        if (ans + 1 == l[-2]):
            break
        else:
            ans += 1
    print(ans)

for i in range(int(input())):
    num = int(input())
    data = [int(i) for i in input().split()]
    data.sort()
    k0 = min(data[-2] - 1, num - 2)
    print(max(0, k0))


test_count = int(input())
for _ in range(test_count):
	n = int(input())
	arr = list(map(int, input().split()))
	arr.sort()
	print(max(min(n - 2, arr[-2] - 1), 0))