#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long int tc;
  cin >> tc;
  while (tc--) {
    long long int n, i, x, y;
    cin >> n;
    vector<long long int> v(n);
    for (i = 0; i < n; i++) cin >> v[i];
    sort(v.begin(), v.end());
    x = v[n - 2];
    x--;
    y = n - 2;
    cout << min(x, y) << "\n";
  }
}
