#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n;
    cin >> n;
    int arr[n];
    for (int i = 0; i < n; i++) {
      cin >> arr[i];
    }
    sort(arr, arr + n);
    int res = INT_MAX;
    res = min(arr[n - 2], min(res, arr[n - 1]));
    res -= 1;
    int cnt = 0;
    for (int i = 0; i < n - 2; i++) {
      if (arr[i] >= 1) {
        cnt++;
      }
    }
    cout << min(cnt, res) << "\n";
  }
  return 0;
}
