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