#include <bits/stdc++.h>
using namespace std;
int main() {
  long long T;
  cin >> T;
  for (int trials = 0; trials < T; trials++) {
    long long N;
    cin >> N;
    vector<long long> theLine(N);
    long long max1 = 0;
    long long max2 = 0;
    for (int z = 0; z < N; z++) {
      cin >> theLine[z];
      if (theLine[z] > max1) {
        max2 = max1;
        max1 = theLine[z];
      } else {
        if (theLine[z] > max2) {
          max2 = theLine[z];
        }
      }
    }
    max2--;
    cout << min(max2, N - 2) << endl;
  }
}
