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