#include <bits/stdc++.h>
using namespace std;
long long const inf = 1e9;
long long const mod = 1e9 + 7;
long double const eps = 1e-9;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int a[n];
    for (int i = (0); i < (n); i++) {
      cin >> a[i];
    }
    sort(a, a + n);
    cout << min(n - 2, a[n - 2] - 1) << endl;
  }
  return 0;
}
