#include <bits/stdc++.h>
using namespace std;
const int N = 100000;
vector<int> v(N + 5);
int n;
int cal() {
  if (n == 2) return 0;
  sort(v.begin(), v.begin() + n, greater<int>());
  int m = min(v[0], v[1]);
  if (m == 1) return 0;
  return min(m - 1, n - 2);
}
void solve() {
  cin >> n;
  for (int i = 0; i < n; ++i) cin >> v[i];
  cout << cal() << "\n";
}
int main() {
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
