#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int m;
    cin >> m;
    vector<int> v(m);
    for (int i = 0; i < m; ++i) {
      cin >> v[i];
    }
    sort(v.begin(), v.end());
    cout << max(0, min(v[m - 2] - 1, m - 2)) << "\n";
  }
}
