#include <bits/stdc++.h>
using namespace std;
const int64_t N = 1e5;
int64_t n, a[N], t;
int32_t main() {
  ios_base::sync_with_stdio(true == false);
  cin >> t;
  for (int64_t T = 0; T < t; T++) {
    cin >> n;
    for (int64_t i = 0; i < n; i++) {
      cin >> a[i];
    }
    sort(a, a + n);
    cout << min(n - 2, a[n - 2] - 1) << '\n';
  }
}
