#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t, n;
  long long a[200005] = {0};
  long long ans;
  cin >> t;
  while (t--) {
    cin >> n;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
    }
    sort(a + 1, a + n + 1);
    ans = min(a[n], a[n - 1]) - 1;
    ans = min(ans, n - 2);
    cout << ans << endl;
  }
  return 0;
}
