#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int arr[n + 5];
    for (int i = 0; i < n; i++) {
      cin >> arr[i];
    }
    sort(arr, arr + n);
    int base1 = arr[n - 1];
    int base2 = arr[n - 2];
    if (base2 <= 1 || n == 2) {
      cout << 0 << endl;
      continue;
    }
    int k = min(n - 2, base2 - 1);
    cout << k << endl;
  }
  return 0;
}
