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