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