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