#include <bits/stdc++.h>
using namespace std;
long long const mod = 1e9 + 7;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t, n, m, i, j, k, cnt = 1, sum1 = 0, ccnt = 0, x, y, z, x1, y1 = 0,
                              z1, total = 0, sum = 0, x2, y2, sz, sum2 = 0;
  cin >> t;
  while (t--) {
    cin >> n;
    long long a[n + 1];
    for (i = 0; i < n; i++) {
      cin >> a[i];
    }
    sort(a, a + n);
    x = a[n - 2] - 1;
    cout << min(x, n - 2) << endl;
  }
  return 0;
}
