#include <bits/stdc++.h>
using namespace std;
int main() {
  long t;
  cin >> t;
  for (long i = 0; i < t; i++) {
    long n;
    cin >> n;
    long max = LONG_MIN;
    long podmax = LONG_MIN;
    for (long i = 0; i < n; i++) {
      long z;
      cin >> z;
      if (max < z) {
        podmax = max;
        max = z;
      } else if (podmax < z)
        podmax = z;
    }
    cout << min(n - 2, podmax - 1) << endl;
  }
  return 0;
}
