#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int N = 2e5 + 5;
const int MOD = (int)1e9 + 7;
const ll INF = (ll)1e18 + 5;
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0),
      cout << fixed << setprecision(15);
  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());
    cout << min(a[1] - 1, n - 2) << "\n";
  }
  return 0;
}
