#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int A[n];
    for (int i = 0; i < n; i++) cin >> A[i];
    sort(A, A + n);
    if (n <= 2) {
      cout << "0" << endl;
    } else {
      int ans = min(A[n - 2] - 1, n - 2);
      cout << ans << endl;
    }
  }
  return 0;
}
