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