#include <bits/stdc++.h>
using namespace std;
int main() {
  int T, n, a[100005];
  cin >> T;
  while (T--) {
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    if (n <= 2) {
      cout << 0 << endl;
      continue;
    }
    sort(a, a + n, greater<int>());
    int base = min(a[0], a[1]);
    cout << min(base - 1, n - 2) << endl;
  }
  return 0;
}
