#include <bits/stdc++.h>
using namespace std;
int tc, n, a[100000];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> tc;
  while (tc--) {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
    sort(a, a + n);
    int base = min(a[n - 1], a[n - 2]) - 1;
    cout << min(base, n - 2) << '\n';
  }
}
