#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
using namespace std;
template <class T>
istream &operator>>(istream &is, vector<T> &v) {
  for (T &a : v) is >> a;
  return is;
}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (const T &t : v) os << t << " ";
  return os << endl;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &v) {
  return os << v.first << " " << v.second;
}
double gcd(double a, double b) { return a < 0.01 ? b : gcd(fmod(b, a), a); }
long long bpow(long long a, long long b, long long m) {
  a %= m;
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
long long int kv;
long long int t, n, x, y, h, k, z, p, q, w;
string s, s1, s2;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(16);
  ;
  cin >> t;
  while (t--) {
    cin >> n;
    vector<long long int> v(n);
    cin >> v;
    p = 0;
    q = 0;
    h = 0;
    for (long long i = 0; i < n; i++) {
      if (v[i] > p) p = v[i], h = i;
    }
    for (long long i = 0; i < n; i++) {
      if (i == h) continue;
      q = max(q, v[i]);
    }
    q--;
    if (q >= n - 2)
      cout << n - 2 << '\n';
    else
      cout << q << '\n';
  }
  return 0;
}
