#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e5 + 10;
const long long INF = 8e18;
const long long MOD = 1e9 + 9;
int main() {
  int t, n, a[MAXN], ma1, ma2;
  cin >> t;
  for (int i = 0; i < t; i++) {
    cin >> n;
    cin >> ma1;
    cin >> ma2;
    if (ma2 > ma1) {
      swap(ma1, ma2);
    }
    for (int j = 2; j < n; j++) {
      cin >> a[j];
      if (a[j] > ma1) {
        ma2 = ma1;
        ma1 = a[j];
      } else if (ma2 < a[j]) {
        ma2 = a[j];
      }
    }
    cout << min(n - 2, ma2 - 1) << '\n';
  }
}
