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