#include <bits/stdc++.h>
using namespace std;
void print(long long x[], long long n) {
  for (int i = 0; i < n; i++) cout << x[i] << " ";
  cout << endl;
}
int main() {
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int x[n];
    for (int i = 0; i < n; i++) cin >> x[i];
    sort(x, x + n);
    int k = x[n - 2] - 1;
    for (; k >= 0; k--) {
      if (n >= k + 2) break;
    }
    cout << k << endl;
  }
  return 0;
}
