#include <bits/stdc++.h>
using namespace std;
long long t, n, a, maks, maks2;
int main() {
  ios_base::sync_with_stdio(0);
  cin >> t;
  while (t--) {
    cin >> n;
    maks = maks2 = 0;
    for (long long i = 1; i <= n; i++) {
      cin >> a;
      if (a >= maks) {
        maks2 = maks;
        maks = a;
      } else if (a >= maks2)
        maks2 = a;
    }
    cout << min(n - 2, maks2 - 1) << '\n';
  }
  return 0;
}
