#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t;
  cin >> t;
  for (long long c = 0; c < t; c++) {
    long long n;
    cin >> n;
    vector<long long> v(n);
    for (long long j = 0; j < n; j++) {
      cin >> v[j];
    }
    sort(v.begin(), v.end());
    auto res = v[n - 2] - 1;
    if (v[n - 2] > n - 2) {
      res = n - 2;
    }
    cout << res << "\n";
  }
}
