#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int x;
  cin >> x;
  vector<long long> result;
  for (long long i = 1; i <= x; i++) {
    long long n;
    cin >> n;
    vector<long long> v(n);
    for (long long h = 0; h <= n - 1; h++) cin >> v[h];
    sort(v.begin(), v.end());
    if (v[n - 1] == 1 || v[n - 2] == 1)
      result.push_back(0);
    else if ((n - 2) < v[n - 2])
      result.push_back(n - 2);
    else if ((n - 2) >= v[n - 2])
      result.push_back(v[n - 2] - 1);
  }
  for (long long i = 0; i <= x - 1; i++) cout << result[i] << endl;
}
