#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 1e9 + 7;
bool comp(int a, int b) { return a > b; }
int run_test() {
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  sort(v.begin(), v.end(), comp);
  int ans = 0;
  int k = v[1];
  if (n - 2 >= k - 1) {
    cout << k - 1 << '\n';
  } else {
    cout << n - 2 << '\n';
  }
  return 0;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tt;
  tt = 1;
  cin >> tt;
  while (tt--) {
    run_test();
  }
}
