#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
const int N = 1e5 + 5;
int a[N];
void sol() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  if (n < 3) {
    cout << 0 << "\n";
    return;
  }
  sort(a + 1, a + n + 1);
  cout << min(a[n - 1] - 1, n - 2) << "\n";
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) sol();
}
