#include <bits/stdc++.h>
using namespace std;
long long t, n, a[100005], max1, max2;
int main() {
  cin >> t;
  while (t--) {
    scanf("%d", &n);
    max1 = max2 = 0;
    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
      if (a[i] > max1) {
        max2 = max1;
        max1 = a[i];
      } else if (a[i] > max2)
        max2 = a[i];
    }
    cout << min(max2 - 1, n - 2) << endl;
  }
}
