#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.precision(10);
  long long int q, n;
  cin >> q;
  while (q--) {
    cin >> n;
    vector<long long int> v(n);
    for (long long int i = 0; i < n; i++) cin >> v[i];
    sort(v.begin(), v.end());
    reverse((v).begin(), (v).end());
    if (v[1] > v.size() - 2)
      cout << v.size() - 2 << endl;
    else
      cout << v[1] - 1 << endl;
  }
}
