#include <bits/stdc++.h>
using namespace std;
vector<int> v;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    v.clear();
    int n, a;
    cin >> n;
    int x = n;
    while (n--) {
      cin >> a;
      v.push_back(a);
    }
    sort(v.rbegin(), v.rend());
    int b = min(v[0], v[1]);
    b--;
    int res = min(b, x - 2);
    cout << (res) << '\n';
  }
  return 0;
}
