#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n, res;
    cin >> n;
    int c[n + 2];
    for (int i = 1; i <= n; i++) {
      cin >> c[i];
    }
    sort(c + 1, c + 1 + n);
    if (c[n - 1] == 1) {
      cout << "0" << endl;
      continue;
    }
    res = min(n - 2, c[n - 1] - 1);
    cout << res << endl;
  }
}
