#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base ::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, i, mx = -1, mx2 = 0, ans, in;
    cin >> n;
    for (i = 1; i <= n; i++) {
      cin >> in;
      if (in >= mx)
        mx2 = mx, mx = in;
      else
        mx2 = max(mx2, in);
    }
    ans = min(n - 2, mx2 - 1);
    cout << ans << endl;
  }
}
