#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
void solve();
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(10);
  int t = 1;
  cin >> t;
  while (t--) solve();
  return 0;
}
void solve() {
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < (int)n; i++) cin >> a[i];
  sort((a).begin(), (a).end());
  int k = min(a[n - 1], a[n - 2]);
  cout << min(k - 1, n - 2) << "\n";
}
