#include <bits/stdc++.h>
using namespace std;
int T, N, A[100005];
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d", &N);
    for (int i = 1; i <= N; i++) scanf("%d", A + i);
    sort(A + 1, A + N + 1);
    printf("%d\n", min(A[N - 1] - 1, N - 2));
  }
}
