#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(17);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < (n); i++) cin >> a[i];
    sort(a.rbegin(), a.rend());
    int k = max(min(n - 2, a[1] - 1), 0);
    cout << k << "\n";
  }
  cout << endl;
  ;
  return 0;
}
