#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5;
int a[maxn + 1];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int tc, n;
  cin >> tc;
  while (tc--) {
    cin >> n;
    for (int i = 0; i < (int)n; ++i) cin >> a[i];
    sort(a, a + n);
    int m1 = a[n - 1], m2 = a[n - 2];
    int h = min(m1, m2) - 1;
    if (h <= 0 or n - 2 <= 0)
      cout << "0\n";
    else {
      cout << min(h, n - 2) << "\n";
    }
  }
  return 0;
}
