#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  int a;
  cin >> T;
  while (T--) {
    cin >> a;
    int ar[a];
    for (int i = 0; i < a; i++) {
      cin >> ar[i];
    }
    sort(ar, ar + a);
    int result = min(a - 2, ar[a - 2] - 1);
    cout << result << endl;
  }
  return 0;
}
