#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    unsigned long long n, r, m1, m2;
    cin >> n;
    vector<unsigned long long> v;
    for (int i = 0; i < n; i++) {
      unsigned long long a;
      cin >> a;
      v.push_back(a);
    }
    sort(v.begin(), v.end());
    m1 = v[n - 1];
    m2 = v[n - 2];
    if (m2 > n - 2) {
      r = n - 2;
    } else {
      r = m2 - 1;
    }
    cout << r << endl;
  }
  return 0;
}
